pokken: backport changes from Diana

This commit is contained in:
2024-06-11 12:25:45 -04:00
parent e06e316b7d
commit 3fd65da7fd
2 changed files with 161 additions and 96 deletions

View File

@ -1,11 +1,11 @@
from typing import Optional, Dict, List, Union
from sqlalchemy import Table, Column, UniqueConstraint, PrimaryKeyConstraint, and_, case
from sqlalchemy.types import Integer, String, TIMESTAMP, Boolean, JSON
from sqlalchemy.types import Integer, String, TIMESTAMP, Boolean, JSON, INTEGER
from sqlalchemy.schema import ForeignKey
from sqlalchemy.sql import func, select, update, delete
from sqlalchemy.sql.functions import coalesce
from sqlalchemy.engine import Row
from sqlalchemy.dialects.mysql import insert
from sqlalchemy.dialects.postgresql import insert
from core.data.schema import BaseData, metadata
from ..const import PokkenConstants
@ -16,13 +16,8 @@ profile = Table(
"pokken_profile",
metadata,
Column("id", Integer, primary_key=True, nullable=False),
Column(
"user",
ForeignKey("aime_user.id", ondelete="cascade", onupdate="cascade"),
nullable=False,
unique=True,
),
Column("trainer_name", String(16)), # optional
Column("user", Integer, ForeignKey("aime_user.id", ondelete="cascade", onupdate="cascade"), nullable=False, unique=True),
Column("trainer_name", String(14)), # optional
Column("home_region_code", Integer),
Column("home_loc_name", String(255)),
Column("pref_code", Integer),
@ -105,20 +100,15 @@ profile = Table(
Column("battle_num_vs_cpu", Integer), # 2
Column("win_cpu", Integer),
Column("battle_num_tutorial", Integer), # 1?
mysql_charset="utf8mb4",
)
pokemon_data = Table(
"pokken_pokemon_data",
"pokken_pokemon",
metadata,
Column("id", Integer, primary_key=True, nullable=False),
Column(
"user",
ForeignKey("aime_user.id", ondelete="cascade", onupdate="cascade"),
nullable=False,
),
Column("char_id", Integer, nullable=False),
Column("illustration_book_no", Integer),
Column("user", Integer, ForeignKey("aime_user.id", ondelete="cascade", onupdate="cascade"), nullable=False),
Column("char_id", Integer),
Column("illustration_book_no", Integer, nullable=False), # This is the fucking pokedex number????
Column("pokemon_exp", Integer),
Column("battle_num_vs_wan", Integer), # 4?
Column("win_vs_wan", Integer),
@ -132,8 +122,7 @@ pokemon_data = Table(
Column("bp_point_res", Integer),
Column("bp_point_def", Integer),
Column("bp_point_sp", Integer),
UniqueConstraint("user", "char_id", name="pokken_pokemon_data_uk"),
mysql_charset="utf8mb4",
UniqueConstraint("user", "illustration_book_no", name="pokken_pokemon_uk"),
)
@ -157,8 +146,8 @@ class PokkenProfileData(BaseData):
return result.lastrowid
async def set_profile_name(self, user_id: int, new_name: str, gender: Union[int, None] = None) -> None:
sql = update(profile).where(profile.c.user == user_id).values(
trainer_name=new_name,
sql = profile.update(profile.c.user == user_id).values(
trainer_name=new_name if new_name else profile.c.trainer_name,
avatar_gender=gender if gender is not None else profile.c.avatar_gender
)
result = await self.execute(sql)
@ -179,12 +168,12 @@ class PokkenProfileData(BaseData):
aid_skill: int,
last_evt: int
) -> None:
sql = update(profile).where(profile.c.user == user_id).values(
sql = profile.update(profile.c.user == user_id).values(
extra_counter=extra_counter,
event_reward_get_flag=evt_reward_get_flg,
total_play_days=total_play_days,
awake_num=awake_num,
use_support_num=use_support_ct,
total_play_days=coalesce(profile.c.total_play_days, 0) + total_play_days,
awake_num=coalesce(profile.c.awake_num, 0) + awake_num,
use_support_num=coalesce(profile.c.use_support_num, 0) + use_support_ct,
beat_num=beat_num,
aid_skill=aid_skill,
last_play_event_id=last_evt
@ -195,7 +184,7 @@ class PokkenProfileData(BaseData):
self.logger.error(f"Failed to put extra data for user {user_id}")
async def update_profile_tutorial_flags(self, user_id: int, tutorial_flags: List) -> None:
sql = update(profile).where(profile.c.user == user_id).values(
sql = profile.update(profile.c.user == user_id).values(
tutorial_progress_flag=tutorial_flags,
)
result = await self.execute(sql)
@ -205,7 +194,7 @@ class PokkenProfileData(BaseData):
)
async def update_profile_achievement_flags(self, user_id: int, achievement_flags: List) -> None:
sql = update(profile).where(profile.c.user == user_id).values(
sql = profile.update(profile.c.user == user_id).values(
achievement_flag=achievement_flags,
)
result = await self.execute(sql)
@ -215,7 +204,7 @@ class PokkenProfileData(BaseData):
)
async def update_profile_event(self, user_id: int, event_state: List, event_flags: List[int], event_param: List[int], last_evt: int = None) -> None:
sql = update(profile).where(profile.c.user == user_id).values(
sql = profile.update(profile.c.user == user_id).values(
event_state=event_state,
event_achievement_flag=event_flags,
event_achievement_param=event_param,
@ -230,12 +219,16 @@ class PokkenProfileData(BaseData):
async def add_profile_points(
self, user_id: int, rank_pts: int, money: int, score_pts: int, grade_max: int
) -> None:
sql = update(profile).where(profile.c.user == user_id).values(
trainer_rank_point = profile.c.trainer_rank_point + rank_pts,
fight_money = profile.c.fight_money + money,
score_point = profile.c.score_point + score_pts,
sql = profile.update(profile.c.user == user_id).values(
trainer_rank_point = coalesce(profile.c.trainer_rank_point, 0) + rank_pts,
wallet = coalesce(profile.c.wallet, 0) + money,
score_point = coalesce(profile.c.score_point, 0) + score_pts,
grade_max_num = grade_max
)
result = await self.execute(sql)
if result is None:
return None
async def get_profile(self, user_id: int) -> Optional[Row]:
sql = profile.select(profile.c.user == user_id)
@ -248,7 +241,7 @@ class PokkenProfileData(BaseData):
self,
user_id: int,
pokemon_id: int,
illust_no: int,
pokedex_number: int,
atk: int,
res: int,
defe: int,
@ -257,7 +250,7 @@ class PokkenProfileData(BaseData):
sql = insert(pokemon_data).values(
user=user_id,
char_id=pokemon_id,
illustration_book_no=illust_no,
illustration_book_no=pokedex_number,
pokemon_exp=0,
battle_num_vs_wan=0,
win_vs_wan=0,
@ -274,7 +267,7 @@ class PokkenProfileData(BaseData):
)
conflict = sql.on_duplicate_key_update(
illustration_book_no=illust_no,
illustration_book_no=pokedex_number,
bp_point_atk=pokemon_data.c.bp_point_atk + atk,
bp_point_res=pokemon_data.c.bp_point_res + res,
bp_point_def=pokemon_data.c.bp_point_def + defe,
@ -293,7 +286,7 @@ class PokkenProfileData(BaseData):
pokemon_id: int,
xp: int
) -> None:
sql = update(pokemon_data).where(and_(pokemon_data.c.user==user_id, pokemon_data.c.char_id==pokemon_id)).values(
sql = pokemon_data.update(and_(pokemon_data.c.user==user_id, pokemon_data.c.illustration_book_no==pokemon_id)).values(
pokemon_exp=coalesce(pokemon_data.c.pokemon_exp, 0) + xp
)
@ -315,6 +308,14 @@ class PokkenProfileData(BaseData):
return None
return result.fetchall()
async def set_latest_mon(self, user_id: int, pokedex_no: int) -> None:
sql = profile.update(profile.c.user == user_id).values(
latest_use_pokemon=pokedex_no
)
result = await self.execute(sql)
if result is None:
self.logger.warning(f"Failed to update user {user_id}'s last used pokemon {pokedex_no}")
async def put_pokemon_battle_result(
self, user_id: int, pokemon_id: int, match_type: PokkenConstants.BATTLE_TYPE, match_result: PokkenConstants.BATTLE_RESULT
) -> None:
@ -322,7 +323,7 @@ class PokkenProfileData(BaseData):
Records the match stats (type and win/loss) for the pokemon and profile
coalesce(pokemon_data.c.win_vs_wan, 0)
"""
sql = update(pokemon_data).where(and_(pokemon_data.c.user==user_id, pokemon_data.c.char_id==pokemon_id)).values(
sql = pokemon_data.update(and_(pokemon_data.c.user==user_id, pokemon_data.c.char_id==pokemon_id)).values(
battle_num_tutorial=coalesce(pokemon_data.c.battle_num_tutorial, 0) + 1 if match_type==PokkenConstants.BATTLE_TYPE.TUTORIAL else coalesce(pokemon_data.c.battle_num_tutorial, 0),
battle_all_num_tutorial=coalesce(pokemon_data.c.battle_all_num_tutorial, 0) + 1 if match_type==PokkenConstants.BATTLE_TYPE.TUTORIAL else coalesce(pokemon_data.c.battle_all_num_tutorial, 0),
@ -353,7 +354,7 @@ class PokkenProfileData(BaseData):
"""
Records profile stats
"""
sql = update(profile).where(profile.c.user==user_id).values(
sql = profile.update(profile.c.user==user_id).values(
ex_ko_num=coalesce(profile.c.ex_ko_num, 0) + exkos,
wko_num=coalesce(profile.c.wko_num, 0) + wkos,
timeup_win_num=coalesce(profile.c.timeup_win_num, 0) + timeout_wins,
@ -367,7 +368,12 @@ class PokkenProfileData(BaseData):
self.logger.warning(f"Failed to update stats for user {user_id}")
async def update_support_team(self, user_id: int, support_id: int, support1: int = None, support2: int = None) -> None:
sql = update(profile).where(profile.c.user==user_id).values(
if support1 == 4294967295:
support1 = None
if support2 == 4294967295:
support2 = None
sql = profile.update(profile.c.user==user_id).values(
support_set_1_1=support1 if support_id == 1 else profile.c.support_set_1_1,
support_set_1_2=support2 if support_id == 1 else profile.c.support_set_1_2,
support_set_2_1=support1 if support_id == 2 else profile.c.support_set_2_1,
@ -379,3 +385,15 @@ class PokkenProfileData(BaseData):
result = await self.execute(sql)
if result is None:
self.logger.warning(f"Failed to update support team {support_id} for user {user_id}")
async def update_rankmatch_data(self, user_id: int, flag: int, rm_max: Optional[int], success: Optional[int], progress: List[int]) -> None:
sql = profile.update(profile.c.user==user_id).values(
rankmatch_flag=flag,
rankmatch_max=rm_max,
rankmatch_progress=progress,
rankmatch_success=success,
)
result = await self.execute(sql)
if result is None:
self.logger.warning(f"Failed to update rankmatch data for user {user_id}")