Update exception function

This commit is contained in:
Cal Corum 2024-11-03 21:54:55 -06:00
parent e70f101cf5
commit 0335b32673
4 changed files with 33 additions and 10 deletions

View File

@ -7,7 +7,10 @@ import aiohttp
import os
AUTH_TOKEN = {'Authorization': f'Bearer {os.environ.get("API_TOKEN")}'}
ENV_DATABASE = os.environ.get('DATABASE').lower()
try:
ENV_DATABASE = os.environ.get('DATABASE').lower()
except Exception as e:
ENV_DATABASE = 'dev'
DB_URL = 'https://pd.manticorum.com/api' if 'prod' in ENV_DATABASE else 'https://pddev.manticorum.com/api'
master_debug = True
PLAYER_CACHE = {}

View File

@ -1,8 +1,16 @@
import logging
from typing import Literal
def log_exception(e: Exception, msg: str = ''):
logging.debug(msg, stack_info=True)
raise e
def log_exception(e: Exception, msg: str = '', level: Literal['debug', 'error', 'info', 'warn'] = 'error'):
if level == 'debug':
logging.debug(msg, stack_info=True)
elif level == 'error':
logging.error(msg, stack_info=True)
elif level == 'info':
logging.info(msg, stack_info=True)
else:
logging.warning(msg, stack_info=True)
raise e(msg)
class GameException(Exception):
pass

View File

@ -246,13 +246,13 @@ class Game(SQLModel, table=True):
if len(home_positions) != 10:
e_msg = f'Only {len(home_positions)} players found on home team'
log_exception(LineupsMissingException(e_msg), e_msg)
log_exception(LineupsMissingException, e_msg)
if len(away_positions) != 10:
e_msg = f'Only {len(away_positions)} players found on away team'
log_exception(LineupsMissingException(e_msg), e_msg)
log_exception(LineupsMissingException, e_msg)
if None in [leadoff_batter, home_pitcher, home_catcher]:
e_msg = f'Could not set the initial pitcher, catcher, and batter'
log_exception(LineupsMissingException(e_msg), e_msg)
log_exception(LineupsMissingException, e_msg)
new_play = Play(
game=self,
@ -282,7 +282,6 @@ class Game(SQLModel, table=True):
return lineup_val
class ManagerAi(ManagerAiBase, table=True):
plays: list['Play'] = Relationship(back_populates='managerai')
def create_ai(session: Session = None):
@ -458,14 +457,13 @@ class PlayerBase(SQLModel):
return f'[{self.name}]({self.b_card_url})'
class Player(PlayerBase, table=True):
cardset: Cardset = Relationship(back_populates='players')
cards: list['Card'] = Relationship(back_populates='player', cascade_delete=True)
lineups: list['Lineup'] = Relationship(back_populates='player', cascade_delete=True)
@property
def with_desc(self):
def name_with_desc(self):
return f'{self.description} {self.name}'

View File

@ -4,6 +4,7 @@ import logging
from sqlalchemy import func
from api_calls import db_get, db_post
from in_game.gameplay_models import CACHE_LIMIT, Card, CardBase, Lineup, Player, PlayerBase, Session, Team, TeamBase, select, or_, Game, Play
from exceptions import log_exception, PlayNotFoundException
def get_games_by_channel(session: Session, channel_id: int) -> list[Game]:
@ -244,3 +245,16 @@ def get_game_lineups(session: Session, this_game: Game, specific_team: Team = No
return session.exec(st).all()
def get_players_last_pa(session: Session, lineup_member: Lineup, none_okay: bool = False):
last_pa = session.exec(select(Play).where(Play.game == lineup_member.game, Play.batter == lineup_member).order_by(Play.id.desc().limit(1))).all()
if len(last_pa) == 1:
return last_pa[0]
else:
if none_okay:
return None
else:
log_exception(PlayNotFoundException, f'No play found for {lineup_member.player.name_with_desc}\'s last AB')
def get_one_lineup(session: Session, this_game: Game, this_team: Team, position: str, active: bool = True) -> Lineup:
return session.exec(select(Lineup).where(Lineup.game == this_game, Lineup.team == this_team, Lineup.position == position, Lineup.active == active)).one()