Skip to content

Instantly share code, notes, and snippets.

@novabyte
Created July 6, 2018 14:00
Show Gist options
  • Save novabyte/1207b7b312e78964f95be7ae9cb7aee5 to your computer and use it in GitHub Desktop.
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.
--[[
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
@novabyte
Copy link
Author

novabyte commented Jul 6, 2018

An RPC function which will join a match or create a new one if none exists:

local nk = require("nakama")

local function create_or_join_match(context, payload)
  -- expected input:
  -- { "level": 15 }
  payload = nk.json_decode(payload)

  local limit = 10
  local authoritative = true
  local label = tostring(payload.level)
  local min_size = 0
  local max_size = 9
  local matches = nk.match_list(limit, authoritative, label, min_size, max_size)

  local match_id
  if (#matches > 0) then
    -- sort tables by number of players
    table.sort(matches, function(a, b)
      return a.size > b.size
    end)
    match_id = matches[1].match_id
  else
    -- no active matches must create one
    local params = {}
    match_id = nk.match_create("matchskeleton", params)
  end

  local response = nk.json_encode({ matchid = match_id })
  return response
end
nk.register_rpc(create_or_join_match, "createorjoin")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment