Feature: model retry + rejoin on login + 5-min inactivity detection
**Feature 1 — Model retry (client)** - `ModelCache.gd`: add `evict(url)` to remove failed entries so re-download is possible - `game_screen.gd`: when GLB load fails, `_schedule_model_retry` queues a 60-second retry (up to 10 attempts); `_do_model_retry` evicts + re-downloads and swaps the placeholder mesh for the real model if/when it succeeds **Feature 2 — Rejoin on login (server + client)** - Server: `GameGetActiveSessionHandler` (`game.get_active_session`) — returns the caller's active session ID + phase, or null when no session exists - Client: `init_screen.gd` checks for an active session after content sync; shows a "Rejoin / Concede" dialog when one is found; rejoin navigates to GameScreen with the session ID already set so `send_ready` → `AlreadyStarted` → `_fetch_state` **Feature 3 — 5-minute inactivity detection (server + client)** - `GameSession`: add `LastActionAt` column + EF migration (`20260317000001_AddLastActionAt`) - `GameHandlerBase.SaveStateAsync`: sets `session.LastActionAt` on every action - `GameInactivityMonitorService`: background poll every 60 s; when a session hasn't had an action in ≥ 5 min, pushes `game.inactive_warning` to the stalled player and `game.opponent_inactive` to their opponent (once per session per server lifetime) - `GameForceConcedeHandler` (`game.force_concede`): lets the waiting player claim the win; validates caller is not the active player and `LastActionAt` is stale - `Game.gd`: new `opponent_inactive` / `inactive_warning` signals + push handlers + `send_force_concede()` helper - `game_screen.gd`: handles `opponent_inactive` with Accept/Wait dialog; handles `inactive_warning` with informational popup 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:
@@ -25,6 +25,14 @@ signal game_over(winner_account_id: String, win_condition: String,
|
||||
## Emitted when the server abandons an Initializing session (e.g. server restart).
|
||||
signal session_abandoned(reason: String)
|
||||
|
||||
## Emitted when the active player has been inactive for 5 minutes.
|
||||
## The local player (waiting for opponent) can call send_force_concede() to claim a win.
|
||||
signal opponent_inactive(session_id: String)
|
||||
|
||||
## Emitted when the local player has been flagged as inactive.
|
||||
## Show a warning — the opponent may accept a forfeit.
|
||||
signal inactive_warning(session_id: String)
|
||||
|
||||
# ── State ─────────────────────────────────────────────────────────────────────
|
||||
|
||||
var session_id : String = ""
|
||||
@@ -194,6 +202,10 @@ func send_concede() -> void:
|
||||
_game_request("game.concede", {})
|
||||
|
||||
|
||||
func send_force_concede() -> void:
|
||||
_game_request("game.force_concede", {})
|
||||
|
||||
|
||||
func send_ascend(incoming_card_instance_id: String) -> void:
|
||||
_game_request("game.ascend",
|
||||
{ "incomingCardInstanceId": incoming_card_instance_id })
|
||||
@@ -289,3 +301,15 @@ func _on_server_push(type: String, payload: Variant) -> void:
|
||||
print("[Game] session abandoned: ", reason)
|
||||
_clear_state()
|
||||
session_abandoned.emit(reason)
|
||||
|
||||
"game.opponent_inactive":
|
||||
var d := payload as Dictionary
|
||||
var sid : String = str(d.get("sessionId", ""))
|
||||
print("[Game] PUSH opponent_inactive session=", sid)
|
||||
opponent_inactive.emit(sid)
|
||||
|
||||
"game.inactive_warning":
|
||||
var d := payload as Dictionary
|
||||
var sid : String = str(d.get("sessionId", ""))
|
||||
print("[Game] PUSH inactive_warning session=", sid)
|
||||
inactive_warning.emit(sid)
|
||||
|
||||
@@ -29,6 +29,15 @@ func _ready() -> void:
|
||||
DirAccess.make_dir_recursive_absolute(_CACHE_DIR)
|
||||
|
||||
|
||||
## Remove a cached entry (memory + disk) so the next load_model call re-downloads it.
|
||||
## Use before retrying a previously-failed URL.
|
||||
func evict(url: String) -> void:
|
||||
_mem.erase(url)
|
||||
var disk := _disk_path(url)
|
||||
if FileAccess.file_exists(disk):
|
||||
DirAccess.remove_absolute(disk)
|
||||
|
||||
|
||||
## Request a model by CDN URL.
|
||||
## callback(node: Node3D) is called on the main thread; node may be null.
|
||||
## If the same URL is already downloading, callbacks are queued and all
|
||||
|
||||
@@ -183,6 +183,8 @@ var _oneshot_callbacks : Dictionary = {} # iid → Callable (animation_finishe
|
||||
# Per-unit model config.
|
||||
var _model_base_angles : Dictionary = {} # iid → float (base facing angle, radians)
|
||||
var _needs_face_rot : Dictionary = {} # iid → bool (team-based 180° flip flag)
|
||||
# Model retry tracking (when real GLB failed to download at placement time).
|
||||
var _unit_model_retries : Dictionary = {} # iid → attempt number (1-based, capped at 10)
|
||||
|
||||
## Animation name aliases: maps the canonical lower-case action to fallback track names.
|
||||
## The AnimationPlayer track list is matched case-insensitively against each entry.
|
||||
@@ -218,6 +220,8 @@ func _ready() -> void:
|
||||
Game.state_updated.connect(_on_state_updated)
|
||||
Game.game_over.connect(_on_game_over)
|
||||
Game.session_abandoned.connect(_on_session_abandoned)
|
||||
Game.opponent_inactive.connect(_on_opponent_inactive)
|
||||
Game.inactive_warning.connect(_on_inactive_warning)
|
||||
|
||||
if not Game.state.is_empty():
|
||||
_on_state_updated(Game.state)
|
||||
@@ -232,6 +236,10 @@ func _exit_tree() -> void:
|
||||
Game.game_over.disconnect(_on_game_over)
|
||||
if Game.session_abandoned.is_connected(_on_session_abandoned):
|
||||
Game.session_abandoned.disconnect(_on_session_abandoned)
|
||||
if Game.opponent_inactive.is_connected(_on_opponent_inactive):
|
||||
Game.opponent_inactive.disconnect(_on_opponent_inactive)
|
||||
if Game.inactive_warning.is_connected(_on_inactive_warning):
|
||||
Game.inactive_warning.disconnect(_on_inactive_warning)
|
||||
# Remove chat overlay added to the viewport root.
|
||||
var root := get_tree().get_root()
|
||||
for child in root.get_children():
|
||||
@@ -703,6 +711,7 @@ func _rebuild_units(state: Dictionary) -> void:
|
||||
_oneshot_callbacks.erase(iid)
|
||||
_model_base_angles.erase(iid)
|
||||
_needs_face_rot.erase(iid)
|
||||
_unit_model_retries.erase(iid)
|
||||
|
||||
# 3. Add units not yet present (newly played, or first render after connect).
|
||||
for iid : String in desired:
|
||||
@@ -801,6 +810,10 @@ func _add_unit_mesh(unit: Dictionary, team_color: Color, is_enemy: bool = false)
|
||||
var needs_face_rotation : bool = _local_is_p1 != is_enemy
|
||||
var base_angle : float = _facing_to_base_angle(facing_dir)
|
||||
|
||||
# Pre-set facing config so _schedule_model_retry can use it even if load fails.
|
||||
_model_base_angles[iid] = base_angle
|
||||
_needs_face_rot[iid] = needs_face_rotation
|
||||
|
||||
var body_ref : Variant = weakref(body)
|
||||
ModelCache.load_model(model_url, func(model_node: Node3D) -> void:
|
||||
# Guard: body may have been freed by a state rebuild before load finished.
|
||||
@@ -810,6 +823,8 @@ func _add_unit_mesh(unit: Dictionary, team_color: Color, is_enemy: bool = false)
|
||||
model_node.queue_free()
|
||||
return
|
||||
if model_node == null:
|
||||
# Download failed — schedule a background retry.
|
||||
_schedule_model_retry(iid, model_url, 1)
|
||||
return
|
||||
|
||||
# Remove the placeholder MeshInstance3D.
|
||||
@@ -821,9 +836,7 @@ func _add_unit_mesh(unit: Dictionary, team_color: Color, is_enemy: bool = false)
|
||||
if not is_equal_approx(model_scale, 1.0):
|
||||
model_node.scale = Vector3.ONE * model_scale
|
||||
|
||||
# Store per-unit model config for movement facing and idle rotation.
|
||||
_model_base_angles[iid] = base_angle
|
||||
_needs_face_rot[iid] = needs_face_rotation
|
||||
# Facing config was already stored above; no need to repeat.
|
||||
|
||||
# Apply idle/team rotation (facing toward opponent).
|
||||
model_node.rotation.y = _idle_rotation(iid)
|
||||
@@ -851,6 +864,78 @@ func _add_unit_mesh(unit: Dictionary, team_color: Color, is_enemy: bool = false)
|
||||
)
|
||||
|
||||
|
||||
## Schedule a retry to (re)download the real GLB model for a unit.
|
||||
## attempt is 1-based; retries stop after MAX_RETRIES.
|
||||
## Only schedules if no equal-or-later retry is already pending for this unit.
|
||||
func _schedule_model_retry(iid: String, model_url: String, attempt: int) -> void:
|
||||
const MAX_RETRIES := 10
|
||||
if attempt > MAX_RETRIES:
|
||||
return
|
||||
# Don't override a retry already queued at this or a higher attempt number.
|
||||
if _unit_model_retries.get(iid, 0) >= attempt:
|
||||
return
|
||||
_unit_model_retries[iid] = attempt
|
||||
print("[GameScreen] Model retry %d/%d scheduled for %s in 60s" % [attempt, MAX_RETRIES, iid])
|
||||
get_tree().create_timer(60.0).timeout.connect(
|
||||
func() -> void: _do_model_retry(iid, model_url, attempt),
|
||||
CONNECT_ONE_SHOT)
|
||||
|
||||
|
||||
## Execute one model retry attempt. Evicts the URL so ModelCache re-downloads it.
|
||||
## If successful, swaps out the placeholder and starts idle animation.
|
||||
## If it fails again, schedules the next attempt.
|
||||
func _do_model_retry(iid: String, model_url: String, attempt: int) -> void:
|
||||
# Was this attempt superseded by a later scheduled retry?
|
||||
if _unit_model_retries.get(iid, -1) != attempt:
|
||||
return
|
||||
# Unit may have left the board while we were waiting.
|
||||
if not _unit_bodies.has(iid):
|
||||
_unit_model_retries.erase(iid)
|
||||
return
|
||||
print("[GameScreen] Model retry %d — downloading %s" % [attempt, model_url])
|
||||
ModelCache.evict(model_url)
|
||||
ModelCache.load_model(model_url, func(model_node: Node3D) -> void:
|
||||
if model_node == null:
|
||||
# Still failing — try again later.
|
||||
_unit_model_retries.erase(iid)
|
||||
_schedule_model_retry(iid, model_url, attempt + 1)
|
||||
return
|
||||
_unit_model_retries.erase(iid)
|
||||
# Use the CURRENT body (may differ from when the retry was first scheduled).
|
||||
var body := _unit_bodies.get(iid) as StaticBody3D
|
||||
if not is_instance_valid(body):
|
||||
model_node.queue_free()
|
||||
return
|
||||
# Remove placeholder mesh.
|
||||
for child in body.get_children():
|
||||
if child is MeshInstance3D:
|
||||
child.queue_free()
|
||||
# Re-read card config in case ContentCache was refreshed since placement.
|
||||
var def_id : String = _unit_def_ids.get(iid, "")
|
||||
var def := ContentCache.get_definition(def_id)
|
||||
var model_scale : float = float(def.get("modelScale", 1.0))
|
||||
var tint_str : String = str(def.get("modelTintColor", ""))
|
||||
var model_tint : Color = Color.WHITE
|
||||
if not tint_str.is_empty() and tint_str != "null":
|
||||
model_tint = Color(tint_str)
|
||||
if not is_equal_approx(model_scale, 1.0):
|
||||
model_node.scale = Vector3.ONE * model_scale
|
||||
model_node.rotation.y = _idle_rotation(iid)
|
||||
_apply_model_tint(model_node, model_tint)
|
||||
var above_tile : float = body.get_meta("above_tile", 0.28)
|
||||
body.add_child(model_node)
|
||||
var m_aabb := _model_local_aabb(model_node)
|
||||
if m_aabb.size != Vector3.ZERO:
|
||||
model_node.position.y = -above_tile - model_node.scale.y * m_aabb.position.y
|
||||
_unit_meshes[iid] = model_node
|
||||
var ap := _find_animation_player(model_node)
|
||||
_anim_players[iid] = ap
|
||||
if ap != null:
|
||||
_play_unit_anim(iid, "idle", true)
|
||||
print("[GameScreen] Model retry %d succeeded for %s" % [attempt, iid])
|
||||
)
|
||||
|
||||
|
||||
## Returns the first Model3D asset dict for a card definition, or {} if none.
|
||||
func _find_model3d_asset(def: Dictionary) -> Dictionary:
|
||||
for asset: Variant in def.get("assets", []):
|
||||
@@ -2538,6 +2623,41 @@ func _on_game_over(winner_id: String, win_condition: String,
|
||||
func _on_session_abandoned(_reason: String) -> void:
|
||||
get_tree().change_scene_to_file("res://scenes/main/MainScreen.tscn")
|
||||
|
||||
|
||||
## Pushed when the opponent (active player) has been inactive ≥ 5 minutes.
|
||||
## Offer the player a choice: accept forfeit or continue waiting.
|
||||
func _on_opponent_inactive(_sid: String) -> void:
|
||||
var dialog := ConfirmationDialog.new()
|
||||
dialog.title = "Opponent Inactive"
|
||||
dialog.dialog_text = (
|
||||
"Your opponent has not acted for 5 minutes.\n"
|
||||
+ "Do you want to claim their forfeit?"
|
||||
)
|
||||
dialog.ok_button_text = "Accept Forfeit"
|
||||
dialog.cancel_button_text = "Wait"
|
||||
add_child(dialog)
|
||||
dialog.confirmed.connect(func() -> void:
|
||||
dialog.queue_free()
|
||||
Game.send_force_concede()
|
||||
)
|
||||
dialog.canceled.connect(func() -> void:
|
||||
dialog.queue_free()
|
||||
)
|
||||
dialog.popup_centered()
|
||||
|
||||
|
||||
## Pushed when the local player is the inactive one — warn them.
|
||||
func _on_inactive_warning(_sid: String) -> void:
|
||||
var dialog := AcceptDialog.new()
|
||||
dialog.title = "Inactivity Warning"
|
||||
dialog.dialog_text = (
|
||||
"You have been inactive for 5 minutes.\n"
|
||||
+ "Your opponent may claim a forfeit."
|
||||
)
|
||||
add_child(dialog)
|
||||
dialog.confirmed.connect(func() -> void: dialog.queue_free())
|
||||
dialog.popup_centered()
|
||||
|
||||
# ── State → scene ─────────────────────────────────────────────────────────────
|
||||
|
||||
func _on_state_updated(state: Dictionary) -> void:
|
||||
|
||||
@@ -113,6 +113,52 @@ func _on_sync_progress(fetched_total: int) -> void:
|
||||
|
||||
|
||||
func _on_sync_complete(_total: int) -> void:
|
||||
_check_active_session()
|
||||
|
||||
|
||||
func _check_active_session() -> void:
|
||||
_set_status("Checking for active games…")
|
||||
Network.request(
|
||||
"game.get_active_session",
|
||||
{},
|
||||
func(_type: String, p: Variant): _on_active_session(p),
|
||||
func(_code: String, _msg: String): _go_to_main()
|
||||
)
|
||||
|
||||
|
||||
func _on_active_session(payload: Variant) -> void:
|
||||
var p := payload as Dictionary
|
||||
var sid : String = str(p.get("sessionId", ""))
|
||||
# A null or zero UUID means no active session.
|
||||
if sid.is_empty() or sid == "00000000-0000-0000-0000-000000000000":
|
||||
_go_to_main()
|
||||
return
|
||||
|
||||
var phase : String = str(p.get("phase", "unknown"))
|
||||
var dialog := ConfirmationDialog.new()
|
||||
dialog.title = "Active Game Found"
|
||||
dialog.dialog_text = "You have an active game in progress (%s phase).\nWould you like to rejoin?" % phase
|
||||
dialog.ok_button_text = "Rejoin"
|
||||
dialog.cancel_button_text = "Concede"
|
||||
add_child(dialog)
|
||||
|
||||
dialog.confirmed.connect(func() -> void:
|
||||
Game.session_id = sid
|
||||
get_tree().change_scene_to_file("res://scenes/game/GameScreen.tscn")
|
||||
)
|
||||
dialog.canceled.connect(func() -> void:
|
||||
_set_status("Conceding game…")
|
||||
Network.request(
|
||||
"game.concede",
|
||||
{ "sessionId": sid },
|
||||
func(_t: String, _rp: Variant): _go_to_main(),
|
||||
func(_c: String, _m: String): _go_to_main()
|
||||
)
|
||||
)
|
||||
dialog.popup_centered()
|
||||
|
||||
|
||||
func _go_to_main() -> void:
|
||||
get_tree().change_scene_to_file("res://scenes/main/MainScreen.tscn")
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user