From 6274c6b0981d4dc2bb7358a4bccf903adf90f643 Mon Sep 17 00:00:00 2001 From: j4nk Date: Mon, 17 Mar 2025 23:52:31 -0400 Subject: [PATCH] lobby server broadcast clientlist everyone upon new registration --- lobby.lua | 22 ++++++++++++++++++++++ test_lobby_client.lua | 9 +++++++-- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/lobby.lua b/lobby.lua index 2130074..9861ca9 100644 --- a/lobby.lua +++ b/lobby.lua @@ -16,6 +16,7 @@ oct_lobby_clientlist = {}; oct_lobby_timers = {}; oct_lobby_timers["clientlist"] = 1000; -- Every 1000 calls to lobby_client broadcast client list +server_needs_broadcast_client_list = false function lobby_server(maxplayers) msg,addr,port = oct_recv(); if (msg ~= "") then @@ -51,9 +52,23 @@ function lobby_server(maxplayers) OCT_LOG_INFO("Registered new client: " .. req_name .. " @ " .. req_addr .. ":" .. req_port) oct_send(response, req_addr, req_port) + -- Everytime client is registered, need to broadcast client list + server_needs_broadcast_client_list = true end end end + + if (server_needs_broadcast_client_list == true) then + msg = json.encode({ + { msg_type = OCT_LOBBY_MSG_CLIENTLIST, + clientlist = oct_lobby_clientlist, + } + }) + for k,v in pairs(oct_lobby_clientlist) do + oct_send(msg, v["addr"], v["port"]) + end + server_needs_broadcast_client_list = false + end end client_connected = false; @@ -88,7 +103,14 @@ function lobby_client(ip, port, name, my_port) OCT_LOG_ERROR("Server rejected registration request, try using a different name") client_connected = 0 client_wait_for_reg_response = false + + -- We received a clientlist from the server + elseif (msg_obj["msg_type"] == OCT_LOBBY_MSG_CLIENTLIST) then + oct_lobby_clientlist = msg_obj["clientlist"] + OCT_LOG_DEBUG("My client list: " .. table_to_string(oct_lobby_clientlist)) + end + end end diff --git a/test_lobby_client.lua b/test_lobby_client.lua index c16ed37..fdbc80c 100644 --- a/test_lobby_client.lua +++ b/test_lobby_client.lua @@ -2,13 +2,18 @@ require("oct_utils") require("termbox_defs") require("lobby") -my_ip = "" my_port = "" +my_name = "" function oct_init(arg, port) OCT_LOG_INFO("Starting test_lobby_client") my_port = port + if (arg == "") then + my_name = "TEST_USER" + else + my_name = arg + end return OCT_NEEDS_NETWORKING, OCT_NOT_NEEDS_TERMBOX; @@ -17,6 +22,6 @@ end first = 1 function oct_loop(key) - lobby_client("127.0.0.1", "2048", "TEST_LOBBY", my_port) + lobby_client("127.0.0.1", "2048", my_name, my_port) end