Skip to content

Commit

Permalink
only update first/last turn end dates when not skipping or undoing
Browse files Browse the repository at this point in the history
  • Loading branch information
mrosack committed Jan 3, 2024
1 parent 6fa6189 commit e1bf044
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
9 changes: 6 additions & 3 deletions lib/services/gameTurnService.int-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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'));
}
});
});
18 changes: 10 additions & 8 deletions lib/util/statsUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit e1bf044

Please sign in to comment.