Created
July 6, 2018 14:00
-
-
Save novabyte/1207b7b312e78964f95be7ae9cb7aee5 to your computer and use it in GitHub Desktop.
An example of a match handler with Nakama server which tracks presence join/leave events for users in the game state.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--[[ | |
Copyright 2018 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. | |
--]] | |
local nk = require("nakama") | |
local M = {} | |
function M.match_init(context, setupstate) | |
local gamestate = { | |
presences = {} | |
} | |
local tickrate = 1 -- per sec | |
local label = "" | |
return gamestate, tickrate, label | |
end | |
function M.match_join_attempt(context, dispatcher, tick, state, presence) | |
local acceptuser = true | |
return state, acceptuser | |
end | |
function M.match_join(context, dispatcher, tick, state, presences) | |
for _, presence in ipairs(presences) do | |
state.presences[presence.session_id] = presence | |
end | |
return state | |
end | |
function M.match_leave(context, dispatcher, tick, state, presences) | |
for _, presence in ipairs(presences) do | |
state.presences[presence.session_id] = nil | |
end | |
return state | |
end | |
function M.match_loop(context, dispatcher, tick, state, messages) | |
for _, presence in pairs(state.presences) do | |
print(("Presence connected %s named %s"):format(presence.user_id, presence.username)) | |
end | |
for _, message in ipairs(messages) do | |
print(("Received %s from %s"):format(message.sender.username, message.data)) | |
local decoded = nk.json_decode(message.data) | |
for k, v in pairs(decoded) do | |
print(("Message contained %s value %s"):format(k, v)) | |
end | |
end | |
return state | |
end | |
return M |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
An RPC function which will join a match or create a new one if none exists: