Update gauntlet methods for Team objects

This commit is contained in:
Cal Corum 2024-12-18 10:00:26 -06:00
parent 07fcbca866
commit f9ced5cb9e

View File

@ -4,28 +4,31 @@ import logging
import random
import discord
from sqlmodel import Session
from in_game import ai_manager
import helpers
from helpers import RARITY, get_or_create_role, send_to_channel, get_channel
from api_calls import db_get, db_post, db_delete, db_patch
from in_game.gameplay_models import Team
from in_game.gameplay_queries import get_team_or_none
logger = logging.getLogger('discord_app')
async def wipe_team(this_team, interaction: discord.Interaction, delete_team: bool = False, delete_runs: bool = False):
async def wipe_team(this_team: Team, interaction: discord.Interaction, delete_team: bool = False, delete_runs: bool = False):
await interaction.edit_original_response(content=f'Looking for cards...')
# Delete cards
# c_query = await db_get('cards', params=[('team_id', this_team['id'])])
# await interaction.edit_original_response(content=f'Found {c_query["count"]} cards; deleting cards...')
# for x in c_query['cards']:
# await db_delete('cards', object_id=x['id'])
c_query = await db_post(f'cards/wipe-team/{this_team["id"]}')
c_query = await db_post(f'cards/wipe-team/{this_team.id}')
# Delete packs
await interaction.edit_original_response(content=f'Done deleting cards; searching for packs...')
p_query = await db_get('packs', params=[('team_id', this_team['id'])])
p_query = await db_get('packs', params=[('team_id', this_team.id)])
await interaction.edit_original_response(content=f'Found {p_query["count"]} packs; deleting packs...')
for x in p_query['packs']:
await db_delete('packs', object_id=x['id'])
@ -33,11 +36,11 @@ async def wipe_team(this_team, interaction: discord.Interaction, delete_team: bo
# Delete team
if delete_team:
await interaction.edit_original_response(content=f'Done deleting packs; now deleting team...')
await db_delete('teams', object_id=this_team['id'])
await db_delete('teams', object_id=this_team.id)
await interaction.edit_original_response(content=f'Team is deleted; now finding the run...')
if delete_runs:
r_query = await db_get('gauntletruns', params=[('team_id', this_team['id']), ('is_active', True)])
r_query = await db_get('gauntletruns', params=[('team_id', this_team.id), ('is_active', True)])
await interaction.edit_original_response(content=f'Found {r_query["count"]} runs; deleting now...')
for x in r_query['runs']:
await db_delete('gauntletruns', object_id=x['id'])
@ -57,7 +60,7 @@ def is_home_team(this_team, this_event, this_run):
return False
async def get_opponent(this_team, this_event, this_run):
async def get_opponent(session: Session, this_team, this_event, this_run) -> Team:
gp = games_played(this_run)
if this_event['id'] == 1:
if gp == 0:
@ -84,7 +87,6 @@ async def get_opponent(this_team, this_event, this_run):
t_id = 58
else:
raise KeyError(f'Huh...I have no idea who you should be playing right now.')
return await db_get('teams', object_id=t_id, none_okay=False)
elif this_event['id'] == 2:
if gp == 0:
t_id = 23
@ -110,7 +112,6 @@ async def get_opponent(this_team, this_event, this_run):
t_id = 28
else:
raise KeyError(f'Huh...I have no idea who you should be playing right now.')
return await db_get('teams', object_id=t_id, none_okay=False)
elif this_event['id'] == 3:
if gp == 0:
t_id = 28
@ -136,7 +137,6 @@ async def get_opponent(this_team, this_event, this_run):
t_id = 19
else:
raise KeyError(f'Huh...I have no idea who you should be playing right now.')
return await db_get('teams', object_id=t_id, none_okay=False)
elif this_event['id'] == 4:
if gp == 0:
t_id = 3
@ -162,7 +162,6 @@ async def get_opponent(this_team, this_event, this_run):
t_id = 79
else:
raise KeyError(f'Hmm...I do not know who you should be playing right now.')
return await db_get('teams', object_id=t_id, none_okay=False)
elif this_event['id'] == 5:
if gp == 0:
t_id = 15
@ -188,7 +187,6 @@ async def get_opponent(this_team, this_event, this_run):
t_id = 19
else:
raise KeyError(f'Hmm...I do not know who you should be playing right now.')
return await db_get('teams', object_id=t_id, none_okay=False)
elif this_event['id'] == 6:
if gp == 0:
t_id = 30
@ -214,10 +212,11 @@ async def get_opponent(this_team, this_event, this_run):
t_id = 29
else:
raise KeyError(f'Hmm...I do not know who you should be playing right now.')
return await db_get('teams', object_id=t_id, none_okay=False)
else:
return None
return await get_team_or_none(session, t_id)
async def build_lineup(this_team, this_game, this_event, sp_name):
return await ai_manager.build_lineup(this_team, this_game.id, f'gauntlet-{this_event["id"]}', sp_name)
@ -305,33 +304,34 @@ async def get_starting_pitcher(this_team, this_game, this_event, this_run):
# }
async def run_draft(interaction: discord.Interaction, main_team, this_event, draft_team=None):
async def run_draft(interaction: discord.Interaction, main_team: Team, this_event, draft_team: Team = None):
logger.info(f'Starting draft for {main_team.abbrev if draft_team is None else draft_team.abbrev}')
if this_event['id'] == 1:
embed_title = f'{main_team["lname"]} - {this_event["name"]} Draft'
embed_title = f'{main_team.lname} - {this_event["name"]} Draft'
embed_description = f'{this_event["name"]}'
base_params = copy.deepcopy(ai_manager.GAUNTLET1_PARAMS)
base_params.extend([('limit', 8), ('cardset_id', 8)])
elif this_event['id'] == 2:
embed_title = f'{main_team["lname"]} - {this_event["name"]} Draft'
embed_title = f'{main_team.lname} - {this_event["name"]} Draft'
embed_description = f'{this_event["name"]}'
base_params = copy.deepcopy(ai_manager.GAUNTLET2_PARAMS)
base_params.extend([('limit', 8)])
elif this_event['id'] == 3:
embed_title = f'{main_team["lname"]} - {this_event["name"]} Draft'
embed_title = f'{main_team.lname} - {this_event["name"]} Draft'
embed_description = f'{this_event["name"]}'
base_params = [('cardset_id', 8), ('cardset_id', 13), ('cardset_id', 14), ('cardset_id', 15), ('limit', 8)]
elif this_event['id'] == 4:
embed_title = f'{main_team["lname"]} - {this_event["name"]} Draft'
embed_title = f'{main_team.lname} - {this_event["name"]} Draft'
embed_description = f'{this_event["name"]}'
base_params = [('cardset_id', 3), ('cardset_id', 4), ('cardset_id', 6), ('cardset_id', 16),
('cardset_id', 15), ('limit', 8)]
elif this_event['id'] == 5:
embed_title = f'{main_team["lname"]} - {this_event["name"]} Draft'
embed_title = f'{main_team.lname} - {this_event["name"]} Draft'
embed_description = f'{this_event["name"]}'
base_params = [('cardset_id', 17), ('cardset_id', 18), ('cardset_id', 19), ('cardset_id', 16),
('cardset_id', 8), ('limit', 8)]
elif this_event['id'] == 6:
embed_title = f'{main_team['lname']} - {this_event['name']} Draft'
embed_title = f'{main_team.lname} - {this_event['name']} Draft'
embed_description = f'{this_event["name"]}'
base_params = [('cardset_id', 20), ('cardset_id', 21), ('cardset_id', 22), ('cardset_id', 16),
('cardset_id', 8), ('limit', 8)]
@ -344,16 +344,16 @@ async def run_draft(interaction: discord.Interaction, main_team, this_event, dra
draft_team = await db_post(
'teams',
payload={
'abbrev': f'Gauntlet-{main_team["abbrev"]}',
'sname': main_team['sname'],
'lname': main_team['lname'],
'gmid': main_team['gmid'],
'gmname': main_team['gmname'],
'abbrev': f'Gauntlet-{main_team.abbrev}',
'sname': main_team.sname,
'lname': main_team.lname,
'gmid': main_team.gmid,
'gmname': main_team.gmname,
'gsheet': 'NONE',
'logo': main_team['logo'] if main_team['logo'] else None,
'color': main_team['color'] if main_team['color'] else None,
'season': main_team['season'],
'has_guide': main_team['has_guide']
'logo': main_team.logo if main_team.logo else None,
'color': main_team.color if main_team.color else None,
'season': main_team.season,
'has_guide': main_team.has_guide
}
)
@ -392,7 +392,7 @@ async def run_draft(interaction: discord.Interaction, main_team, this_event, dra
elif this_event['id'] in [5, 6]:
g_query = await db_get(
'games',
params=[('season', draft_team['season']), ('team1_id', draft_team['id']), ('gauntlet_id', this_event['id'])]
params=[('season', draft_team.season), ('team1_id', draft_team.id), ('gauntlet_id', this_event['id'])]
)
if g_query['count'] > 4:
game_count = g_query['count']
@ -1521,7 +1521,7 @@ async def run_draft(interaction: discord.Interaction, main_team, this_event, dra
this_pack = await db_post(
'packs/one',
payload={
'team_id': draft_team['id'],
'team_id': draft_team.id,
'pack_type_id': 2,
'open_time': datetime.datetime.timestamp(datetime.datetime.now()) * 1000
}
@ -1529,21 +1529,21 @@ async def run_draft(interaction: discord.Interaction, main_team, this_event, dra
await db_post(
'cards',
payload={'cards': [
{'player_id': x['player_id'], 'team_id': draft_team['id'], 'pack_id': this_pack['id']} for x in all_players
{'player_id': x['player_id'], 'team_id': draft_team.id, 'pack_id': this_pack['id']} for x in all_players
]}
)
await db_post(
'gauntletruns',
payload={
'team_id': draft_team['id'],
'team_id': draft_team.id,
'gauntlet_id': this_event['id']
}
)
final_embed = get_embeds(False)[0]
final_embed.title = f'{main_team["lname"]} - {this_event["name"]} Draft'
if main_team['logo']:
final_embed.set_thumbnail(url=main_team["logo"])
final_embed.title = f'{main_team.lname} - {this_event["name"]} Draft'
if main_team.logo:
final_embed.set_thumbnail(url=main_team.logo)
return final_embed