mai2: add tables for rivals and favorite music

This commit is contained in:
2024-06-08 19:04:27 -04:00
parent efd249d808
commit f94d22ab0d
5 changed files with 165 additions and 12 deletions

View File

@ -187,14 +187,14 @@ ghost = Table(
Column("shopId", Integer),
Column("regionCode", Integer),
Column("typeId", Integer),
Column("musicId", Integer),
Column("rival", Integer),
Column("difficulty", Integer),
Column("version", Integer),
Column("resultBitList", JSON),
Column("resultNum", Integer),
Column("achievement", Integer),
UniqueConstraint(
"user", "version", "musicId", "difficulty", name="mai2_profile_ghost_uk"
"user", "version", "rival", "difficulty", name="mai2_profile_ghost_uk"
),
mysql_charset="utf8mb4",
)
@ -209,7 +209,7 @@ extend = Table(
nullable=False,
),
Column("version", Integer, nullable=False),
Column("selectMusicId", Integer),
Column("selectrival", Integer),
Column("selectDifficultyId", Integer),
Column("categoryIndex", Integer),
Column("musicIndex", Integer),
@ -239,7 +239,7 @@ option = Table(
nullable=False,
),
Column("version", Integer, nullable=False),
Column("selectMusicId", Integer),
Column("selectrival", Integer),
Column("optionKind", Integer),
Column("noteSpeed", Integer),
Column("slideSpeed", Integer),
@ -491,6 +491,24 @@ consec_logins = Table(
mysql_charset="utf8mb4",
)
rival = Table(
"mai2_user_rival",
metadata,
Column("id", Integer, primary_key=True, nullable=False),
Column(
"user",
ForeignKey("aime_user.id", ondelete="cascade", onupdate="cascade"),
nullable=False,
),
Column(
"rival",
ForeignKey("aime_user.id", ondelete="cascade", onupdate="cascade"),
nullable=False,
),
Column("show", Boolean, nullable=False, server_default="0"),
UniqueConstraint("user", "rival", name="mai2_user_rival_uk"),
mysql_charset="utf8mb4",
)
class Mai2ProfileData(BaseData):
async def put_profile_detail(
@ -843,3 +861,41 @@ class Mai2ProfileData(BaseData):
if result is None:
return None
return result.fetchone()
async def get_rivals(self, user_id: int) -> Optional[List[Row]]:
result = await self.execute(rival.select(rival.c.user == user_id))
if result:
return result.fetchall()
async def get_rivals_game(self, user_id: int) -> Optional[List[Row]]:
result = await self.execute(rival.select(and_(rival.c.user == user_id, rival.c.show == True)).limit(3))
if result:
return result.fetchall()
async def set_rival_shown(self, user_id: int, rival_id: int, is_shown: bool) -> None:
sql = rival.update(and_(rival.c.user == user_id, rival.c.rival == rival_id)).values(
show = is_shown
)
result = await self.execute(sql)
if not result:
self.logger.error(f"Failed to set rival {rival_id} shown status to {is_shown} for user {user_id}")
async def add_rival(self, user_id: int, rival_id: int) -> Optional[int]:
sql = insert(rival).values(
user = user_id,
rival = rival_id
)
conflict = sql.on_duplicate_key_do_nothing()
result = await self.execute(conflict)
if result:
return result.lastrowid
self.logger.error(f"Failed to add music {rival_id} as favorite for user {user_id}!")
async def remove_rival(self, user_id: int, rival_id: int) -> None:
result = await self.execute(rival.delete(and_(rival.c.user == user_id, rival.c.rival == rival_id)))
if not result:
self.logger.error(f"Failed to remove rival {rival_id} for user {user_id}!")