Fixed bug in ui, oct_utils. Lobby client now has user interface

This commit is contained in:
j4nk 2025-03-22 21:59:43 -04:00
parent 6ccade6e7c
commit 5ee6510748
4 changed files with 121 additions and 45 deletions

View File

@ -28,6 +28,7 @@ function lobby_server(maxplayers)
-- The main state machine -- The main state machine
if (msg_obj["msg_type"] == OCT_LOBBY_MSG_CLIENTREG) then if (msg_obj["msg_type"] == OCT_LOBBY_MSG_CLIENTREG) then
-- Client registration -- Client registration
req_name = msg_obj["name"] req_name = msg_obj["name"]
req_port = msg_obj["port"] req_port = msg_obj["port"]
@ -71,17 +72,72 @@ function lobby_server(maxplayers)
end end
end end
-- Variables for lobby client
lobby_first_call = true;
client_connected = false; client_connected = false;
oct_started = false; oct_started = false;
client_wait_for_reg_response = false; client_wait_for_reg_response = false;
client_filling_in_connect_form = true;
-- UI elements for lobby client
lobby_ip_textbox = nil
lobby_port_textbox = nil
lobby_name_textbox = nil
lobby_connect_message = nil -- messages displayed when attempting connection
client_connect_ip = nil
client_connect_port = nil
client_connect_name = nil
--function lobby_client(ip, port, name, my_port)
function lobby_client(key, ch, my_port)
if (lobby_first_call)
then
lobby_ip_textbox = create_textbox("lobby_ip_textbox", 50, 20, 15, "IP: ")
lobby_port_textbox = create_textbox("lobby_port_textbox", 50, 22, 15, "Port: ")
lobby_name_textbox = create_textbox("lobby_name_textbox", 50, 24, 15, "Name: ")
lobby_connect_message = oct_tb_sprite_new()
lobby_connect_message["x"] = 50
lobby_connect_message["y"] = 26
lobby_connect_message["shape"] = ""
register_textbox(lobby_port_textbox)
register_textbox(lobby_name_textbox)
register_textbox(lobby_ip_textbox)
lobby_first_call = false;
end
if (client_filling_in_connect_form)
then
connect_info = handle_textbox(key, ch)
if (connect_info)
then
if (connect_info["lobby_name_textbox"] == "" or
connect_info["lobby_port_textbox"] == "" or
connect_info["lobby_name_textbox"] == "")
then
OCT_LOG_WARNING("Not all lobby connect information filled in")
lobby_connect_message["shape"] = "Please fill out all fields"
lobby_connect_message["fg"] = TB_RED
else
lobby_connect_message["shape"] = "Connecting..."
lobby_connect_message["fg"] = TB_WHITE
OCT_LOG_INFO("Trying to connect as " .. connect_info["lobby_name_textbox"] .. " to " .. connect_info["lobby_ip_textbox"] .. ":" .. connect_info["lobby_port_textbox"])
client_connect_ip = connect_info["lobby_ip_textbox"]
client_connect_port = connect_info["lobby_port_textbox"]
client_connect_name = connect_info["lobby_name_textbox"]
client_filling_in_connect_form = false
end
end
end
function lobby_client(ip, port, name, my_port)
-- If not already connected, connect sending info -- If not already connected, connect sending info
if (not client_connected and client_wait_for_reg_response == false) then if (not client_connected and client_wait_for_reg_response == false and not client_filling_in_connect_form) then
msg_to_server = json.encode({ msg_to_server = json.encode({
{ name = name, msg_type=OCT_LOBBY_MSG_CLIENTREG, port=my_port } { name = client_connect_name, msg_type=OCT_LOBBY_MSG_CLIENTREG, port=my_port }
}) })
oct_send(msg_to_server, ip, port) oct_send(msg_to_server, client_connect_ip, client_connect_port)
client_wait_for_reg_response = true -- prevent this from being run again client_wait_for_reg_response = true -- prevent this from being run again
end end
@ -94,6 +150,8 @@ function lobby_client(ip, port, name, my_port)
if (client_wait_for_reg_response == true and if (client_wait_for_reg_response == true and
msg_obj["msg_type"] == OCT_LOBBY_MSG_ACK) then msg_obj["msg_type"] == OCT_LOBBY_MSG_ACK) then
OCT_LOG_INFO("Server accepted registration request!") OCT_LOG_INFO("Server accepted registration request!")
lobby_connect_message["shape"] = "Connected"
lobby_connect_message["fg"] = TB_GREEN
client_connected = 1 client_connected = 1
client_wait_for_reg_response = false client_wait_for_reg_response = false
@ -101,6 +159,8 @@ function lobby_client(ip, port, name, my_port)
elseif (client_wait_for_reg_response == true and elseif (client_wait_for_reg_response == true and
msg_obj["msg_type"] == OCT_LOBBY_MSG_NAK) then msg_obj["msg_type"] == OCT_LOBBY_MSG_NAK) then
OCT_LOG_ERROR("Server rejected registration request, try using a different name") OCT_LOG_ERROR("Server rejected registration request, try using a different name")
lobby_connect_message["shape"] = "Server rejected request, try changing name or port"
lobby_connect_message["fg"] = TB_RED
client_connected = 0 client_connected = 0
client_wait_for_reg_response = false client_wait_for_reg_response = false
@ -108,9 +168,7 @@ function lobby_client(ip, port, name, my_port)
elseif (msg_obj["msg_type"] == OCT_LOBBY_MSG_CLIENTLIST) then elseif (msg_obj["msg_type"] == OCT_LOBBY_MSG_CLIENTLIST) then
oct_lobby_clientlist = msg_obj["clientlist"] oct_lobby_clientlist = msg_obj["clientlist"]
OCT_LOG_DEBUG("My client list: " .. table_to_string(oct_lobby_clientlist)) OCT_LOG_DEBUG("My client list: " .. table_to_string(oct_lobby_clientlist))
end end
end end
end end

