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
54 changes: 49 additions & 5 deletions apps/api/src/guild/guild.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,24 @@ import { PlayerService } from "#player";
import { flatten } from "@statsify/util";
import type { ReturnModelType } from "@typegoose/typegoose";

function getExpHistory(days: string[] = [], expHistory: number[] = []) {
return Object.fromEntries(days.map((day, index) => [day, expHistory[index] ?? 0]));
}

function mergeExpHistory(
cachedDays: string[] | undefined,
cachedExpHistory: number[] | undefined,
currentExpHistory: Record<string, number>
) {
const combinedExpHistory = getExpHistory(cachedDays, cachedExpHistory);

Object.entries(currentExpHistory).forEach(([day, exp]) => {
combinedExpHistory[day] = Math.max(combinedExpHistory[day] ?? 0, exp);
});

return combinedExpHistory;
}

@Injectable()
export class GuildService {
private readonly logger = new Logger("GuildService");
Expand Down Expand Up @@ -84,10 +102,7 @@ export class GuildService {

// Merge the exp history from hypixel and the cached guild
const combinedExpHistory: Record<string, number> = {
...cacheMember?.expHistoryDays?.reduce(
(acc, day, index) => ({ ...acc, [day]: cacheMember.expHistory[index] }),
{}
),
...getExpHistory(cacheMember?.expHistoryDays, cacheMember?.expHistory),
...Object.fromEntries(
member.expHistoryDays.map((day, index) => [day, member.expHistory[index]])
),
Expand Down Expand Up @@ -117,8 +132,14 @@ export class GuildService {
.lean()
.exec();

const combinedGuildExpHistory = mergeExpHistory(
cachedGuild?.expHistoryDays,
cachedGuild?.expHistory,
guildExpHistory
);

// Get scaled gexp
Object.entries(guildExpHistory)
Object.entries(combinedGuildExpHistory)
.sort()
.toReversed()
.slice(0, 30)
Expand Down Expand Up @@ -261,3 +282,26 @@ export class GuildService {
return Math.round((exp - 700_000) / 33 + 250_000);
}
}

if (import.meta.vitest) {
const { suite, it, expect } = import.meta.vitest;

suite("GuildService", () => {
it("preserves cached guild exp history when refreshed member totals are lower", () => {
const combined = mergeExpHistory(
["2026-05-12", "2026-05-11", "2026-05-10"],
[500, 400, 300],
{
"2026-05-12": 250,
"2026-05-11": 450,
}
);

expect(combined).toEqual({
"2026-05-12": 500,
"2026-05-11": 450,
"2026-05-10": 300,
});
});
});
}
5 changes: 3 additions & 2 deletions apps/api/src/guild/leaderboards/guild-leaderboard.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export class GuildLeaderboardService extends LeaderboardService {
}, {} as Record<string, boolean>);

selector.nameFormatted = true;
selector.name = true;

return await Promise.all(
ids.map(async (id) => {
Expand All @@ -67,8 +68,8 @@ export class GuildLeaderboardService extends LeaderboardService {
.lean()
.exec();

const additionalStats = flatten(guild) as LeaderboardAdditionalStats;
additionalStats.name = additionalStats.nameFormatted;
const additionalStats = flatten(guild ?? {}) as LeaderboardAdditionalStats;
additionalStats.name = additionalStats.nameFormatted ?? guild?.name ?? id;
Comment thread
ugcodrr marked this conversation as resolved.

return additionalStats;
})
Expand Down
5 changes: 3 additions & 2 deletions apps/api/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"extends": "../../tsconfig.base.json",
"include": [
"src",
"eslint.config.js"
"eslint.config.js",
"vitest.config.ts"
]
}
}
11 changes: 11 additions & 0 deletions apps/api/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* Copyright (c) Statsify
*
* This source code is licensed under the GNU GPL v3 license found in the
* LICENSE file in the root directory of this source tree.
* https://github.com/Statsify/statsify/blob/main/LICENSE
*/

import { config } from "../../vitest.shared.js";

export default await config();
Loading