From ecebd5d7747be0193e9eb361775a78d3bdb546bf Mon Sep 17 00:00:00 2001 From: beerpsi Date: Thu, 14 Mar 2024 09:59:00 +0700 Subject: [PATCH] Deduplicate rating list functions --- titles/chuni/base.py | 26 +++++---------- titles/chuni/schema/profile.py | 35 +++++--------------- titles/ongeki/base.py | 52 ++++++++---------------------- titles/ongeki/schema/profile.py | 57 +++++---------------------------- 4 files changed, 38 insertions(+), 132 deletions(-) diff --git a/titles/chuni/base.py b/titles/chuni/base.py index 864a573..c3b9ca2 100644 --- a/titles/chuni/base.py +++ b/titles/chuni/base.py @@ -924,26 +924,16 @@ class ChuniBase: if "userRecentPlayerList" in upsert: # TODO: Seen in Air, maybe implement sometime for rp in upsert["userRecentPlayerList"]: pass - - if "userRatingBaseList" in upsert: - await self.data.profile.put_profile_rating_best( + + for rating_type in {"userRatingBaseList", "userRatingBaseHotList", "userRatingBaseNextList"}: + if rating_type not in upsert: + continue + + await self.data.profile.put_profile_rating( user_id, self.version, - upsert["userRatingBaseList"] - ) - - if "userRatingBaseHotList" in upsert: - await self.data.profile.put_profile_rating_hot( - user_id, - self.version, - upsert["userRatingBaseHotList"] - ) - - if "userRatingBaseNextList" in upsert: - await self.data.profile.put_profile_rating_next( - user_id, - self.version, - upsert["userRatingBaseNextList"] + rating_type, + upsert[rating_type], ) return {"returnCode": "1"} diff --git a/titles/chuni/schema/profile.py b/titles/chuni/schema/profile.py index 2859085..9864928 100644 --- a/titles/chuni/schema/profile.py +++ b/titles/chuni/schema/profile.py @@ -734,7 +734,13 @@ class ChuniProfileData(BaseData): "total_play_count": total_play_count } - async def _put_profile_rating(self, rating_type: str, aime_id: int, version: int, rating_data: List[Dict]): + async def put_profile_rating( + self, + aime_id: int, + version: int, + rating_type: str, + rating_data: List[Dict], + ): inserted_values = [ {"user": aime_id, "version": version, "type": rating_type, "index": i, **x} for (i, x) in enumerate(rating_data) @@ -746,33 +752,8 @@ class ChuniProfileData(BaseData): if result is None: self.logger.warn( - f"put_profile_rating_{rating_type}: Could not insert rating entries, aime_id: {aime_id}", + f"put_profile_rating: Could not insert {rating_type}, aime_id: {aime_id}", ) return return result.lastrowid - - async def put_profile_rating_best(self, aime_id: int, version: int, rating_best_data: List[Dict]): - return await self._put_profile_rating( - "best", - aime_id, - version, - rating_best_data, - ) - - async def put_profile_rating_hot(self, aime_id: int, version: int, rating_hot_data: List[Dict]): - return await self._put_profile_rating( - "hot", - aime_id, - version, - rating_hot_data, - ) - - async def put_profile_rating_next(self, aime_id: int, version: int, rating_next_data: List[Dict]): - return await self._put_profile_rating( - "next", - aime_id, - version, - rating_next_data, - ) - diff --git a/titles/ongeki/base.py b/titles/ongeki/base.py index 721bdfe..ca5a38c 100644 --- a/titles/ongeki/base.py +++ b/titles/ongeki/base.py @@ -1067,47 +1067,23 @@ class OngekiBase: if "userKopList" in upsert: for x in upsert["userKopList"]: await self.data.profile.put_kop(user_id, x) - - if "userRatingBaseBestList" in upsert: - await self.data.profile.put_profile_rating_best( - user_id, - self.version, - upsert["userRatingBaseBestList"] - ) - - if "userRatingBaseBestNewList" in upsert: - await self.data.profile.put_profile_rating_best_new( - user_id, - self.version, - upsert["userRatingBaseBestNewList"] - ) - - if "userRatingBaseHotList" in upsert: - await self.data.profile.put_profile_rating_hot( - user_id, - self.version, - upsert["userRatingBaseHotList"] - ) + + for rating_type in { + "userRatingBaseBestList", + "userRatingBaseBestNewList", + "userRatingBaseHotList", + "userRatingBaseNextList", + "userRatingBaseNextNewList", + "userRatingBaseHotNextList", + }: + if rating_type not in upsert: + continue - if "userRatingBaseNextList" in upsert: - await self.data.profile.put_profile_rating_next( + await self.data.profile.put_profile_rating( user_id, self.version, - upsert["userRatingBaseNextList"] - ) - - if "userRatingBaseNextNewList" in upsert: - await self.data.profile.put_profile_rating_next_new( - user_id, - self.version, - upsert["userRatingBaseNextNewList"] - ) - - if "userRatingBaseHotNextList" in upsert: - await self.data.profile.put_profile_rating_next_hot( - user_id, - self.version, - upsert["userRatingBaseHotNextList"] + rating_type, + upsert[rating_type], ) return {"returnCode": 1, "apiName": "upsertUserAll"} diff --git a/titles/ongeki/schema/profile.py b/titles/ongeki/schema/profile.py index 8ff8df3..b42a0a3 100644 --- a/titles/ongeki/schema/profile.py +++ b/titles/ongeki/schema/profile.py @@ -528,6 +528,7 @@ class OngekiProfileData(BaseData): ) return None return result.lastrowid + async def delete_rival(self, aime_id: int, rival_id: int) -> Optional[int]: sql = delete(rival).where(rival.c.user==aime_id, rival.c.rivalUserId==rival_id) result = await self.execute(sql) @@ -536,7 +537,13 @@ class OngekiProfileData(BaseData): else: return result.rowcount - async def _put_profile_rating(self, rating_type: str, aime_id: int, version: int, rating_data: List[Dict]): + async def put_profile_rating( + self, + aime_id: int, + version: int, + rating_type: str, + rating_data: List[Dict], + ): inserted_values = [ {"user": aime_id, "version": version, "type": rating_type, "index": i, **x} for (i, x) in enumerate(rating_data) @@ -553,51 +560,3 @@ class OngekiProfileData(BaseData): return return result.lastrowid - - async def put_profile_rating_best(self, aime_id: int, version: int, rating_best_data: List[Dict]): - return await self._put_profile_rating( - "best", - aime_id, - version, - rating_best_data, - ) - - async def put_profile_rating_best_new(self, aime_id: int, version: int, rating_best_new_data: List[Dict]): - return await self._put_profile_rating( - "best_new", - aime_id, - version, - rating_best_new_data, - ) - - async def put_profile_rating_hot(self, aime_id: int, version: int, rating_hot_data: List[Dict]): - return await self._put_profile_rating( - "hot", - aime_id, - version, - rating_hot_data, - ) - - async def put_profile_rating_next(self, aime_id: int, version: int, rating_next_data: List[Dict]): - return await self._put_profile_rating( - "next", - aime_id, - version, - rating_next_data, - ) - - async def put_profile_rating_next_new(self, aime_id: int, version: int, rating_next_new_data: List[Dict]): - return await self._put_profile_rating( - "next_new", - aime_id, - version, - rating_next_new_data, - ) - - async def put_profile_rating_next_hot(self, aime_id: int, version: int, rating_next_hot_data: List[Dict]): - return await self._put_profile_rating( - "next_hot", - aime_id, - version, - rating_next_hot_data, - )