Skip to content

fix: clamp period deltas at 0 in processTimeframe to prevent negative…#252

Open
Tamcodes4 wants to merge 1 commit into
codepvg:mainfrom
Tamcodes4:feature/fix-negative-period-delta
Open

fix: clamp period deltas at 0 in processTimeframe to prevent negative…#252
Tamcodes4 wants to merge 1 commit into
codepvg:mainfrom
Tamcodes4:feature/fix-negative-period-delta

Conversation

@Tamcodes4

Copy link
Copy Markdown
Contributor

Description

This PR fixes an issue where the daily, weekly, and monthly leaderboards could display negative solved counts and negative scores when a user's cumulative totals temporarily decreased (for example, due to an inconsistent API response).

processTimeframe() in scripts/sync-leaderboard.js previously calculated period deltas using direct subtraction, which could produce negative values. This change clamps the solved counts to a minimum of 0 using Math.max(0, ...) before recalculating the derived score and totalSolved values.

Linked Issue

Fixes #238

Changes Made

  • Replaced direct subtraction with Math.max(0, current - previous) for:

    • easySolved
    • mediumSolved
    • hardSolved
  • Left the existing score and totalSolved calculations unchanged so they automatically derive from the clamped values.

  • Preserved the existing calculation flow while preventing invalid negative values from propagating to the leaderboard.

Type of Change

  • Bug fix
  • New feature
  • UI/Visual update
  • Documentation update
  • Refactor

Testing

  • Tested locally
  • Tested on mobile viewport (if applicable)
  • No console errors introduced

Manual Verification

Verified the logic using a scenario where a user's previous cumulative snapshot exceeded the current snapshot. Confirmed that:

  • easySolved, mediumSolved, and hardSolved are clamped to 0 instead of becoming negative.
  • score is recalculated from the clamped values and never becomes negative.
  • totalSolved is derived from the corrected solved counts and remains non-negative.

No automated tests were added since this repository does not currently include a test suite for the leaderboard sync scripts.

Checklist

  • My code follows the project's coding style
  • I have formatted my code locally by running npx prettier --write . before submitting
  • I am submitting my PR from a dedicated feature/* branch, not the main branch
  • I have performed a self-review of my code
  • My changes generate no new warnings or errors
  • I have updated documentation if required
  • I have linked the relevant issue

Screenshots / Screen Recording

N/A – Backend data-processing change only. No UI or visual changes.

@github-actions

Copy link
Copy Markdown
Contributor

Thank you for submitting a pull request.

Please ensure your changes comply with the project's contribution guidelines and that all workflow checks pass successfully.

Formatting and Branching

  • Please confirm you have formatted your code locally using npx prettier --write ., or you can simply comment /format on this PR to have our bot do it for you!
  • Ensure this PR is made from a feature/* branch and not main.

Note: This project is currently maintained by a solo maintainer, so reviews and responses may sometimes take a little time. Thanks for your patience.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Daily/weekly/monthly leaderboards can show negative solved counts and negative scores

1 participant