Skip to content

Commit 7be66e9

Browse files
authored
fix: rounding issues causing daily leaderboard to be out of order sometimes (@fehmer) (monkeytypegame#6303)
1 parent 73182d4 commit 7be66e9

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

backend/__tests__/utils/misc.spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,20 @@ describe("Misc Utils", () => {
6262
timestamp: 1653591901000,
6363
expectedScore: 1196200960717699,
6464
},
65+
{
66+
wpm: 196.205,
67+
acc: 96.075,
68+
timestamp: 1653591901000,
69+
expectedScore: 1196210960817699,
70+
},
71+
{
72+
// this one is particularly important - in JS 154.39 * 100 is equal to 15438.999999999998
73+
// thanks floating point errors!
74+
wpm: 154.39,
75+
acc: 96.14,
76+
timestamp: 1740333827000,
77+
expectedScore: 1154390961421373,
78+
},
6579
];
6680

6781
_.each(testCases, ({ wpm, acc, timestamp, expectedScore }) => {

backend/src/utils/misc.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,10 @@ export function matchesAPattern(text: string, pattern: string): boolean {
6767
}
6868

6969
export function kogascore(wpm: number, acc: number, timestamp: number): number {
70-
const normalizedWpm = Math.floor(wpm * 100);
71-
const normalizedAcc = Math.floor(acc * 100);
70+
// its safe to round after multiplying by 100 (99.99 * 100 rounded will be 9999 not 100)
71+
// rounding is necessary to protect against floating point errors
72+
const normalizedWpm = Math.round(wpm * 100);
73+
const normalizedAcc = Math.round(acc * 100);
7274

7375
const padAmount = 100000;
7476
const firstPart = (padAmount + normalizedWpm) * padAmount;

0 commit comments

Comments
 (0)