fix(daily-challenge): fix duplicated top 10%/50% placements

This commit is contained in:
MingxuanGame
2025-08-22 13:38:52 +00:00
parent 32df7a4ce8
commit 2ac56ed3bf
3 changed files with 56 additions and 5 deletions

View File

@@ -24,6 +24,7 @@ class DailyChallengeStatsBase(SQLModel, UTCBaseModel):
daily_streak_best: int = Field(default=0)
daily_streak_current: int = Field(default=0)
last_update: datetime | None = Field(default=None, sa_column=Column(DateTime))
last_day_streak: datetime | None = Field(default=None, sa_column=Column(DateTime))
last_weekly_streak: datetime | None = Field(default=None, sa_column=Column(DateTime))
playcount: int = Field(default=0)
top_10p_placements: int = Field(default=0)
@@ -103,4 +104,5 @@ async def process_daily_challenge_score(session: AsyncSession, user_id: int, roo
else:
stats.weekly_streak_current = 1
stats.last_update = now
stats.last_day_streak = now
stats.last_weekly_streak = now

View File

@@ -156,13 +156,14 @@ async def process_daily_challenge_top():
stats = await session.get(DailyChallengeStats, score.user_id)
if stats is None: # not execute
return
if total_score_count < 10 or ceil(i + 1 / total_score_count) <= 0.1:
stats.top_10p_placements += 1
if total_score_count < 2 or ceil(i + 1 / total_score_count) <= 0.5:
stats.top_50p_placements += 1
if stats.last_update is None or stats.last_update.replace(tzinfo=UTC).date() != now.date():
if total_score_count < 10 or ceil(i + 1 / total_score_count) <= 0.1:
stats.top_10p_placements += 1
if total_score_count < 2 or ceil(i + 1 / total_score_count) <= 0.5:
stats.top_50p_placements += 1
s.append(s)
participated_users.append(score.user_id)
stats.last_update = now
await session.commit()
del s
@@ -176,3 +177,4 @@ async def process_daily_challenge_top():
stats.last_weekly_streak.replace(tzinfo=UTC), now - timedelta(days=7)
):
stats.weekly_streak_current = 0
stats.last_update = now

View File

@@ -0,0 +1,47 @@
"""daily_challenge: add last_day_streak
Revision ID: 178873984b22
Revises: 5b76689f6e4b
Create Date: 2025-08-22 13:34:31.282236
"""
from __future__ import annotations
from collections.abc import Sequence
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision: str = "178873984b22"
down_revision: str | Sequence[str] | None = "5b76689f6e4b"
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None
def upgrade() -> None:
"""Upgrade schema."""
# ### commands auto generated by Alembic - please adjust! ###
op.alter_column(
"daily_challenge_stats",
"last_update",
new_column_name="last_day_streak",
existing_type=sa.DateTime(),
nullable=True,
)
op.add_column("daily_challenge_stats", sa.Column("last_update", sa.DateTime(), nullable=True))
# ### end Alembic commands ###
def downgrade() -> None:
"""Downgrade schema."""
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column(
"daily_challenge_stats",
"last_update",
existing_type=sa.DateTime(),
nullable=True,
)
op.alter_column("daily_challenge_stats", "last_day_streak", new_column_name="last_update")
# ### end Alembic commands ###