View File

@ -20,6 +20,7 @@ function convert_known_host_to_ip(host)
if (host == "localhost.localdomain" or host == "localhost") then if (host == "localhost.localdomain" or host == "localhost") then
return "127.0.0.1" return "127.0.0.1"
end end
return host
end end
-- taken from https://stackoverflow.com/questions/9168058/how-to-dump-a-table-to-console -- taken from https://stackoverflow.com/questions/9168058/how-to-dump-a-table-to-console

View File

@ -1,27 +1,36 @@
require("oct_utils") require("oct_utils")
require("termbox_defs") require("termbox_defs")
require("lobby") require("lobby")
require("ui")
my_port = "" my_port = ""
my_name = "" my_name = ""
-- lobby_ip_textbox = create_textbox("lobby_ip_textbox", 50, 20, 15, "IP: ")
-- lobby_port_textbox = create_textbox("lobby_port_textbox", 50, 22, 15, "Port: ")
-- lobby_name_textbox = create_textbox("lobby_name_textbox", 50, 24, 15, "Name: ")
function oct_init(arg, port) function oct_init(arg, port)
OCT_LOG_INFO("Starting test_lobby_client") OCT_LOG_INFO("Starting test_lobby_client")
my_port = port -- my_port = port
if (arg == "") then -- if (arg == "") then
my_name = "TEST_USER" -- my_name = "TEST_USER"
else -- else
my_name = arg -- my_name = arg
end -- end
return OCT_NEEDS_NETWORKING, OCT_NOT_NEEDS_TERMBOX; my_port = port
return OCT_NEEDS_NETWORKING, OCT_NEEDS_TERMBOX;
end end
first = 1 first = 1
function oct_loop(key) function oct_loop(key, ch)
lobby_client("127.0.0.1", "2048", my_name, my_port) --handle_textbox(key, ch)
--lobby_client("127.0.0.1", "2048", my_name, my_port)
lobby_client(key, ch, my_port)
end end

68
ui.lua
View File

@ -90,40 +90,48 @@ end
function handle_textbox(key, ch) function handle_textbox(key, ch)
local to_return = nil local to_return = nil
-- Convert ch from a character code to a character if (registered_textboxes[1] == nil)
ch = string.char(ch)
-- if nothing was pressed, ch is '\0' (null byte)
if (ch ~= '\0')
then then
-- Update the contents of the textbox, this isn't displayed by default OCT_LOG_WARNING("Called handle_textbox when no textboxes are registered")
registered_textboxes[1]["contents"] = registered_textboxes[1]["contents"] .. ch else
end -- Convert ch from a character code to a character
ch = string.char(ch)
if (key == TB_KEY_BACKSPACE2 or key == TB_KEY_BACKSPACE) -- if nothing was pressed, ch is '\0' (null byte)
then if (ch ~= '\0')
registered_textboxes[1]["contents"] = string.sub(registered_textboxes[1]["contents"], 1, string.len(registered_textboxes[1]["contents"])-1) then
end -- Update the contents of the textbox, this isn't displayed by default
registered_textboxes[1]["contents"] = registered_textboxes[1]["contents"] .. ch
-- Update the user-visible sprite
registered_textboxes[1]["sprite"]["shape"] = registered_textboxes[1]["label"] .. textbox_contents_to_shape(registered_textboxes[1]["contents"], registered_textboxes[1]["max_length"])
-- Submit our registered textboxes
if (key == TB_KEY_ENTER)
then
to_return = {}
for i=1,#registered_textboxes
do
curr = registered_textboxes[i]
to_return[curr["id"]] = curr["contents"]
end end
end
-- Rotate the registered textboxes if (key == TB_KEY_BACKSPACE2 or key == TB_KEY_BACKSPACE)
if (key == TB_KEY_TAB) then
then registered_textboxes[1]["contents"] = string.sub(registered_textboxes[1]["contents"], 1, string.len(registered_textboxes[1]["contents"])-1)
table.insert(registered_textboxes, 1, table.remove(registered_textboxes)) end
update_registered_textboxes_appearance()
-- Update the user-visible sprite
if (registered_textboxes[1])
then
registered_textboxes[1]["sprite"]["shape"] = registered_textboxes[1]["label"] .. textbox_contents_to_shape(registered_textboxes[1]["contents"], registered_textboxes[1]["max_length"])
end
-- Submit our registered textboxes
if (key == TB_KEY_ENTER)
then
to_return = {}
for i=1,#registered_textboxes
do
curr = registered_textboxes[i]
to_return[curr["id"]] = curr["contents"]
end
end
-- Rotate the registered textboxes
if (key == TB_KEY_TAB)
then
table.insert(registered_textboxes, 1, table.remove(registered_textboxes))
update_registered_textboxes_appearance()
end
end end
return to_return return to_return