-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathservice.go
57 lines (51 loc) · 1004 Bytes
/
service.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 omikuji
import (
"math/rand"
"time"
)
// Service provides omikuji.
type Service interface {
Hiku() Omikuji
}
// New returns a new Service.
func New() Service {
return &DefaultService{
random: rand.New(rand.NewSource(time.Now().UnixNano())),
}
}
// DefaultService is a default implementation of Service.
type DefaultService struct {
random *rand.Rand // Random generator
time func() time.Time // Time provider (optional)
}
// Hiku peforms おみくじを引く.
// If the current date is shogatsu, it always returns the DaiKichi.
func (s *DefaultService) Hiku() Omikuji {
if s.isShogatsu() {
return DaiKichi
}
n := s.random.Intn(6)
switch n {
default:
return Kyo
case 2, 3:
return ShoKichi
case 4:
return ChuKichi
case 5:
return DaiKichi
}
}
func (s *DefaultService) isShogatsu() bool {
now := time.Now()
if s.time != nil {
now = s.time()
}
if now.Month() == time.January {
switch now.Day() {
case 1, 2, 3:
return true
}
}
return false
}