forked from defold/game-xoxo-nakama-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tictactoe_match.lua
126 lines (105 loc) · 4.03 KB
/
tictactoe_match.lua
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
--[[
Copyright 2020 The Defold Foundation Authors & Contributors
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.
]]--
local tictactoe = require("tictactoe_state")
local nk = require("nakama")
local M = {}
local OP_CODE_MOVE = 1
local OP_CODE_STATE = 2
local function pprint(t)
if type(t) ~= "table" then
nk.logger_info(tostring(t))
else
for k,v in pairs(t) do
nk.logger_info(string.format("%s = %s", tostring(k), tostring(v)))
end
end
end
local function broadcast_gamestate_to_recipient(dispatcher, gamestate, recipient)
nk.logger_info("broadcast_gamestate")
local active_player = tictactoe.get_active_player(gamestate)
local other_player = tictactoe.get_other_player(gamestate)
local your_turn = active_player.user_id == recipient.user_id
local message = {
state = gamestate,
active_player = active_player,
other_player = other_player,
your_turn = your_turn,
}
local encoded_message = nk.json_encode(message)
dispatcher.broadcast_message(OP_CODE_STATE, encoded_message, { recipient })
end
local function broadcast_gamestate(dispatcher, gamestate)
local player = tictactoe.get_active_player(gamestate)
local opponent = tictactoe.get_other_player(gamestate)
broadcast_gamestate_to_recipient(dispatcher, gamestate, player)
broadcast_gamestate_to_recipient(dispatcher, gamestate, opponent)
end
function M.match_init(context, setupstate)
nk.logger_info("match_init")
local gamestate = tictactoe.new_game()
local tickrate = 1 -- per sec
local label = ""
return gamestate, tickrate, label
end
function M.match_join_attempt(context, dispatcher, tick, gamestate, presence, metadata)
nk.logger_info("match_join_attempt")
local acceptuser = true
return gamestate, acceptuser
end
function M.match_join(context, dispatcher, tick, gamestate, presences)
nk.logger_info("match_join")
for _, presence in ipairs(presences) do
tictactoe.add_player(gamestate, presence)
end
if tictactoe.player_count(gamestate) == 2 then
broadcast_gamestate(dispatcher, gamestate)
end
return gamestate
end
function M.match_leave(context, dispatcher, tick, gamestate, presences)
nk.logger_info("match_leave")
-- end match if someone leaves
return nil
end
function M.match_loop(context, dispatcher, tick, gamestate, messages)
nk.logger_info("match_loop")
for _, message in ipairs(messages) do
nk.logger_info(string.format("Received %s from %s", message.data, message.sender.username))
pprint(message)
if message.op_code == OP_CODE_MOVE then
local decoded = nk.json_decode(message.data)
local row = decoded.row
local col = decoded.col
gamestate = tictactoe.player_move(gamestate, row, col)
if gamestate.winner or gamestate.draw then
gamestate.rematch_countdown = 10
end
broadcast_gamestate(dispatcher, gamestate)
end
end
if gamestate.rematch_countdown then
gamestate.rematch_countdown = gamestate.rematch_countdown - 1
if gamestate.rematch_countdown == 0 then
gamestate = tictactoe.rematch(gamestate)
end
broadcast_gamestate(dispatcher, gamestate)
end
return gamestate
end
function M.match_terminate(context, dispatcher, tick, gamestate, grace_seconds)
nk.logger_info("match_terminate")
local message = "Server shutting down in " .. grace_seconds .. " seconds"
dispatcher.broadcast_message(2, message)
return nil
end
return M