Skip to content

Commit b7ca3fc

Browse files
fix(challenge): prevent duplicate AFK performance notification (#7045)
1 parent c95b6bd commit b7ca3fc

File tree

1 file changed

+21
-12
lines changed

1 file changed

+21
-12
lines changed

frontend/src/ts/controllers/challenge-controller.ts

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export function clearActive(): void {
3737
function verifyRequirement(
3838
result: CompletedEvent,
3939
requirements: NonNullable<Challenge["requirements"]>,
40-
requirementType: keyof NonNullable<Challenge["requirements"]>
40+
requirementType: keyof NonNullable<Challenge["requirements"]>,
4141
): [boolean, string[]] {
4242
let requirementsMet = true;
4343
let failReasons: string[] = [];
@@ -146,27 +146,36 @@ export function verify(result: CompletedEvent): string | null {
146146
try {
147147
const afk = (result.afkDuration / result.testDuration) * 100;
148148

149+
// Show a challenge-specific notification when AFK percentage exceeds 10%
149150
if (afk > 10) {
150-
Notifications.add(`Challenge failed: AFK time is greater than 10%`, 0);
151+
// Safe access to challenge display name with fallback
152+
const activeChallengeName =
153+
TestState.activeChallenge?.display ?? "Challenge";
154+
155+
Notifications.add(
156+
`${activeChallengeName} incomplete: Away for ${Math.round(afk)}% of test`,
157+
0,
158+
{ duration: 4 },
159+
);
151160
return null;
152161
}
153162

154163
if (TestState.activeChallenge.requirements === undefined) {
155164
Notifications.add(
156165
`${TestState.activeChallenge.display} challenge passed!`,
157-
1
166+
1,
158167
);
159168
return TestState.activeChallenge.name;
160169
} else {
161170
let requirementsMet = true;
162171
const failReasons: string[] = [];
163172
for (const requirementType of Misc.typedKeys(
164-
TestState.activeChallenge.requirements
173+
TestState.activeChallenge.requirements,
165174
)) {
166175
const [passed, requirementFailReasons] = verifyRequirement(
167176
result,
168177
TestState.activeChallenge.requirements,
169-
requirementType
178+
requirementType,
170179
);
171180
if (!passed) {
172181
requirementsMet = false;
@@ -180,20 +189,20 @@ export function verify(result: CompletedEvent): string | null {
180189
1,
181190
{
182191
duration: 5,
183-
}
192+
},
184193
);
185194
}
186195
Notifications.add(
187196
`${TestState.activeChallenge.display} challenge passed!`,
188-
1
197+
1,
189198
);
190199
return TestState.activeChallenge.name;
191200
} else {
192201
Notifications.add(
193202
`${
194203
TestState.activeChallenge.display
195204
} challenge failed: ${failReasons.join(", ")}`,
196-
0
205+
0,
197206
);
198207
return null;
199208
}
@@ -202,7 +211,7 @@ export function verify(result: CompletedEvent): string | null {
202211
console.error(e);
203212
Notifications.add(
204213
`Something went wrong when verifying challenge: ${(e as Error).message}`,
205-
0
214+
0,
206215
);
207216
return null;
208217
}
@@ -226,7 +235,7 @@ export async function setup(challengeName: string): Promise<boolean> {
226235
}
227236

228237
const challenge = list.find(
229-
(c) => c.name.toLowerCase() === challengeName.toLowerCase()
238+
(c) => c.name.toLowerCase() === challengeName.toLowerCase(),
230239
);
231240
let notitext;
232241
try {
@@ -263,7 +272,7 @@ export async function setup(challengeName: string): Promise<boolean> {
263272
} else if (challenge.type === "script") {
264273
Loader.show();
265274
const response = await fetch(
266-
"/challenges/" + (challenge.parameters[0] as string)
275+
"/challenges/" + (challenge.parameters[0] as string),
267276
);
268277
Loader.hide();
269278
if (response.status !== 200) {
@@ -330,7 +339,7 @@ export async function setup(challengeName: string): Promise<boolean> {
330339
} catch (e) {
331340
Notifications.add(
332341
Misc.createErrorMessage(e, "Failed to load challenge"),
333-
-1
342+
-1,
334343
);
335344
return false;
336345
}

0 commit comments

Comments
 (0)