diff --git a/src/core/game/StatsImpl.ts b/src/core/game/StatsImpl.ts index d0b98f53d..1a1a7618d 100644 --- a/src/core/game/StatsImpl.ts +++ b/src/core/game/StatsImpl.ts @@ -28,6 +28,16 @@ import { import { Player, TerraNullius } from "./Game"; import { Stats } from "./Stats"; +type BigIntLike = bigint | number; +function _bigint(value: BigIntLike): bigint { + switch (typeof value) { + case "bigint": + return value; + case "number": + return BigInt(Math.floor(value)); + } +} + export class StatsImpl implements Stats { private readonly data: AllPlayersStats = {}; @@ -52,21 +62,21 @@ export class StatsImpl implements Stats { return data; } - private _addAttack(player: Player, index: number, value: number | bigint) { + private _addAttack(player: Player, index: number, value: BigIntLike) { const p = this._makePlayerStats(player); if (p === undefined) return; if (p.attacks === undefined) p.attacks = [0n]; while (p.attacks.length <= index) p.attacks.push(0n); - p.attacks[index] += BigInt(value); + p.attacks[index] += _bigint(value); } - private _addBetrayal(player: Player, value: number | bigint) { + private _addBetrayal(player: Player, value: BigIntLike) { const data = this._makePlayerStats(player); if (data === undefined) return; if (data.betrayals === undefined) { - data.betrayals = BigInt(value); + data.betrayals = _bigint(value); } else { - data.betrayals += BigInt(value); + data.betrayals += _bigint(value); } } @@ -74,21 +84,21 @@ export class StatsImpl implements Stats { player: Player, type: BoatUnit, index: number, - value: number | bigint, + value: BigIntLike, ) { const p = this._makePlayerStats(player); if (p === undefined) return; if (p.boats === undefined) p.boats = { [type]: [0n] }; if (p.boats[type] === undefined) p.boats[type] = [0n]; while (p.boats[type].length <= index) p.boats[type].push(0n); - p.boats[type][index] += BigInt(value); + p.boats[type][index] += _bigint(value); } private _addBomb( player: Player, nukeType: NukeType, index: number, - value: number | bigint, + value: BigIntLike, ): void { const type = unitTypeToBombUnit[nukeType]; const p = this._makePlayerStats(player); @@ -96,22 +106,22 @@ export class StatsImpl implements Stats { if (p.bombs === undefined) p.bombs = { [type]: [0n] }; if (p.bombs[type] === undefined) p.bombs[type] = [0n]; while (p.bombs[type].length <= index) p.bombs[type].push(0n); - p.bombs[type][index] += BigInt(value); + p.bombs[type][index] += _bigint(value); } - private _addGold(player: Player, index: number, value: number | bigint) { + private _addGold(player: Player, index: number, value: BigIntLike) { const p = this._makePlayerStats(player); if (p === undefined) return; if (p.gold === undefined) p.gold = [0n]; while (p.gold.length <= index) p.gold.push(0n); - p.gold[index] += BigInt(value); + p.gold[index] += _bigint(value); } private _addOtherUnit( player: Player, otherUnitType: OtherUnitType, index: number, - value: number | bigint, + value: BigIntLike, ) { const type = unitTypeToOtherUnit[otherUnitType]; const p = this._makePlayerStats(player); @@ -119,13 +129,13 @@ export class StatsImpl implements Stats { if (p.units === undefined) p.units = { [type]: [0n] }; if (p.units[type] === undefined) p.units[type] = [0n]; while (p.units[type].length <= index) p.units[type].push(0n); - p.units[type][index] += BigInt(value); + p.units[type][index] += _bigint(value); } attack( player: Player, target: Player | TerraNullius, - troops: number | bigint, + troops: BigIntLike, ): void { this._addAttack(player, ATTACK_INDEX_SENT, troops); if (target.isPlayer()) { @@ -136,7 +146,7 @@ export class StatsImpl implements Stats { attackCancel( player: Player, target: Player | TerraNullius, - troops: number | bigint, + troops: BigIntLike, ): void { this._addAttack(player, ATTACK_INDEX_CANCEL, troops); this._addAttack(player, ATTACK_INDEX_SENT, -troops); @@ -153,17 +163,13 @@ export class StatsImpl implements Stats { this._addBoat(player, "trade", BOAT_INDEX_SENT, 1); } - boatArriveTrade(player: Player, target: Player, gold: number | bigint): void { + boatArriveTrade(player: Player, target: Player, gold: BigIntLike): void { this._addBoat(player, "trade", BOAT_INDEX_ARRIVE, 1); this._addGold(player, GOLD_INDEX_TRADE, gold); this._addGold(target, GOLD_INDEX_TRADE, gold); } - boatCapturedTrade( - player: Player, - target: Player, - gold: number | bigint, - ): void { + boatCapturedTrade(player: Player, target: Player, gold: BigIntLike): void { this._addBoat(player, "trade", BOAT_INDEX_CAPTURE, 1); this._addGold(player, GOLD_INDEX_STEAL, gold); } @@ -175,7 +181,7 @@ export class StatsImpl implements Stats { boatSendTroops( player: Player, target: Player | TerraNullius, - troops: number | bigint, + troops: BigIntLike, ): void { this._addBoat(player, "trans", BOAT_INDEX_SENT, 1); } @@ -183,16 +189,12 @@ export class StatsImpl implements Stats { boatArriveTroops( player: Player, target: Player | TerraNullius, - troops: number | bigint, + troops: BigIntLike, ): void { this._addBoat(player, "trans", BOAT_INDEX_ARRIVE, 1); } - boatDestroyTroops( - player: Player, - target: Player, - troops: number | bigint, - ): void { + boatDestroyTroops(player: Player, target: Player, troops: BigIntLike): void { this._addBoat(player, "trans", BOAT_INDEX_DESTROY, 1); } @@ -216,11 +218,11 @@ export class StatsImpl implements Stats { this._addBomb(player, type, BOMB_INDEX_INTERCEPT, 1); } - goldWork(player: Player, gold: number | bigint): void { + goldWork(player: Player, gold: BigIntLike): void { this._addGold(player, GOLD_INDEX_WORK, gold); } - goldWar(player: Player, captured: Player, gold: number | bigint): void { + goldWar(player: Player, captured: Player, gold: BigIntLike): void { this._addGold(player, GOLD_INDEX_WAR, gold); }