Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions backend/__tests__/api/controllers/result.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,28 @@ describe("result controller test", () => {
15.1 + 2 - 5, //duration + incompleteTestSeconds-afk
);
});
it("should keep incomplete xp for zen", async () => {
//GIVEN
await enableUsersXpGain(true, 1);
const completedEvent = buildCompletedEvent({
mode: "zen",
mode2: "zen",
});

//WHEN
const { body } = await mockApp
.post("/results")
.set("Authorization", `Bearer ${uid}`)
.send({
result: completedEvent,
})
.expect(200);

//THEN
expect(body.data.xp).toEqual(5);
expect(body.data.xpBreakdown).toEqual({ incomplete: 5 });
expect(userIncrementXpMock).toHaveBeenCalledWith(uid, 5);
});
it("should fail if result saving is disabled", async () => {
//GIVEN
await enableResultsSaving(false);
Expand Down Expand Up @@ -882,9 +904,17 @@ async function enableResultsSaving(enabled: boolean): Promise<void> {
mockConfig,
);
}
async function enableUsersXpGain(enabled: boolean): Promise<void> {
async function enableUsersXpGain(
enabled: boolean,
gainMultiplier = 0,
): Promise<void> {
const mockConfig = await configuration;
mockConfig.users.xp = { ...mockConfig.users.xp, enabled, funboxBonus: 1 };
mockConfig.users.xp = {
...mockConfig.users.xp,
enabled,
funboxBonus: 1,
gainMultiplier,
};

vi.spyOn(Configuration, "getCachedConfiguration").mockResolvedValue(
mockConfig,
Expand Down
47 changes: 25 additions & 22 deletions backend/src/api/controllers/result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -734,14 +734,38 @@ async function calculateXp(
funboxBonus: funboxBonusConfiguration,
} = xpConfiguration;

if (mode === "zen" || !enabled) {
if (!enabled) {
return {
xp: 0,
};
}

const breakdown: XpBreakdown = {};

let incompleteXp = 0;
if (incompleteTests !== undefined && incompleteTests.length > 0) {
incompleteTests.forEach((it: { acc: number; seconds: number }) => {
let mod = (it.acc - 50) / 50;
if (mod < 0) mod = 0;
incompleteXp += Math.round(it.seconds * mod);
});
breakdown.incomplete = incompleteXp;
} else if (incompleteTestSeconds && incompleteTestSeconds > 0) {
incompleteXp = Math.round(incompleteTestSeconds);
breakdown.incomplete = incompleteXp;
}

if (gainMultiplier !== 1) {
breakdown.configMultiplier = gainMultiplier;
}

if (mode === "zen") {
return {
xp: Math.round(incompleteXp * gainMultiplier),
breakdown,
};
}

const baseXp = Math.round((testDuration - afkDuration) * 2);
breakdown.base = baseXp;

Expand Down Expand Up @@ -807,19 +831,6 @@ async function calculateXp(
}
}

let incompleteXp = 0;
if (incompleteTests !== undefined && incompleteTests.length > 0) {
incompleteTests.forEach((it: { acc: number; seconds: number }) => {
let mod = (it.acc - 50) / 50;
if (mod < 0) mod = 0;
incompleteXp += Math.round(it.seconds * mod);
});
breakdown.incomplete = incompleteXp;
} else if (incompleteTestSeconds && incompleteTestSeconds > 0) {
incompleteXp = Math.round(incompleteTestSeconds);
breakdown.incomplete = incompleteXp;
}

const accuracyModifier = (acc - 50) / 50;

let dailyBonus = 0;
Expand All @@ -844,14 +855,6 @@ async function calculateXp(
const totalXp =
Math.round((xpAfterAccuracy + incompleteXp) * gainMultiplier) + dailyBonus;

if (gainMultiplier !== 1) {
// breakdown.push([
// "configMultiplier",
// Math.round((xpAfterAccuracy + incompleteXp) * (gainMultiplier - 1)),
// ]);
breakdown.configMultiplier = gainMultiplier;
}

const isAwardingDailyBonus = dailyBonus > 0;

return {
Expand Down
Loading