-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathslot.go
85 lines (70 loc) · 1.29 KB
/
slot.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
package main
import (
crand "crypto/rand"
"math"
"math/big"
"math/rand"
)
const (
slotMinValue = 0
slotMaxValue = 7
)
type Slot struct {
slots [3]int
currentSlotIndex int
isFinished bool
intervalTime int
}
func NewSlot(seed int64, interval int) *Slot {
if seed == 0 {
i, _ := crand.Int(crand.Reader, big.NewInt(math.MaxInt64))
seed = i.Int64()
}
rand.Seed(seed)
s := Slot{}
for i := 0; i < 3; i++ {
slotValue := rand.Intn(slotMaxValue + 1)
s.slots[i] = slotValue
}
s.intervalTime = interval
return &s
}
func (s *Slot) Switch() {
s.slots[s.currentSlotIndex] = s.NextValue()
}
func (s *Slot) Select() {
if 2 <= s.currentSlotIndex {
s.isFinished = true
return
}
s.currentSlotIndex++
}
func (s *Slot) IsFinished() bool {
return s.isFinished
}
func (s *Slot) Slots() [3]int {
return s.slots
}
func (s *Slot) PreviousValue() int {
v := s.slots[s.currentSlotIndex] - 1
if v < slotMinValue {
v = slotMaxValue
}
return v
}
func (s *Slot) CurrentValue() int {
return s.slots[s.currentSlotIndex]
}
func (s *Slot) CurrentSlotIndex() int {
return s.currentSlotIndex
}
func (s *Slot) NextValue() int {
v := s.slots[s.currentSlotIndex] + 1
if slotMaxValue < v {
v = slotMinValue
}
return v
}
func (s *Slot) IntervalTime() int {
return s.intervalTime
}