forked from topfreegames/khan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.go
69 lines (59 loc) · 1.35 KB
/
helper.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
package loadtest
import (
"fmt"
"io/ioutil"
"math/rand"
"os"
"strings"
uuid "github.com/satori/go.uuid"
)
var dictionary []string
// LoadRandomWords loads random words into memory for random clan name generation
func LoadRandomWords() {
if dictionary != nil {
return
}
file, err := os.Open("/usr/share/dict/words")
if err != nil {
panic(err)
}
bytes, err := ioutil.ReadAll(file)
if err != nil {
panic(err)
}
dictionary = strings.Split(string(bytes), "\n")
}
func getRandomScore() int {
return rand.Intn(1000)
}
func getRandomPlayerName() string {
return fmt.Sprintf("PlayerName-%s", uuid.NewV4().String()[:8])
}
func getRandomClanName() string {
if dictionary == nil {
return fmt.Sprintf("ClanName-%s", uuid.NewV4().String()[:8])
}
numberOfWords := rand.Intn(3) + 1
pieces := []string{}
for i := 0; i < numberOfWords; i++ {
pieces = append(pieces, dictionary[rand.Intn(len(dictionary))])
}
return strings.Join(pieces, " ")
}
func getRandomPublicID() string {
return uuid.NewV4().String()
}
func getScoreFromMetadata(metadata interface{}) int {
if metadata != nil {
metadataMap := metadata.(map[string]interface{})
if score, ok := metadataMap["score"]; ok {
return int(score.(float64))
}
}
return 0
}
func getMetadataWithRandomScore() map[string]interface{} {
return map[string]interface{}{
"score": getRandomScore(),
}
}