forked from brianvoe/gofakeit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.go
102 lines (85 loc) · 2.63 KB
/
game.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package gofakeit
import (
"fmt"
"math/rand"
"strings"
)
// Gamertag will generate a random video game username
func Gamertag() string { return gamertag(globalFaker.Rand) }
// Gamertag will generate a random video game username
func (f *Faker) Gamertag() string { return gamertag(f.Rand) }
func gamertag(r *rand.Rand) string {
str := ""
num := number(r, 1, 4)
switch num {
case 1:
str = fmt.Sprintf("%s%ser", title(nounConcrete(r)), title(verbAction(r)))
case 2:
str = fmt.Sprintf("%s%s", title(adjectiveDescriptive(r)), title(animal(r)))
case 3:
str = fmt.Sprintf("%s%s", title(adjectiveDescriptive(r)), title(nounConcrete(r)))
case 4:
str = fmt.Sprintf("%s%s", title(fruit(r)), title(adjectiveDescriptive(r)))
}
// Randomly determine if we should add a number
if r.Intn(3) == 1 {
str += digitN(r, uint(number(r, 1, 3)))
}
// Remove any spaces
str = strings.Replace(str, " ", "", -1)
return str
}
// Dice will generate a random set of dice
func Dice(numDice uint, sides []uint) []uint { return dice(globalFaker.Rand, numDice, sides) }
// Dice will generate a random set of dice
func (f *Faker) Dice(numDice uint, sides []uint) []uint { return dice(f.Rand, numDice, sides) }
func dice(r *rand.Rand, numDice uint, sides []uint) []uint {
dice := make([]uint, numDice)
// If we dont have any sides well set the sides to 6
if len(sides) == 0 {
sides = []uint{6}
}
for i := range dice {
// If sides[i] doesnt exist use the first side
if len(sides)-1 < i {
dice[i] = uint(number(r, 1, int(sides[0])))
} else {
dice[i] = uint(number(r, 1, int(sides[i])))
}
}
return dice
}
func addGameLookup() {
AddFuncLookup("gamertag", Info{
Display: "Gamertag",
Category: "game",
Description: "Random gamertag",
Example: "footinterpret63",
Output: "string",
Generate: func(r *rand.Rand, m *MapParams, info *Info) (interface{}, error) {
return gamertag(r), nil
},
})
AddFuncLookup("dice", Info{
Display: "Dice",
Category: "game",
Description: "Random dice outputs",
Example: "footinterpret63",
Output: "[]uint",
Params: []Param{
{Field: "numdice", Display: "Number of Dice", Type: "uint", Default: "1", Description: "Number of dice to roll"},
{Field: "sides", Display: "Number of Sides", Type: "[]uint", Default: "[6]", Description: "Number of sides on each dice"},
},
Generate: func(r *rand.Rand, m *MapParams, info *Info) (interface{}, error) {
numDice, err := info.GetUint(m, "numdice")
if err != nil {
return nil, err
}
sides, err := info.GetUintArray(m, "sides")
if err != nil {
return nil, err
}
return dice(r, numDice, sides), nil
},
})
}