From b12938bcd83dcddf8eee7588939c13fb7c72b628 Mon Sep 17 00:00:00 2001 From: Kevin Trocolli Date: Wed, 14 Jun 2023 03:00:52 -0400 Subject: [PATCH] pokken: add partial profile save logic --- titles/pokken/base.py | 56 +++++++++++++- titles/pokken/const.py | 12 +-- titles/pokken/schema/item.py | 14 +++- titles/pokken/schema/profile.py | 126 ++++++++++++++++++++++++++++---- 4 files changed, 184 insertions(+), 24 deletions(-) diff --git a/titles/pokken/base.py b/titles/pokken/base.py index 40e6444..3ec4475 100644 --- a/titles/pokken/base.py +++ b/titles/pokken/base.py @@ -1,6 +1,6 @@ from datetime import datetime, timedelta import json, logging -from typing import Any, Dict +from typing import Any, Dict, List import random from core.data import Data @@ -274,6 +274,60 @@ class PokkenBase: res.result = 1 res.type = jackal_pb2.MessageType.SAVE_USER + req = request.save_user + user_id = req.banapass_id + + tut_flgs: List[int] = [] + ach_flgs: List[int] = [] + evt_flgs: List[int] = [] + evt_params: List[int] = [] + + get_rank_pts: int = req.get_trainer_rank_point if req.get_trainer_rank_point else 0 + get_money: int = req.get_money + get_score_pts: int = req.get_score_point if req.get_score_point else 0 + grade_max: int = req.grade_max_num + extra_counter: int = req.extra_counter + evt_reward_get_flg: int = req.event_reward_get_flag + num_continues: int = req.continue_num + total_play_days: int = req.total_play_days + awake_num: int = req.awake_num # ? + use_support_ct: int = req.use_support_num + beat_num: int = req.beat_num # ? + evt_state: int = req.event_state + aid_skill: int = req.aid_skill + last_evt: int = req.last_play_event_id + + battle = req.battle_data + mon = req.pokemon_data + + self.data.profile.update_support_team(user_id, 1, req.support_set_1[0], req.support_set_1[1]) + self.data.profile.update_support_team(user_id, 2, req.support_set_2[0], req.support_set_2[1]) + self.data.profile.update_support_team(user_id, 3, req.support_set_3[0], req.support_set_3[1]) + + if req.trainer_name_pending: # we're saving for the first time + self.data.profile.set_profile_name(user_id, req.trainer_name_pending, req.avatar_gender if req.avatar_gender else None) + + for tut_flg in req.tutorial_progress_flag: + tut_flgs.append(tut_flg) + + self.data.profile.update_profile_tutorial_flags(user_id, tut_flgs) + + for ach_flg in req.achievement_flag: + ach_flgs.append(ach_flg) + + self.data.profile.update_profile_tutorial_flags(user_id, ach_flg) + + for evt_flg in req.event_achievement_flag: + evt_flgs.append(evt_flg) + + for evt_param in req.event_achievement_param: + evt_params.append(evt_param) + + self.data.profile.update_profile_event(user_id, evt_state, evt_flgs, evt_params, ) + + for reward in req.reward_data: + self.data.item.add_reward(user_id, reward.get_category_id, reward.get_content_id, reward.get_type_id) + return res.SerializeToString() def handle_save_ingame_log(self, data: jackal_pb2.Request) -> bytes: diff --git a/titles/pokken/const.py b/titles/pokken/const.py index 2eb5357..e7ffdd8 100644 --- a/titles/pokken/const.py +++ b/titles/pokken/const.py @@ -11,14 +11,14 @@ class PokkenConstants: VERSION_NAMES = "Pokken Tournament" class BATTLE_TYPE(Enum): - BATTLE_TYPE_TUTORIAL = 1 - BATTLE_TYPE_AI = 2 - BATTLE_TYPE_LAN = 3 - BATTLE_TYPE_WAN = 4 + TUTORIAL = 1 + AI = 2 + LAN = 3 + WAN = 4 class BATTLE_RESULT(Enum): - BATTLE_RESULT_WIN = 1 - BATTLE_RESULT_LOSS = 2 + WIN = 1 + LOSS = 2 @classmethod def game_ver_to_string(cls, ver: int): diff --git a/titles/pokken/schema/item.py b/titles/pokken/schema/item.py index 4919ea0..32bff2a 100644 --- a/titles/pokken/schema/item.py +++ b/titles/pokken/schema/item.py @@ -31,4 +31,16 @@ class PokkenItemData(BaseData): Items obtained as rewards """ - pass + def add_reward(self, user_id: int, category: int, content: int, item_type: int) -> Optional[int]: + sql = insert(item).values( + user=user_id, + category=category, + content=content, + type=item_type, + ) + + result = self.execute(sql) + if result is None: + self.logger.warn(f"Failed to insert reward for user {user_id}: {category}-{content}-{item_type}") + return None + return result.lastrowid diff --git a/titles/pokken/schema/profile.py b/titles/pokken/schema/profile.py index 8e536f1..94e15e8 100644 --- a/titles/pokken/schema/profile.py +++ b/titles/pokken/schema/profile.py @@ -1,4 +1,4 @@ -from typing import Optional, Dict, List +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.schema import ForeignKey @@ -125,7 +125,7 @@ pokemon_data = Table( Column("win_vs_lan", Integer), Column("battle_num_vs_cpu", Integer), # 2 Column("win_cpu", Integer), - Column("battle_all_num_tutorial", Integer), + Column("battle_all_num_tutorial", Integer), # ??? Column("battle_num_tutorial", Integer), # 1? Column("bp_point_atk", Integer), Column("bp_point_res", Integer), @@ -147,11 +147,10 @@ class PokkenProfileData(BaseData): return None return result.lastrowid - def set_profile_name(self, user_id: int, new_name: str) -> None: - sql = ( - update(profile) - .where(profile.c.user == user_id) - .values(trainer_name=new_name) + 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, + avatar_gender=gender if gender is not None else profile.c.avatar_gender ) result = self.execute(sql) if result is None: @@ -159,8 +158,38 @@ class PokkenProfileData(BaseData): f"Failed to update pokken profile name for user {user_id}!" ) - def update_profile_tutorial_flags(self, user_id: int, tutorial_flags: Dict) -> None: - pass + def update_profile_tutorial_flags(self, user_id: int, tutorial_flags: List) -> None: + sql = update(profile).where(profile.c.user == user_id).values( + tutorial_progress_flag=tutorial_flags, + ) + result = self.execute(sql) + if result is None: + self.logger.error( + f"Failed to update pokken profile tutorial flags for user {user_id}!" + ) + + def update_profile_achievement_flags(self, user_id: int, achievement_flags: List) -> None: + sql = update(profile).where(profile.c.user == user_id).values( + achievement_flag=achievement_flags, + ) + result = self.execute(sql) + if result is None: + self.logger.error( + f"Failed to update pokken profile achievement flags for user {user_id}!" + ) + + 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( + event_state=event_state, + event_achievement_flag=event_flags, + event_achievement_param=event_param, + last_play_event_id=last_evt if last_evt is not None else profile.c.last_play_event_id, + ) + result = self.execute(sql) + if result is None: + self.logger.error( + f"Failed to update pokken profile event state for user {user_id}!" + ) def add_profile_points( self, user_id: int, rank_pts: int, money: int, score_pts: int @@ -174,18 +203,53 @@ class PokkenProfileData(BaseData): return None return result.fetchone() - def put_pokemon_data( + def put_pokemon( self, user_id: int, pokemon_id: int, illust_no: int, - get_exp: int, atk: int, res: int, defe: int, - sp: int, + sp: int ) -> Optional[int]: - pass + sql = insert(pokemon_data).values( + user=user_id, + char_id=pokemon_id, + illustration_book_no=illust_no, + bp_point_atk=atk, + bp_point_res=res, + bp_point_defe=defe, + bp_point_sp=sp, + ) + + conflict = sql.on_duplicate_key_update( + illustration_book_no=illust_no, + bp_point_atk=atk, + bp_point_res=res, + bp_point_defe=defe, + bp_point_sp=sp, + ) + + result = self.execute(conflict) + if result is None: + self.logger.warn(f"Failed to insert pokemon ID {pokemon_id} for user {user_id}") + return None + return result.lastrowid + + def add_pokemon_xp( + self, + user_id: int, + 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( + pokemon_exp=pokemon_data.c.pokemon_exp + xp + ) + + result = self.execute(sql) + if result is None: + self.logger.warn(f"Failed to add {xp} XP to pokemon ID {pokemon_id} for user {user_id}") def get_pokemon_data(self, user_id: int, pokemon_id: int) -> Optional[Row]: pass @@ -193,13 +257,29 @@ class PokkenProfileData(BaseData): def get_all_pokemon_data(self, user_id: int) -> Optional[List[Row]]: pass - def put_results( - self, user_id: int, pokemon_id: int, match_type: int, match_result: int + def put_pokemon_battle_result( + self, user_id: int, pokemon_id: int, match_type: PokkenConstants.BATTLE_TYPE, match_result: PokkenConstants.BATTLE_RESULT ) -> None: """ Records the match stats (type and win/loss) for the pokemon and profile """ - pass + sql = update(pokemon_data).where(and_(pokemon_data.c.user==user_id, pokemon_data.c.char_id==pokemon_id)).values( + battle_num_tutorial=pokemon_data.c.battle_num_tutorial + 1 if match_type==PokkenConstants.BATTLE_TYPE.TUTORIAL else pokemon_data.c.battle_num_tutorial, + battle_all_num_tutorial=pokemon_data.c.battle_all_num_tutorial + 1 if match_type==PokkenConstants.BATTLE_TYPE.TUTORIAL else pokemon_data.c.battle_all_num_tutorial, + + battle_num_vs_cpu=pokemon_data.c.battle_num_vs_cpu + 1 if match_type==PokkenConstants.BATTLE_TYPE.AI else pokemon_data.c.battle_num_vs_cpu, + win_cpu=pokemon_data.c.win_cpu + 1 if match_type==PokkenConstants.BATTLE_TYPE.AI and match_result==PokkenConstants.BATTLE_RESULT.WIN else pokemon_data.c.win_cpu, + + battle_num_vs_lan=pokemon_data.c.battle_num_vs_lan + 1 if match_type==PokkenConstants.BATTLE_TYPE.LAN else pokemon_data.c.battle_num_vs_lan, + win_vs_lan=pokemon_data.c.win_vs_lan + 1 if match_type==PokkenConstants.BATTLE_TYPE.LAN and match_result==PokkenConstants.BATTLE_RESULT.WIN else pokemon_data.c.win_vs_lan, + + battle_num_vs_wan=pokemon_data.c.battle_num_vs_wan + 1 if match_type==PokkenConstants.BATTLE_TYPE.WAN else pokemon_data.c.battle_num_vs_wan, + win_vs_wan=pokemon_data.c.win_vs_wan + 1 if match_type==PokkenConstants.BATTLE_TYPE.WAN and match_result==PokkenConstants.BATTLE_RESULT.WIN else pokemon_data.c.win_vs_wan, + ) + + result = self.execute(sql) + if result is None: + self.logger.warn(f"Failed to record match stats for user {user_id}'s pokemon {pokemon_id} (type {match_type.name} | result {match_result.name})") def put_stats( self, @@ -215,3 +295,17 @@ class PokkenProfileData(BaseData): Records profile stats """ pass + + def update_support_team(self, user_id: int, support_id: int, support1: int = 4294967295, support2: int = 4294967295) -> None: + sql = update(profile).where(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, + support_set_2_2=support2 if support_id == 2 else profile.c.support_set_2_2, + support_set_3_1=support1 if support_id == 3 else profile.c.support_set_3_1, + support_set_3_2=support2 if support_id == 3 else profile.c.support_set_3_2, + ) + + result = self.execute(sql) + if result is None: + self.logger.warn(f"Failed to update support team {support_id} for user {user_id}")