siegebreak/client/Assets/Scripts/Core/MatchStateController.cs
caelum0x 9a2088a85d fix: record real match results for the local relay provider + stop a player re-queueing mid-match
The entire progression backend (Elo rating, XP, gold, card shards, match
history, leaderboard) was unreachable for every real match played over the
only working transport ("local" SESSION_PROVIDER). POST /matches/:id/complete
requires MATCH_SERVER_SECRET, a secret no Unity client holds — and
MatchStateController.Finish() was a bare TODO that never called anything.
A fully played real match (host simulates, king tower falls, Finish() runs)
just vanished: no rating change, no XP/gold, no shards, no history entry.

Fix: add POST /sessions/:id/complete (JWT-authed), callable only by the
session's deterministic host (sessionStore.hostPlayerId) — the one client
that actually ran the authoritative simulation. It validates the reported
participants are exactly this session's participants, rejects an invalid
result pairing (e.g. both players "win"), and derives deckId/trophiesDelta
server-side rather than trusting them from the request. Internally it reuses
the same completeMatch() logic the trusted-server route already used (match
recording refactored into a shared function in modules/matches/index.ts).
Wired the Unity side for real: MatchStateController now raises
OnMatchFinished from Finish(), NetworkBootstrap exposes the joined SessionId,
and MatchSimulationController reports the result via the new
SessionClient.CompleteMatch on the host only.

Also closes a related matchmaking gap found while probing this: nothing
stopped a player from calling /matchmaking/queue again while already
matched into a live session, silently abandoning whoever they were paired
with (that opponent's session would never get a second participant and had
no way to detect or recover from it). /matchmaking/queue now rejects a
re-queue while an existing ticket's session is still live
(409 already_in_match), and /sessions/:id/complete clears both participants'
tickets on a successful completion so they're immediately free to queue for
a real next match instead of waiting out the session's 1h TTL.

Verified for real: real local Postgres + Redis, real `npx tsx src/index.ts`,
real HTTP + WS clients (two guest signups, deck create, queue, matcher-tick
pairing, WS relay connect, host/guest role assignment) driving the actual
new endpoints end to end — confirmed rating/XP/gold/shards/leaderboard/match
history all land correctly on a host-reported win/loss, confirmed the guest
cannot report, confirmed idempotent replay returns 409, confirmed an invalid
result pairing is rejected, confirmed the re-queue guard blocks the
ghost-opponent scenario and reproduced it being blocked, and confirmed both
players are freed to queue again right after a real completion. 115 unit +
14 integration tests still green, `tsc --noEmit` clean. Unity-side changes
verified by hand (brace/paren balance, call-site/type cross-check) — no
Unity Editor/dotnet/mono available in this sandbox.
2026-10-26 18:15:23 +03:00

131 lines
5.1 KiB
C#

using System;
using System.Collections.Generic;
using UnityEngine;
namespace Siegebreak.Core
{
// Convention: "A" is always the host, "B" is always the guest — same
// host-first convention used everywhere else in this stack
// (services/src/modules/sessions/sessionStore.ts hostPlayerId() = the
// first session participant; RelayWelcome.participants[0] on the client).
// This matters for Finish()/OnMatchFinished below: only the host's own
// copy of this controller ever reaches Finish() (the guest only ever
// calls SyncResultFromServer, which never raises the event), so whoever
// consumes OnMatchFinished can map PlayerAWins/PlayerBWins straight onto
// "host won"/"guest won" without a second lookup.
public enum MatchResult { InProgress, PlayerAWins, PlayerBWins, Draw }
/// <summary>
/// Server-authoritative match orchestration: owns the win-condition
/// check and the match clock. Elixir, decks, and hero state live in
/// their own managers per player — this class only asks "is the match
/// over, and who won."
/// </summary>
public class MatchStateController : MonoBehaviour
{
[SerializeField] private float matchDurationSeconds = 180f; // 3:00, like CR's regulation time
[SerializeField] private float overtimeDurationSeconds = 60f;
[SerializeField] private List<TowerController> playerATowers;
[SerializeField] private List<TowerController> playerBTowers;
public MatchResult Result { get; private set; } = MatchResult.InProgress;
/// <summary>
/// Raised exactly once, only from the host's own Finish() (see the
/// class doc on <see cref="MatchResult"/>) — the real signal that
/// was previously just a TODO comment here, now consumed by
/// MatchSimulationController to report the outcome to the backend
/// (POST /sessions/:id/complete) so a real played match actually
/// lands rating/XP/gold/shards instead of vanishing.
/// </summary>
public event Action<MatchResult> OnMatchFinished;
private float _elapsed;
private bool _inOvertime;
private void Start()
{
foreach (var tower in playerATowers) tower.OnDestroyed += OnTowerDestroyed;
foreach (var tower in playerBTowers) tower.OnDestroyed += OnTowerDestroyed;
}
// Drive this from the server's fixed simulation tick, same cadence
// as ElixirManager.Tick.
public void ServerTick(float deltaTime)
{
if (Result != MatchResult.InProgress) return;
_elapsed += deltaTime;
var kingA = FindKing(playerATowers);
var kingB = FindKing(playerBTowers);
if (kingA != null && kingA.IsDestroyed) { Finish(MatchResult.PlayerBWins); return; }
if (kingB != null && kingB.IsDestroyed) { Finish(MatchResult.PlayerAWins); return; }
var regulationEnd = matchDurationSeconds;
var overtimeEnd = matchDurationSeconds + overtimeDurationSeconds;
if (!_inOvertime && _elapsed >= regulationEnd)
{
if (TotalHp(playerATowers) != TotalHp(playerBTowers))
{
Finish(TotalHp(playerATowers) > TotalHp(playerBTowers)
? MatchResult.PlayerAWins
: MatchResult.PlayerBWins);
return;
}
// Tied on tower HP at regulation end — go to overtime,
// sudden-death on next tower destroyed.
_inOvertime = true;
}
if (_inOvertime && _elapsed >= overtimeEnd)
{
Finish(MatchResult.Draw);
}
}
private void OnTowerDestroyed(TowerController tower)
{
if (_inOvertime && Result == MatchResult.InProgress)
{
// Sudden death: any tower falling in overtime ends the match
// immediately in favor of whoever didn't lose the tower.
var attackerWonMatch = playerATowers.Contains(tower) ? MatchResult.PlayerBWins : MatchResult.PlayerAWins;
Finish(attackerWonMatch);
}
}
private void Finish(MatchResult result)
{
Result = result;
OnMatchFinished?.Invoke(result);
}
// Guest-side reconciliation only: the guest never runs ServerTick
// itself (it has no towers/clock authority), so it mirrors whatever
// result the host's state_snapshot reports to drive its own
// match-result UI.
public void SyncResultFromServer(MatchResult result)
{
Result = result;
}
private static TowerController FindKing(List<TowerController> towers)
{
foreach (var t in towers)
if (t.Slot == TowerSlot.King) return t;
return null;
}
private static int TotalHp(List<TowerController> towers)
{
var total = 0;
foreach (var t in towers) total += t.CurrentHp;
return total;
}
}
}