diff --git a/src/core/StatsSchemas.ts b/src/core/StatsSchemas.ts index 8543f4bab3..b6cc07fd4b 100644 --- a/src/core/StatsSchemas.ts +++ b/src/core/StatsSchemas.ts @@ -22,10 +22,10 @@ export const BoatUnitSchema = z.enum(boatUnits); export type BoatUnit = z.infer; export type BoatUnitType = UnitType.TradeShip | UnitType.TransportShip; -// export const unitTypeToBoatUnit = { -// [UnitType.TradeShip]: "trade", -// [UnitType.TransportShip]: "trans", -// } as const satisfies Record; +export const unitTypeToBoatUnit = { + [UnitType.TradeShip]: "trade", + [UnitType.TransportShip]: "trans", +} as const satisfies Record; export const otherUnits = [ "city", diff --git a/src/core/execution/TradeShipExecution.ts b/src/core/execution/TradeShipExecution.ts index 5c9d4b484e..a94f59d06b 100644 --- a/src/core/execution/TradeShipExecution.ts +++ b/src/core/execution/TradeShipExecution.ts @@ -56,7 +56,13 @@ export class TradeShipExecution implements Execution { targetUnit: this._dstPort, lastSetSafeFromPirates: ticks, }); - this.mg.stats().boatSendTrade(this.origOwner, this._dstPort.owner()); + this.mg + .stats() + .boatSendTrade( + this.origOwner, + this._dstPort.owner(), + UnitType.TradeShip, + ); } if (!this.tradeShip.isActive()) { @@ -187,7 +193,12 @@ export class TradeShipExecution implements Execution { // Record stats this.mg .stats() - .boatCapturedTrade(this.tradeShip!.owner(), this.origOwner, gold); + .boatCapturedTrade( + this.tradeShip!.owner(), + this.origOwner, + UnitType.TradeShip, + gold, + ); } else { this.srcPort.owner().addGold(gold); this._dstPort.owner().addGold(gold, this._dstPort.tile()); @@ -214,7 +225,12 @@ export class TradeShipExecution implements Execution { // Record stats this.mg .stats() - .boatArriveTrade(this.srcPort.owner(), this._dstPort.owner(), gold); + .boatArriveTrade( + this.srcPort.owner(), + this._dstPort.owner(), + UnitType.TradeShip, + gold, + ); } return; } diff --git a/src/core/execution/TransportShipExecution.ts b/src/core/execution/TransportShipExecution.ts index 77ad21327e..bc4a3801f6 100644 --- a/src/core/execution/TransportShipExecution.ts +++ b/src/core/execution/TransportShipExecution.ts @@ -158,7 +158,12 @@ export class TransportShipExecution implements Execution { // Record stats this.mg .stats() - .boatSendTroops(this.attacker, this.target, this.boat.troops()); + .boatSendTroops( + this.attacker, + this.target, + UnitType.TransportShip, + this.boat.troops(), + ); } tick(ticks: number) { @@ -241,7 +246,12 @@ export class TransportShipExecution implements Execution { // Record stats this.mg .stats() - .boatArriveTroops(this.attacker, this.target, survivors); + .boatArriveTroops( + this.attacker, + this.target, + UnitType.TransportShip, + survivors, + ); if (deaths) { this.mg.displayMessage( "events_display.attack_cancelled_retreat", @@ -273,7 +283,12 @@ export class TransportShipExecution implements Execution { // Record stats this.mg .stats() - .boatArriveTroops(this.attacker, this.target, this.boat.troops()); + .boatArriveTroops( + this.attacker, + this.target, + UnitType.TransportShip, + this.boat.troops(), + ); return; case PathStatus.NEXT: this.boat.move(result.node); diff --git a/src/core/game/Stats.ts b/src/core/game/Stats.ts index 7e508002ca..09e294365c 100644 --- a/src/core/game/Stats.ts +++ b/src/core/game/Stats.ts @@ -1,5 +1,10 @@ import { AllPlayersStats } from "../Schemas"; -import { NukeType, OtherUnitType, PlayerStats } from "../StatsSchemas"; +import { + BoatUnitType, + NukeType, + OtherUnitType, + PlayerStats, +} from "../StatsSchemas"; import { Player, TerraNullius } from "./Game"; export interface Stats { @@ -29,25 +34,32 @@ export interface Stats { lobbyFillTime(fillTimeMs: number): void; // Player sends a trade ship to target - boatSendTrade(player: Player, target: Player): void; + boatSendTrade(player: Player, target: Player, type: BoatUnitType): void; // Player's trade ship arrives at target, both players earn gold - boatArriveTrade(player: Player, target: Player, gold: number | bigint): void; + boatArriveTrade( + player: Player, + target: Player, + type: BoatUnitType, + gold: number | bigint, + ): void; // Player's trade ship, captured from target, arrives. Player earns gold. boatCapturedTrade( player: Player, target: Player, + type: BoatUnitType, gold: number | bigint, ): void; // Player destroys target's trade ship - boatDestroyTrade(player: Player, target: Player): void; + boatDestroyTrade(player: Player, target: Player, type: BoatUnitType): void; // Player sends a transport ship to target with troops boatSendTroops( player: Player, target: Player | TerraNullius, + type: BoatUnitType, troops: number | bigint, ): void; @@ -55,6 +67,7 @@ export interface Stats { boatArriveTroops( player: Player, target: Player | TerraNullius, + type: BoatUnitType, troops: number | bigint, ): void; @@ -62,6 +75,7 @@ export interface Stats { boatDestroyTroops( player: Player, target: Player, + type: BoatUnitType, troops: number | bigint, ): void; diff --git a/src/core/game/StatsImpl.ts b/src/core/game/StatsImpl.ts index c2195bf725..eb125d1d8a 100644 --- a/src/core/game/StatsImpl.ts +++ b/src/core/game/StatsImpl.ts @@ -7,7 +7,7 @@ import { BOAT_INDEX_CAPTURE, BOAT_INDEX_DESTROY, BOAT_INDEX_SENT, - BoatUnit, + BoatUnitType, BOMB_INDEX_INTERCEPT, BOMB_INDEX_LAND, BOMB_INDEX_LAUNCH, @@ -28,6 +28,7 @@ import { PLAYER_INDEX_HUMAN, PLAYER_INDEX_NATION, PlayerStats, + unitTypeToBoatUnit, unitTypeToBombUnit, unitTypeToOtherUnit, } from "../StatsSchemas"; @@ -97,10 +98,11 @@ export class StatsImpl implements Stats { private _addBoat( player: Player, - type: BoatUnit, + boatType: BoatUnitType, index: number, value: BigIntLike, ) { + const type = unitTypeToBoatUnit[boatType]; const p = this._makePlayerStats(player); if (p === undefined) return; p.boats ??= { [type]: [0n] }; @@ -188,43 +190,60 @@ export class StatsImpl implements Stats { this._addBetrayal(player, 1); } - boatSendTrade(player: Player, target: Player): void { - this._addBoat(player, "trade", BOAT_INDEX_SENT, 1); + boatSendTrade(player: Player, target: Player, type: BoatUnitType): void { + this._addBoat(player, type, BOAT_INDEX_SENT, 1); } - boatArriveTrade(player: Player, target: Player, gold: BigIntLike): void { - this._addBoat(player, "trade", BOAT_INDEX_ARRIVE, 1); + boatArriveTrade( + player: Player, + target: Player, + type: BoatUnitType, + gold: BigIntLike, + ): void { + this._addBoat(player, type, BOAT_INDEX_ARRIVE, 1); this._addGold(player, GOLD_INDEX_TRADE, gold); this._addGold(target, GOLD_INDEX_TRADE, gold); } - boatCapturedTrade(player: Player, target: Player, gold: BigIntLike): void { - this._addBoat(player, "trade", BOAT_INDEX_CAPTURE, 1); + boatCapturedTrade( + player: Player, + target: Player, + type: BoatUnitType, + gold: BigIntLike, + ): void { + this._addBoat(player, type, BOAT_INDEX_CAPTURE, 1); this._addGold(player, GOLD_INDEX_STEAL, gold); } - boatDestroyTrade(player: Player, target: Player): void { - this._addBoat(player, "trade", BOAT_INDEX_DESTROY, 1); + boatDestroyTrade(player: Player, target: Player, type: BoatUnitType): void { + this._addBoat(player, type, BOAT_INDEX_DESTROY, 1); } boatSendTroops( player: Player, target: Player | TerraNullius, + type: BoatUnitType, troops: BigIntLike, ): void { - this._addBoat(player, "trans", BOAT_INDEX_SENT, 1); + this._addBoat(player, type, BOAT_INDEX_SENT, 1); } boatArriveTroops( player: Player, target: Player | TerraNullius, + type: BoatUnitType, troops: BigIntLike, ): void { - this._addBoat(player, "trans", BOAT_INDEX_ARRIVE, 1); + this._addBoat(player, type, BOAT_INDEX_ARRIVE, 1); } - boatDestroyTroops(player: Player, target: Player, troops: BigIntLike): void { - this._addBoat(player, "trans", BOAT_INDEX_DESTROY, 1); + boatDestroyTroops( + player: Player, + target: Player, + type: BoatUnitType, + troops: BigIntLike, + ): void { + this._addBoat(player, type, BOAT_INDEX_DESTROY, 1); } bombLaunch( diff --git a/src/core/game/UnitImpl.ts b/src/core/game/UnitImpl.ts index 9444ed70b8..d27f748b15 100644 --- a/src/core/game/UnitImpl.ts +++ b/src/core/game/UnitImpl.ts @@ -302,10 +302,17 @@ export class UnitImpl implements Unit { case UnitType.TransportShip: this.mg .stats() - .boatDestroyTroops(destroyer, this._owner, this._troops); + .boatDestroyTroops( + destroyer, + this._owner, + UnitType.TransportShip, + this._troops, + ); break; case UnitType.TradeShip: - this.mg.stats().boatDestroyTrade(destroyer, this._owner); + this.mg + .stats() + .boatDestroyTrade(destroyer, this._owner, UnitType.TradeShip); break; case UnitType.City: case UnitType.DefensePost: diff --git a/tests/Stats.test.ts b/tests/Stats.test.ts index a60f918126..57551c3aed 100644 --- a/tests/Stats.test.ts +++ b/tests/Stats.test.ts @@ -61,7 +61,7 @@ describe("Stats", () => { }); test("boatSendTrade", () => { - stats.boatSendTrade(player1, player2); + stats.boatSendTrade(player1, player2, UnitType.TradeShip); expect(stats.stats()).toStrictEqual({ client1: { boats: { @@ -72,7 +72,7 @@ describe("Stats", () => { }); test("boatArriveTrade", () => { - stats.boatArriveTrade(player1, player2, 1); + stats.boatArriveTrade(player1, player2, UnitType.TradeShip, 1); expect(stats.stats()).toStrictEqual({ client1: { boats: { trade: [0n, 1n] }, @@ -85,7 +85,7 @@ describe("Stats", () => { }); test("boatCapturedTrade", () => { - stats.boatCapturedTrade(player1, player2, 1); + stats.boatCapturedTrade(player1, player2, UnitType.TradeShip, 1); expect(stats.stats()).toStrictEqual({ client1: { boats: { trade: [0n, 0n, 1n] }, @@ -95,7 +95,7 @@ describe("Stats", () => { }); test("boatDestroyTrade", () => { - stats.boatDestroyTrade(player1, player2); + stats.boatDestroyTrade(player1, player2, UnitType.TradeShip); expect(stats.stats()).toStrictEqual({ client1: { boats: { trade: [0n, 0n, 0n, 1n] }, @@ -104,7 +104,7 @@ describe("Stats", () => { }); test("boatSendTroops", () => { - stats.boatSendTroops(player1, player2, 1); + stats.boatSendTroops(player1, player2, UnitType.TransportShip, 1); expect(stats.stats()).toStrictEqual({ client1: { boats: { @@ -115,7 +115,7 @@ describe("Stats", () => { }); test("boatArriveTroops", () => { - stats.boatArriveTroops(player1, player2, 1); + stats.boatArriveTroops(player1, player2, UnitType.TransportShip, 1); expect(stats.stats()).toStrictEqual({ client1: { boats: { trans: [0n, 1n] }, @@ -124,7 +124,7 @@ describe("Stats", () => { }); test("boatDestroyTroops", () => { - stats.boatDestroyTroops(player1, player2, 1); + stats.boatDestroyTroops(player1, player2, UnitType.TransportShip, 1); expect(stats.stats()).toStrictEqual({ client1: { boats: { trans: [0n, 0n, 0n, 1n] },