Spectator fix
This commit is contained in:
@@ -80,7 +80,11 @@ func _clear_state() -> void:
|
||||
## so the server re-registers this connection and we get state updates again.
|
||||
## The state check prevents sending stale session IDs from games that already ended.
|
||||
func _on_auth_reconnect(_uid: String, _dn: String) -> void:
|
||||
if not session_id.is_empty() and not state.is_empty():
|
||||
if session_id.is_empty():
|
||||
return
|
||||
if is_spectating:
|
||||
_request_spectator_state()
|
||||
elif not state.is_empty():
|
||||
send_ready()
|
||||
|
||||
|
||||
@@ -89,8 +93,14 @@ func start_spectating(sid: String) -> void:
|
||||
is_spectating = true
|
||||
state = {}
|
||||
session_id = sid
|
||||
_request_spectator_state()
|
||||
|
||||
|
||||
func _request_spectator_state() -> void:
|
||||
if session_id.is_empty():
|
||||
return
|
||||
Network.request("game.spectate",
|
||||
{ "sessionId": sid },
|
||||
{ "sessionId": session_id },
|
||||
func(_t: String, p: Variant) -> void:
|
||||
if debug_output:
|
||||
print("[Game] <<< game.spectate.ack")
|
||||
@@ -389,7 +399,10 @@ func request_sync() -> void:
|
||||
if session_id.is_empty(): return
|
||||
if debug_output:
|
||||
print("[Game] requesting full state sync")
|
||||
_fetch_state()
|
||||
if is_spectating:
|
||||
_request_spectator_state()
|
||||
else:
|
||||
_fetch_state()
|
||||
|
||||
|
||||
func _player_by_id(account_id: String) -> Dictionary:
|
||||
|
||||
@@ -353,6 +353,14 @@ unique_name_in_owner = true
|
||||
custom_minimum_size = Vector2(0, 36)
|
||||
layout_mode = 2
|
||||
|
||||
[node name="SpectatorLeaveBtn" type="Button" parent="HUD/ActionRail/RailMargin/RailHBox/Controls/RightVBox"]
|
||||
unique_name_in_owner = true
|
||||
visible = false
|
||||
custom_minimum_size = Vector2(0, 36)
|
||||
layout_mode = 2
|
||||
theme_override_font_sizes/font_size = 12
|
||||
text = "Leave Spectator"
|
||||
|
||||
[node name="HandArea" type="PanelContainer" parent="HUD"]
|
||||
anchors_preset = -1
|
||||
anchor_top = 1.0
|
||||
@@ -427,6 +435,34 @@ offset_right = -1.0
|
||||
offset_bottom = 9.0
|
||||
scale = Vector2(1, 0.96000004)
|
||||
|
||||
[node name="SpectatorPlayersPanel" type="PanelContainer" parent="HUD"]
|
||||
unique_name_in_owner = true
|
||||
visible = false
|
||||
anchors_preset = -1
|
||||
anchor_top = 1.0
|
||||
anchor_right = 0.75
|
||||
anchor_bottom = 1.0
|
||||
offset_top = -200.0
|
||||
offset_bottom = 0.0
|
||||
|
||||
[node name="SpectatorMargin" type="MarginContainer" parent="HUD/SpectatorPlayersPanel"]
|
||||
layout_mode = 2
|
||||
theme_override_constants/margin_left = 8
|
||||
theme_override_constants/margin_top = 8
|
||||
theme_override_constants/margin_right = 8
|
||||
theme_override_constants/margin_bottom = 8
|
||||
|
||||
[node name="SpectatorScroll" type="ScrollContainer" parent="HUD/SpectatorPlayersPanel/SpectatorMargin"]
|
||||
layout_mode = 2
|
||||
size_flags_vertical = 3
|
||||
horizontal_scroll_mode = 0
|
||||
|
||||
[node name="SpectatorPlayerList" type="VBoxContainer" parent="HUD/SpectatorPlayersPanel/SpectatorMargin/SpectatorScroll"]
|
||||
unique_name_in_owner = true
|
||||
layout_mode = 2
|
||||
size_flags_horizontal = 3
|
||||
theme_override_constants/separation = 4
|
||||
|
||||
[node name="TileDrawerToggleBtn" type="Button" parent="HUD"]
|
||||
unique_name_in_owner = true
|
||||
custom_minimum_size = Vector2(36, 48)
|
||||
|
||||
@@ -74,6 +74,11 @@ enum Mode {
|
||||
@onready var _hand_area : PanelContainer = $HUD/HandArea
|
||||
@onready var _hand_spin_left : TextureButton = %HandSpinLeft
|
||||
@onready var _hand_spin_right : TextureButton = %HandSpinRight
|
||||
@onready var _sp_st_panel : PanelContainer = $"HUD/SP-ST Panel Container"
|
||||
@onready var _deck_discard_panel : Control = $"HUD/ActionRail/RailMargin/RailHBox/Deck-Discard Panel Container"
|
||||
@onready var _spectator_panel : PanelContainer = %SpectatorPlayersPanel
|
||||
@onready var _spectator_list : VBoxContainer = %SpectatorPlayerList
|
||||
@onready var _spectator_leave_btn : Button = %SpectatorLeaveBtn
|
||||
|
||||
var count : int = 0
|
||||
var _cam_tween : Tween = null
|
||||
@@ -353,6 +358,7 @@ func _ready() -> void:
|
||||
if Game.is_spectating:
|
||||
_is_spectating = true
|
||||
_setup_spectator_ui()
|
||||
Game.request_sync()
|
||||
# State was already applied via game.spectate ack — re-emit if present.
|
||||
if not Game.state.is_empty():
|
||||
_on_state_updated(Game.state)
|
||||
@@ -423,21 +429,21 @@ func _setup_spectator_ui() -> void:
|
||||
lbl.position = Vector2(0, 8)
|
||||
_hud.add_child(lbl)
|
||||
|
||||
# "Leave" button for spectators (top-right corner).
|
||||
var leave_btn := Button.new()
|
||||
leave_btn.text = "Leave"
|
||||
leave_btn.set_anchors_preset(Control.PRESET_TOP_RIGHT)
|
||||
leave_btn.position = Vector2(-90, 8)
|
||||
leave_btn.custom_minimum_size = Vector2(80, 32)
|
||||
leave_btn.pressed.connect(func() -> void:
|
||||
Game.stop_spectating()
|
||||
get_tree().change_scene_to_file("res://scenes/free_play/FreePlayScreen.tscn"))
|
||||
_hud.add_child(leave_btn)
|
||||
# Hide player-only HUD areas completely.
|
||||
_hand_area.visible = false
|
||||
_sp_st_panel.visible = false
|
||||
_deck_discard_panel.visible = false
|
||||
|
||||
# Hide action buttons and the settings panel — spectators cannot interact with the game.
|
||||
# Hide action-only buttons; Settings and Chat remain accessible.
|
||||
_draw_btn.visible = false
|
||||
_end_turn_btn.visible = false
|
||||
_settings_btn.visible = false
|
||||
|
||||
# Show the spectator player summary panel and the leave button in the action rail.
|
||||
_spectator_panel.visible = true
|
||||
_spectator_leave_btn.visible = true
|
||||
_spectator_leave_btn.pressed.connect(func() -> void:
|
||||
Game.stop_spectating()
|
||||
get_tree().change_scene_to_file("res://scenes/free_play/FreePlayScreen.tscn"))
|
||||
|
||||
# ── Environment ───────────────────────────────────────────────────────────────
|
||||
|
||||
@@ -4401,11 +4407,12 @@ func _on_hand_card_clicked(iid: String, _panel: PanelContainer) -> void:
|
||||
_last_click_iid = iid
|
||||
print("Clicked x times: ", count)
|
||||
count += 1
|
||||
if not Game.is_my_turn(): return
|
||||
|
||||
var card := _find_hand_card(iid)
|
||||
#print(card.get("name", "name failed"))
|
||||
if card.is_empty(): return
|
||||
if not Game.is_my_turn() and card.get("cardType", "") != "Instinct":
|
||||
return
|
||||
|
||||
# ArchSpirit in hand → Ascend or Transition.
|
||||
if card.get("cardType", "") == "ArchSpirit":
|
||||
@@ -4975,35 +4982,32 @@ func _wrapped_hand_delta(index: int, total: int) -> int:
|
||||
# ── HUD update ────────────────────────────────────────────────────────────────
|
||||
|
||||
func _update_hud(state: Dictionary) -> void:
|
||||
var phase : String = state.get("phase", "")
|
||||
var turn : int = state.get("turnNumber", 1)
|
||||
var is_mine : bool = (not _is_spectating) and Game.is_my_turn()
|
||||
var my : Dictionary = Game.get_my_player()
|
||||
var opp : Dictionary = Game.get_opponent()
|
||||
#print("[MY]", my)
|
||||
_phase_lbl.text = "Phase: %s" % phase
|
||||
if _is_spectating:
|
||||
var active_id : String = str(state.get("activePlayerAccountId", ""))
|
||||
_turn_lbl.text = "Turn %d — Spectating" % turn
|
||||
_turn_lbl.modulate = Color(1.0, 0.76, 0.2)
|
||||
else:
|
||||
_turn_lbl.text = "Turn %d — %s" % [turn, "Your turn" if is_mine else "Opponent's turn"]
|
||||
_turn_lbl.modulate = Color(0.4, 1.0, 0.5) if is_mine else Color(1.0, 0.6, 0.6)
|
||||
var phase : String = state.get("phase", "")
|
||||
var turn : int = state.get("turnNumber", 1)
|
||||
_phase_lbl.text = "Phase: %s" % phase
|
||||
|
||||
_sp_lbl.text = "%d" % int(my.get("currentSp", 0))
|
||||
_st_lbl.text = "%d" % int(my.get("currentSt", 0))
|
||||
_deck_lbl.text = "%d" % int(my.get("deckCount", 0))
|
||||
var discard = Game.get_my_player().get("discard", [])
|
||||
var discCount : int = discard.size()
|
||||
if (discCount <= 0):
|
||||
discCount = 0
|
||||
_discard_lbl.text = "%d" % discCount
|
||||
if _is_spectating:
|
||||
_update_hud_spectator(state, turn)
|
||||
return
|
||||
|
||||
# ── Player path ───────────────────────────────────────────────────────────
|
||||
var is_mine : bool = Game.is_my_turn()
|
||||
var my : Dictionary = Game.get_my_player()
|
||||
var opp : Dictionary = Game.get_opponent()
|
||||
|
||||
_turn_lbl.text = "Turn %d — %s" % [turn, "Your turn" if is_mine else "Opponent's turn"]
|
||||
_turn_lbl.modulate = Color(0.4, 1.0, 0.5) if is_mine else Color(1.0, 0.6, 0.6)
|
||||
|
||||
_sp_lbl.text = "%d" % int(my.get("currentSp", 0))
|
||||
_st_lbl.text = "%d" % int(my.get("currentSt", 0))
|
||||
_deck_lbl.text = "%d" % int(my.get("deckCount", 0))
|
||||
var discard : Array = my.get("discard", [])
|
||||
_discard_lbl.text = "%d" % max(0, discard.size())
|
||||
|
||||
_opp_name_lbl.text = Game.get_display_name(str(opp.get("accountId", "")))
|
||||
_opp_hand_lbl.text = "Hand: %d" % int(opp.get("handCount", 0))
|
||||
_opp_deck_lbl.text = "Deck: %d" % int(opp.get("deckCount", 0))
|
||||
|
||||
# Turn order strip — visible only in N-player (>2) games.
|
||||
var all_players : Array = state.get("players", [])
|
||||
if all_players.size() > 2:
|
||||
_opp_panel.visible = false
|
||||
@@ -5013,16 +5017,12 @@ func _update_hud(state: Dictionary) -> void:
|
||||
_opp_panel.visible = true
|
||||
_turn_order_strip.visible = false
|
||||
|
||||
if _is_spectating:
|
||||
_draw_btn.visible = false
|
||||
_end_turn_btn.visible = false
|
||||
else:
|
||||
_draw_btn.visible = (phase == "Draw" and is_mine)
|
||||
_end_turn_btn.visible = (phase == "Main" and is_mine)
|
||||
_end_turn_btn.disabled = not is_mine
|
||||
_draw_btn.visible = (phase == "Draw" and is_mine)
|
||||
_end_turn_btn.visible = (phase == "Main" and is_mine)
|
||||
_end_turn_btn.disabled = not is_mine
|
||||
|
||||
if phase == "Placement":
|
||||
var my_arch : Dictionary = my.get("archSpirit", {})
|
||||
var my_arch : Dictionary = my.get("archSpirit", {})
|
||||
var i_have_placed : bool = not my_arch.is_empty() and my_arch.get("position") != null
|
||||
if not i_have_placed:
|
||||
_phase_lbl.text = "Phase: Placement — click a tile to place your ArchSpirit"
|
||||
@@ -5036,6 +5036,118 @@ func _update_hud(state: Dictionary) -> void:
|
||||
_set_mode(Mode.NONE)
|
||||
|
||||
|
||||
func _update_hud_spectator(state: Dictionary, turn: int) -> void:
|
||||
var active_id : String = str(state.get("activePlayerAccountId", ""))
|
||||
var active_name : String = Game.get_display_name(active_id)
|
||||
_turn_lbl.text = "Turn %d — %s is acting" % [turn, active_name]
|
||||
_turn_lbl.modulate = Color(1.0, 0.76, 0.2)
|
||||
|
||||
# Always hide the 2-player opponent panel; show turn strip for all player counts.
|
||||
_opp_panel.visible = false
|
||||
var all_players : Array = state.get("players", [])
|
||||
_turn_order_strip.visible = all_players.size() > 1
|
||||
if _turn_order_strip.visible:
|
||||
_rebuild_turn_order_strip(state)
|
||||
|
||||
_draw_btn.visible = false
|
||||
_end_turn_btn.visible = false
|
||||
|
||||
_rebuild_spectator_panel(state)
|
||||
|
||||
|
||||
## Rebuild the spectator player-summary list. One row per player sorted by seatIndex.
|
||||
## Shows name (with active-turn highlight), SP, ST, deck count, and a Discard button.
|
||||
func _rebuild_spectator_panel(state: Dictionary) -> void:
|
||||
for c in _spectator_list.get_children():
|
||||
c.queue_free()
|
||||
|
||||
var active_id : String = str(state.get("activePlayerAccountId", ""))
|
||||
var players : Array = state.get("players", []).duplicate()
|
||||
var team_colors : Dictionary = state.get("teamColors", {})
|
||||
|
||||
players.sort_custom(func(a: Dictionary, b: Dictionary) -> bool:
|
||||
return int(a.get("seatIndex", 0)) < int(b.get("seatIndex", 0)))
|
||||
|
||||
for p: Dictionary in players:
|
||||
var pid : String = str(p.get("accountId", ""))
|
||||
var is_active : bool = pid == active_id
|
||||
var eliminated: bool = bool(p.get("isEliminated", false))
|
||||
var team_idx : int = int(p.get("teamIndex", -1))
|
||||
var hex_str : String = str(team_colors.get(str(team_idx), ""))
|
||||
var display : String = Game.get_display_name(pid)
|
||||
|
||||
var row := PanelContainer.new()
|
||||
row.custom_minimum_size = Vector2(0, 40)
|
||||
row.self_modulate = Color(1, 1, 1, 0.4 if eliminated else 1.0)
|
||||
|
||||
var style := StyleBoxFlat.new()
|
||||
style.bg_color = Color(0.18, 0.16, 0.08, 0.92) if is_active else Color(0.10, 0.10, 0.13, 0.85)
|
||||
style.border_width_left = 4
|
||||
style.border_color = Color(1.0, 0.85, 0.2) if is_active \
|
||||
else (Color.html("#" + hex_str) if not hex_str.is_empty() else Color(0.4, 0.4, 0.4))
|
||||
row.add_theme_stylebox_override("panel", style)
|
||||
|
||||
var margin := MarginContainer.new()
|
||||
margin.add_theme_constant_override("margin_left", 8)
|
||||
margin.add_theme_constant_override("margin_right", 8)
|
||||
margin.add_theme_constant_override("margin_top", 4)
|
||||
margin.add_theme_constant_override("margin_bottom", 4)
|
||||
row.add_child(margin)
|
||||
|
||||
var hbox := HBoxContainer.new()
|
||||
hbox.add_theme_constant_override("separation", 12)
|
||||
margin.add_child(hbox)
|
||||
|
||||
var name_lbl := Label.new()
|
||||
name_lbl.text = ("★ " if is_active else "") + display + (" [X]" if eliminated else "")
|
||||
name_lbl.add_theme_font_size_override("font_size", 12)
|
||||
name_lbl.custom_minimum_size = Vector2(120, 0)
|
||||
name_lbl.size_flags_horizontal = Control.SIZE_EXPAND_FILL
|
||||
name_lbl.vertical_alignment = VERTICAL_ALIGNMENT_CENTER
|
||||
if is_active:
|
||||
name_lbl.add_theme_color_override("font_color", Color(1.0, 0.9, 0.3))
|
||||
elif eliminated:
|
||||
name_lbl.add_theme_color_override("font_color", Color(0.4, 0.4, 0.4))
|
||||
hbox.add_child(name_lbl)
|
||||
|
||||
var sp_lbl := Label.new()
|
||||
sp_lbl.text = "SP: %d" % int(p.get("currentSp", 0))
|
||||
sp_lbl.add_theme_font_size_override("font_size", 11)
|
||||
sp_lbl.vertical_alignment = VERTICAL_ALIGNMENT_CENTER
|
||||
sp_lbl.tooltip_text = "Spirit Points"
|
||||
hbox.add_child(sp_lbl)
|
||||
|
||||
var st_lbl := Label.new()
|
||||
st_lbl.text = "ST: %d" % int(p.get("currentSt", 0))
|
||||
st_lbl.add_theme_font_size_override("font_size", 11)
|
||||
st_lbl.vertical_alignment = VERTICAL_ALIGNMENT_CENTER
|
||||
st_lbl.tooltip_text = "Sacrifice Tokens"
|
||||
hbox.add_child(st_lbl)
|
||||
|
||||
var deck_lbl := Label.new()
|
||||
deck_lbl.text = "Deck: %d" % int(p.get("deckCount", 0))
|
||||
deck_lbl.add_theme_font_size_override("font_size", 11)
|
||||
deck_lbl.vertical_alignment = VERTICAL_ALIGNMENT_CENTER
|
||||
hbox.add_child(deck_lbl)
|
||||
|
||||
var disc_cards : Array = p.get("discard", [])
|
||||
var disc_count : int = disc_cards.size() if not disc_cards.is_empty() else int(p.get("discardCount", 0))
|
||||
var disc_btn := Button.new()
|
||||
disc_btn.text = "Discard: %d" % disc_count
|
||||
disc_btn.add_theme_font_size_override("font_size", 11)
|
||||
disc_btn.custom_minimum_size = Vector2(80, 0)
|
||||
if not disc_cards.is_empty():
|
||||
var cap_display := display
|
||||
var cap_cards := disc_cards
|
||||
disc_btn.pressed.connect(func() -> void:
|
||||
_show_discard_for("%s Discard" % cap_display, cap_cards))
|
||||
else:
|
||||
disc_btn.disabled = true
|
||||
hbox.add_child(disc_btn)
|
||||
|
||||
_spectator_list.add_child(row)
|
||||
|
||||
|
||||
## Rebuild the turn-order chip strip for N-player games.
|
||||
## One chip per player sorted by seatIndex; highlights the active player,
|
||||
## dims eliminated players, and marks the local player as "You".
|
||||
|
||||
Reference in New Issue
Block a user