Client: Add AI opponent support to custom lobby
- CustomLobby.gd: add add_ai(difficulty, strategy) method sending game.ai.add; add ai_slots: Array state; handle game.ai.slots_changed push updating ai_slots and emitting ai_slots_changed signal; clear ai_slots in clear_state() - LobbyRoomScreen.tscn: add AddAiBtn (Button, hidden by default) between guest ready indicator and additional players container - lobby_room_screen.gd: wire AddAiBtn (host-only, visible when guest slot is empty and host is not ready); _on_add_ai_pressed() opens a dialog with OptionButton for difficulty (D1 Novice - D10 Mythic) and strategy (Balanced/Aggressive/Control/Ramp/Swarm); guest slot display renders AI name + difficulty when an AI occupies seat 1; _ai_slot_for_seat() helper; connect/disconnect ai_slots_changed -> _refresh_display 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:
@@ -57,6 +57,9 @@ signal lobby_notice(message: String)
|
||||
## Pushed (or acked) when the host changes a seat's team assignment.
|
||||
signal team_assignment_changed(seat_teams: Dictionary)
|
||||
|
||||
## Emitted when the AI slot list for the current lobby changes (add / server push).
|
||||
signal ai_slots_changed(ai_slots: Array)
|
||||
|
||||
# ── State ─────────────────────────────────────────────────────────────────────
|
||||
|
||||
var lobby_id : String = ""
|
||||
@@ -72,6 +75,8 @@ var game_mode : String = "FFA"
|
||||
var team_count : int = 1
|
||||
## Seat index → team index overrides. Empty = use default (seat % team_count).
|
||||
var seat_teams : Dictionary = {}
|
||||
## Current AI slots in this lobby (array of dicts: accountId, displayName, difficulty, strategy, seatIndex).
|
||||
var ai_slots : Array = []
|
||||
|
||||
# ── Lifecycle ─────────────────────────────────────────────────────────────────
|
||||
|
||||
@@ -93,6 +98,7 @@ func clear_state() -> void:
|
||||
game_mode = "FFA"
|
||||
team_count = 1
|
||||
seat_teams = {}
|
||||
ai_slots = []
|
||||
|
||||
# ── Public actions ────────────────────────────────────────────────────────────
|
||||
|
||||
@@ -320,6 +326,21 @@ func reassign_team(seat_index: int, team_index: int) -> void:
|
||||
print("[CustomLobby] ERR reassign_team ", code, " ", msg))
|
||||
|
||||
|
||||
## Host-only: add an AI player to the current open lobby.
|
||||
## difficulty is 1-10, strategy is one of: Aggressive, Control, Balanced, Ramp, Swarm.
|
||||
func add_ai(difficulty: int, strategy: String,
|
||||
on_success: Callable = Callable(),
|
||||
on_error: Callable = Callable()) -> void:
|
||||
Network.request("game.ai.add",
|
||||
{ "lobbyId": lobby_id, "difficulty": difficulty, "strategy": strategy },
|
||||
func(_t: String, p: Variant) -> void:
|
||||
var d := p as Dictionary
|
||||
if on_success.is_valid(): on_success.call(d),
|
||||
func(code: String, msg: String) -> void:
|
||||
print("[CustomLobby] ERR add_ai ", code, " ", msg)
|
||||
if on_error.is_valid(): on_error.call(code, msg))
|
||||
|
||||
|
||||
# ── Private ───────────────────────────────────────────────────────────────────
|
||||
|
||||
func _on_server_push(type: String, payload: Variant) -> void:
|
||||
@@ -412,6 +433,11 @@ func _on_server_push(type: String, payload: Variant) -> void:
|
||||
"game.custom.lobby.rule_suggestion.rejected":
|
||||
rule_suggestion_rejected.emit()
|
||||
|
||||
"game.ai.slots_changed":
|
||||
var d := payload as Dictionary
|
||||
ai_slots = d.get("slots", []) as Array
|
||||
ai_slots_changed.emit(ai_slots)
|
||||
|
||||
"game.custom.lobby.team_reassigned":
|
||||
var d := payload as Dictionary
|
||||
var raw := d.get("seatTeams", {}) as Dictionary
|
||||
|
||||
@@ -200,6 +200,12 @@ modulate = Color(0.6, 0.6, 0.6, 1)
|
||||
layout_mode = 2
|
||||
text = "Not ready"
|
||||
|
||||
[node name="AddAiBtn" type="Button" parent="Layout/MainSplit/LeftPanel/LeftContent/LeftMargin/LeftInner"]
|
||||
unique_name_in_owner = true
|
||||
layout_mode = 2
|
||||
text = "Add AI Opponent"
|
||||
visible = false
|
||||
|
||||
[node name="AdditionalPlayersContainer" type="VBoxContainer" parent="Layout/MainSplit/LeftPanel/LeftContent/LeftMargin/LeftInner"]
|
||||
unique_name_in_owner = true
|
||||
layout_mode = 2
|
||||
|
||||
@@ -41,6 +41,7 @@ const RULES_DIALOG = preload("res://scenes/free_play/components/RulesEditorDialo
|
||||
@onready var suggestion_accept_btn : Button = %SuggestionAcceptBtn
|
||||
@onready var suggestion_reject_btn : Button = %SuggestionRejectBtn
|
||||
@onready var deck_picker_dialog : AcceptDialog = %DeckPickerDialog
|
||||
@onready var add_ai_btn : Button = %AddAiBtn
|
||||
@onready var deck_picker_container : VBoxContainer = %DeckPickerContainer
|
||||
@onready var preset_list_dialog : AcceptDialog = %PresetListDialog
|
||||
@onready var preset_list_container : VBoxContainer = %PresetListContainer
|
||||
@@ -74,6 +75,7 @@ func _exit_tree() -> void:
|
||||
CustomLobby.rule_suggestion_rejected.disconnect(_on_rule_suggestion_rejected)
|
||||
CustomLobby.lobby_notice.disconnect(_on_lobby_notice)
|
||||
CustomLobby.team_assignment_changed.disconnect(_on_team_assignment_changed)
|
||||
CustomLobby.ai_slots_changed.disconnect(_on_ai_slots_changed)
|
||||
|
||||
# ── Setup ─────────────────────────────────────────────────────────────────────
|
||||
|
||||
@@ -125,6 +127,10 @@ func _setup_ui() -> void:
|
||||
deck_picker_dialog.confirmed.connect(_on_deck_picker_confirmed)
|
||||
preset_list_dialog.confirmed.connect(_on_preset_confirmed)
|
||||
|
||||
# Add AI button (host-only, shown when guest slot is empty)
|
||||
add_ai_btn.visible = false
|
||||
add_ai_btn.pressed.connect(_on_add_ai_pressed)
|
||||
|
||||
|
||||
func _connect_signals() -> void:
|
||||
CustomLobby.guest_joined.connect(_on_guest_joined)
|
||||
@@ -140,6 +146,7 @@ func _connect_signals() -> void:
|
||||
CustomLobby.rule_suggestion_rejected.connect(_on_rule_suggestion_rejected)
|
||||
CustomLobby.lobby_notice.connect(_on_lobby_notice)
|
||||
CustomLobby.team_assignment_changed.connect(_on_team_assignment_changed)
|
||||
CustomLobby.ai_slots_changed.connect(_on_ai_slots_changed)
|
||||
|
||||
# ── Refresh ───────────────────────────────────────────────────────────────────
|
||||
|
||||
@@ -163,16 +170,27 @@ func _refresh_display() -> void:
|
||||
host_name_label.text = "Host: %s%s" % [str(lobby.get("hostDisplayName", Auth.display_name)), team_suffix_0]
|
||||
host_deck_label.text = "Deck: (selected)"
|
||||
|
||||
# Guest slot
|
||||
# Guest slot — may be a human, an AI, or empty
|
||||
var team_suffix_1 : String = _team_label(game_mode, 1, team_count)
|
||||
var has_guest : bool = lobby.get("hasGuest", false) or lobby.get("guestAccountId", "") != ""
|
||||
if has_guest:
|
||||
var ai_in_guest_slot : Dictionary = _ai_slot_for_seat(1)
|
||||
if not ai_in_guest_slot.is_empty():
|
||||
guest_name_label.text = "AI: %s%s" % [str(ai_in_guest_slot.get("displayName", "AI")), team_suffix_1]
|
||||
guest_deck_label.text = "Difficulty %d" % int(ai_in_guest_slot.get("difficulty", 1))
|
||||
guest_ready_indicator.text = "✓ READY"
|
||||
guest_ready_indicator.modulate = Color(0.2, 0.9, 0.3)
|
||||
elif has_guest:
|
||||
guest_name_label.text = "Guest: %s%s" % [str(lobby.get("guestDisplayName", "Guest")), team_suffix_1]
|
||||
guest_deck_label.text = "Deck: (selected)"
|
||||
else:
|
||||
guest_name_label.text = "Waiting for guest…"
|
||||
guest_deck_label.text = ""
|
||||
|
||||
# Show Add AI button only for host when guest slot is truly empty (max 2 players)
|
||||
# and neither player is ready
|
||||
var slot_open : bool = (not has_guest and ai_in_guest_slot.is_empty() and max_players == 2)
|
||||
add_ai_btn.visible = CustomLobby.is_host and slot_open and not CustomLobby.i_am_ready
|
||||
|
||||
# Additional players (3+) from lobby dict.
|
||||
_rebuild_additional_players_panel(lobby, max_players)
|
||||
|
||||
@@ -511,6 +529,69 @@ func _format_rules_summary(rules: Dictionary) -> String:
|
||||
return "\n".join(lines) if not lines.is_empty() else "(no changes)"
|
||||
|
||||
|
||||
func _ai_slot_for_seat(seat: int) -> Dictionary:
|
||||
for slot: Dictionary in CustomLobby.ai_slots:
|
||||
if int(slot.get("seatIndex", -1)) == seat:
|
||||
return slot
|
||||
return {}
|
||||
|
||||
|
||||
func _on_ai_slots_changed(_slots: Array) -> void:
|
||||
_refresh_display()
|
||||
|
||||
|
||||
func _on_add_ai_pressed() -> void:
|
||||
var dlg := AcceptDialog.new()
|
||||
dlg.title = "Add AI Opponent"
|
||||
dlg.ok_button_text = "Add"
|
||||
var vbox := VBoxContainer.new()
|
||||
vbox.add_theme_constant_override("separation", 8)
|
||||
|
||||
# Difficulty picker
|
||||
var diff_label := Label.new()
|
||||
diff_label.text = "Difficulty"
|
||||
diff_label.add_theme_font_size_override("font_size", 12)
|
||||
var diff_opts := OptionButton.new()
|
||||
var diff_names := ["D1 – Novice", "D2 – Apprentice", "D3 – Journeyman",
|
||||
"D4 – Adept", "D5 – Expert", "D6 – Master",
|
||||
"D7 – Grandmaster", "D8 – Champion", "D9 – Legend", "D10 – Mythic"]
|
||||
for dn: String in diff_names:
|
||||
diff_opts.add_item(dn)
|
||||
diff_opts.selected = 0 # default D1
|
||||
|
||||
# Strategy picker
|
||||
var strat_label := Label.new()
|
||||
strat_label.text = "Strategy"
|
||||
strat_label.add_theme_font_size_override("font_size", 12)
|
||||
var strat_opts := OptionButton.new()
|
||||
var strategies := ["Balanced", "Aggressive", "Control", "Ramp", "Swarm"]
|
||||
for s: String in strategies:
|
||||
strat_opts.add_item(s)
|
||||
strat_opts.selected = 0 # default Balanced
|
||||
|
||||
vbox.add_child(diff_label)
|
||||
vbox.add_child(diff_opts)
|
||||
vbox.add_child(strat_label)
|
||||
vbox.add_child(strat_opts)
|
||||
dlg.add_child(vbox)
|
||||
add_child(dlg)
|
||||
|
||||
dlg.confirmed.connect(func():
|
||||
var difficulty := diff_opts.selected + 1 # 1-indexed
|
||||
var strategy := strategies[strat_opts.selected]
|
||||
add_ai_btn.disabled = true
|
||||
CustomLobby.add_ai(difficulty, strategy,
|
||||
func(_d: Dictionary) -> void:
|
||||
add_ai_btn.disabled = false
|
||||
_refresh_display(),
|
||||
func(_code: String, _msg: String) -> void:
|
||||
add_ai_btn.disabled = false
|
||||
_show_toast("Failed to add AI: " + _msg))
|
||||
dlg.queue_free())
|
||||
dlg.canceled.connect(func(): dlg.queue_free())
|
||||
dlg.popup_centered(Vector2(260, 160))
|
||||
|
||||
|
||||
func _on_lobby_left() -> void:
|
||||
get_tree().change_scene_to_file("res://scenes/free_play/FreePlayScreen.tscn")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user