Hi Everyone!
I am trying to write a Lua dedicated server in which players register their name and cards deck id and try to start the match passing these data aside with a random seed and a Boolean to determine which player goes first in the match.
I decided to send the name and deck id in the meta data to the server
I noticed that meta data gets passed in join attempt function while I need the data in match join function so I cached the data. the problem is if the name and deck id are the same in both clients, the match starts, however if I changed any of them the match does not start.
also number of presences is always 1 not 2
here is my code to look into
local matchMetadata = {}
function M.match_join_attempt(context, dispatcher, tick, state, presence, metadata)
local acceptuser = true
if (state.host == nil) then
state.host = presence.user_id
end
if (matchMetadata.metadata == nil) then
matchMetadata.metadata = {}
end
matchMetadata.metadata[presence.user_id] = metadata;
nk.logger_error("metadata: " .. nk.json_encode(metadata))
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
nk.logger_error("passed: " .. presence.session_id)
end
for _, presence in ipairs(state.presences) do
nk.logger_error("cached: " .. presence.session_id)
end
-- Broadcast if number of presense > 1
nk.logger_error("pres: " .. pres)
nk.logger_error("Number of presences: " .. #presences)
nk.logger_error("Number of cached presences: " .. #state.presences)
pres = pres + 1
if (pres> 1) then
for _, presence in ipairs(presences) do
nk.logger_error("Presence: " .. nk.json_encode(presence))
nk.logger_error("State: " .. nk.json_encode(state))
nk.logger_error("context: " .. nk.json_encode(context))
local metadata = {}
for _, other_presence in pairs(state.presences) do
if other_presence.session_id ~= presence.session_id then
metadata = matchMetadata.metadata[other_presence.user_id]
nk.logger_error("metadata: " .. nk.json_encode(metadata))
break
end
end
dispatcher.broadcast_message(OP_STARTGAME, nk.json_encode({
["IsMyTurn"] = state.presences[presence.session_id] == presence,
["RandomSeed"] = math.random(1, 1000000), -- generate random int
["OtherPlayerName"] = metadata["PlayerName"] ,-- I want to access meta data player name filed
["OtherPlayerImage"] = metadata["PlayerImage"] -- I want to access meta data player name filed
}))
end
end
return state
end