From c7b45aecf4fd7169ed30294b2a90658d9fee24bc Mon Sep 17 00:00:00 2001 From: Cal Corum Date: Tue, 24 Dec 2024 16:11:28 -0600 Subject: [PATCH] Fixed only_forced bug Fixed scouting caching bug --- command_logic/logic_gameplay.py | 2 +- in_game/gameplay_queries.py | 112 +++++++------------------------- 2 files changed, 23 insertions(+), 91 deletions(-) diff --git a/command_logic/logic_gameplay.py b/command_logic/logic_gameplay.py index 1da4e6a..1f7867c 100644 --- a/command_logic/logic_gameplay.py +++ b/command_logic/logic_gameplay.py @@ -719,7 +719,7 @@ def advance_runners(session: Session, this_play: Play, num_bases: int, only_forc if this_play.on_third: this_play.on_third_final = 3 - if this_play.on_second: + elif this_play.on_second: if this_play.on_third: if num_bases > 0: this_play.on_third_final = 4 diff --git a/in_game/gameplay_queries.py b/in_game/gameplay_queries.py index be22f45..fada2fc 100644 --- a/in_game/gameplay_queries.py +++ b/in_game/gameplay_queries.py @@ -172,17 +172,20 @@ async def get_batter_scouting_or_none(session: Session, card: Card, skip_cache: if s_query['count'] != 2: log_exception(DatabaseError, f'Scouting for {card.player.name_with_desc} was not found.') - this_scouting = session.exec(select(BatterScouting).where(BatterScouting.battingcard_id == s_query['ratings'][0]['battingcard']['id'])).all() - logger.info(f'this_scouting: {this_scouting}') - # this_scouting = session.get(BatterScouting, s_query['ratings'][0]['battingcard']['id']) + bs_query = session.exec(select(BatterScouting).where(BatterScouting.battingcard_id == s_query['ratings'][0]['battingcard']['id'])).all() + logger.info(f'bs_query: {bs_query}') - if len(this_scouting) > 0: + if len(bs_query) > 0: + this_scouting = bs_query[0] + logger.info(f'this_scouting: {this_scouting}') logger.info(f'we found a cached scouting: {this_scouting} / created {this_scouting.created}') tdelta = datetime.datetime.now() - this_scouting.created logger.debug(f'tdelta: {tdelta}') - if tdelta.total_seconds() < CACHE_LIMIT: + if tdelta.total_seconds() < CACHE_LIMIT and None not in [this_scouting.battingcard, this_scouting.ratings_vl, this_scouting.ratings_vr]: + logger.info(f'returning cached scouting') return this_scouting else: + logger.info(f'deleting cached scouting') session.delete(this_scouting.battingcard) session.delete(this_scouting.ratings_vl) session.delete(this_scouting.ratings_vr) @@ -226,19 +229,22 @@ async def get_pitcher_scouting_or_none(session: Session, card: Card, skip_cache: if s_query['count'] != 2: log_exception(DatabaseError, f'Scouting for {card.player.name_with_desc} was not found.') - this_scouting = session.exec(select(PitcherScouting).where(PitcherScouting.pitchingcard_id == s_query['ratings'][0]['pitchingcard']['id'])).all() - logger.info(f'this_scouting: {this_scouting}') + bs_query = session.exec(select(PitcherScouting).where(PitcherScouting.pitchingcard_id == s_query['ratings'][0]['pitchingcard']['id'])).all() + logger.info(f'bs_query: {bs_query}') # this_scouting = session.get(PitcherScouting, s_query['ratings'][0]['pitchingcard']['id']) - if len(this_scouting) > 0: + if len(bs_query) > 0: + this_scouting = bs_query[0] logger.info(f'we found a cached scouting: {this_scouting} / created {this_scouting.created}') tdelta = datetime.datetime.now() - this_scouting.created logger.debug(f'tdelta: {tdelta}') - if tdelta.total_seconds() < CACHE_LIMIT: + if tdelta.total_seconds() < CACHE_LIMIT and None not in [this_scouting.battingcard, this_scouting.ratings_vl, this_scouting.ratings_vr]: + logger.info(f'returning cached scouting') return this_scouting else: + logger.info(f'deleting cached scouting') session.delete(this_scouting.pitchingcard) session.delete(this_scouting.ratings_vl) session.delete(this_scouting.ratings_vr) @@ -276,85 +282,6 @@ async def get_pitcher_scouting_or_none(session: Session, card: Card, skip_cache: pos_rating = await get_position(session, card, 'P') return scouting - # if not skip_cache and card.pitcherscouting is not None: - # this_scouting = session.get(PitcherScouting, card.pitcherscouting.id) - # # s_query = session.exec(select(PitcherScouting).where(PitcherScouting.pitchingcard_id == card.)) - - # if this_scouting is not None: - # logger.info(f'we found a cached scouting: {this_scouting} / created {this_scouting.created}') - # tdelta = datetime.datetime.now() - this_scouting.created - # logger.debug(f'tdelta: {tdelta}') - # if tdelta.total_seconds() < CACHE_LIMIT: - # return this_scouting - # else: - # session.delete(this_scouting) - # session.commit() - - # def cache_scouting(pitching_card: dict, ratings_vr: dict, ratings_vl: dict) -> PitcherScouting: - # valid_bc = PitchingCardBase.model_validate(pitching_card, from_attributes=True) - # db_bc = PitchingCard.model_validate(valid_bc) - - # valid_vl = PitchingRatingsBase.model_validate(ratings_vl, from_attributes=True) - # db_vl = PitchingRatings.model_validate(valid_vl) - - # valid_vr = PitchingRatingsBase.model_validate(ratings_vr, from_attributes=True) - # db_vr = PitchingRatings.model_validate(valid_vr) - - # db_scouting = PitcherScouting( - # pitchingcard=db_bc, - # ratings_vl=db_vl, - # ratings_vr=db_vr - # ) - - # session.add(db_scouting) - # session.commit() - # session.refresh(db_scouting) - # return db_scouting - - # s_query = await db_get(f'pitchingcardratings/player/{card.player.id}', none_okay=False) - # if s_query['count'] == 2: - # scouting = cache_scouting( - # pitching_card=s_query['ratings'][0]['pitchingcard'], - # ratings_vr=s_query['ratings'][0] if s_query['ratings'][0]['vs_hand'] == 'R' else s_query['ratings'][1], - # ratings_vl=s_query['ratings'][0] if s_query['ratings'][0]['vs_hand'] == 'L' else s_query['ratings'][1] - # ) - # pos_rating = await get_position(session, card, 'P') - # return scouting - - # return None - - -# async def get_position_rating_or_none(session: Session, card: Card, position: str, skip_cache: bool = False) -> PositionRating | None: -# logger.info(f'Getting position rating for card ID') -# if not skip_cache: -# ratings = session.exec(select(PositionRating).where(PositionRating.player == card.player, PositionRating.variant == card.variant, PositionRating.position == position).limit(1)).all() - -# """Test all of this; rebuild DB""" - -# if len(ratings) > 0: -# logger.info(f'we found a cached position: {ratings[0]} / created {ratings[0].created}') -# tdelta = datetime.datetime.now() - ratings[0].created -# logger.debug(f'tdelta: {tdelta}') -# if tdelta.total_seconds() < CACHE_LIMIT: -# return ratings[0] -# else: -# session.delete(ratings[0]) -# session.commit() - -# def cache_rating(json_data: dict) -> PositionRating: -# valid_position = PositionRatingBase.model_validate(json_data, from_attributes=True) -# db_position = PositionRating.model_validate(valid_position) -# session.add(db_position) -# session.commit() -# session.refresh(db_position) -# return db_position - -# p_query = await db_get('cardpositions', params=[('player_id', card.player.id)]) -# if p_query['count'] > 0: -# return cache_rating(p_query['positions'][0]) - -# return None - def get_player_id_from_dict(json_data: dict) -> int: logger.info(f'Getting player from dict {json_data}') @@ -443,7 +370,8 @@ async def get_or_create_ai_card(session: Session, player: Player, team: Team, sk logger.info(f'we found a cached card: {this_card} / created: {this_card.created}') tdelta = datetime.datetime.now() - this_card.created logger.debug(f'tdelta: {tdelta}') - if tdelta.total_seconds() < CACHE_LIMIT: + if tdelta.total_seconds() < CACHE_LIMIT and (this_card.pitcherscouting is not None or this_card.batterscouting is not None): + logger.info(f'returning this_card') return this_card else: logger.info(f'deleting card record') @@ -522,9 +450,13 @@ async def get_card_or_none(session: Session, card_id: int, skip_cache: bool = Fa logger.info(f'we found a cached card: {this_card} / created: {this_card.created}') tdelta = datetime.datetime.now() - this_card.created logger.debug(f'tdelta: {tdelta}') - if tdelta.total_seconds() < CACHE_LIMIT: + if tdelta.total_seconds() < CACHE_LIMIT and (this_card.pitcherscouting is not None or this_card.batterscouting is not None): + logger.info(f'returning this_card') return this_card else: + logger.info(f'deleting this_card') + session.delete(this_card.batterscouting) + session.delete(this_card.pitcherscouting) session.delete(this_card) session.commit()