From e1bf044d92a9ac81dbe0788c3cb7f437f5ddb5f5 Mon Sep 17 00:00:00 2001 From: Mike Rosack Date: Wed, 3 Jan 2024 12:30:20 -0600 Subject: [PATCH] only update first/last turn end dates when not skipping or undoing --- lib/services/gameTurnService.int-spec.ts | 9 ++++++--- lib/util/statsUtil.ts | 18 ++++++++++-------- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/lib/services/gameTurnService.int-spec.ts b/lib/services/gameTurnService.int-spec.ts index 90ccac2..2ba487f 100644 --- a/lib/services/gameTurnService.int-spec.ts +++ b/lib/services/gameTurnService.int-spec.ts @@ -179,7 +179,8 @@ describe('GameTurnService_updateTurnStatistics', () => { '2023': 2 }); expect(data.firstTurnEndDate).to.deep.equal(new Date('2023-11-02T09:00:00.000Z')); - expect(data.lastTurnEndDate).to.deep.equal(new Date('2023-11-03T23:00:00.000Z')); + // Turn 3 was a skip, last turn date shouldn't be updated! + expect(data.lastTurnEndDate).to.deep.equal(new Date('2023-11-02T23:00:00.000Z')); } StatsUtil.updateTurnStatistics(game, turn3, user, true); @@ -213,7 +214,8 @@ describe('GameTurnService_updateTurnStatistics', () => { '2023': 2 }); expect(data.firstTurnEndDate).to.deep.equal(new Date('2023-11-02T09:00:00.000Z')); - expect(data.lastTurnEndDate).to.deep.equal(new Date('2023-11-03T23:00:00.000Z')); + // Don't update last turn date on undo! + expect(data.lastTurnEndDate).to.deep.equal(new Date('2023-11-02T23:00:00.000Z')); } StatsUtil.updateTurnStatistics(game, turn2, user, true); @@ -247,7 +249,8 @@ describe('GameTurnService_updateTurnStatistics', () => { '2023': 1 }); expect(data.firstTurnEndDate).to.deep.equal(new Date('2023-11-02T09:00:00.000Z')); - expect(data.lastTurnEndDate).to.deep.equal(new Date('2023-11-03T23:00:00.000Z')); + // Don't update last turn date on undo! + expect(data.lastTurnEndDate).to.deep.equal(new Date('2023-11-02T23:00:00.000Z')); } }); }); diff --git a/lib/util/statsUtil.ts b/lib/util/statsUtil.ts index cdb84bb..bccd3ec 100644 --- a/lib/util/statsUtil.ts +++ b/lib/util/statsUtil.ts @@ -57,20 +57,22 @@ export class StatsUtil { const undoInc = undo ? -1 : 1; if (gameTurn.endDate) { - if (!turnData.firstTurnEndDate || gameTurn.endDate < turnData.firstTurnEndDate) { - turnData.firstTurnEndDate = gameTurn.endDate; - } - - if (!turnData.lastTurnEndDate || gameTurn.endDate > turnData.lastTurnEndDate) { - turnData.lastTurnEndDate = gameTurn.endDate; - } - turnData.turnLengthBuckets = turnData.turnLengthBuckets || {}; turnData.yearBuckets = turnData.yearBuckets || {}; if (gameTurn.skipped) { turnData.turnsSkipped = (turnData.turnsSkipped || 0) + undoInc; } else { + if (!undo) { + if (!turnData.firstTurnEndDate || gameTurn.endDate < turnData.firstTurnEndDate) { + turnData.firstTurnEndDate = gameTurn.endDate; + } + + if (!turnData.lastTurnEndDate || gameTurn.endDate > turnData.lastTurnEndDate) { + turnData.lastTurnEndDate = gameTurn.endDate; + } + } + turnData.turnsPlayed = (turnData.turnsPlayed || 0) + undoInc; const timeTaken = gameTurn.endDate.getTime() - gameTurn.startDate.getTime();