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
62 changes: 57 additions & 5 deletions apps/api/src/guild/guild.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,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] }),
{}
),
...this.getExpHistory(cacheMember?.expHistoryDays, cacheMember?.expHistory),
...Object.fromEntries(
member.expHistoryDays.map((day, index) => [day, member.expHistory[index]])
),
Expand Down Expand Up @@ -117,8 +114,10 @@ export class GuildService {
.lean()
.exec();

const combinedGuildExpHistory = this.getCombinedExpHistory(cachedGuild, guildExpHistory);

// Get scaled gexp
Object.entries(guildExpHistory)
Object.entries(combinedGuildExpHistory)
.sort()
.toReversed()
.slice(0, 30)
Expand Down Expand Up @@ -255,9 +254,62 @@ export class GuildService {
}
}

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

private getCombinedExpHistory(
cachedGuild: Guild | null,
guildExpHistory: Record<string, number>
) {
// Preserve cached guild totals so GEXP does not drop when member history disappears.
const combinedGuildExpHistory = this.getExpHistory(
cachedGuild?.expHistoryDays,
cachedGuild?.expHistory
);

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

return combinedGuildExpHistory;
}

private scaleGexp(exp: number) {
if (exp <= 200_000) return exp;
if (exp <= 700_000) return (exp - 200_000) / 10 + 200_000;
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 service = Object.create(GuildService.prototype) as {
getCombinedExpHistory: (
cachedGuild: Pick<Guild, "expHistoryDays" | "expHistory">,
guildExpHistory: Record<string, number>
) => Record<string, number>;
};

const combined = service.getCombinedExpHistory(
{
expHistoryDays: ["2026-05-12", "2026-05-11", "2026-05-10"],
expHistory: [500, 400, 300],
} as Guild,
{
"2026-05-12": 250,
"2026-05-11": 450,
}
);

expect(combined).toEqual({
"2026-05-12": 500,
"2026-05-11": 450,
"2026-05-10": 300,
});
});
});
}
4 changes: 2 additions & 2 deletions apps/api/src/guild/leaderboards/guild-leaderboard.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,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;

return additionalStats;
})
Expand Down
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