-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathfortune.go
62 lines (54 loc) · 1.2 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
58
59
60
61
62
package lib
import (
"log"
"math/rand"
"time"
)
// Verbos は、ログ出力レベルを制御
var Verbos bool
// Fortune は、おみくじに関する型
type Fortune struct {
fortuneList map[uint]string
luckiest string
today time.Time
}
// NewFortune は、おみくじの初期化
func NewFortune(now time.Time) Fortune {
// おみくじのタネを初期化
rand.Seed(time.Now().UnixNano())
return Fortune{
fortuneList: map[uint]string{
0: "大吉",
1: "中吉",
2: "吉",
3: "凶",
4: "大凶",
},
luckiest: "大吉",
today: now,
}
}
func (f Fortune) isNewyearsDay() bool {
if Verbos {
log.Printf("[DEBUG] today is %v %v", f.today.Day(), f.today.Month())
}
// Month is defined here: https://golang.org/pkg/time/#Month
if f.today.Month().String() == "January" {
switch f.today.Day() {
case 1, 2, 3:
return true
}
}
return false
}
// DrawFortuneSlip は、おみくじを引く関数
// return ( 運勢, is正月 )
func (f Fortune) DrawFortuneSlip() (string, bool) {
// 正月には大吉を引く
if f.isNewyearsDay() {
return f.luckiest, true
}
// 等確率でひく
r := rand.Intn(len(f.fortuneList))
return f.fortuneList[uint(r)], false
}