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.
201 lines
4.6 KiB
C#
201 lines
4.6 KiB
C#
using System;
|
|
|
|
namespace Siegebreak.Services
|
|
{
|
|
// Serializable data-transfer objects mirroring the backend contract
|
|
// (services/). Fields are public and named to match the JSON exactly so
|
|
// Unity's JsonUtility can (de)serialize them directly.
|
|
|
|
[Serializable]
|
|
public class AuthTokens
|
|
{
|
|
public string accessToken;
|
|
public string refreshToken;
|
|
}
|
|
|
|
[Serializable]
|
|
public class GuestLoginRequest
|
|
{
|
|
public string deviceId;
|
|
}
|
|
|
|
[Serializable]
|
|
public class AppleLoginRequest
|
|
{
|
|
public string identityToken;
|
|
}
|
|
|
|
[Serializable]
|
|
public class GoogleLoginRequest
|
|
{
|
|
public string idToken;
|
|
}
|
|
|
|
[Serializable]
|
|
public class RefreshRequest
|
|
{
|
|
public string refreshToken;
|
|
}
|
|
|
|
[Serializable]
|
|
public class QueueRequest
|
|
{
|
|
public string deckId;
|
|
}
|
|
|
|
[Serializable]
|
|
public class StatusResponse
|
|
{
|
|
public string status;
|
|
}
|
|
|
|
/// <summary>Matchmaking ticket as returned by GET /matchmaking/status.</summary>
|
|
[Serializable]
|
|
public class MatchTicket
|
|
{
|
|
public string status; // "queued" | "matched" (empty when no ticket)
|
|
public string sessionId; // present once matched
|
|
public string deckId;
|
|
public string queuedAt;
|
|
|
|
public bool IsMatched => status == "matched" && !string.IsNullOrEmpty(sessionId);
|
|
}
|
|
|
|
[Serializable]
|
|
public class SessionParticipant
|
|
{
|
|
public string playerId;
|
|
public string deckId;
|
|
}
|
|
|
|
[Serializable]
|
|
public class SessionConnection
|
|
{
|
|
public string provider; // "local" | "photon"
|
|
public string roomName;
|
|
}
|
|
|
|
/// <summary>Allocated realtime room from GET /matches/session/:id.</summary>
|
|
[Serializable]
|
|
public class GameSession
|
|
{
|
|
public string id;
|
|
public string mode;
|
|
public string region;
|
|
public string status;
|
|
public long createdAt;
|
|
public SessionParticipant[] participants;
|
|
public SessionConnection connection;
|
|
}
|
|
|
|
[Serializable]
|
|
public class WalletDto
|
|
{
|
|
public int gold;
|
|
public int gems;
|
|
}
|
|
|
|
[Serializable]
|
|
public class Wallet
|
|
{
|
|
public string playerId;
|
|
public int gold;
|
|
public int gems;
|
|
}
|
|
|
|
[Serializable]
|
|
public class PlayerRecord
|
|
{
|
|
public int wins;
|
|
public int losses;
|
|
public int draws;
|
|
public int played;
|
|
public float winRate;
|
|
}
|
|
|
|
/// <summary>Authenticated progression from GET /players/me.</summary>
|
|
[Serializable]
|
|
public class PlayerProfile
|
|
{
|
|
public string id;
|
|
public string displayName;
|
|
public int level;
|
|
public int xp;
|
|
public int rating;
|
|
public WalletDto wallet;
|
|
public PlayerRecord record;
|
|
}
|
|
|
|
[Serializable]
|
|
public class MatchOpponent
|
|
{
|
|
public string playerId;
|
|
public string displayName;
|
|
public string result;
|
|
}
|
|
|
|
[Serializable]
|
|
public class MatchHistoryEntry
|
|
{
|
|
public string matchId;
|
|
public string mode;
|
|
public string region;
|
|
public string endedAt;
|
|
public string result;
|
|
public int trophiesDelta;
|
|
public string deckId;
|
|
public MatchOpponent[] opponents;
|
|
}
|
|
|
|
[Serializable]
|
|
public class Product
|
|
{
|
|
public string productId;
|
|
public int gems;
|
|
}
|
|
|
|
[Serializable]
|
|
public class PurchaseRequest
|
|
{
|
|
public string platform; // "ios" | "android"
|
|
public string receipt;
|
|
}
|
|
|
|
[Serializable]
|
|
public class PurchaseResult
|
|
{
|
|
public string status; // "granted"
|
|
public string productId;
|
|
public int gemsGranted;
|
|
}
|
|
|
|
/// <summary>Realtime match-found frame from WS /matchmaking/subscribe.</summary>
|
|
[Serializable]
|
|
public class MatchSocketMessage
|
|
{
|
|
public string type; // "matched" | "error"
|
|
public string sessionId;
|
|
public string error;
|
|
}
|
|
|
|
/// <summary>One participant's outcome for POST /sessions/:id/complete.</summary>
|
|
[Serializable]
|
|
public class SessionCompleteParticipant
|
|
{
|
|
public string playerId;
|
|
public string result; // "win" | "loss" | "draw"
|
|
}
|
|
|
|
/// <summary>
|
|
/// Body for POST /sessions/:id/complete — only the host-side client may
|
|
/// send this (see services/src/modules/sessions/index.ts); the backend
|
|
/// derives deckId/trophiesDelta itself from the session record rather
|
|
/// than trusting them from the request.
|
|
/// </summary>
|
|
[Serializable]
|
|
public class SessionCompleteRequest
|
|
{
|
|
public SessionCompleteParticipant[] participants;
|
|
}
|
|
}
|