-
Notifications
You must be signed in to change notification settings - Fork 26
/
match.lua
63 lines (52 loc) · 1.51 KB
/
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
local nk = require("nakama")
local M = {}
function M.match_init(context, setupstate)
local gamestate = {
players = {}
}
local tickrate = 30
local label = {
kind = setupstate.kind;
engine = setupstate.engine;
}
return gamestate, tickrate, nk.json_encode(label)
end
function M.match_join_attempt(context, dispatcher, tick, state, presence, metadata)
local acceptuser = true
return state, acceptuser
end
function M.match_join(context, dispatcher, tick, state, presences)
for _, presence in ipairs(presences) do
state.players[presence.session_id] = {
presence = presence;
tick = tick;
}
end
return state
end
function M.match_leave(context, dispatcher, tick, state, presences)
for _, presence in ipairs(presences) do
state.players[presence.session_id] = nil
end
return state
end
function M.match_loop(context, dispatcher, tick, state, messages)
for _, m in ipairs(messages) do
dispatcher.broadcast_message(m.op_code, m.data, nil, m.sender)
state.players[m.sender.session_id].tick = tick
end
for session_id, player in pairs(state.players) do
if tick - player.tick > 200 then
nk.logger_info(string.format("kicking %q", session_id))
dispatcher.match_kick({ player.presence })
end
end
return state
end
function M.match_signal(context, dispatcher, tick, state, data)
return state
end
function M.match_terminate(context, dispatcher, tick, state, grace_seconds)
return state
end
return M