fixed finally webrtc
This commit is contained in:
@@ -154,6 +154,7 @@ func _check_command_line_args():
|
||||
LogManager.log("Auto-joining: No address provided, fetching available rooms (mode: " + str(current_mode) + ")...", LogManager.CATEGORY_UI)
|
||||
network_manager.set_local_player_count(local_count)
|
||||
is_auto_joining = true
|
||||
is_joining_attempt = true # Mark as joining attempt so connection failure handler works
|
||||
# Create timer for retrying room fetches
|
||||
room_fetch_timer = Timer.new()
|
||||
room_fetch_timer.name = "RoomFetchTimer"
|
||||
@@ -188,8 +189,11 @@ func _on_rooms_fetched_display(rooms: Array):
|
||||
"""Display available rooms when fetched (non-auto-join mode)"""
|
||||
# Only handle if not in auto-join mode (auto-join has its own handler)
|
||||
if is_auto_joining:
|
||||
LogManager.log("GameUI: Ignoring rooms_fetched_display - still in auto-join mode", LogManager.CATEGORY_UI)
|
||||
return # Let auto-join handler take care of it
|
||||
|
||||
LogManager.log("GameUI: Received rooms for display: " + str(rooms.size()) + " rooms", LogManager.CATEGORY_UI)
|
||||
|
||||
# Hide loading indicator - request completed
|
||||
_hide_loading_indicator()
|
||||
|
||||
@@ -237,12 +241,11 @@ func _on_rooms_fetched_auto_join(rooms: Array):
|
||||
# Stop retrying - we found rooms!
|
||||
if room_fetch_timer:
|
||||
room_fetch_timer.stop()
|
||||
is_auto_joining = false
|
||||
|
||||
# Hide room fetch status UI
|
||||
_hide_room_fetch_status()
|
||||
# DON'T set is_auto_joining = false yet - wait until connection succeeds or fails
|
||||
# DON'T hide room fetch status UI yet - keep it visible in case join fails
|
||||
|
||||
# Disconnect from signal since we're done
|
||||
# Disconnect from auto-join handler (we'll connect to display handler if join fails)
|
||||
if network_manager.rooms_fetched.is_connected(_on_rooms_fetched_auto_join):
|
||||
network_manager.rooms_fetched.disconnect(_on_rooms_fetched_auto_join)
|
||||
|
||||
@@ -258,6 +261,9 @@ func _on_rooms_fetched_auto_join(rooms: Array):
|
||||
if room_fetch_timer:
|
||||
room_fetch_timer.start()
|
||||
is_auto_joining = true
|
||||
# Reconnect to auto-join handler
|
||||
if not network_manager.rooms_fetched.is_connected(_on_rooms_fetched_auto_join):
|
||||
network_manager.rooms_fetched.connect(_on_rooms_fetched_auto_join)
|
||||
# Keep showing status UI
|
||||
return
|
||||
|
||||
@@ -269,7 +275,20 @@ func _on_rooms_fetched_auto_join(rooms: Array):
|
||||
network_manager.set_local_player_count(local_count)
|
||||
if network_manager.join_game(room_code):
|
||||
# Connection callback will handle starting the game
|
||||
# Note: We'll hide the UI and set is_auto_joining = false in _on_connection_succeeded
|
||||
pass
|
||||
else:
|
||||
# Join failed immediately - switch to display mode
|
||||
LogManager.log("Auto-join failed immediately, switching to room browser mode", LogManager.CATEGORY_UI)
|
||||
is_auto_joining = false
|
||||
# Connect to display handler
|
||||
if not network_manager.rooms_fetched.is_connected(_on_rooms_fetched_display):
|
||||
network_manager.rooms_fetched.connect(_on_rooms_fetched_display)
|
||||
# Keep room fetch status UI visible (with refresh button)
|
||||
_show_room_fetch_status()
|
||||
# Fetch and display available rooms
|
||||
_show_loading_indicator()
|
||||
_start_room_fetch()
|
||||
|
||||
func _retry_room_fetch():
|
||||
"""Retry fetching available rooms"""
|
||||
@@ -375,6 +394,25 @@ func _create_refresh_button():
|
||||
|
||||
func _on_refresh_button_pressed():
|
||||
"""Handle refresh button click"""
|
||||
LogManager.log("GameUI: Refresh button pressed", LogManager.CATEGORY_UI)
|
||||
|
||||
# CRITICAL: Ensure we're not in auto-join mode when refreshing manually
|
||||
# This prevents _on_rooms_fetched_display from ignoring the signal
|
||||
if is_auto_joining:
|
||||
LogManager.log("GameUI: Switching from auto-join to display mode for refresh", LogManager.CATEGORY_UI)
|
||||
is_auto_joining = false
|
||||
# Stop auto-join timer if it's running
|
||||
if room_fetch_timer:
|
||||
room_fetch_timer.stop()
|
||||
# Disconnect auto-join handler
|
||||
if network_manager and network_manager.rooms_fetched.is_connected(_on_rooms_fetched_auto_join):
|
||||
network_manager.rooms_fetched.disconnect(_on_rooms_fetched_auto_join)
|
||||
|
||||
# Ensure display handler is connected (in case it was disconnected)
|
||||
if network_manager and not network_manager.rooms_fetched.is_connected(_on_rooms_fetched_display):
|
||||
LogManager.log("GameUI: Reconnecting rooms_fetched signal to display handler", LogManager.CATEGORY_UI)
|
||||
network_manager.rooms_fetched.connect(_on_rooms_fetched_display)
|
||||
|
||||
# Disable button and start cooldown
|
||||
if refresh_button:
|
||||
refresh_button.disabled = true
|
||||
@@ -394,10 +432,17 @@ func _on_refresh_cooldown_finished():
|
||||
|
||||
func _update_last_fetch_time():
|
||||
"""Update the last fetch time label with current datetime"""
|
||||
# Try to find the label if it's null (might not be ready yet)
|
||||
if not last_fetch_label:
|
||||
last_fetch_label = get_node_or_null("Control/MainMenu/VBoxContainer/RoomFetchStatusContainer/LastFetchLabel")
|
||||
|
||||
if last_fetch_label:
|
||||
var now = Time.get_datetime_dict_from_system()
|
||||
var time_str = "%02d:%02d:%02d" % [now.hour, now.minute, now.second]
|
||||
last_fetch_label.text = "Last fetched: " + time_str
|
||||
LogManager.log("GameUI: Updated last fetch time to: " + time_str, LogManager.CATEGORY_UI)
|
||||
else:
|
||||
LogManager.log_error("GameUI: Cannot update last fetch time - last_fetch_label is null!")
|
||||
|
||||
func _create_room_list_container():
|
||||
"""Create the container for displaying available rooms"""
|
||||
@@ -519,11 +564,23 @@ func _on_network_mode_changed(index: int):
|
||||
var mode_names = ["ENet", "WebRTC", "WebSocket"]
|
||||
LogManager.log("GameUI: Network mode changed to: " + mode_names[actual_mode], LogManager.CATEGORY_UI)
|
||||
|
||||
# If WebRTC is selected, fetch available rooms (unless we're auto-joining or hosting)
|
||||
if actual_mode == 1 and not is_auto_joining and not is_hosting: # WebRTC mode
|
||||
_start_room_fetch()
|
||||
elif actual_mode != 1: # Not WebRTC mode
|
||||
# Handle room fetching based on mode
|
||||
if actual_mode == 1: # WebRTC mode
|
||||
# Only fetch if not auto-joining and not hosting
|
||||
if not is_auto_joining and not is_hosting and not network_manager.is_hosting:
|
||||
LogManager.log("GameUI: Switched to WebRTC mode, fetching rooms", LogManager.CATEGORY_UI)
|
||||
# Ensure display handler is connected
|
||||
if network_manager and not network_manager.rooms_fetched.is_connected(_on_rooms_fetched_display):
|
||||
network_manager.rooms_fetched.connect(_on_rooms_fetched_display)
|
||||
# Show room fetch status UI (with refresh button)
|
||||
_show_room_fetch_status()
|
||||
# Fetch rooms
|
||||
_start_room_fetch()
|
||||
else:
|
||||
LogManager.log("GameUI: Switched to WebRTC mode but skipping room fetch (auto_joining: " + str(is_auto_joining) + ", hosting: " + str(is_hosting) + ")", LogManager.CATEGORY_UI)
|
||||
else: # Not WebRTC mode (ENet or WebSocket)
|
||||
# Hide room fetch status if switching away from WebRTC
|
||||
LogManager.log("GameUI: Switched away from WebRTC mode, hiding room fetch UI", LogManager.CATEGORY_UI)
|
||||
_hide_room_fetch_status()
|
||||
|
||||
func _on_host_pressed():
|
||||
@@ -572,6 +629,16 @@ func _on_join_pressed():
|
||||
func _on_connection_succeeded():
|
||||
LogManager.log("GameUI: Connection succeeded signal received, starting game", LogManager.CATEGORY_UI)
|
||||
is_joining_attempt = false
|
||||
|
||||
# If we were in auto-join mode, now we can safely exit it
|
||||
if is_auto_joining:
|
||||
is_auto_joining = false
|
||||
# Hide room fetch status UI since we're connecting
|
||||
_hide_room_fetch_status()
|
||||
# Disconnect from auto-join handler if still connected
|
||||
if network_manager.rooms_fetched.is_connected(_on_rooms_fetched_auto_join):
|
||||
network_manager.rooms_fetched.disconnect(_on_rooms_fetched_auto_join)
|
||||
|
||||
# Check if node is still valid before starting game
|
||||
if not is_inside_tree():
|
||||
LogManager.log_error("GameUI: Cannot start game - node not in tree", LogManager.CATEGORY_UI)
|
||||
@@ -593,6 +660,28 @@ func _on_connection_failed():
|
||||
elif mode == 2:
|
||||
mode_name = "WebSocket"
|
||||
|
||||
# If we were in auto-join mode, switch to display mode and show rooms
|
||||
if is_auto_joining:
|
||||
LogManager.log("Connection failed during auto-join, switching to room browser mode", LogManager.CATEGORY_UI)
|
||||
is_auto_joining = false
|
||||
# Stop auto-join timer
|
||||
if room_fetch_timer:
|
||||
room_fetch_timer.stop()
|
||||
# Disconnect from auto-join handler
|
||||
if network_manager.rooms_fetched.is_connected(_on_rooms_fetched_auto_join):
|
||||
network_manager.rooms_fetched.disconnect(_on_rooms_fetched_auto_join)
|
||||
# Connect to display handler instead
|
||||
if not network_manager.rooms_fetched.is_connected(_on_rooms_fetched_display):
|
||||
network_manager.rooms_fetched.connect(_on_rooms_fetched_display)
|
||||
# Show room fetch status UI (refresh button and room list)
|
||||
_show_room_fetch_status()
|
||||
# Fetch and display available rooms
|
||||
_show_loading_indicator()
|
||||
_start_room_fetch()
|
||||
# Show error message
|
||||
_show_connection_error("Failed to auto-join room. Showing available rooms below.")
|
||||
return
|
||||
|
||||
if is_joining_attempt:
|
||||
var code_hint = (" (" + last_join_address + ")") if not last_join_address.is_empty() else ""
|
||||
_show_connection_error("Failed to join room" + code_hint + ". Did you enter the correct code?")
|
||||
@@ -603,6 +692,9 @@ func _on_connection_failed():
|
||||
_show_room_fetch_status()
|
||||
_show_loading_indicator()
|
||||
_start_room_fetch()
|
||||
# Also connect to display handler if not already connected
|
||||
if not network_manager.rooms_fetched.is_connected(_on_rooms_fetched_display):
|
||||
network_manager.rooms_fetched.connect(_on_rooms_fetched_display)
|
||||
else:
|
||||
_show_connection_error("Connection failed (" + mode_name + "). Please try again.")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user