77 lines
2.8 KiB
Lua
77 lines
2.8 KiB
Lua
require("oct_utils")
|
|
require("termbox_defs")
|
|
json = require "json"
|
|
|
|
-- begin message type constants
|
|
OCT_LOBBY_MSG_CLIENTREG = 0; -- Client registration message
|
|
OCT_LOBBY_MSG_CLIENTUNREG = 1; -- Client unreg message
|
|
OCT_LOBBY_MSG_CLIENTLIST = 2; -- Client list broadcast message
|
|
OCT_LOBBY_MSG_START = 3; -- Start the game message
|
|
OCT_LOBBY_MSG_PING = 4; -- Querying connection
|
|
OCT_LOBBY_MSG_PONG = 5; -- Responding to connection query
|
|
-- end message type constants
|
|
|
|
oct_lobby_clientlist = {};
|
|
oct_lobby_timers = {};
|
|
oct_lobby_timers["clientlist"] = 1000; -- Every 1000 calls to lobby_client broadcast client list
|
|
|
|
function lobby_server(maxplayers)
|
|
msg,addr,port = oct_recv();
|
|
if (msg ~= "") then
|
|
msg_dec = json.decode(msg);
|
|
if (msg_dec) then
|
|
-- OCT_LOG_INFO("RECEIVED from " .. addr .. " : msg_dec")
|
|
-- Message registering a new client
|
|
if (msg_dec["msgtype"] == OCT_LOBBY_MSG_CLIENTREG) then
|
|
-- first port var is the outgoing port of the client, basically useless
|
|
-- client sends the incoming port in the message
|
|
client_port = msg_dec["port"];
|
|
if (client_port == nil) then OCT_LOG_WARNING("Client registration from " .. addr .. " with no port"); return; end
|
|
name = msg_dec["name"];
|
|
if (name == nil) then OCT_LOG_WARNING("Client registration from " .. addr .. " with no name"); return; end
|
|
-- Now check if there is already someone in the server with the same name
|
|
if (oct_lobby_clientlist[name] ~= nil) then
|
|
OCT_LOG_ERROR(addr .. ":" .. port .. " tried to register a name already registered: " .. name);
|
|
return;
|
|
end
|
|
-- Now check if there is already someone in the server with the same ip/port combo
|
|
for k,v in pairs(oct_lobby_clientlist) do
|
|
if (v["addr"] == addr and v["port"] == client_port) then
|
|
OCT_LOG_ERROR(addr .. ":" .. port .. " tried to reregister");
|
|
return;
|
|
end
|
|
end
|
|
-- All good, register them
|
|
oct_lobby_clientlist[name] = {addr=addr,port=client_port};
|
|
OCT_LOG_INFO("Registered new client: " .. name .. " @ " .. addr .. ":" .. client_port);
|
|
end
|
|
else
|
|
OCT_LOG_WARNING("Could not decode received json from " .. addr);
|
|
end
|
|
end
|
|
end
|
|
|
|
client_connected = false;
|
|
oct_started = false;
|
|
|
|
function lobby_client(ip, port, name)
|
|
-- If not already connected, connect sending info
|
|
if (not client_connected) then
|
|
msg_to_server = json.encode({ {name = name} })
|
|
oct_send(msg, ip, port)
|
|
end
|
|
|
|
msg_from_server,servaddr,servport = oct_recv();
|
|
msg_from_server_dec = json.decode(msg_from_server);
|
|
if (msg_from_server_dec) then
|
|
if (msg_from_server_dec["clientlist"] ~= nil) then
|
|
oct_lobby_clientlist = msg_from_server_dec["clientlist"];
|
|
elseif (msg_from_server_dec["start"] ~= nil) then
|
|
oct_started = true;
|
|
end
|
|
else
|
|
OCT_LOG_WARNING("Could not decode received json from server");
|
|
end
|
|
end
|
|
|