Files
g0v0-server/app/tasks/special_statistics.py
MingxuanGame 33f321952d feat(custom-rulesets): support custom rulesets (#23)
* feat(custom_ruleset): add custom rulesets support

* feat(custom-ruleset): add version check

* feat(custom-ruleset): add LegacyIO API to get ruleset hashes

* feat(pp): add check for rulesets whose pp cannot be calculated

* docs(readme): update README to include support for custom rulesets

* fix(custom-ruleset): make `rulesets` empty instead of throw a error when version check is disabled

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* chore(custom-ruleset): apply the latest changes of generator

c891bcd159

and

e25041ad3b

* feat(calculator): add fallback performance calculation for unsupported modes

* fix(calculator): remove debug print

* fix: resolve reviews

* feat(calculator): add difficulty calculation checks

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-10-26 21:10:36 +08:00

91 lines
3.3 KiB
Python

from app.config import settings
from app.const import BANCHOBOT_ID
from app.database.statistics import UserStatistics
from app.database.user import User
from app.dependencies.database import with_db
from app.log import logger
from app.models.score import GameMode
from sqlalchemy import exists
from sqlmodel import select
async def create_rx_statistics():
async with with_db() as session:
users = (await session.exec(select(User.id))).all()
total_users = len(users)
logger.info(f"Ensuring RX/AP statistics exist for {total_users} users")
rx_created = 0
ap_created = 0
for i in users:
if i == BANCHOBOT_ID:
continue
if settings.enable_rx:
for mode in (
GameMode.OSURX,
GameMode.TAIKORX,
GameMode.FRUITSRX,
):
is_exist = (
await session.exec(
select(exists()).where(
UserStatistics.user_id == i,
UserStatistics.mode == mode,
)
)
).first()
if not is_exist:
statistics_rx = UserStatistics(mode=mode, user_id=i)
session.add(statistics_rx)
rx_created += 1
if settings.enable_ap:
is_exist = (
await session.exec(
select(exists()).where(
UserStatistics.user_id == i,
UserStatistics.mode == GameMode.OSUAP,
)
)
).first()
if not is_exist:
statistics_ap = UserStatistics(mode=GameMode.OSUAP, user_id=i)
session.add(statistics_ap)
ap_created += 1
await session.commit()
if rx_created or ap_created:
logger.success(
f"Created {rx_created} RX statistics rows and {ap_created} AP statistics rows during backfill"
)
async def create_custom_ruleset_statistics():
async with with_db() as session:
users = (await session.exec(select(User.id))).all()
total_users = len(users)
logger.info(f"Ensuring custom ruleset statistics exist for {total_users} users")
created_count = 0
for i in users:
if i == BANCHOBOT_ID:
continue
for mode in GameMode:
if not mode.is_custom_ruleset():
continue
is_exist = (
await session.exec(
select(exists()).where(
UserStatistics.user_id == i,
UserStatistics.mode == mode,
)
)
).first()
if not is_exist:
statistics = UserStatistics(mode=mode, user_id=i)
session.add(statistics)
created_count += 1
await session.commit()
if created_count:
logger.success(f"Created {created_count} custom ruleset statistics rows during backfill")