From 772305cac8e0fbdfbd211c0f4a0acc63224d4ba9 Mon Sep 17 00:00:00 2001 From: Pavan Kumar VH Date: Fri, 4 Sep 2026 00:43:41 +0530 Subject: [PATCH] Fix negative streak handling in getFreebuffStreakGlmWeeklyUnits The function didn't validate that streak is non-negative. If streak was negative, Math.floor(streak / INTERVAL) would be negative, and Math.min with the positive max would return the negative value, resulting in a negative GLM bonus. Added Math.max(0, streak) to ensure streak is non-negative before processing. Also added a test case to verify the negative streak clamping behavior. --- common/src/util/__tests__/freebuff-streak.test.ts | 11 +++++++++++ common/src/util/freebuff-streak.ts | 3 ++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/common/src/util/__tests__/freebuff-streak.test.ts b/common/src/util/__tests__/freebuff-streak.test.ts index 190271a36d..2adc6227a8 100644 --- a/common/src/util/__tests__/freebuff-streak.test.ts +++ b/common/src/util/__tests__/freebuff-streak.test.ts @@ -5,6 +5,7 @@ import { calculateFreebuffStreak, getFreebuffDailyStreakRewardPool, getFreebuffStreakGlmBonusUnits, + getFreebuffStreakGlmWeeklyUnits, getFreebuffUsageDateKey, } from '../freebuff-streak' @@ -199,4 +200,14 @@ describe('freebuff streak rewards', () => { }), ).toBeNull() }) + + test('getFreebuffStreakGlmWeeklyUnits clamps negative streaks to zero', () => { + // Negative streak values would produce negative bonus units without the clamp. + expect(getFreebuffStreakGlmWeeklyUnits(-1)).toBe(0) + expect(getFreebuffStreakGlmWeeklyUnits(-100)).toBe(0) + // Zero and positive values work as before. + expect(getFreebuffStreakGlmWeeklyUnits(0)).toBe(0) + expect(getFreebuffStreakGlmWeeklyUnits(7)).toBe(1) + expect(getFreebuffStreakGlmWeeklyUnits(14)).toBe(2) + }) }) diff --git a/common/src/util/freebuff-streak.ts b/common/src/util/freebuff-streak.ts index f61f22832a..d7c01068f7 100644 --- a/common/src/util/freebuff-streak.ts +++ b/common/src/util/freebuff-streak.ts @@ -116,8 +116,9 @@ export function isFreebuffStreakGlmBonusActive(): boolean { * The GLM pool resets daily since 2026-07-29 (weekly before), so these units * refill at that cadence. The name keeps its historical "Weekly". */ export function getFreebuffStreakGlmWeeklyUnits(streak: number): number { + const safeStreak = Math.max(0, streak) const tiers = Math.min( - Math.floor(streak / FREEBUFF_STREAK_REWARD_INTERVAL_DAYS), + Math.floor(safeStreak / FREEBUFF_STREAK_REWARD_INTERVAL_DAYS), FREEBUFF_STREAK_REWARD_BONUS_MAX_MULTIPLIER, ) return tiers * FREEBUFF_STREAK_BONUS_SESSION_UNITS