-
Notifications
You must be signed in to change notification settings - Fork 5
/
matchmaking.go
665 lines (553 loc) · 18.6 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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
package main
import (
"fmt"
"github.com/ChrisHines/GoSkills/skills"
"github.com/ChrisHines/GoSkills/skills/trueskill"
"log"
"math"
"math/rand"
"os"
"path"
"sort"
"sync"
"time"
)
// shut up the unused package warning
var _ = log.Ldate
//Matchmaking regions are for bitwise comparisons
const (
MATCHMAKING_REGION_NA = 1
MATCHMAKING_REGION_EU = 2
MATCHMAKING_REGION_KR = 4
MATCHMAKING_REGION_SEA = 8
MATCHMAKING_REGION_CN = 16
MATCHMAKING_TYPE_1V1 = 1
MATCHMAKING_LONG_PROCESS_NOSHOW = 1
MATCHMAKING_LONG_PROCESS_DRAW = 2
)
var (
matchmakingMatchTimeout int64 = 2 * 60 * 60
// The time before long processes can be launched
matchmakingLongProcessUnlockTime int64 = 60
// The time that a long process takes.
matchmakingLongProcessResponseTime int64 = 240
matchmakingRatingScalePerSecond float64 = 0.08
matchmakingRadiusMultiplier float64 = 5.00
)
type Matchmaker struct {
// The actual matchmaker
register chan *ClientConnection
callback chan bool
unregister chan *ClientConnection
participants map[*ClientConnection]*MatchmakerParticipant
regionParticipants map[BattleNetRegion]map[*ClientConnection]*MatchmakerParticipant
matchCache map[int64]*MatchmakerMatch
matchParticipantCache map[int64]*MatchmakerMatchParticipant
matchParticipantsCache map[int64][]*MatchmakerMatchParticipant
logger *log.Logger
logFile *os.File
sync.RWMutex
}
type MatchmakerParticipant struct {
connection *ClientConnection
client *Client
enrollTime time.Time // We track when this started, so
team skills.Team
points int64
rating float64
radius int64 // x * points per division
regions []BattleNetRegion
queueType int64 // 1v1, 2v2
match chan *MatchmakerMatch
abort chan bool
matching bool
vetoes []*Map
opponent *MatchmakerParticipant
selectedMap *Map
}
type MatchmakerPotentialMatch struct {
opponent *MatchmakerParticipant
ratingDifference float64
region BattleNetRegion
}
type MatchmakerMatch struct {
Id int64
MapId *int64
AddTime int64
EndTime int64
Quality float64
Region BattleNetRegion
Channel string
ChatRoom string
longProcessCount int64 `db:"-"`
longProcessType int64 `db:"-"`
longProcessActive bool `db:"-"`
longProcessInitiator *Client `db:"-"`
longProcessResponse chan bool `db:"-"`
}
type MatchmakerMatchParticipant struct {
Id int64
MatchId *int64
ClientId *int64
Points int64
RatingMean float64
RatingStdDev float64
QueueTime float64
}
func initMatchmaking() {
matchmaker = &Matchmaker{
register: make(chan *ClientConnection, 256),
callback: make(chan bool, 256),
unregister: make(chan *ClientConnection),
participants: make(map[*ClientConnection]*MatchmakerParticipant),
regionParticipants: map[BattleNetRegion]map[*ClientConnection]*MatchmakerParticipant{
BATTLENET_REGION_NA: make(map[*ClientConnection]*MatchmakerParticipant),
BATTLENET_REGION_EU: make(map[*ClientConnection]*MatchmakerParticipant),
BATTLENET_REGION_KR: make(map[*ClientConnection]*MatchmakerParticipant),
BATTLENET_REGION_CN: make(map[*ClientConnection]*MatchmakerParticipant),
BATTLENET_REGION_SEA: make(map[*ClientConnection]*MatchmakerParticipant),
},
matchCache: make(map[int64]*MatchmakerMatch),
matchParticipantCache: make(map[int64]*MatchmakerMatchParticipant),
matchParticipantsCache: make(map[int64][]*MatchmakerMatchParticipant),
}
var logfile string = path.Join(logPath, fmt.Sprintf("%d-mm.log", os.Getpid()))
file, err := os.Create(logfile)
if err != nil {
log.Println("Failed to create log file", logfile, "for matchmaker")
matchmaker.logger = log.New(os.Stdout, fmt.Sprintf("mm"), log.Ldate|log.Ltime|log.Lshortfile)
} else {
log.Println("Logging matchmaker to", logfile)
matchmaker.logger = log.New(file, "", log.Ldate|log.Ltime|log.Lshortfile)
matchmaker.logFile = file
}
go matchmaker.run()
}
func NewMatchmakerParticipant(connection *ClientConnection) *MatchmakerParticipant {
// TrueSkill stuff
team := skills.NewTeam()
team.AddPlayer(connection.client.Id, skills.NewRating(connection.client.RatingMean, connection.client.RatingStdDev))
return &MatchmakerParticipant{
connection: connection,
client: connection.client,
enrollTime: time.Now(),
team: team,
points: connection.client.LadderPoints,
rating: connection.client.RatingMean,
// radius: connection.client.LadderSearchRadius,
radius: 0, // Fast fix for new radius
regions: connection.client.LadderSearchRegions,
matching: false,
abort: make(chan bool),
match: make(chan *MatchmakerMatch),
}
}
func (self *MatchmakerMatch) CanLongProcess() bool {
return (time.Now().Unix() - self.AddTime) >= matchmakingLongProcessUnlockTime
}
func (self *MatchmakerMatch) EndMatch() {
matchmaker.EndMatch(self.Id)
}
func (self *MatchmakerMatch) longProcessProc(initiator *Client, process int) {
self.longProcessActive = false
if process == MATCHMAKING_LONG_PROCESS_DRAW {
matchmaker.logger.Println("Game", self.Id, "ended with no result")
self.EndMatch()
} else if process == MATCHMAKING_LONG_PROCESS_NOSHOW {
matchmaker.logger.Println("Game", self.Id, "ended with walkover for", initiator.Id, initiator.Username)
var opponent *Client
if initiator.PendingMatchmakingOpponentId != nil {
opponent = clientCache.Get(*initiator.PendingMatchmakingOpponentId)
}
if opponent == nil {
self.EndMatch()
} else {
self.CreateForfeit(opponent)
}
}
}
func (self *MatchmakerMatch) StartLongProcess(initiator *Client, process int) bool {
if !self.CanLongProcess() {
return false
}
if self.longProcessActive {
return false
}
matchmaker.logger.Println("Game", self.Id, "long process", process, "requested by", initiator.Id, initiator.Username)
self.longProcessCount += 1
self.longProcessActive = true
self.longProcessInitiator = initiator
self.longProcessResponse = make(chan bool)
// If they're spamming the feature just abort the match.
if self.longProcessCount == 3 {
matchmaker.logger.Println("Game", self.Id, "long process spam aborted game")
self.EndMatch()
return true
}
go func() {
timer := time.NewTimer(time.Second * time.Duration(matchmakingLongProcessResponseTime))
select {
case <-timer.C:
self.longProcessProc(initiator, process)
case response := <-self.longProcessResponse:
matchmaker.logger.Println("Game", self.Id, "long process client response:", response)
// Client has responded to us. Continue if they confirm the long process.
if response {
self.longProcessProc(initiator, process)
}
self.longProcessActive = false
return
}
}()
return true
}
func (mmm *MatchmakerMatch) CreateForfeit(client *Client) (result *MatchResult, players []*MatchResultPlayer, err error) {
matchmaker.logger.Println("Forfeiting", client.Id, client.Username)
// Attempt to find the opponent.
var opponentClient *Client
if client.PendingMatchmakingOpponentId != nil {
opponentClient = clientCache.Get(*client.PendingMatchmakingOpponentId)
}
if opponentClient == nil {
matchmaker.logger.Println("Opponent client not found")
return nil, nil, ErrLadderPlayerNotFound
}
// Attempt to find the region stats records for each player
playerRegion, err := client.RegionStats(mmm.Region)
if playerRegion == nil {
return nil, nil, ErrLadderPlayerNotFound
}
opponentRegion, err := opponentClient.RegionStats(mmm.Region)
if opponentRegion == nil {
return nil, nil, ErrLadderPlayerNotFound
}
// Attempt to record a new match result
result = &MatchResult{
DateTime: time.Now().Unix(),
MapId: mmm.MapId,
MatchmakerMatchId: &mmm.Id,
Region: mmm.Region,
}
err = dbMap.Insert(result)
if err != nil {
matchmaker.logger.Println("Forfeit insert error", err)
return nil, nil, ErrDbInsert
}
// Create new match result players
var player, opponent MatchResultPlayer
player.MatchId = &result.Id
player.ClientId = &client.Id
player.Victory = false
player.Race = "Forfeit"
opponent.MatchId = &result.Id
opponent.ClientId = &opponentClient.Id
opponent.Victory = true
opponent.Race = "Walkover"
// Make the necessary record/point adjustments for a forfeit
player.PointsBefore = playerRegion.LadderPoints
opponent.PointsBefore = opponentRegion.LadderPoints
client.ForfeitMatchmadeMatch()
player.PointsAfter = playerRegion.LadderPoints
opponent.PointsAfter = opponentRegion.LadderPoints
player.PointsDifference = player.PointsAfter - player.PointsBefore
opponent.PointsDifference = opponent.PointsAfter - opponent.PointsBefore
// Update our regional stats
playerRegion.Forfeits += 1
opponentRegion.Walkovers += 1
_, uerr := dbMap.Update(playerRegion, opponentRegion)
if uerr != nil {
matchmaker.logger.Println(uerr)
return nil, nil, ErrDbInsert
}
// Remove pending matches from both clients.
client.PendingMatchmakingId = nil
client.PendingMatchmakingOpponentId = nil
opponentClient.PendingMatchmakingId = nil
opponentClient.PendingMatchmakingOpponentId = nil
_, uerr = dbMap.Update(client, opponentClient)
if uerr != nil {
matchmaker.logger.Println(uerr)
return nil, nil, ErrDbInsert
}
// Insert our match result players
err = dbMap.Insert(&player, &opponent)
if err != nil {
matchmaker.logger.Println(err)
return nil, nil, ErrDbInsert
}
players = []*MatchResultPlayer{&player, &opponent}
return
}
func (mm *Matchmaker) Match(id int64) *MatchmakerMatch {
match, ok := mm.matchCache[id]
if !ok {
match = &MatchmakerMatch{}
err := dbMap.SelectOne(match, "SELECT * FROM matchmaker_matches WHERE Id=? LIMIT 1", id)
if err != nil || match.Id == 0 {
return nil
}
mm.matchCache[id] = match
match.longProcessActive = false
}
return match
}
func (mm *Matchmaker) MatchParticipant(id int64) *MatchmakerMatchParticipant {
m, ok := mm.matchParticipantCache[id]
if !ok {
m = &MatchmakerMatchParticipant{}
err := dbMap.SelectOne(m, "SELECT * FROM matchmaker_match_participants WHERE Id=? LIMIT 1", id)
if err != nil || m.Id == 0 {
return nil
}
mm.matchParticipantCache[id] = m
}
return m
}
func (mm *Matchmaker) MatchParticipants(id int64) []*MatchmakerMatchParticipant {
m, ok := mm.matchParticipantsCache[id]
if !ok {
res, err := dbMap.Select(&MatchmakerMatchParticipant{}, "SELECT * FROM matchmaker_match_participants WHERE MatchId=?", id)
m = make([]*MatchmakerMatchParticipant, 0, len(res))
if err != nil {
return nil
}
for x := range res {
m = append(m, res[x].(*MatchmakerMatchParticipant))
}
mm.matchParticipantsCache[id] = m
}
return m
}
func (mm *Matchmaker) EndMatch(id int64) {
match := mm.Match(id)
if match != nil {
participants := mm.MatchParticipants(id)
match.EndTime = time.Now().Unix()
for x := range participants {
if participants[x] == nil {
log.Println("Nil participant for", id)
continue
}
var client *Client = nil
if participants[x].ClientId != nil {
client = clientCache.Get(*participants[x].ClientId)
}
if client.PendingMatchmakingId != nil && *client.PendingMatchmakingId == id {
client.PendingMatchmakingId = nil
client.PendingMatchmakingOpponentId = nil
client.PendingMatchmakingRegion = 0
dbMap.Update(client)
go client.Broadcast("MMI", nil)
}
}
dbMap.Update(match)
}
}
//Match 2 players against each other.
func (mm *Matchmaker) makeMatch(player1, player2 *MatchmakerParticipant, region BattleNetRegion) {
quality := player1.Quality(player2)
go func() {
mm.unregister <- player1.connection
mm.unregister <- player2.connection
}()
vetoes1, _ := player1.connection.client.Vetoes()
vetoes2, _ := player2.connection.client.Vetoes()
selectedMap := maps.Random(region, vetoes1, vetoes2)
if selectedMap == nil {
selectedMap = maps.Random(region)
if selectedMap == nil {
log.Println("No map found while matching", player1.client.Username, player2.client.Username)
go func() {
player1.abort <- true
}()
go func() {
player2.abort <- true
}()
return
}
}
battleNetChannel := fmt.Sprintf("eros%d%d%d%d", region, player1.client.Id, player2.client.Id, rand.Intn(99))
erosChatRoom := cleanChatRoomName(fmt.Sprintf("MM%d%d%d", region, player1.client.Id, player2.client.Id))
player1.opponent = player2
player2.opponent = player1
player1.selectedMap = selectedMap
player2.selectedMap = selectedMap
var match MatchmakerMatch
match.AddTime = time.Now().Unix()
match.Quality = quality
match.Region = region
match.MapId = &selectedMap.Id
match.Channel = battleNetChannel
match.longProcessActive = false
room := GetChatRoom(erosChatRoom, "", false, false)
if room != nil {
match.ChatRoom = erosChatRoom
}
err := dbMap.Insert(&match)
mm.matchCache[match.Id] = &match
if err == nil {
var p1, p2 MatchmakerMatchParticipant
p1time := time.Since(player1.enrollTime)
p2time := time.Since(player2.enrollTime)
p1.MatchId = &match.Id
p2.MatchId = &match.Id
p1.ClientId = &player1.connection.client.Id
p2.ClientId = &player2.connection.client.Id
p1.Points = player1.points
p2.Points = player2.points
p1.RatingMean = player1.connection.client.RatingMean
p2.RatingMean = player2.connection.client.RatingMean
p1.RatingStdDev = player1.connection.client.RatingStdDev
p2.RatingStdDev = player2.connection.client.RatingStdDev
p1.QueueTime = p1time.Seconds()
p2.QueueTime = p2time.Seconds()
err = dbMap.Insert(&p1, &p2)
if err != nil {
mm.matchParticipantCache[p1.Id] = &p1
mm.matchParticipantCache[p2.Id] = &p2
mm.matchParticipantsCache[match.Id] = []*MatchmakerMatchParticipant{&p1, &p2}
matchmaker.logger.Println("Insert error", err)
}
} else {
matchmaker.logger.Println("Insert failed", err)
}
go func() {
player1.match <- &match
}()
go func() {
player2.match <- &match
}()
}
// Matchmaking worker
func (mm *Matchmaker) run() {
ticker := time.NewTicker(time.Second * 1)
go func() {
for {
//Go primer:
//Select works similarly to switch syntactically, but uses channels.
//Whichever route presents a value first will be executed.
//A channel is (in this case) a fifo that links goroutines.
select {
case <-ticker.C:
// Maintain a list of participants we've already looped through.
// Any comparisons against them will be duplicate.
//TODO: We can use the regional map if required in the future.
complete := make([]*MatchmakerParticipant, 0, len(mm.participants))
for k, v := range mm.participants {
if v.matching {
continue
}
potentials := make([]MatchmakerPotentialMatch, 0, len(mm.participants))
outer:
for l, w := range mm.participants {
// Check we're not comparing ourself
if k == l {
continue
}
// Check we're not the same client.
if k.client.Id == l.client.Id {
continue
}
// Check we're not already matched.
if w.matching {
continue
}
//Check against overall complete participants and skip them
for _, x := range complete {
if x == w {
// Continue the loop above this one
continue outer
}
}
//log.Println("Compare", k.client.Id, "to", l.client.Id, "quality", quality, b1, b2)
if match, regions := v.IsMatch(w); match {
potentials = append(potentials, MatchmakerPotentialMatch{opponent: w,
ratingDifference: math.Abs(float64(v.rating - w.rating)),
region: regions[rand.Intn(len(regions))],
})
}
}
// If we have potential matches, find the lowest difference and match them.
if len(potentials) > 0 {
sort.Sort(ByRatingDifference(potentials))
x := potentials[0].opponent
v.matching = true
x.matching = true
go mm.makeMatch(v, x, potentials[0].region)
}
//Mark our scent
complete = append(complete, v)
}
case client := <-mm.register:
delete(mm.participants, client)
delete(mm.regionParticipants[BATTLENET_REGION_NA], client)
delete(mm.regionParticipants[BATTLENET_REGION_EU], client)
delete(mm.regionParticipants[BATTLENET_REGION_KR], client)
delete(mm.regionParticipants[BATTLENET_REGION_CN], client)
delete(mm.regionParticipants[BATTLENET_REGION_SEA], client)
mm.participants[client] = NewMatchmakerParticipant(client)
for _, region := range client.client.LadderSearchRegions {
mm.regionParticipants[region][client] = mm.participants[client]
}
go func() {
mm.callback <- true
}()
case client := <-mm.unregister:
delete(mm.participants, client)
delete(mm.regionParticipants[BATTLENET_REGION_NA], client)
delete(mm.regionParticipants[BATTLENET_REGION_EU], client)
delete(mm.regionParticipants[BATTLENET_REGION_KR], client)
delete(mm.regionParticipants[BATTLENET_REGION_CN], client)
delete(mm.regionParticipants[BATTLENET_REGION_SEA], client)
}
}
}()
}
func (mp *MatchmakerParticipant) Quality(opponent *MatchmakerParticipant) float64 {
teams := []skills.Team{mp.team, opponent.team}
var calc trueskill.TwoPlayerCalc
quality := calc.CalcMatchQual(skills.DefaultGameInfo, teams)
return quality
}
// Worst math ever
func (mp *MatchmakerParticipant) SearchBoundaries() (maxDifference, variance float64) {
var (
elapsed = float64(time.Since(mp.enrollTime).Seconds())
r float64
)
r = 1 + float64(matchmakingRatingScalePerSecond*elapsed)
matchmakingRadiusMultiplier := math.Floor(elapsed / matchmakingRadiusMultiplier)
return matchmakingRadiusMultiplier, r
}
func (mp *MatchmakerParticipant) IsMatch(mp2 *MatchmakerParticipant) (match bool, regions []BattleNetRegion) {
match = false
regions = make([]BattleNetRegion, 0, 5)
for _, x := range mp.regions {
for _, y := range mp2.regions {
if x == y {
regions = append(regions, x)
break
}
}
}
if len(regions) == 0 {
return
}
r1 := mp.rating
r2 := mp2.rating
d1, _ := divisions.GetDivision(mp.points)
d2, _ := divisions.GetDivision(mp2.points)
// mp1d, r1v := mp.SearchBoundaries()
// mp2d, r2v := mp2.SearchBoundaries()
_, r1v := mp.SearchBoundaries()
_, r2v := mp2.SearchBoundaries()
diff := math.Abs(float64(ladder.GetDifference(d1, d2)))
r1Match := (r1+r1v >= r2) && (r1-r1v <= r2) && (mp.radius == 0 || diff < float64(mp.radius)) // && diff <= mp1d
r2Match := (r2+r2v >= r1) && (r2-r2v <= r1) && (mp2.radius == 0 || diff < float64(mp2.radius)) // && diff <= mp2d
match = r1Match && r2Match
return
}
type ByRatingDifference []MatchmakerPotentialMatch
func (a ByRatingDifference) Len() int { return len(a) }
func (a ByRatingDifference) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByRatingDifference) Less(i, j int) bool { return a[i].ratingDifference < a[j].ratingDifference }