-
Notifications
You must be signed in to change notification settings - Fork 0
/
fortune.go
57 lines (50 loc) · 1 KB
/
fortune.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
package fortune_telling
import (
"errors"
"math/rand"
)
//Telling the fortune result struct
type Telling struct {
Level string
Content string
Detail1 string
Detail2 string
}
//Ask ask for a sign with key,
//if key has asked, err is not nil
func Ask(key string) (Telling, error) {
defer func() { signedMap[key]++ }()
if !HasAsked(key) {
return genTelling(), nil
}
return Telling{}, errors.New("already asked for a fortune-telling")
}
//HasAsked check whether the key has asked
func HasAsked(key string) bool {
return signedMap[key] > 0
}
func genTelling() Telling {
var index = rand.Intn(len(signs))
return signs[index]
}
//String return stars the Level indicates
func (t Telling) String() string {
return LevelStars(t.Level)
}
//LevelStars return a stars string matched with the level
func LevelStars(l string) string {
var (
sep = levelMap[l]
ret string
)
for i := 0; i < 6; i++ {
var cell string
if i < sep {
cell = "★"
} else {
cell = "☆"
}
ret += cell
}
return ret
}