diff --git a/src/core/StatsSchemas.ts b/src/core/StatsSchemas.ts index a911d9459..962703020 100644 --- a/src/core/StatsSchemas.ts +++ b/src/core/StatsSchemas.ts @@ -101,6 +101,7 @@ export const PlayerStatsSchema = z betrayals: BigIntStringSchema.optional(), killedAt: BigIntStringSchema.optional(), conquests: BigIntStringSchema.optional(), + maxTilesOwned: BigIntStringSchema.optional(), boats: z.partialRecord(BoatUnitSchema, AtLeastOneNumberSchema).optional(), bombs: z.partialRecord(BombUnitSchema, AtLeastOneNumberSchema).optional(), gold: AtLeastOneNumberSchema.optional(), diff --git a/src/core/game/GameImpl.ts b/src/core/game/GameImpl.ts index ee0a7783a..2e9b800f4 100644 --- a/src/core/game/GameImpl.ts +++ b/src/core/game/GameImpl.ts @@ -561,10 +561,12 @@ 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({ @@ -585,6 +587,7 @@ 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/core/game/Stats.ts b/src/core/game/Stats.ts index 3dc644f2d..1a9c9bddc 100644 --- a/src/core/game/Stats.ts +++ b/src/core/game/Stats.ts @@ -8,6 +8,9 @@ export interface Stats { numMirvsLaunched(): bigint; + // Update max tiles tracking for a player (called when tile ownership changes) + updateMaxTiles(player: Player): void; + // Player attacks target attack( player: Player, diff --git a/src/core/game/StatsImpl.ts b/src/core/game/StatsImpl.ts index b62956108..5843f5bed 100644 --- a/src/core/game/StatsImpl.ts +++ b/src/core/game/StatsImpl.ts @@ -41,6 +41,7 @@ function _bigint(value: BigIntLike): bigint { export class StatsImpl implements Stats { private readonly data: AllPlayersStats = {}; + private readonly maxTilesTracking: Map = new Map(); private _numMirvLaunched: bigint = 0n; @@ -58,6 +59,28 @@ export class StatsImpl implements Stats { return this.data; } + /** + * Update max tiles tracking for a player + * Called whenever player's tile count changes + */ + updateMaxTiles(player: Player): void { + const clientID = player.clientID(); + if (clientID === null) return; + + const currentTiles = _bigint(player.numTilesOwned()); + const maxTiles = this.maxTilesTracking.get(clientID) ?? 0n; + + if (currentTiles > maxTiles) { + this.maxTilesTracking.set(clientID, currentTiles); + + // Update the player stats + const p = this._makePlayerStats(player); + if (p !== undefined) { + p.maxTilesOwned = currentTiles; + } + } + } + private _makePlayerStats(player: Player): PlayerStats { const clientID = player.clientID(); if (clientID === null) return undefined;