/
matchmaking.go
419 lines (343 loc) · 10.2 KB
/
matchmaking.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
// entities
// @author LanguageY++2013 2022/11/8 09:49
// @company soulgame
package entities
import (
"fmt"
"github.com/Languege/flexmatch/service/match/proto/open"
"github.com/juju/errors"
"log"
"sort"
"sync"
"time"
"github.com/Languege/flexmatch/service/match/pubsub"
"github.com/Languege/flexmatch/common/logger"
)
type Matchmaking struct {
//对局配置
Conf *open.MatchmakingConfiguration
//规则集封装
rsw *MatchmakingRuleSetWrapper
//事件处理通知
eventPubs pubsub.MultiPublisher
//票据队列
TicketQueue TicketQueue
//票据批次通道
batchTicketChan chan []*open.MatchmakingTicket
//票据批次处理器 (TODO:如何根据负载动态调整处理器的数量?)
BatchTicketProcessors []*BatchTicketProcessor
//票据字典 key-票据唯一ID (TODO:所有媒介共用一个,还是每个媒介独立)
ticketMap map[string]*open.MatchmakingTicket
//票据并发保护
ticketGuard *sync.Mutex
//对局字典 key-对局唯一ID
matchMap map[string]*Match
//对局数据保护
matchGuard *sync.Mutex
//匹配成功计数
MatchSucceedCounter int64
}
func NewMatchmaking(conf *open.MatchmakingConfiguration) *Matchmaking {
e := &Matchmaking{
Conf: conf,
//eventPubs: newMatcheventPubscribeManager(conf.MatchEventQueueTopic),
batchTicketChan: make(chan []*open.MatchmakingTicket, 10),
ticketMap: map[string]*open.MatchmakingTicket{},
ticketGuard: &sync.Mutex{},
matchMap: map[string]*Match{},
matchGuard: &sync.Mutex{},
}
//规则集解析封装
e.rsw = NewMatchmakingRuleSetWrapper(e.Conf.RuleSet)
e.TicketQueue = NewRealTicketQueue(e.batchTicketChan, e.rsw.MatchPlayerNum,
WithTicketQueueTimeout(e.Conf.RequestTimeoutSeconds))
//启动批前排序监听,对批次进行排序化划分
go e.TicketWatch()
//根据配置启动处理器相关
e.BatchTicketProcessors = []*BatchTicketProcessor{
MewBatchTicketProcessor(e),
}
return e
}
// TicketInput 票据输入
func (e *Matchmaking) TicketInput(ticket *open.MatchmakingTicket) (err error) {
//保存票据
err = e.TicketSave(ticket)
if err != nil {
err = errors.Trace(err)
return
}
return e.TicketQueued(ticket)
}
// TicketQueued 票据入队列
func (e *Matchmaking) TicketQueued(ticket *open.MatchmakingTicket) (err error) {
//票据入队列
err = e.TicketQueue.Input(ticket)
if err != nil {
err = errors.Trace(err)
return
}
//更新票据状态-队列中 (默认值)
ticket.Status = open.MatchmakingTicketStatus_QUEUED.String()
//设置预计时间
ticket.EstimatedWaitTime = e.EstimatedWaitTime()
//queued事件上报
queuedEvent := &open.MatchEvent{
MatchEventType: open.MatchEventType_MatchmakingQueued,
Tickets: []*open.MatchmakingTicket{ticket},
}
publisher.Send(e.Conf.MatchEventQueueTopic, queuedEvent)
return
}
// checkTicketsTimeout 检测票据是否超时
func (e *Matchmaking) checkTicketsTimeout(tickets []*open.MatchmakingTicket) (ret []*open.MatchmakingTicket) {
ret = make([]*open.MatchmakingTicket, 0, len(tickets))
now := time.Now().Unix()
timeoutTickets := make([]*open.MatchmakingTicket, 0, len(tickets)/2)
for _, t := range tickets {
if now-t.StartTime >= e.Conf.RequestTimeoutSeconds {
t.Status = open.MatchmakingTicketStatus_TIMED_OUT.String()
timeoutTickets = append(timeoutTickets, t)
continue
}
ret = append(ret, t)
}
if len(timeoutTickets) > 0 {
//构建票据超时事件,并推送到事件队列
timeOutEvent := &open.MatchEvent{
MatchEventType: open.MatchEventType_MatchmakingTimedOut,
Tickets: timeoutTickets,
Reason: "TimedOut",
Message: "tickets are timeout when they were popped from batch tickets channel",
}
publisher.Send(e.Conf.MatchEventQueueTopic, timeOutEvent)
}
return
}
// preBatchSort 批前排序
func (e *Matchmaking) preBatchSort(tickets []*open.MatchmakingTicket) {
if e.rsw.preBatchSortAlgorithm == nil || len(e.rsw.Algorithm.SortByAttributes) == 0 {
return
}
//排序属性
sort.Slice(tickets, func(i, j int) bool {
//获取票据属性
for _, attrName := range e.rsw.Algorithm.SortByAttributes {
vi := getTicketFloatAttributeValue(tickets[i], attrName, "avg")
vj := getTicketFloatAttributeValue(tickets[j], attrName, "avg")
if vi != vj {
return vi < vj
}
}
return false
})
}
func (e *Matchmaking) batchProcessorNum() int {
return len(e.BatchTicketProcessors)
}
// randomDivideBatch 随机划分批次
func (e *Matchmaking) randomDivideBatch(tickets []*open.MatchmakingTicket) (batches []*[]*open.MatchmakingTicket) {
batchProcessorNum := e.batchProcessorNum()
batchSize := len(tickets) / batchProcessorNum
if batchSize < int(e.rsw.MatchPlayerNum) {
batchSize = int(e.rsw.MatchPlayerNum)
}
if batchSize >= len(tickets) {
return []*[]*open.MatchmakingTicket{&tickets}
}
batchNum := len(tickets) / batchSize
for i := 0; i < batchNum; i++ {
var batch []*open.MatchmakingTicket
if i < batchNum-1 {
batch = tickets[i*batchSize : (i+1)*batchSize]
} else {
batch = tickets[i*batchSize : (i+1)*batchSize]
}
batches = append(batches, &batch)
}
return
}
// divideBatch 划分批次
func (e *Matchmaking) divideBatch(tickets []*open.MatchmakingTicket) (batches []*[]*open.MatchmakingTicket) {
if e.rsw.batchDistanceRule != nil {
//设置了批次距离规则
batches = e.rsw.DivideBatch(tickets)
return
}
//默认规则,随机划分批次
return e.randomDivideBatch(tickets)
}
// TicketWatch 信号监听:
// 1. 票据输入
// 2. 票据超时检测
// 3. 关闭检测
func (e *Matchmaking) TicketWatch() {
defer func() {
if r := recover();r != nil {
logger.DPanicf("TicketWatch recover %s", r)
}
}()
for {
select {
case tickets, ok := <-e.batchTicketChan:
if !ok && len(tickets) == 0 {
log.Println("票据已关闭并且队列内无缓冲数据,协程退出")
return
}
//票据超时检测
tickets = e.checkTicketsTimeout(tickets)
if len(tickets) == 0 {
break
}
//修改票据状态为-SEARCHING
e.TicketSearching(tickets)
//批前排序算法
e.preBatchSort(tickets)
//划分批次
batches := e.divideBatch(tickets)
//随机分配到批次处理器
e.DispatchBatches(batches)
}
}
}
func (e *Matchmaking) TicketSearching(tickets []*open.MatchmakingTicket) {
//更新状态
for _, ticket := range tickets {
ticket.Status = open.MatchmakingTicketStatus_SEARCHING.String()
}
//发送事件
ev := &open.MatchEvent{
MatchEventType: open.MatchEventType_MatchmakingSearching,
Tickets: tickets,
EstimatedWaitMillis: 5000,
}
publisher.Send(e.Conf.MatchEventQueueTopic, ev)
}
// DispatchBatches 票据批次分派
func (e *Matchmaking) DispatchBatches(batches []*[]*open.MatchmakingTicket) {
for _, batch := range batches {
e.BatchTicketProcessors[0].Input(*batch)
}
}
// 数据清理
func (e *Matchmaking) CleanUp() {
}
// 配置重载
func (e *Matchmaking) Reload(conf *open.MatchmakingConfiguration) {
}
// TicketFind 票据查找
func (t *Matchmaking) TicketFind(ticketId string) (ticket *open.MatchmakingTicket, ok bool) {
t.ticketGuard.Lock()
defer t.ticketGuard.Unlock()
ticket, ok = t.ticketMap[ticketId]
return
}
// TicketInsert 票据插入
func (t *Matchmaking) TicketInsert(ticket *open.MatchmakingTicket) {
t.ticketGuard.Lock()
defer t.ticketGuard.Unlock()
t.ticketMap[ticket.TicketId] = ticket
return
}
// TicketSave 票据保存
func (t *Matchmaking) TicketSave(ticket *open.MatchmakingTicket) error {
t.ticketGuard.Lock()
defer t.ticketGuard.Unlock()
_, ok := t.ticketMap[ticket.TicketId]
if ok {
return fmt.Errorf("ticket %s has exist", ticket.TicketId)
}
if (ticket.Status == "" || ticket.Status == open.MatchmakingTicketStatus_QUEUED.String()) &&
ticket.StartTime == 0 {
ticket.StartTime = time.Now().Unix()
}
t.ticketMap[ticket.TicketId] = ticket
return nil
}
//TicketRemove 票据删除
func (t *Matchmaking) TicketRemove(ticketId string) {
t.ticketGuard.Lock()
defer t.ticketGuard.Unlock()
delete(t.ticketMap, ticketId)
}
// MatchInsert 对局数据插入
func (e *Matchmaking) MatchInsert(match *Match) {
e.matchGuard.Lock()
defer e.matchGuard.Unlock()
e.matchMap[match.MatchId] = match
}
// MatchFind 匹配查找
func (e *Matchmaking) MatchFind(matchId string) (match *Match, ok bool) {
e.matchGuard.Lock()
defer e.matchGuard.Unlock()
match, ok = e.matchMap[matchId]
return
}
//MatchRemove 匹配移除,接收匹配完成时调用(接受超时、全部接收、任意拒绝)
func (e *Matchmaking) MatchRemove(matchId string) {
e.matchGuard.Lock()
defer e.matchGuard.Unlock()
delete(e.matchMap, matchId)
return
}
// AcceptMatch 玩家接受对局
func (e *Matchmaking) AcceptMatch(ticketId string, userId int64, acceptType open.AcceptanceType) (err error) {
var (
ticket *open.MatchmakingTicket
match *Match
ok bool
)
//查找票据
ticket, ok = e.TicketFind(ticketId)
if !ok {
err = errors.Trace(fmt.Errorf("ticket %s not exist", ticketId))
return
}
//查找对局
match, ok = e.MatchFind(ticket.MatchId)
if !ok {
err = errors.Trace(fmt.Errorf("match %s not exist", ticket.MatchId))
return
}
//更新玩家接受对局状态
for _, player := range ticket.Players {
if player.UserId == userId {
player.Accepted = acceptType == open.AcceptanceType_ACCEPT
if player.Accepted {
//对局接受事件上报
match.AcceptMatch(ticketId)
} else {
//直接取消对局
match.RejectMatch(ticketId)
}
break
}
}
return
}
// StopMatch 停止/取消匹配
func (m *Matchmaking) StopMatch(ticketId string) (err error) {
//查看票据是否存在
ticket, ok := m.TicketFind(ticketId)
if !ok {
err = fmt.Errorf("ticket %s not exit", ticketId)
return
}
//验证用户是否可以取消匹配,不可取消匹配的阶段:PLACING、COMPLETED
if ticket.Status == open.MatchmakingTicketStatus_PLACING.String() ||
ticket.Status == open.MatchmakingTicketStatus_COMPLETED.String() {
err = fmt.Errorf("ticket %s cannot be canceled when it's in %s", ticketId, ticket.Status)
return
}
//标识取消请求
ticket.CancelRequest = true
return
}
//EstimatedWaitTime 预计耗时
func (m *Matchmaking) EstimatedWaitTime() int64 {
var total int64
for _, p := range m.BatchTicketProcessors {
total += p.EstimatedWaitTime()
}
return total / int64(len(m.BatchTicketProcessors))
}