diff --git a/titles/chuni/schema/score.py b/titles/chuni/schema/score.py index 67cf141..7712e3f 100644 --- a/titles/chuni/schema/score.py +++ b/titles/chuni/schema/score.py @@ -6,7 +6,7 @@ from sqlalchemy.schema import ForeignKey from sqlalchemy.engine import Row from sqlalchemy.sql import func, select from sqlalchemy.dialects.mysql import insert - +from sqlalchemy.sql.expression import exists from core.data.schema import BaseData, metadata course = Table( @@ -189,9 +189,28 @@ class ChuniScoreData(BaseData): return None return result.fetchall() - def put_playlog(self, aime_id: int, playlog_data: Dict) -> Optional[int]: + def put_playlog(self, aime_id: int, playlog_data: Dict, version: int) -> Optional[int]: + # Calculate the ROM version that should be inserted into the DB, based on the version of the ggame being inserted + # We only need from Version 10 (Plost) and back, as newer versions include romVersion in their upsert + # This matters both for gameRankings, as well as a future DB update to keep version data separate + romVer = { + 10: "1.50.0", + 9: "1.45.0", + 8: "1.40.0", + 7: "1.35.0", + 6: "1.30.0", + 5: "1.25.0", + 4: "1.20.0", + 3: "1.15.0", + 2: "1.10.0", + 1: "1.05.0", + 0: "1.00.0" + } + playlog_data["user"] = aime_id playlog_data = self.fix_bools(playlog_data) + if "romVersion" not in playlog_data: + playlog_data["romVersion"] = romVer.get(version, "1.00.0") sql = insert(playlog).values(**playlog_data) conflict = sql.on_duplicate_key_update(**playlog_data) @@ -201,9 +220,39 @@ class ChuniScoreData(BaseData): return None return result.lastrowid + def get_rankings(self, version: int) -> Optional[List[Dict]]: + # Calculates the ROM version that should be fetched for rankings, based on the game version being retrieved + # This prevents tracks that are not accessible in your version from counting towards the 10 results + romVer = { + 13: "2.10%", + 12: "2.05%", + 11: "2.00%", + 10: "1.50%", + 9: "1.45%", + 8: "1.40%", + 7: "1.35%", + 6: "1.30%", + 5: "1.25%", + 4: "1.20%", + 3: "1.15%", + 2: "1.10%", + 1: "1.05%", + 0: "1.00%" + } + sql = select([playlog.c.musicId.label('id'), func.count(playlog.c.musicId).label('point')]).where((playlog.c.level != 4) & (playlog.c.romVersion.like(romVer.get(version, "%")))).group_by(playlog.c.musicId).order_by(func.count(playlog.c.musicId).desc()).limit(10) + result = self.execute(sql) + + if result is None: + return None + + rows = result.fetchall() + return [dict(row) for row in rows] + def get_rival_music(self, rival_id: int) -> Optional[List[Dict]]: - sql = select(playlog).where(playlog.c.user == rival_id) + sql = select(best_score).where(best_score.c.user == rival_id) + result = self.execute(sql) if result is None: return None return result.fetchall() +