Skip to content

Commit 8be3a3c

Browse files
authored
fix(leaderboard): use minTimeTyping from configuration (@fehmer) (monkeytypegame#6710)
We added `minTimeTyping` to the configuration but it was only used to create the indicies.
1 parent 0c1e1f2 commit 8be3a3c

File tree

3 files changed

+24
-15
lines changed

3 files changed

+24
-15
lines changed

backend/src/api/controllers/result.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ import {
6565
import { MonkeyRequest } from "../types";
6666
import { getFunbox, checkCompatibility } from "@monkeytype/funbox";
6767
import { tryCatch } from "@monkeytype/util/trycatch";
68+
import { getCachedConfiguration } from "../../init/configuration";
6869

6970
try {
7071
if (!anticheatImplemented()) throw new Error("undefined");
@@ -491,12 +492,18 @@ export async function addResult(
491492
const stopOnLetterTriggered =
492493
completedEvent.stopOnLetter && completedEvent.acc < 100;
493494

495+
const minTimeTyping = (await getCachedConfiguration(true)).leaderboards
496+
.minTimeTyping;
497+
498+
const userEligibleForLeaderboard =
499+
user.banned !== true &&
500+
user.lbOptOut !== true &&
501+
(isDevEnvironment() || (user.timeTyping ?? 0) > minTimeTyping);
502+
494503
const validResultCriteria =
495504
canFunboxGetPb(completedEvent) &&
496505
!completedEvent.bailedOut &&
497-
user.banned !== true &&
498-
user.lbOptOut !== true &&
499-
(isDevEnvironment() || (user.timeTyping ?? 0) > 7200) &&
506+
userEligibleForLeaderboard &&
500507
!stopOnLetterTriggered;
501508

502509
const selectedBadgeId = user.inventory?.badges?.find((b) => b.selected)?.id;
@@ -579,19 +586,11 @@ export async function addResult(
579586

580587
const weeklyXpLeaderboardConfig = req.ctx.configuration.leaderboards.weeklyXp;
581588
let weeklyXpLeaderboardRank = -1;
582-
const eligibleForWeeklyXpLeaderboard =
583-
user.banned !== true &&
584-
user.lbOptOut !== true &&
585-
(isDevEnvironment() || (user.timeTyping ?? 0) > 7200);
586589

587590
const weeklyXpLeaderboard = WeeklyXpLeaderboard.get(
588591
weeklyXpLeaderboardConfig
589592
);
590-
if (
591-
eligibleForWeeklyXpLeaderboard &&
592-
xpGained.xp > 0 &&
593-
weeklyXpLeaderboard
594-
) {
593+
if (userEligibleForLeaderboard && xpGained.xp > 0 && weeklyXpLeaderboard) {
595594
weeklyXpLeaderboardRank = await weeklyXpLeaderboard.addResult(
596595
weeklyXpLeaderboardConfig,
597596
{

backend/src/dal/leaderboards.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ export async function update(
121121
}> {
122122
const key = `lbPersonalBests.${mode}.${mode2}.${language}`;
123123
const lbCollectionName = `leaderboards.${language}.${mode}.${mode2}`;
124+
const minTimeTyping = (await getCachedConfiguration(true)).leaderboards
125+
.minTimeTyping;
124126
const lb = db.collection<DBUser>("users").aggregate<LeaderboardEntry>(
125127
[
126128
{
@@ -144,7 +146,7 @@ export async function update(
144146
$ne: true,
145147
},
146148
timeTyping: {
147-
$gt: isDevEnvironment() ? 0 : 7200,
149+
$gt: isDevEnvironment() ? 0 : minTimeTyping,
148150
},
149151
},
150152
},

frontend/src/ts/pages/leaderboards.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import {
1515
endOfDay,
1616
endOfWeek,
1717
format,
18+
formatDuration,
19+
intervalToDuration,
1820
startOfDay,
1921
startOfWeek,
2022
subDays,
@@ -43,6 +45,7 @@ import {
4345
LanguageSchema,
4446
} from "@monkeytype/contracts/schemas/languages";
4547
import { isSafeNumber } from "@monkeytype/util/numbers";
48+
import * as ServerConfiguration from "../ape/server-configuration";
4649

4750
const LeaderboardTypeSchema = z.enum(["allTime", "weekly", "daily"]);
4851
type LeaderboardType = z.infer<typeof LeaderboardTypeSchema>;
@@ -629,13 +632,18 @@ function fillUser(): void {
629632
return;
630633
}
631634

635+
const minTimeTyping =
636+
ServerConfiguration.get()?.leaderboards.minTimeTyping ?? 7200;
637+
632638
if (
633639
isAuthenticated() &&
634640
!isDevEnvironment() &&
635-
(DB.getSnapshot()?.typingStats?.timeTyping ?? 0) < 7200
641+
(DB.getSnapshot()?.typingStats?.timeTyping ?? 0) < minTimeTyping
636642
) {
637643
$(".page.pageLeaderboards .bigUser").html(
638-
'<div class="warning">Your account must have 2 hours typed to be placed on the leaderboard.</div>'
644+
`<div class="warning">Your account must have ${formatDuration(
645+
intervalToDuration({ start: 0, end: minTimeTyping * 1000 })
646+
)} typed to be placed on the leaderboard.</div>`
639647
);
640648
return;
641649
}

0 commit comments

Comments
 (0)