forked from heroiclabs/nakama-project-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatch_handler.go
527 lines (459 loc) · 15.3 KB
/
match_handler.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
// Copyright 2020 The Nakama Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"context"
"database/sql"
"encoding/json"
"math/rand"
"time"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
"github.com/heroiclabs/nakama-common/runtime"
"github.com/heroiclabs/nakama-project-template/api"
)
const (
moduleName = "tic-tac-toe"
tickRate = 5
maxEmptySec = 30
delayBetweenGamesSec = 5
turnTimeFastSec = 10
turnTimeNormalSec = 20
)
var winningPositions = [][]int32{
{0, 1, 2},
{3, 4, 5},
{6, 7, 8},
{0, 3, 6},
{1, 4, 7},
{2, 5, 8},
{0, 4, 8},
{2, 4, 6},
}
// Compile-time check to make sure all required functions are implemented.
var _ runtime.Match = &MatchHandler{}
type MatchLabel struct {
Open int `json:"open"`
Fast int `json:"fast"`
}
type MatchHandler struct {
marshaler *protojson.MarshalOptions
unmarshaler *protojson.UnmarshalOptions
tfServingAddress string
}
type MatchState struct {
random *rand.Rand
label *MatchLabel
emptyTicks int
ai bool
messages chan runtime.MatchData
// Currently connected users, or reserved spaces.
presences map[string]runtime.Presence
// Number of users currently in the process of connecting to the match.
joinsInProgress int
// True if there's a game currently in progress.
playing bool
// Current state of the board.
board []api.Mark
// Mark assignments to player user IDs.
marks map[string]api.Mark
// Whose turn it currently is.
mark api.Mark
// Ticks until they must submit their move.
deadlineRemainingTicks int64
// The winner of the current game.
winner api.Mark
// The winner positions.
winnerPositions []int32
// Ticks until the next game starts, if applicable.
nextGameRemainingTicks int64
}
func (ms *MatchState) ConnectedCount() int {
count := 0
for _, p := range ms.presences {
if p != nil {
count++
}
}
return count
}
func (m *MatchHandler) MatchInit(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, params map[string]interface{}) (interface{}, int, string) {
fast, ok := params["fast"].(bool)
if !ok {
logger.Error("invalid match init parameter \"fast\"")
return nil, 0, ""
}
ai, _ := params["ai"].(bool)
label := &MatchLabel{
Open: 1,
}
if fast {
label.Fast = 1
}
labelJSON, err := json.Marshal(label)
if err != nil {
logger.WithField("error", err).Error("match init failed")
labelJSON = []byte("{}")
}
state := &MatchState{
random: rand.New(rand.NewSource(time.Now().UnixNano())),
label: label,
ai: ai,
presences: make(map[string]runtime.Presence, 2),
messages: make(chan runtime.MatchData, 1),
}
// Automatically add AI player
if ai {
state.presences[aiUserId] = aiPresenceObj
}
return state, tickRate, string(labelJSON)
}
func (m *MatchHandler) MatchJoinAttempt(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, dispatcher runtime.MatchDispatcher, tick int64, state interface{}, presence runtime.Presence, metadata map[string]string) (interface{}, bool, string) {
s := state.(*MatchState)
// Check if it's a user attempting to rejoin after a disconnect.
if presence, ok := s.presences[presence.GetUserId()]; ok {
if presence == nil {
// User rejoining after a disconnect.
s.joinsInProgress++
return s, true, ""
} else {
// User attempting to join from 2 different devices at the same time.
return s, false, "already joined"
}
}
// Check if match is full.
if len(s.presences)+s.joinsInProgress >= 2 {
return s, false, "match full"
}
// New player attempting to connect.
s.joinsInProgress++
return s, true, ""
}
func (m *MatchHandler) MatchJoin(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, dispatcher runtime.MatchDispatcher, tick int64, state interface{}, presences []runtime.Presence) interface{} {
s := state.(*MatchState)
t := time.Now().UTC()
for _, presence := range presences {
s.emptyTicks = 0
s.presences[presence.GetUserId()] = presence
s.joinsInProgress--
// Check if we must send a message to this user to update them on the current game state.
var opCode api.OpCode
var msg proto.Message
if s.playing {
// There's a game still currently in progress, the player is re-joining after a disconnect. Give them a state update.
opCode = api.OpCode_OPCODE_UPDATE
msg = &api.Update{
Board: s.board,
Mark: s.mark,
Deadline: t.Add(time.Duration(s.deadlineRemainingTicks/tickRate) * time.Second).Unix(),
}
} else if s.board != nil && s.marks != nil && s.marks[presence.GetUserId()] > api.Mark_MARK_UNSPECIFIED {
// There's no game in progress but we still have a completed game that the user was part of.
// They likely disconnected before the game ended, and have since forfeited because they took too long to return.
opCode = api.OpCode_OPCODE_DONE
msg = &api.Done{
Board: s.board,
Winner: s.winner,
WinnerPositions: s.winnerPositions,
NextGameStart: t.Add(time.Duration(s.nextGameRemainingTicks/tickRate) * time.Second).Unix(),
}
}
// Send a message to the user that just joined, if one is needed based on the logic above.
if msg != nil {
buf, err := m.marshaler.Marshal(msg)
if err != nil {
logger.Error("error encoding message: %v", err)
} else {
_ = dispatcher.BroadcastMessage(int64(opCode), buf, []runtime.Presence{presence}, nil, true)
}
}
}
// Check if match was open to new players, but should now be closed.
if len(s.presences) >= 2 && s.label.Open != 0 {
s.label.Open = 0
if labelJSON, err := json.Marshal(s.label); err != nil {
logger.Error("error encoding label: %v", err)
} else {
if err := dispatcher.MatchLabelUpdate(string(labelJSON)); err != nil {
logger.Error("error updating label: %v", err)
}
}
}
return s
}
func (m *MatchHandler) MatchLeave(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, dispatcher runtime.MatchDispatcher, tick int64, state interface{}, presences []runtime.Presence) interface{} {
s := state.(*MatchState)
for _, presence := range presences {
s.presences[presence.GetUserId()] = nil
}
var humanPlayersRemaining []runtime.Presence
for userId, presence := range s.presences {
if presence != nil && userId != aiUserId {
humanPlayersRemaining = append(humanPlayersRemaining, presence)
}
}
// Notify remaining player that the opponent has left the game
if len(humanPlayersRemaining) == 1 {
_ = dispatcher.BroadcastMessage(
int64(api.OpCode_OPCODE_OPPONENT_LEFT), nil,
humanPlayersRemaining, nil, true)
} else if s.ai && len(humanPlayersRemaining) == 0 {
delete(s.presences, aiUserId)
s.ai = false
}
return s
}
func (m *MatchHandler) MatchLoop(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, dispatcher runtime.MatchDispatcher, tick int64, state interface{}, messages []runtime.MatchData) interface{} {
s := state.(*MatchState)
if s.ConnectedCount()+s.joinsInProgress == 0 {
s.emptyTicks++
if s.emptyTicks >= maxEmptySec*tickRate {
// Match has been empty for too long, close it.
logger.Info("closing idle match")
return nil
}
}
t := time.Now().UTC()
// If there's no game in progress check if we can (and should) start one!
if !s.playing {
// Between games any disconnected users are purged, there's no in-progress game for them to return to anyway.
for userID, presence := range s.presences {
if presence == nil {
delete(s.presences, userID)
}
}
// Check if we need to update the label so the match now advertises itself as open to join.
if len(s.presences) < 2 && s.label.Open != 1 {
s.label.Open = 1
if labelJSON, err := json.Marshal(s.label); err != nil {
logger.Error("error encoding label: %v", err)
} else {
if err := dispatcher.MatchLabelUpdate(string(labelJSON)); err != nil {
logger.Error("error updating label: %v", err)
}
}
}
// Check if we have enough players to start a game.
if len(s.presences) < 2 {
return s
}
// Check if enough time has passed since the last game.
if s.nextGameRemainingTicks > 0 {
s.nextGameRemainingTicks--
return s
}
// We can start a game! Set up the game state and assign the marks to each player.
s.playing = true
s.board = make([]api.Mark, 9)
s.marks = make(map[string]api.Mark, 2)
marks := []api.Mark{api.Mark_MARK_X, api.Mark_MARK_O}
for userID := range s.presences {
if s.ai {
if userID == aiUserId {
s.marks[userID] = api.Mark_MARK_O
} else {
s.marks[userID] = api.Mark_MARK_X
}
} else {
s.marks[userID] = marks[0]
marks = marks[1:]
}
}
s.mark = api.Mark_MARK_X
s.winner = api.Mark_MARK_UNSPECIFIED
s.winnerPositions = nil
s.deadlineRemainingTicks = calculateDeadlineTicks(s.label)
s.nextGameRemainingTicks = 0
// Notify the players a new game has started.
buf, err := m.marshaler.Marshal(&api.Start{
Board: s.board,
Marks: s.marks,
Mark: s.mark,
Deadline: t.Add(time.Duration(s.deadlineRemainingTicks/tickRate) * time.Second).Unix(),
})
if err != nil {
logger.Error("error encoding message: %v", err)
} else {
_ = dispatcher.BroadcastMessage(int64(api.OpCode_OPCODE_START), buf, nil, nil, true)
}
return s
}
// Append AI moves, if any
select {
case msg := <-s.messages:
messages = append(messages, msg)
default:
}
// There's a game in progress. Check for input, update match state, and send messages to clients.
for _, message := range messages {
switch api.OpCode(message.GetOpCode()) {
case api.OpCode_OPCODE_MOVE:
mark := s.marks[message.GetUserId()]
if s.mark != mark {
// It is not this player's turn.
_ = dispatcher.BroadcastMessage(int64(api.OpCode_OPCODE_REJECTED), nil, []runtime.Presence{message}, nil, true)
continue
}
msg := &api.Move{}
err := m.unmarshaler.Unmarshal(message.GetData(), msg)
if err != nil {
// Client sent bad data.
_ = dispatcher.BroadcastMessage(int64(api.OpCode_OPCODE_REJECTED), nil, []runtime.Presence{message}, nil, true)
continue
}
if msg.Position < 0 || msg.Position > 8 || s.board[msg.Position] != api.Mark_MARK_UNSPECIFIED {
// Client sent a position outside the board, or one that has already been played.
_ = dispatcher.BroadcastMessage(int64(api.OpCode_OPCODE_REJECTED), nil, []runtime.Presence{message}, nil, true)
continue
}
// Update the game state.
s.board[msg.Position] = mark
switch mark {
case api.Mark_MARK_X:
s.mark = api.Mark_MARK_O
case api.Mark_MARK_O:
s.mark = api.Mark_MARK_X
}
s.deadlineRemainingTicks = calculateDeadlineTicks(s.label)
// Check if game is over through a winning move.
winCheck:
for _, winningPosition := range winningPositions {
for _, position := range winningPosition {
if s.board[position] != mark {
continue winCheck
}
}
// Update state to reflect the winner, and schedule the next game.
s.winner = mark
s.winnerPositions = winningPosition
s.playing = false
s.deadlineRemainingTicks = 0
s.nextGameRemainingTicks = delayBetweenGamesSec * tickRate
}
// Check if game is over because no more moves are possible.
tie := true
for _, mark := range s.board {
if mark == api.Mark_MARK_UNSPECIFIED {
tie = false
break
}
}
if tie {
// Update state to reflect the tie, and schedule the next game.
s.playing = false
s.deadlineRemainingTicks = 0
s.nextGameRemainingTicks = delayBetweenGamesSec * tickRate
}
var opCode api.OpCode
var outgoingMsg proto.Message
if s.playing {
opCode = api.OpCode_OPCODE_UPDATE
outgoingMsg = &api.Update{
Board: s.board,
Mark: s.mark,
Deadline: t.Add(time.Duration(s.deadlineRemainingTicks/tickRate) * time.Second).Unix(),
}
} else {
opCode = api.OpCode_OPCODE_DONE
outgoingMsg = &api.Done{
Board: s.board,
Winner: s.winner,
WinnerPositions: s.winnerPositions,
NextGameStart: t.Add(time.Duration(s.nextGameRemainingTicks/tickRate) * time.Second).Unix(),
}
}
buf, err := m.marshaler.Marshal(outgoingMsg)
if err != nil {
logger.Error("error encoding message: %v", err)
} else {
_ = dispatcher.BroadcastMessage(int64(opCode), buf, nil, nil, true)
}
case api.OpCode_OPCODE_INVITE_AI:
if s.ai {
logger.Error("AI player is already playing")
continue
}
var activePlayers []runtime.Presence
for userId, presence := range s.presences {
if presence == nil {
delete(s.presences, userId)
} else if userId != aiUserId {
activePlayers = append(activePlayers, presence)
}
}
logger.Debug("active users: %d", len(activePlayers))
if len(activePlayers) != 1 {
logger.Error("one active player is required to enable AI mode")
continue
}
s.ai = true
s.presences[aiUserId] = aiPresenceObj
if s.marks[activePlayers[0].GetUserId()] == api.Mark_MARK_O {
s.marks[aiUserId] = api.Mark_MARK_X
} else {
s.marks[aiUserId] = api.Mark_MARK_O
}
logger.Info("AI player joined match")
default:
// No other opcodes are expected from the client, so automatically treat it as an error.
_ = dispatcher.BroadcastMessage(int64(api.OpCode_OPCODE_REJECTED), nil, []runtime.Presence{message}, nil, true)
}
}
// Keep track of the time remaining for the player to submit their move. Idle players forfeit.
if s.playing {
s.deadlineRemainingTicks--
if s.deadlineRemainingTicks <= 0 {
// The player has run out of time to submit their move.
s.playing = false
switch s.mark {
case api.Mark_MARK_X:
s.winner = api.Mark_MARK_O
case api.Mark_MARK_O:
s.winner = api.Mark_MARK_X
}
s.deadlineRemainingTicks = 0
s.nextGameRemainingTicks = delayBetweenGamesSec * tickRate
buf, err := m.marshaler.Marshal(&api.Done{
Board: s.board,
Winner: s.winner,
NextGameStart: t.Add(time.Duration(s.nextGameRemainingTicks/tickRate) * time.Second).Unix(),
})
if err != nil {
logger.Error("error encoding message: %v", err)
} else {
_ = dispatcher.BroadcastMessage(int64(api.OpCode_OPCODE_DONE), buf, nil, nil, true)
}
}
}
// The next turn is AI's
if s.ai && s.mark == s.marks[aiUserId] {
if err := m.aiTurn(s); err != nil {
logger.Error("error making AI turn: %v", err)
}
}
return s
}
func (m *MatchHandler) MatchSignal(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, dispatcher runtime.MatchDispatcher, tick int64, state interface{}, data string) (interface{}, string) {
return state, ""
}
func (m *MatchHandler) MatchTerminate(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, dispatcher runtime.MatchDispatcher, tick int64, state interface{}, graceSeconds int) interface{} {
return state
}
func calculateDeadlineTicks(l *MatchLabel) int64 {
if l.Fast == 1 {
return turnTimeFastSec * tickRate
} else {
return turnTimeNormalSec * tickRate
}
}