Fix FreePlayScreen join: deck selection before join
- Cache _all_decks from server; auto-select first deck on load - Join flow: if multiple decks, show inline deck-picker dialog before proceeding to password prompt / join - Remove stub _get_first_deck_id() (was always returning "") - _populate_create_deck_buttons now uses _all_decks cache and auto-presses first button to reflect the default selection - Guard in _do_join: show alert if _selected_deck still empty Generated with [Claude Code](https://claude.ai/code) via [Happy](https://happy.engineering) Co-Authored-By: Claude <noreply@anthropic.com> Co-Authored-By: Happy <yesreply@happy.engineering>
This commit is contained in:
@@ -1,11 +1,5 @@
|
||||
extends Control
|
||||
## FreePlayScreen — browse open lobbies, active games, and create new lobbies.
|
||||
##
|
||||
## Layout (programmatic):
|
||||
## VBox
|
||||
## ├─ Header: "FREE PLAY" + Back button
|
||||
## ├─ Tab bar: [Open Lobbies] [Active Games] + [+ Create] (right)
|
||||
## └─ Content area (lobby list / active games list)
|
||||
|
||||
# ── Node refs ─────────────────────────────────────────────────────────────────
|
||||
|
||||
@@ -27,17 +21,20 @@ extends Control
|
||||
|
||||
# ── State ─────────────────────────────────────────────────────────────────────
|
||||
|
||||
var _current_tab : String = "open" # "open" | "active"
|
||||
var _open_lobbies : Array = []
|
||||
var _active_games : Array = []
|
||||
var _selected_deck : String = ""
|
||||
var _current_tab : String = "open"
|
||||
var _open_lobbies : Array = []
|
||||
var _active_games : Array = []
|
||||
var _all_decks : Array = [] # cached from server
|
||||
var _selected_deck : String = ""
|
||||
var _pending_join_lobby : Dictionary = {}
|
||||
var _deck_btn_group : ButtonGroup = null
|
||||
|
||||
# ── Lifecycle ─────────────────────────────────────────────────────────────────
|
||||
|
||||
func _ready() -> void:
|
||||
Chat.set_bubble_visible(true)
|
||||
_setup_ui()
|
||||
_load_decks() # load decks first; lobbies load after decks arrive
|
||||
_refresh_lobbies()
|
||||
|
||||
|
||||
@@ -54,25 +51,34 @@ func _setup_ui() -> void:
|
||||
refresh_btn.pressed.connect(_refresh_lobbies)
|
||||
search_field.text_changed.connect(func(_t): _refresh_lobbies())
|
||||
create_btn.pressed.connect(_show_create_dialog)
|
||||
|
||||
create_dialog.confirmed.connect(_on_create_confirmed)
|
||||
join_password_dialog.confirmed.connect(_on_join_password_confirmed)
|
||||
|
||||
_switch_tab("open")
|
||||
_load_decks_for_create()
|
||||
|
||||
|
||||
func _switch_tab(tab: String) -> void:
|
||||
_current_tab = tab
|
||||
var open_active = tab == "open"
|
||||
lobby_list_container.visible = open_active
|
||||
active_list_container.visible = not open_active
|
||||
open_tab_btn.modulate = Color.WHITE if open_active else Color(0.6, 0.6, 0.6)
|
||||
active_tab_btn.modulate = Color(0.6, 0.6, 0.6) if open_active else Color.WHITE
|
||||
var is_open := tab == "open"
|
||||
lobby_list_container.visible = is_open
|
||||
active_list_container.visible = not is_open
|
||||
open_tab_btn.modulate = Color.WHITE if is_open else Color(0.6, 0.6, 0.6)
|
||||
active_tab_btn.modulate = Color(0.6, 0.6, 0.6) if is_open else Color.WHITE
|
||||
_refresh_lobbies()
|
||||
|
||||
# ── Data loading ──────────────────────────────────────────────────────────────
|
||||
|
||||
func _load_decks() -> void:
|
||||
Network.request("collection.get_decks", {},
|
||||
func(_t: String, p: Variant) -> void:
|
||||
var d := p as Dictionary
|
||||
_all_decks = d.get("decks", [])
|
||||
_populate_create_deck_buttons()
|
||||
# Auto-select first deck so _selected_deck is never empty.
|
||||
if not _all_decks.is_empty() and _selected_deck.is_empty():
|
||||
_selected_deck = str(_all_decks[0].get("id", "")),
|
||||
func(_c: String, _m: String) -> void: pass)
|
||||
|
||||
|
||||
func _refresh_lobbies() -> void:
|
||||
if _current_tab == "open":
|
||||
_load_open_lobbies()
|
||||
@@ -105,28 +111,17 @@ func _load_active_games() -> void:
|
||||
func(code: String, msg: String) -> void:
|
||||
print("[FreePlay] ERR list_active ", code, " ", msg))
|
||||
|
||||
|
||||
func _load_decks_for_create() -> void:
|
||||
Network.request("collection.get_decks", {},
|
||||
func(_t: String, p: Variant) -> void:
|
||||
var d := p as Dictionary
|
||||
var decks : Array = d.get("decks", [])
|
||||
_populate_deck_buttons(decks),
|
||||
func(_c: String, _m: String) -> void: pass)
|
||||
|
||||
# ── List population ───────────────────────────────────────────────────────────
|
||||
|
||||
func _populate_open_lobbies() -> void:
|
||||
for child in lobby_list_container.get_children():
|
||||
child.queue_free()
|
||||
|
||||
if _open_lobbies.is_empty():
|
||||
var lbl := Label.new()
|
||||
lbl.text = "No open lobbies found."
|
||||
lbl.modulate = Color(0.6, 0.6, 0.6)
|
||||
lobby_list_container.add_child(lbl)
|
||||
return
|
||||
|
||||
for lobby in _open_lobbies:
|
||||
lobby_list_container.add_child(_make_lobby_row(lobby))
|
||||
|
||||
@@ -134,14 +129,12 @@ func _populate_open_lobbies() -> void:
|
||||
func _populate_active_games() -> void:
|
||||
for child in active_list_container.get_children():
|
||||
child.queue_free()
|
||||
|
||||
if _active_games.is_empty():
|
||||
var lbl := Label.new()
|
||||
lbl.text = "No active games right now."
|
||||
lbl.modulate = Color(0.6, 0.6, 0.6)
|
||||
active_list_container.add_child(lbl)
|
||||
return
|
||||
|
||||
for game in _active_games:
|
||||
active_list_container.add_child(_make_active_game_row(game))
|
||||
|
||||
@@ -151,8 +144,8 @@ func _make_lobby_row(lobby: Dictionary) -> Control:
|
||||
row.custom_minimum_size = Vector2(0, 48)
|
||||
|
||||
var name_lbl := Label.new()
|
||||
var has_pw : bool = lobby.get("rules", {}).get("hasPassword", false)
|
||||
var has_guest : bool = lobby.get("hasGuest", false)
|
||||
var has_pw : bool = lobby.get("rules", {}).get("hasPassword", false)
|
||||
var has_guest : bool = lobby.get("hasGuest", false)
|
||||
var host_name : String = lobby.get("hostDisplayName", "?")
|
||||
name_lbl.text = "%s%s — Host: %s [%s]" % [
|
||||
"🔒 " if has_pw else "",
|
||||
@@ -181,7 +174,7 @@ func _make_active_game_row(game: Dictionary) -> Control:
|
||||
var row := HBoxContainer.new()
|
||||
row.custom_minimum_size = Vector2(0, 48)
|
||||
|
||||
var name_lbl := Label.new()
|
||||
var name_lbl := Label.new()
|
||||
var host_name : String = game.get("hostDisplayName", "?")
|
||||
var guest_name : String = game.get("guestDisplayName", "?")
|
||||
var has_pw : bool = game.get("hasPassword", false)
|
||||
@@ -208,44 +201,86 @@ func _make_active_game_row(game: Dictionary) -> Control:
|
||||
return wrapper
|
||||
|
||||
|
||||
func _populate_deck_buttons(decks: Array) -> void:
|
||||
func _populate_create_deck_buttons() -> void:
|
||||
for child in deck_select_container.get_children():
|
||||
child.queue_free()
|
||||
if decks.is_empty():
|
||||
_deck_btn_group = ButtonGroup.new()
|
||||
|
||||
if _all_decks.is_empty():
|
||||
var lbl := Label.new()
|
||||
lbl.text = "No decks found. Build one in My Collection first."
|
||||
lbl.modulate = Color(0.7, 0.7, 0.7)
|
||||
deck_select_container.add_child(lbl)
|
||||
return
|
||||
for deck in decks:
|
||||
var btn := Button.new()
|
||||
var did : String = str(deck.get("id", ""))
|
||||
|
||||
for i in range(_all_decks.size()):
|
||||
var deck := _all_decks[i] as Dictionary
|
||||
var did : String = str(deck.get("id", ""))
|
||||
var name : String = str(deck.get("name", "Unnamed Deck"))
|
||||
btn.text = name
|
||||
btn.toggle_mode = true
|
||||
btn.button_group = _get_or_create_deck_group()
|
||||
var btn := Button.new()
|
||||
btn.text = name
|
||||
btn.toggle_mode = true
|
||||
btn.button_group = _deck_btn_group
|
||||
# Auto-press first button to reflect the auto-selected deck.
|
||||
if i == 0:
|
||||
btn.button_pressed = true
|
||||
btn.pressed.connect(func(): _selected_deck = did)
|
||||
deck_select_container.add_child(btn)
|
||||
|
||||
# ── Deck button group helper ──────────────────────────────────────────────────
|
||||
|
||||
var _deck_btn_group : ButtonGroup = null
|
||||
|
||||
func _get_or_create_deck_group() -> ButtonGroup:
|
||||
if _deck_btn_group == null:
|
||||
_deck_btn_group = ButtonGroup.new()
|
||||
return _deck_btn_group
|
||||
|
||||
# ── Action handlers ───────────────────────────────────────────────────────────
|
||||
|
||||
func _on_join_pressed(lobby: Dictionary) -> void:
|
||||
var has_pw : bool = lobby.get("rules", {}).get("hasPassword", false)
|
||||
_pending_join_lobby = lobby
|
||||
# If more than one deck, ask which to use; otherwise proceed immediately.
|
||||
if _all_decks.size() > 1:
|
||||
_show_join_deck_picker()
|
||||
else:
|
||||
_after_deck_selected()
|
||||
|
||||
|
||||
func _show_join_deck_picker() -> void:
|
||||
var dialog := AcceptDialog.new()
|
||||
dialog.title = "Select Your Deck"
|
||||
dialog.min_size = Vector2(300, 260)
|
||||
|
||||
var scroll := ScrollContainer.new()
|
||||
scroll.set_anchors_preset(Control.PRESET_FULL_RECT)
|
||||
scroll.custom_minimum_size = Vector2(0, 200)
|
||||
dialog.add_child(scroll)
|
||||
|
||||
var vbox := VBoxContainer.new()
|
||||
vbox.size_flags_horizontal = Control.SIZE_EXPAND_FILL
|
||||
scroll.add_child(vbox)
|
||||
|
||||
var grp := ButtonGroup.new()
|
||||
for i in range(_all_decks.size()):
|
||||
var deck := _all_decks[i] as Dictionary
|
||||
var did : String = str(deck.get("id", ""))
|
||||
var name : String = str(deck.get("name", "Unnamed Deck"))
|
||||
var btn := Button.new()
|
||||
btn.text = name
|
||||
btn.toggle_mode = true
|
||||
btn.button_group = grp
|
||||
if _selected_deck == did or (i == 0 and _selected_deck.is_empty()):
|
||||
btn.button_pressed = true
|
||||
btn.pressed.connect(func(): _selected_deck = did)
|
||||
vbox.add_child(btn)
|
||||
|
||||
dialog.confirmed.connect(func():
|
||||
dialog.queue_free()
|
||||
_after_deck_selected())
|
||||
dialog.canceled.connect(func(): dialog.queue_free())
|
||||
add_child(dialog)
|
||||
dialog.popup_centered()
|
||||
|
||||
|
||||
func _after_deck_selected() -> void:
|
||||
var has_pw : bool = _pending_join_lobby.get("rules", {}).get("hasPassword", false)
|
||||
if has_pw:
|
||||
join_password_field.text = ""
|
||||
join_password_dialog.popup_centered()
|
||||
else:
|
||||
_do_join(lobby, "")
|
||||
_do_join(_pending_join_lobby, "")
|
||||
|
||||
|
||||
func _on_join_password_confirmed() -> void:
|
||||
@@ -254,13 +289,16 @@ func _on_join_password_confirmed() -> void:
|
||||
|
||||
func _do_join(lobby: Dictionary, password: String) -> void:
|
||||
var lid : String = str(lobby.get("lobbyId", ""))
|
||||
|
||||
if _selected_deck.is_empty():
|
||||
_selected_deck = _get_first_deck_id()
|
||||
OS.alert("Please select a deck first.", "No Deck")
|
||||
return
|
||||
|
||||
CustomLobby.join(lid, _selected_deck, password,
|
||||
func(_p: Variant) -> void:
|
||||
get_tree().change_scene_to_file("res://scenes/free_play/LobbyRoomScreen.tscn"),
|
||||
func(code: String, msg: String) -> void:
|
||||
OS.alert("Could not join lobby: %s — %s" % [code, msg], "Join Failed"))
|
||||
OS.alert("Could not join: %s — %s" % [code, msg], "Join Failed"))
|
||||
|
||||
|
||||
func _on_spectate_pressed(session_id: String) -> void:
|
||||
@@ -269,10 +307,8 @@ func _on_spectate_pressed(session_id: String) -> void:
|
||||
|
||||
|
||||
func _show_create_dialog() -> void:
|
||||
lobby_name_field.text = ""
|
||||
lobby_name_field.text = ""
|
||||
lobby_password_field.text = ""
|
||||
if _selected_deck.is_empty():
|
||||
_selected_deck = _get_first_deck_id()
|
||||
create_dialog.popup_centered()
|
||||
|
||||
|
||||
@@ -290,10 +326,3 @@ func _on_create_confirmed() -> void:
|
||||
get_tree().change_scene_to_file("res://scenes/free_play/LobbyRoomScreen.tscn"),
|
||||
func(code: String, msg: String) -> void:
|
||||
OS.alert("Could not create lobby: %s — %s" % [code, msg], "Create Failed"))
|
||||
|
||||
|
||||
func _get_first_deck_id() -> String:
|
||||
for child in deck_select_container.get_children():
|
||||
if child is Button and child.has_method("get_meta"):
|
||||
return ""
|
||||
return ""
|
||||
|
||||
Reference in New Issue
Block a user