87 lines
3.1 KiB
Python
87 lines
3.1 KiB
Python
|
|
import logging
|
|
from sqlmodel import Session, select
|
|
|
|
from in_game.game_helpers import legal_check, CardLegalityException
|
|
from in_game.gameplay_models import Game, Lineup, Team
|
|
from in_game.gameplay_queries import get_card_or_none
|
|
|
|
|
|
async def get_lineups_from_sheets(session: Session, sheets, this_game: Game, this_team: Team, lineup_num: int, roster_num: int) -> list[Lineup]:
|
|
logging.debug(f'sheets: {sheets}')
|
|
|
|
this_sheet = sheets.open_by_key(this_team.gsheet)
|
|
logging.debug(f'this_sheet: {this_sheet}')
|
|
|
|
r_sheet = this_sheet.worksheet_by_title('My Rosters')
|
|
logging.debug(f'r_sheet: {r_sheet}')
|
|
|
|
if lineup_num == 1:
|
|
row_start = 9
|
|
row_end = 17
|
|
else:
|
|
row_start = 18
|
|
row_end = 26
|
|
|
|
if roster_num == 1:
|
|
l_range = f'H{row_start}:I{row_end}'
|
|
elif roster_num == 2:
|
|
l_range = f'J{row_start}:K{row_end}'
|
|
else:
|
|
l_range = f'L{row_start}:M{row_end}'
|
|
|
|
logging.debug(f'l_range: {l_range}')
|
|
raw_cells = r_sheet.range(l_range)
|
|
logging.debug(f'raw_cells: {raw_cells}')
|
|
|
|
try:
|
|
lineup_cells = [(row[0].value, int(row[1].value)) for row in raw_cells]
|
|
logging.debug(f'lineup_cells: {lineup_cells}')
|
|
except ValueError as e:
|
|
logging.error(f'Could not pull roster for {this_team.abbrev}: {e}')
|
|
raise ValueError(f'Uh oh. Looks like your roster might not be saved. I am reading blanks when I try to get the card IDs')
|
|
|
|
all_lineups = []
|
|
all_pos = []
|
|
card_ids = []
|
|
for index, row in enumerate(lineup_cells):
|
|
if '' in row:
|
|
break
|
|
|
|
if row[0].upper() not in all_pos:
|
|
all_pos.append(row[0].upper())
|
|
else:
|
|
raise SyntaxError(f'You have more than one {row[0].upper()} in this lineup. Please update and set the lineup again.')
|
|
|
|
this_card = await get_card_or_none(session, card_id=int(row[1]))
|
|
if this_card is None:
|
|
raise LookupError(
|
|
f'Your {row[0].upper()} has a Card ID of {int(row[1])} and I cannot find that card. Did you sell it by chance? Or maybe you sold a duplicate and the bot sold the one you were using?'
|
|
)
|
|
if this_card.team_id != this_team.id:
|
|
raise SyntaxError(f'Easy there, champ. Looks like card ID {row[1]} belongs to the {this_card.team.lname}. Try again with only cards you own.')
|
|
card_id = row[1]
|
|
card_ids.append(str(card_id))
|
|
|
|
this_lineup = Lineup(
|
|
position=row[0].upper(),
|
|
batting_order=index + 1,
|
|
game=this_game,
|
|
team=this_team,
|
|
player=this_card.player,
|
|
card=this_card
|
|
)
|
|
all_lineups.append(this_lineup)
|
|
|
|
legal_data = await legal_check([card_ids], difficulty_name=this_game.game_type)
|
|
logging.debug(f'legal_data: {legal_data}')
|
|
if not legal_data['legal']:
|
|
raise CardLegalityException(f'The following cards appear to be illegal for this game mode:\n{legal_data["error_string"]}')
|
|
|
|
if len(all_lineups) != 9:
|
|
raise Exception(f'I was only able to pull in {len(all_lineups)} batters from Sheets. Please check your saved lineup and try again.')
|
|
|
|
return all_lineups
|
|
|
|
|
|
|