-
Notifications
You must be signed in to change notification settings - Fork 0
/
happy.go
240 lines (220 loc) · 5.26 KB
/
happy.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package happy
import (
"context"
"fmt"
"runtime"
"time"
"github.com/cnlisea/happy/auto"
"github.com/cnlisea/happy/delay"
"github.com/cnlisea/happy/heartbeat"
"github.com/cnlisea/happy/log"
"github.com/cnlisea/happy/pmgr"
"github.com/cnlisea/happy/pmgr/player"
"github.com/cnlisea/happy/proxy"
"github.com/cnlisea/happy/vote"
)
type Happy interface {
Context() context.Context
Init() error
Resume(begin bool, curRound uint32)
Run(resume bool)
Cost(mode CostMode)
Event(e *Event)
Heartbeat(interval time.Duration) error
Msg(msg *proxy.Msg)
MsgByUser(f func(userKey interface{}, data interface{}, delay proxy.Delay, curRound, maxRound uint32, pMgr *pmgr.PMgr, extend map[string]interface{}))
Owner(userKey interface{})
PlayerMsg(msg proxy.PlayerMsg)
Plugin(p *Plugin)
RoundBeginPolicy(policy RoundBeginPolicy)
LogSetting(path string, level log.Level) error
Log() *log.Logger
}
type _Happy struct {
ctx context.Context
delay *delay.Delay
heartbeat *heartbeat.Heartbeat
pMgr *pmgr.PMgr
event *Event
plugin *Plugin
costMode CostMode
auto *auto.Auto
disbandVote *vote.Vote
quickVote *vote.Vote
msgChan chan *proxy.Msg
byUserHandler func(userKey interface{}, data interface{}, delay proxy.Delay, curRound, maxRound uint32, pMgr *pmgr.PMgr, extend map[string]interface{})
playerMsg proxy.PlayerMsg
roundBeginPolicy RoundBeginPolicy
ownerUserKey interface{}
game proxy.Game
begin bool
curRound, maxRound uint32
extend map[string]interface{}
log *log.Logger
}
func New(ctx context.Context, maxRound uint32, game proxy.Game, extend map[string]interface{}) Happy {
if ctx == nil {
ctx = context.Background()
}
return &_Happy{
ctx: ctx,
delay: delay.New(),
pMgr: pmgr.New(),
msgChan: make(chan *proxy.Msg, 100),
game: game,
maxRound: maxRound,
extend: extend,
}
}
func (h *_Happy) Context() context.Context {
return h.ctx
}
func (h *_Happy) Resume(begin bool, curRound uint32) {
h.begin = begin
h.curRound = curRound
}
func (h *_Happy) Init() error {
h.auto = auto.New(h.delay, func(op bool) time.Duration {
a := h.game.Auto()
if a == nil {
return 0
}
ts := a.AutoTs
if op {
ts = a.ReadyOpTs
if h.begin {
ts = a.OpTs
}
}
return ts
})
h.auto.Callback(&auto.Callback{
Auto: func(key interface{}) {
p := h.pMgr.Get(key)
if p == nil {
return
}
if p.Auto() {
return
}
p.SetAuto(true)
p.SetAutoTs(0)
},
Op: func(key interface{}) {
p := h.pMgr.Get(key)
if p == nil {
return
}
if !p.Auto() || !p.Op() {
return
}
p.SetOp(false)
h.game.PlayerAuto(key)
},
})
h.pMgr.Watch(pmgr.WatchKindLine, func(key interface{}, p *player.Player) {
if h.event != nil && h.event.PlayerLine != nil {
h.event.PlayerLine(h, key, h.pMgr, h.extend)
}
// 玩家踢出
h.PlayerKickOut(key)
})
h.pMgr.Watch(pmgr.WatchKindReady, func(key interface{}, p *player.Player) {
if h.event != nil && h.event.PlayerReady != nil {
h.event.PlayerReady(h, key, h.pMgr, h.extend)
}
})
h.pMgr.Watch(pmgr.WatchKindOp, func(key interface{}, p *player.Player) {
if h.event != nil && h.event.PlayerOp != nil {
h.event.PlayerOp(h, key, h.pMgr, h.extend)
}
if p.Op() {
h.AutoPlayer(key)
}
})
h.pMgr.Watch(pmgr.WatchKindAuto, func(key interface{}, p *player.Player) {
if h.event != nil && h.event.PlayerAuto != nil {
h.event.PlayerAuto(h, key, h.pMgr, h.extend)
}
if p.Op() {
switch p.Auto() {
case true:
h.AutoPlayer(key)
default:
h.AutoPlayerDel(key)
h.AutoPlayer(key)
}
}
})
h.pMgr.Watch(pmgr.WatchKindScore, func(key interface{}, p *player.Player) {
if h.event != nil && h.event.PlayerScore != nil {
h.event.PlayerScore(h, key, h.pMgr, h.extend)
}
})
h.pMgr.Watch(pmgr.WatchKindSite, func(key interface{}, p *player.Player) {
if h.event != nil && h.event.PlayerSite != nil {
h.event.PlayerSite(h, key, h.pMgr, h.extend)
}
})
if h.log == nil {
if err := h.LogSetting("", log.LevelDebug); err != nil {
return err
}
}
return h.game.Init(h.ctx, h, h.pMgr, h.playerMsg, h.log)
}
func (h *_Happy) Run(resume bool) {
defer func() {
err := recover()
switch err {
case nil:
case PanicDoneExit:
h.log.Info("done exit")
default:
var buf = make([]byte, 4096)
n := runtime.Stack(buf, false)
buf = buf[:n]
h.log.Err("run fail",
log.Bool("resume", resume),
log.String("err", fmt.Sprintln(err)),
log.ByteString("stack", buf))
}
}()
if resume {
time.Sleep(200 * time.Millisecond)
if h.playerMsg != nil {
userKeys := make([]interface{}, 0, h.pMgr.Len())
h.pMgr.Range(func(key interface{}, p *player.Player) bool {
userKeys = append(userKeys, key)
return true
})
h.playerMsg.ReConn(h.ctx, userKeys)
}
if h.begin {
h.begin = false
h.curRound--
h.RoundBegin(true, false)
}
}
h.Loop()
}
func (h *_Happy) Loop() {
var msg *proxy.Msg
Loop:
for {
if h.msgChan == nil {
break Loop
}
select {
case msg = <-h.msgChan:
if msg == nil {
continue
}
h.MsgHandler(msg)
case <-h.delay.Done():
h.delay.Handler()
case <-h.ctx.Done():
h.Finish(false)
}
}
}