From 9150a1f8208c7c73eae4e3d19d38406e3be6786a Mon Sep 17 00:00:00 2001 From: Trajkov Dimitar Date: Thu, 25 Dec 2025 12:02:01 +0100 Subject: [PATCH] fix: unresolved comments --- src/client/TerritoryPatternsModal.ts | 10 ----- src/core/Schemas.ts | 2 +- src/core/execution/PlayerExecution.ts | 2 + src/core/execution/SurrenderExecution.ts | 5 ++- src/core/game/GameImpl.ts | 3 -- src/server/MatchmakingPoller.ts | 47 ++++++++++++++++++------ src/server/Worker.ts | 10 ++++- 7 files changed, 50 insertions(+), 29 deletions(-) diff --git a/src/client/TerritoryPatternsModal.ts b/src/client/TerritoryPatternsModal.ts index e9173e75c..40a12d1fc 100644 --- a/src/client/TerritoryPatternsModal.ts +++ b/src/client/TerritoryPatternsModal.ts @@ -292,14 +292,4 @@ export class TerritoryPatternsModal extends LitElement { render(preview, this.previewButton); this.requestUpdate(); } - - private isLoggedIn(): boolean { - if (this.userMeResponse === false) { - return false; - } - return ( - this.userMeResponse.user.discord !== undefined || - this.userMeResponse.user.email !== undefined - ); - } } diff --git a/src/core/Schemas.ts b/src/core/Schemas.ts index 2e04a799b..eeddfa0c1 100644 --- a/src/core/Schemas.ts +++ b/src/core/Schemas.ts @@ -152,7 +152,7 @@ export enum LogSeverity { // Utility types // -const TeamCountConfigSchema = z.union([ +export const TeamCountConfigSchema = z.union([ z.number(), z.literal(Duos), z.literal(Trios), diff --git a/src/core/execution/PlayerExecution.ts b/src/core/execution/PlayerExecution.ts index c7e452e5d..a68a5032e 100644 --- a/src/core/execution/PlayerExecution.ts +++ b/src/core/execution/PlayerExecution.ts @@ -115,6 +115,8 @@ export class PlayerExecution implements Execution { } } } + + this.mg.stats().updateMaxTiles(this.player); } private removeClusters() { diff --git a/src/core/execution/SurrenderExecution.ts b/src/core/execution/SurrenderExecution.ts index d8e677a65..61a3e9c37 100644 --- a/src/core/execution/SurrenderExecution.ts +++ b/src/core/execution/SurrenderExecution.ts @@ -31,7 +31,10 @@ export class SurrenderExecution implements Execution { // Find the opponent (the other human player in duel) const players = mg .players() - .filter((p) => p.type() === PlayerType.Human && p !== this.player); + .filter( + (p) => + p.type() === PlayerType.Human && p !== this.player && p.isAlive(), + ); if (players.length !== 1) { console.warn("Cannot surrender: expected exactly one opponent"); return; diff --git a/src/core/game/GameImpl.ts b/src/core/game/GameImpl.ts index 3ef3d5a25..f978d3baf 100644 --- a/src/core/game/GameImpl.ts +++ b/src/core/game/GameImpl.ts @@ -562,12 +562,10 @@ export class GameImpl implements Game { previousOwner._lastTileChange = this._ticks; previousOwner._tiles.delete(tile); previousOwner._borderTiles.delete(tile); - this._stats.updateMaxTiles(previousOwner); } this._map.setOwnerID(tile, owner.smallID()); owner._tiles.add(tile); owner._lastTileChange = this._ticks; - this._stats.updateMaxTiles(owner); this.updateBorders(tile); this._map.setFallout(tile, false); this.addUpdate({ @@ -588,7 +586,6 @@ export class GameImpl implements Game { previousOwner._lastTileChange = this._ticks; previousOwner._tiles.delete(tile); previousOwner._borderTiles.delete(tile); - this._stats.updateMaxTiles(previousOwner); this._map.setOwnerID(tile, 0); this.updateBorders(tile); diff --git a/src/server/MatchmakingPoller.ts b/src/server/MatchmakingPoller.ts index 555705bea..655bcf728 100644 --- a/src/server/MatchmakingPoller.ts +++ b/src/server/MatchmakingPoller.ts @@ -1,17 +1,24 @@ import { Logger } from "winston"; +import { z } from "zod"; import { ServerConfig } from "../core/configuration/Config"; -import { TeamCountConfig } from "../core/Schemas"; +import { TeamCountConfigSchema } from "../core/Schemas"; import { generateID, simpleHash } from "../core/Util"; -export interface MatchAssignment { - players: string[]; // Player tokens - config: { - queueType: "ranked" | "unranked"; - gameMode: "ffa" | "team" | "duel" | "duos" | "trios" | "quads"; - playerCount: number; - teamConfig?: TeamCountConfig; - }; -} +const MatchAssignmentSchema = z.object({ + players: z.array(z.string()), + config: z.object({ + queueType: z.enum(["ranked", "unranked"]), + gameMode: z.enum(["ffa", "team", "duel", "duos", "trios", "quads"]), + playerCount: z.number().int().positive(), + teamConfig: TeamCountConfigSchema.optional(), + }), +}); + +const MatchmakingResponseSchema = z.object({ + assignment: MatchAssignmentSchema.optional(), +}); + +export type MatchAssignment = z.infer; export class MatchmakingPoller { private serverId: string; @@ -74,6 +81,9 @@ export class MatchmakingPoller { ccu: ccu, }); + const controller = new AbortController(); + const timeoutId = setTimeout(() => controller.abort(), 20000); + const response = await fetch( `${this.config.jwtIssuer()}/matchmaking/checkin`, { @@ -87,12 +97,25 @@ export class MatchmakingPoller { ccu: ccu, gameId: gameId, }), + signal: controller.signal, }, ); - if (response.ok) { - const data = await response.json(); + clearTimeout(timeoutId); + if (response.ok) { + const rawData = await response.json(); + const result = MatchmakingResponseSchema.safeParse(rawData); + + if (!result.success) { + this.log.error("Invalid matchmaking response", { + error: result.error.message, + }); + await this.sleep(10000); + continue; + } + + const data = result.data; if (data.assignment) { this.log.info("Received match assignment", { gameId, diff --git a/src/server/Worker.ts b/src/server/Worker.ts index f4ffcd792..c3da6b214 100644 --- a/src/server/Worker.ts +++ b/src/server/Worker.ts @@ -84,7 +84,10 @@ export async function startWorker() { const selectedMap = selectMapForRanked({ playerCount: assignment.config.playerCount, gameMode: - assignment.config.gameMode === "ffa" ? GameMode.FFA : GameMode.Team, + assignment.config.gameMode === "ffa" || + assignment.config.gameMode === "duel" + ? GameMode.FFA + : GameMode.Team, queueType: assignment.config.queueType, matchMode: assignment.config.gameMode, }); @@ -587,7 +590,10 @@ async function pollLobby(gm: GameManager) { const selectedMap = selectMapForRanked({ playerCount: assignment.config.playerCount, gameMode: - assignment.config.gameMode === "ffa" ? GameMode.FFA : GameMode.Team, + assignment.config.gameMode === "ffa" || + assignment.config.gameMode === "duel" + ? GameMode.FFA + : GameMode.Team, queueType: assignment.config.queueType, matchMode: assignment.config.gameMode, });