mai2: add tables for rivals and favorite music

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

View File

@ -0,0 +1,48 @@
"""mai2_add_favs_rivals
Revision ID: 4a02e623e5e6
Revises: 8ad40a6e7be2
Create Date: 2024-06-08 19:02:43.856395
"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import mysql
# revision identifiers, used by Alembic.
revision = '4a02e623e5e6'
down_revision = '8ad40a6e7be2'
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('mai2_item_favorite_music',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('user', sa.Integer(), nullable=False),
sa.Column('musicId', sa.Integer(), nullable=False),
sa.ForeignKeyConstraint(['user'], ['aime_user.id'], onupdate='cascade', ondelete='cascade'),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('user', 'musicId', name='mai2_item_favorite_music_uk'),
mysql_charset='utf8mb4'
)
op.create_table('mai2_user_rival',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('user', sa.Integer(), nullable=False),
sa.Column('rival', sa.Integer(), nullable=False),
sa.Column('show', sa.Boolean(), server_default='0', nullable=False),
sa.ForeignKeyConstraint(['rival'], ['aime_user.id'], onupdate='cascade', ondelete='cascade'),
sa.ForeignKeyConstraint(['user'], ['aime_user.id'], onupdate='cascade', ondelete='cascade'),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('user', 'rival', name='mai2_user_rival_uk'),
mysql_charset='utf8mb4'
)
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table('mai2_user_rival')
op.drop_table('mai2_item_favorite_music')
# ### end Alembic commands ###

View File

@ -1,7 +1,7 @@
"""mai2_buddies_support """mai2_buddies_support
Revision ID: 81e44dd6047a Revision ID: 81e44dd6047a
Revises: d8950c7ce2fc Revises: 6a7e8277763b
Create Date: 2024-03-12 19:10:37.063907 Create Date: 2024-03-12 19:10:37.063907
""" """

View File

@ -890,21 +890,32 @@ class Mai2Base:
async def handle_get_user_favorite_item_api_request(self, data: Dict) -> Dict: async def handle_get_user_favorite_item_api_request(self, data: Dict) -> Dict:
user_id = data.get("userId", 0) user_id = data.get("userId", 0)
kind = data.get("kind", 2) # 1 is fav music, 2 is rival user IDs kind = data.get("kind", 0) # 1 is fav music, 2 is rival user IDs
next_index = data.get("nextIndex", 0) next_index = data.get("nextIndex", 0)
max_ct = data.get("maxCount", 100) # always 100 max_ct = data.get("maxCount", 100) # always 100
is_all = data.get("isAllFavoriteItem", False) # always false is_all = data.get("isAllFavoriteItem", False) # always false
id_list: List[Dict] = []
if user_id:
if kind == 1:
fav_music = await self.data.item.get_fav_music(user_id)
if fav_music:
for fav in fav_music:
id_list.append({"orderId": 0, "id": fav["musicId"]})
if len(id_list) >= 100: # Lazy but whatever
break
elif kind == 2:
rivals = await self.data.profile.get_rivals_game(user_id)
if rivals:
for rival in rivals:
id_list.append({"orderId": 0, "id": rival["rival"]})
"""
class userFavoriteItemList:
orderId: int, never checked
id: int, either song ID for kind 1, or rival user ID for kind 2
"""
return { return {
"userId": user_id, "userId": user_id,
"kind": kind, "kind": kind,
"nextIndex": 0, "nextIndex": 0,
"userFavoriteItemList": [], "userFavoriteItemList": id_list,
} }
async def handle_get_user_recommend_rate_music_api_request(self, data: Dict) -> Dict: async def handle_get_user_recommend_rate_music_api_request(self, data: Dict) -> Dict:

View File

@ -134,6 +134,20 @@ favorite = Table(
mysql_charset="utf8mb4", mysql_charset="utf8mb4",
) )
fav_music = Table(
"mai2_item_favorite_music",
metadata,
Column("id", Integer, primary_key=True, nullable=False),
Column(
"user",
ForeignKey("aime_user.id", ondelete="cascade", onupdate="cascade"),
nullable=False,
),
Column("musicId", Integer, nullable=False),
UniqueConstraint("user", "musicId", name="mai2_item_favorite_music_uk"),
mysql_charset="utf8mb4",
)
charge = Table( charge = Table(
"mai2_item_charge", "mai2_item_charge",
metadata, metadata,
@ -451,6 +465,30 @@ class Mai2ItemData(BaseData):
return None return None
return result.fetchall() return result.fetchall()
async def get_fav_music(self, user_id: int) -> Optional[List[Row]]:
result = await self.execute(fav_music.select(fav_music.c.user == user_id))
if result:
return result.fetchall()
async def add_fav_music(self, user_id: int, music_id: int) -> Optional[int]:
sql = insert(fav_music).values(
user = user_id,
musicId = music_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 {music_id} as favorite for user {user_id}!")
async def remove_fav_music(self, user_id: int, music_id: int) -> None:
result = await self.execute(fav_music.delete(and_(fav_music.c.user == user_id, fav_music.c.musicId == music_id)))
if not result:
self.logger.error(f"Failed to remove music {music_id} as favorite for user {user_id}!")
async def put_card( async def put_card(
self, self,
user_id: int, user_id: int,

View File

@ -187,14 +187,14 @@ ghost = Table(
Column("shopId", Integer), Column("shopId", Integer),
Column("regionCode", Integer), Column("regionCode", Integer),
Column("typeId", Integer), Column("typeId", Integer),
Column("musicId", Integer), Column("rival", Integer),
Column("difficulty", Integer), Column("difficulty", Integer),
Column("version", Integer), Column("version", Integer),
Column("resultBitList", JSON), Column("resultBitList", JSON),
Column("resultNum", Integer), Column("resultNum", Integer),
Column("achievement", Integer), Column("achievement", Integer),
UniqueConstraint( UniqueConstraint(
"user", "version", "musicId", "difficulty", name="mai2_profile_ghost_uk" "user", "version", "rival", "difficulty", name="mai2_profile_ghost_uk"
), ),
mysql_charset="utf8mb4", mysql_charset="utf8mb4",
) )
@ -209,7 +209,7 @@ extend = Table(
nullable=False, nullable=False,
), ),
Column("version", Integer, nullable=False), Column("version", Integer, nullable=False),
Column("selectMusicId", Integer), Column("selectrival", Integer),
Column("selectDifficultyId", Integer), Column("selectDifficultyId", Integer),
Column("categoryIndex", Integer), Column("categoryIndex", Integer),
Column("musicIndex", Integer), Column("musicIndex", Integer),
@ -239,7 +239,7 @@ option = Table(
nullable=False, nullable=False,
), ),
Column("version", Integer, nullable=False), Column("version", Integer, nullable=False),
Column("selectMusicId", Integer), Column("selectrival", Integer),
Column("optionKind", Integer), Column("optionKind", Integer),
Column("noteSpeed", Integer), Column("noteSpeed", Integer),
Column("slideSpeed", Integer), Column("slideSpeed", Integer),
@ -491,6 +491,24 @@ consec_logins = Table(
mysql_charset="utf8mb4", 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): class Mai2ProfileData(BaseData):
async def put_profile_detail( async def put_profile_detail(
@ -843,3 +861,41 @@ class Mai2ProfileData(BaseData):
if result is None: if result is None:
return None return None
return result.fetchone() 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}!")