Skip to content

Commit 3961ab5

Browse files
committed
Fix bug in high score updates (#70).
The overall high score was reused to the per-game-type high score.
1 parent 15d7eed commit 3961ab5

2 files changed

Lines changed: 26 additions & 23 deletions

File tree

src/stats/StatisticsMonitor.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ describe("StatisticsMonitor", () => {
4141
expect(stats.counters.get(event)).toBe(123);
4242
expect(stats.counters.get(`${event}.squares`)).toBe(12);
4343
expect(stats.counters.get(`${event}.triangles`)).toBe(123);
44+
45+
stats.updateHighScore(event, 24, "hexagons");
46+
expect(stats.counters.get(event)).toBe(123);
47+
expect(stats.counters.get(`${event}.squares`)).toBe(12);
48+
expect(stats.counters.get(`${event}.triangles`)).toBe(123);
49+
expect(stats.counters.get(`${event}.hexagons`)).toBe(24);
4450
});
4551

4652
test("serialize and unserialize", async () => {

src/stats/StatisticsMonitor.ts

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -176,20 +176,18 @@ export class StatisticsMonitor {
176176

177177
execute(): void {
178178
const counters = this.stats.counters;
179-
const currentMain = counters.get(this.eventType);
180-
counters.set(
181-
this.eventType,
182-
Math.max(this.value, currentMain || 0),
183-
);
184-
this.memo = { mainValue: currentMain };
179+
const updateCounter = (eventKey: string) => {
180+
const currentValue = counters.get(eventKey);
181+
counters.set(eventKey, Math.max(this.value, currentValue || 0));
182+
return currentValue;
183+
};
185184

185+
this.memo = {};
186+
this.memo.mainValue = updateCounter(this.eventType);
186187
if (this.subtype) {
187-
const currentSubtype = counters.get(this.eventType);
188-
counters.set(
188+
this.memo.subtypeValue = updateCounter(
189189
`${this.eventType}.${this.subtype}`,
190-
Math.max(this.value, currentSubtype || 0),
191190
);
192-
this.memo = { subtypeValue: currentSubtype };
193191
}
194192

195193
this.stats.writeToStorage();
@@ -199,21 +197,20 @@ export class StatisticsMonitor {
199197
if (!this.memo) return;
200198

201199
const counters = this.stats.counters;
202-
if (this.memo.mainValue === undefined) {
203-
counters.delete(this.eventType);
204-
} else {
205-
counters.set(this.eventType, this.memo.mainValue);
206-
}
207-
208-
if (this.subtype) {
209-
if (this.memo.subtypeValue === undefined) {
210-
counters.delete(`${this.eventType}.${this.subtype}`);
200+
const restoreCounter = (eventKey: string, oldValue?: number) => {
201+
if (oldValue === undefined) {
202+
counters.delete(eventKey);
211203
} else {
212-
counters.set(
213-
`${this.eventType}.${this.subtype}`,
214-
this.memo.subtypeValue,
215-
);
204+
counters.set(eventKey, oldValue);
216205
}
206+
};
207+
208+
restoreCounter(this.eventType, this.memo.mainValue);
209+
if (this.subtype) {
210+
restoreCounter(
211+
`${this.eventType}.${this.subtype}`,
212+
this.memo.subtypeValue,
213+
);
217214
}
218215

219216
this.stats.writeToStorage();

0 commit comments

Comments
 (0)