-
Notifications
You must be signed in to change notification settings - Fork 0
/
state.go
103 lines (83 loc) · 1.31 KB
/
state.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package player
import "time"
func (p *Player) Line() bool {
return p.offlineTs == 0
}
func (p *Player) OfflineTs() int64 {
return p.offlineTs
}
func (p *Player) Offline(ts int64) {
if ts <= 0 {
ts = time.Now().Unix()
}
p.offlineTs = ts
if f := p.watch["line"]; f != nil {
f(p)
}
}
func (p *Player) Online() {
p.offlineTs = 0
if f := p.watch["line"]; f != nil {
f(p)
}
}
func (p *Player) WatchLine(f func(*Player)) {
p.watch["line"] = f
}
func (p *Player) Ready() bool {
return p.state&1 == 1
}
func (p *Player) SetReady(b bool) {
if p.Ready() == b {
return
}
if b {
p.state |= 1
} else {
p.state ^= 1
}
if f := p.watch["ready"]; f != nil {
f(p)
}
}
func (p *Player) WatchReady(f func(*Player)) {
p.watch["ready"] = f
}
func (p *Player) Op() bool {
return p.state&2 == 2
}
func (p *Player) SetOp(b bool) {
if p.Op() == b {
return
}
if b {
p.state |= 2
} else {
p.state ^= 2
}
if f := p.watch["op"]; f != nil {
f(p)
}
}
func (p *Player) WatchOp(f func(*Player)) {
p.watch["op"] = f
}
func (p *Player) View() bool {
return p.state&4 == 4
}
func (p *Player) SetView(b bool) {
if p.View() == b {
return
}
if b {
p.state |= 4
} else {
p.state ^= 4
}
if f := p.watch["view"]; f != nil {
f(p)
}
}
func (p *Player) WatchView(f func(*Player)) {
p.watch["view"] = f
}