Skip to content

Commit

Permalink
Fix client fsm (#251)
Browse files Browse the repository at this point in the history
* fix dialog popup stacking

* Make client fsm more robust

* make client fsm more stable
  • Loading branch information
jonathaneeckhout committed Feb 13, 2024
1 parent b835ef0 commit 574a333
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 19 deletions.
87 changes: 71 additions & 16 deletions components/connection/ClientFSM/ClientFSM.gd
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ var _create_account_pressed: bool = false
var _username: String = ""
var _password: String = ""

var _login_retry_timer: Timer = null


func _ready():
# Get the ClientFSMRPC component.
Expand All @@ -45,6 +47,17 @@ func _ready():
ClientFSMServerRPC.COMPONENT_NAME
)

_login_retry_timer = Timer.new()
_login_retry_timer.name = "LoginRetryTimer"
_login_retry_timer.autostart = false
_login_retry_timer.one_shot = true
_login_retry_timer.wait_time = RETRY_TIME
_login_retry_timer.timeout.connect(_on_login_retry_timer_timeout)
add_child(_login_retry_timer)

_client_gateway_client.client_connected.connect(_on_gateway_server_disconnected)
_client_server_client.client_connected.connect(_on_server_server_disconnected)

# Ensure the ClientFSMServerRPC component is present
assert(_client_fsm_server_rpc != null, "Failed to get ClientFSMServerRPC component")

Expand Down Expand Up @@ -113,16 +126,12 @@ func _connect_to_gateway() -> bool:
if !_client_gateway_client.websocket_client_start(config.client_gateway_client_address):
GodotLogger.warn("Could not connect to gateway=[%s]" % config.client_gateway_client_address)

JUI.alertbox("Error connecting to gateway", login_panel)

return false

# Wait until you receive the server_connected signal
if !await _client_gateway_client.client_connected:
GodotLogger.warn("Could not connect to gateway=[%s]" % config.client_gateway_client_address)

JUI.alertbox("Error connecting to gateway", login_panel)

return false

GodotLogger.info("Connected to gateway=[%s]" % config.client_gateway_client_address)
Expand Down Expand Up @@ -181,13 +190,22 @@ func _handle_login():
login_panel.show()
login_panel.show_login_container()

JUI.clear_dialog()

if not _login_retry_timer.is_stopped():
GodotLogger.error("Wait to handle login again until login timer is done")
return

# Try to connect to the gateway server
if !await _connect_to_gateway():
GodotLogger.error("Client could not connect to gateway server")

await get_tree().create_timer(RETRY_TIME).timeout
JUI.alertbox(
"Error connecting to gateway, retrying in %d seconds" % RETRY_TIME, login_panel
)

_fsm.call_deferred(STATES.LOGIN)
if _login_retry_timer.is_stopped():
_login_retry_timer.start()

return

Expand All @@ -209,8 +227,6 @@ func _handle_login():

JUI.alertbox("Login to gateway server failed", login_panel)

_fsm.call_deferred(STATES.LOGIN)

return

GodotLogger.info("Login to gateway server successful")
Expand All @@ -225,29 +241,26 @@ func _handle_login():

JUI.alertbox("Server error, please try again", login_panel)

_fsm.call_deferred(STATES.LOGIN)

return

# Disconnect the client from the gateway server
GodotLogger.info("Disconnect from gateway server")
_client_gateway_client.websocket_client_disconnect()

_connected_to_gateway = false

GodotLogger.info(
(
"Connecting client to world=[%s] with address=[%s]"
% [server_info["name"], server_info["address"]]
)
)

# Disconnect from previous game server
_client_server_client.websocket_client_disconnect()

# Connect to the gameserver
if !await _connect_to_server():
GodotLogger.error("Client could not connect to server")

_fsm.call_deferred(STATES.LOGIN)
JUI.alertbox("Could not connect to server", login_panel)

return

Expand All @@ -262,6 +275,9 @@ func _handle_login():

JUI.alertbox("Authentication with server failed", login_panel)

# Disconnect from previous game server
_client_server_client.websocket_client_disconnect()

return

J.server_client_multiplayer_connection = _client_server_client
Expand All @@ -288,9 +304,12 @@ func _handle_create_account():
if !await _connect_to_gateway():
GodotLogger.error("Client could not connect to gateway server")

await get_tree().create_timer(RETRY_TIME).timeout
JUI.alertbox(
"Error connecting to gateway, retrying in %d seconds" % RETRY_TIME, login_panel
)

_fsm.call_deferred(STATES.LOGIN)
if _login_retry_timer.is_stopped():
_login_retry_timer.start()

return

Expand Down Expand Up @@ -356,3 +375,39 @@ func _on_create_account_pressed(username: String, password: String):
_password = password

_fsm.call_deferred(STATES.CREATE_ACCOUNT)


func _on_gateway_server_disconnected(connected: bool):
if not connected and state == STATES.LOGIN:
_connected_to_gateway = false

JUI.alertbox(
"Disconnected from gateway server, retrying in %d seconds" % RETRY_TIME, login_panel
)

if _login_retry_timer.is_stopped():
_login_retry_timer.start()


func _on_server_server_disconnected(connected: bool):
if connected:
return

if state == STATES.LOGIN:
JUI.alertbox(
"Disconnected from game server, retrying in %d seconds" % RETRY_TIME, login_panel
)

if _login_retry_timer.is_stopped():
_login_retry_timer.start()

if state == STATES.RUNNING:
_map.set_physics_process(false)
_map.queue_free()

_fsm.call_deferred(STATES.LOGIN)


func _on_login_retry_timer_timeout():
if state == STATES.LOGIN:
_fsm.call_deferred(STATES.LOGIN)
2 changes: 1 addition & 1 deletion components/connection/ClientFSM/ClientFSMGatewayRPC.gd
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func _get_server():
)
if server == null:
GodotLogger.warn("Could not find server=[%s]" % server_name)
_get_server_response.rpc_id(id, true, "World", "", 0, "")
_get_server_response.rpc_id(id, true, "World", "", "")
return

# Create an unique cookie
Expand Down
17 changes: 15 additions & 2 deletions scripts/singletons/jui.gd
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@ extends Node
var above_ui: bool = false
var chat_active: bool = false

var dialog: AcceptDialog = null


func alertbox(message: String, parent: Node) -> void:
var dialog = AcceptDialog.new()
if dialog != null:
dialog.queue_free()

dialog = AcceptDialog.new()
dialog.title = "Alert"
dialog.dialog_text = message
dialog.unresizable = true
Expand All @@ -17,11 +22,19 @@ func alertbox(message: String, parent: Node) -> void:
func confirmationbox(
message: String, parent: Node, title: String, confirmed_action: Callable
) -> void:
var dialog = ConfirmationDialog.new()
if dialog != null:
dialog.queue_free()

dialog = ConfirmationDialog.new()
dialog.title = title
dialog.dialog_text = message
dialog.unresizable = true
dialog.connect("close_requested", dialog.queue_free)
dialog.confirmed.connect(confirmed_action)
parent.add_child(dialog)
dialog.popup_centered()


func clear_dialog() -> void:
if dialog != null:
dialog.queue_free()

0 comments on commit 574a333

Please sign in to comment.