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.
129 lines
5.5 KiB
C#
129 lines
5.5 KiB
C#
using System;
|
|
using System.Threading.Tasks;
|
|
using UnityEngine;
|
|
using Siegebreak.Services;
|
|
|
|
namespace Siegebreak.Networking
|
|
{
|
|
/// <summary>
|
|
/// Entry point for realtime session start, in host-authoritative server
|
|
/// simulation mode (not P2P) so combat resolution always happens
|
|
/// server-side. It resolves the backend-allocated session (room + provider)
|
|
/// and hands it to the transport.
|
|
///
|
|
/// Two transports are wired against the same session contract
|
|
/// (<see cref="GameSession.connection"/>.provider):
|
|
/// - "local" (default) connects <see cref="MatchRelayClient"/> to the
|
|
/// backend's own WS relay (services/src/modules/sessions/relay.ts).
|
|
/// The backend deterministically names one participant the host
|
|
/// (<c>RelayWelcome.isHost</c>); that client runs the authoritative
|
|
/// simulation and broadcasts state, the other sends input only.
|
|
/// - "photon" is the Photon Fusion integration point
|
|
/// (runner.StartGame(new StartGameArgs { GameMode = GameMode.Server,
|
|
/// SessionName = room, ... })) — not wired yet; it requires a Photon
|
|
/// App Id (PHOTON_APP_ID) and the Fusion SDK package, both of which
|
|
/// need a Photon account signup the agent can't do unattended (see
|
|
/// BUILD-LOG.md). Swapping to it only touches this class and the
|
|
/// transport beneath it — matchmaking, allocation, and session lookup
|
|
/// stay the same either way.
|
|
/// </summary>
|
|
public class NetworkBootstrap : MonoBehaviour
|
|
{
|
|
private readonly SessionClient _sessions = new SessionClient();
|
|
private MatchRelayClient _relay;
|
|
|
|
/// <summary>Raised once the allocated session is resolved and ready to join.</summary>
|
|
public event Action<GameSession> OnSessionResolved;
|
|
|
|
/// <summary>Raised if the session couldn't be resolved or the transport rejected the join.</summary>
|
|
public event Action<string> OnJoinFailed;
|
|
|
|
/// <summary>Raised once the relay reports this client's role for the match.</summary>
|
|
public event Action<RelayWelcome> OnRoleAssigned;
|
|
|
|
/// <summary>Raised for every gameplay frame relayed from the other participant.</summary>
|
|
public event Action<string> OnMatchFrame;
|
|
|
|
/// <summary>True once the relay has confirmed this client runs the authoritative simulation.</summary>
|
|
public bool IsHost => _relay != null && _relay.IsHost;
|
|
|
|
public bool IsRelayConnected => _relay != null && _relay.IsConnected;
|
|
|
|
/// <summary>
|
|
/// The session id this client last joined. Exposed so gameplay code
|
|
/// (MatchSimulationController, on match end) can report the result
|
|
/// back to POST /sessions/:id/complete without needing its own copy
|
|
/// of the id plumbed through separately.
|
|
/// </summary>
|
|
public string SessionId { get; private set; }
|
|
|
|
/// <summary>
|
|
/// Resolve the allocated session for <paramref name="sessionId"/> and
|
|
/// start the realtime transport against its room. The region is taken
|
|
/// from the session when available, falling back to the caller's hint.
|
|
/// </summary>
|
|
public async Task JoinMatch(string sessionId, string region)
|
|
{
|
|
SessionId = sessionId;
|
|
GameSession session = await _sessions.GetSession(sessionId);
|
|
if (session == null || session.connection == null)
|
|
{
|
|
OnJoinFailed?.Invoke("session_unavailable");
|
|
return;
|
|
}
|
|
|
|
OnSessionResolved?.Invoke(session);
|
|
await StartTransport(session, string.IsNullOrEmpty(session.region) ? region : session.region);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Send a gameplay frame (card deploy input, authoritative snapshot,
|
|
/// etc.) to the other participant over whichever transport is live.
|
|
/// No-ops if the transport isn't connected yet — callers (e.g.
|
|
/// CardDeploymentManager) should treat that the same as any other
|
|
/// dropped-frame case rather than throwing.
|
|
/// </summary>
|
|
public Task SendMatchFrame(string json)
|
|
{
|
|
if (_relay == null || !_relay.IsConnected) return Task.CompletedTask;
|
|
return _relay.Send(json);
|
|
}
|
|
|
|
/// <summary>Drain the live transport's queued events. Call every frame.</summary>
|
|
public void Update()
|
|
{
|
|
_relay?.Update();
|
|
}
|
|
|
|
private async Task StartTransport(GameSession session, string region)
|
|
{
|
|
string room = session.connection.roomName;
|
|
string provider = session.connection.provider;
|
|
|
|
Debug.Log($"[NetworkBootstrap] Joining {provider} room '{room}' (mode {session.mode}, region {region}).");
|
|
|
|
if (provider == "photon")
|
|
{
|
|
// Photon Fusion integration point — see class doc. Not wired:
|
|
// no Fusion SDK package and no PHOTON_APP_ID configured.
|
|
Debug.LogWarning("[NetworkBootstrap] SESSION_PROVIDER=photon is not wired on the client yet.");
|
|
OnJoinFailed?.Invoke("photon_not_wired");
|
|
return;
|
|
}
|
|
|
|
_relay?.Dispose();
|
|
_relay = new MatchRelayClient();
|
|
_relay.OnWelcome += welcome => OnRoleAssigned?.Invoke(welcome);
|
|
_relay.OnFrame += frame => OnMatchFrame?.Invoke(frame);
|
|
_relay.OnError += error => OnJoinFailed?.Invoke(error);
|
|
|
|
await _relay.Connect(session.id);
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
_relay?.Dispose();
|
|
}
|
|
}
|
|
}
|