Redoing lobby, client registration implemented

This commit is contained in:
j4nk 2025-03-16 23:15:27 -04:00
parent 7cf075d1a6
commit 97124b1e64
4 changed files with 106 additions and 43 deletions

102
lobby.lua
View File

@ -8,7 +8,8 @@ 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
OCT_LOBBY_MSG_ACK = 5; -- "yes", used to respond to ping and other stuff
OCT_LOBBY_MSG_NAK = 6; -- "no"
-- end message type constants
oct_lobby_clientlist = {};
@ -18,59 +19,76 @@ oct_lobby_timers["clientlist"] = 1000; -- Every 1000 calls to lobby_client broad
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);
msg_obj = json.decode(msg)[1] -- dumps to 1-elem array, get first elem
if (msg_obj == nil) then
OCT_LOG_WARNING("Malformed json received")
end
-- The main state machine
if (msg_obj["msg_type"] == OCT_LOBBY_MSG_CLIENTREG) then
-- Client registration
req_name = msg_obj["name"]
req_port = msg_obj["port"]
req_addr = convert_known_host_to_ip(addr)
-- First, check if clientlist contains a conflicting username
if (oct_lobby_clientlist[req_name] ~= nil) then
response = json.encode({
{ msg_type = OCT_LOBBY_MSG_NAK }
})
OCT_LOG_WARNING("Rejected attempt to register from client: " .. req_name .. " @ " .. req_addr .. ":" .. req_port)
oct_send(response, req_addr, req_port)
else
response = json.encode({
{ msg_type = OCT_LOBBY_MSG_ACK }
})
oct_lobby_clientlist[req_name] = {}
oct_lobby_clientlist[req_name]["addr"] = req_addr
oct_lobby_clientlist[req_name]["port"] = req_port
OCT_LOG_INFO("Registered new client: " .. req_name .. " @ " .. req_addr .. ":" .. req_port)
oct_send(response, req_addr, req_port)
end
else
OCT_LOG_WARNING("Could not decode received json from " .. addr);
end
end
end
client_connected = false;
oct_started = false;
client_wait_for_reg_response = false;
function lobby_client(ip, port, name)
function lobby_client(ip, port, name, my_port)
-- If not already connected, connect sending info
if (not client_connected) then
msg_to_server = json.encode({ {name = name} })
oct_send(msg, ip, port)
if (not client_connected and client_wait_for_reg_response == false) then
msg_to_server = json.encode({
{ name = name, msg_type=OCT_LOBBY_MSG_CLIENTREG, port=my_port }
})
oct_send(msg_to_server, ip, port)
client_wait_for_reg_response = true -- prevent this from being run again
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;
msg,addr,port = oct_recv()
if (msg ~= "") then
msg_obj = json.decode(msg)[1]
-- State machine code here
-- Registration was successful
if (client_wait_for_reg_response == true and
msg_obj["msg_type"] == OCT_LOBBY_MSG_ACK) then
OCT_LOG_INFO("Server accepted registration request!")
client_connected = 1
client_wait_for_reg_response = false
-- Registration was unsuccessful
elseif (client_wait_for_reg_response == true and
msg_obj["msg_type"] == OCT_LOBBY_MSG_NAK) then
OCT_LOG_ERROR("Server rejected registration request, try using a different name")
client_connected = 0
client_wait_for_reg_response = false
end
else
OCT_LOG_WARNING("Could not decode received json from server");
end
end

View File

@ -13,4 +13,27 @@ function load_termbox_sprite(file_name)
return contents;
end
-- 127.0.0.1 will come back as "localhost.localdomain"
-- I don't think my program can handle this, so just change it back
-- Keep this in a function in case we run into others
function convert_known_host_to_ip(host)
if (host == "localhost.localdomain" or host == "localhost") then
return "127.0.0.1"
end
end
-- taken from https://stackoverflow.com/questions/9168058/how-to-dump-a-table-to-console
function table_to_string(o)
if type(o) == 'table' then
local s = '{ '
for k,v in pairs(o) do
if type(k) ~= 'number' then k = '"'..k..'"' end
s = s .. '['..k..'] = ' .. table_to_string(v) .. ','
end
return s .. '} '
else
return tostring(o)
end
end

View File

@ -15,7 +15,7 @@ end
counter = 0;
function oct_loop(key)
if counter == 10000000 then
if counter == 1000000 then
oct_send("First message!\nnewline\nnewline\nnewline", "127.0.0.1", "1234");
oct_send("Second message!\nhey!", "127.0.0.1", "1234");
counter = 0;

22
test_lobby_client.lua Normal file
View File

@ -0,0 +1,22 @@
require("oct_utils")
require("termbox_defs")
require("lobby")
my_ip = ""
my_port = ""
function oct_init(arg, port)
OCT_LOG_INFO("Starting test_lobby_client")
my_port = port
return OCT_NEEDS_NETWORKING, OCT_NOT_NEEDS_TERMBOX;
end
first = 1
function oct_loop(key)
lobby_client("127.0.0.1", "2048", "TEST_LOBBY", my_port)
end