Fixed bug in ui, oct_utils. Lobby client now has user interface
This commit is contained in:
parent
6ccade6e7c
commit
5ee6510748
70
lobby.lua
70
lobby.lua
|
@ -28,6 +28,7 @@ function lobby_server(maxplayers)
|
|||
|
||||
-- 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"]
|
||||
|
@ -71,17 +72,72 @@ function lobby_server(maxplayers)
|
|||
end
|
||||
end
|
||||
|
||||
-- Variables for lobby client
|
||||
lobby_first_call = true;
|
||||
client_connected = false;
|
||||
oct_started = 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 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({
|
||||
{ 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
|
||||
end
|
||||
|
||||
|
@ -94,6 +150,8 @@ function lobby_client(ip, port, name, my_port)
|
|||
if (client_wait_for_reg_response == true and
|
||||
msg_obj["msg_type"] == OCT_LOBBY_MSG_ACK) then
|
||||
OCT_LOG_INFO("Server accepted registration request!")
|
||||
lobby_connect_message["shape"] = "Connected"
|
||||
lobby_connect_message["fg"] = TB_GREEN
|
||||
client_connected = 1
|
||||
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
|
||||
msg_obj["msg_type"] == OCT_LOBBY_MSG_NAK) then
|
||||
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_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
|
||||
oct_lobby_clientlist = msg_obj["clientlist"]
|
||||
OCT_LOG_DEBUG("My client list: " .. table_to_string(oct_lobby_clientlist))
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -20,6 +20,7 @@ function convert_known_host_to_ip(host)
|
|||
if (host == "localhost.localdomain" or host == "localhost") then
|
||||
return "127.0.0.1"
|
||||
end
|
||||
return host
|
||||
end
|
||||
|
||||
-- taken from https://stackoverflow.com/questions/9168058/how-to-dump-a-table-to-console
|
||||
|
|
|
@ -1,27 +1,36 @@
|
|||
require("oct_utils")
|
||||
require("termbox_defs")
|
||||
require("lobby")
|
||||
require("ui")
|
||||
|
||||
my_port = ""
|
||||
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)
|
||||
OCT_LOG_INFO("Starting test_lobby_client")
|
||||
|
||||
my_port = port
|
||||
if (arg == "") then
|
||||
my_name = "TEST_USER"
|
||||
else
|
||||
my_name = arg
|
||||
end
|
||||
-- my_port = port
|
||||
-- if (arg == "") then
|
||||
-- my_name = "TEST_USER"
|
||||
-- else
|
||||
-- my_name = arg
|
||||
-- end
|
||||
|
||||
return OCT_NEEDS_NETWORKING, OCT_NOT_NEEDS_TERMBOX;
|
||||
my_port = port
|
||||
|
||||
return OCT_NEEDS_NETWORKING, OCT_NEEDS_TERMBOX;
|
||||
|
||||
end
|
||||
|
||||
first = 1
|
||||
|
||||
function oct_loop(key)
|
||||
lobby_client("127.0.0.1", "2048", my_name, my_port)
|
||||
function oct_loop(key, ch)
|
||||
--handle_textbox(key, ch)
|
||||
--lobby_client("127.0.0.1", "2048", my_name, my_port)
|
||||
lobby_client(key, ch, my_port)
|
||||
end
|
||||
|
||||
|
|
8
ui.lua
8
ui.lua
|
@ -90,6 +90,10 @@ end
|
|||
|
||||
function handle_textbox(key, ch)
|
||||
local to_return = nil
|
||||
if (registered_textboxes[1] == nil)
|
||||
then
|
||||
OCT_LOG_WARNING("Called handle_textbox when no textboxes are registered")
|
||||
else
|
||||
-- Convert ch from a character code to a character
|
||||
ch = string.char(ch)
|
||||
|
||||
|
@ -106,7 +110,10 @@ function handle_textbox(key, ch)
|
|||
end
|
||||
|
||||
-- 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)
|
||||
|
@ -125,6 +132,7 @@ function handle_textbox(key, ch)
|
|||
table.insert(registered_textboxes, 1, table.remove(registered_textboxes))
|
||||
update_registered_textboxes_appearance()
|
||||
end
|
||||
end
|
||||
|
||||
return to_return
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue