272 lines
9.5 KiB
Python
272 lines
9.5 KiB
Python
import copy
|
|
import datetime
|
|
import logging
|
|
|
|
import discord
|
|
import requests
|
|
from peewee import DatabaseError
|
|
|
|
import ai_manager
|
|
import helpers
|
|
from helpers import RARITY
|
|
from db_calls import db_get, db_post
|
|
|
|
|
|
def get_game_code(this_team, this_event, this_run):
|
|
return f'gauntlet-{this_event["id"]}-run-{this_run["id"]}-game-{games_played(this_run) + 1}'
|
|
|
|
|
|
def games_played(this_run):
|
|
return this_run['wins'] + this_run['losses']
|
|
|
|
|
|
def is_home_team(this_team, this_event, this_run):
|
|
if this_event['id'] == 1:
|
|
return True
|
|
return False
|
|
|
|
|
|
def get_opponent(this_team, this_event, this_run):
|
|
if this_event['id'] == 1:
|
|
gp = games_played(this_run)
|
|
if gp == 1:
|
|
return db_get('teams', object_id=3, none_okay=False)
|
|
else:
|
|
return db_get('teams', object_id=19, none_okay=False)
|
|
else:
|
|
return None
|
|
|
|
|
|
def build_lineup(this_team, this_game, this_event):
|
|
if this_event['id'] == 1:
|
|
return ai_manager.build_lineup(this_team, this_game.id, f'gauntlet-{this_event["id"]}')
|
|
else:
|
|
raise KeyError(f'Lineups not found for Gauntlet {this_event["id"]}')
|
|
|
|
|
|
def get_starting_pitcher(this_team, this_game, this_event):
|
|
if this_event['id'] == 1:
|
|
set_params = ai_manager.MINOR_CARDSET_PARAMS
|
|
params = [
|
|
('mlbclub', this_team['lname']), ('pos_include', 'SP'), ('pos_exclude', 'RP'),
|
|
('inc_dex', False), ('sort_by', 'cost-desc'), ('limit', 5)
|
|
]
|
|
params.extend(set_params)
|
|
else:
|
|
raise KeyError(f'Pitcher not found for Gauntlet {this_event["id"]}')
|
|
|
|
# Pull pitchers
|
|
try:
|
|
pitchers = db_get(
|
|
endpoint='players',
|
|
params=params,
|
|
timeout=10
|
|
)
|
|
except ConnectionError as e:
|
|
logging.error(f'Could not get pitchers for {this_team["lname"]}: {e}')
|
|
raise ConnectionError(f'Error pulling starting pitchers for the {this_team["lname"]}. Cal help plz.')
|
|
|
|
if pitchers['count'] == 0:
|
|
raise DatabaseError(f'Could not find any SP for {this_team["abbrev"]}. Seems like a Cal issue.')
|
|
card_id = ai_manager.get_or_create_card(pitchers['players'][0], this_team)
|
|
|
|
return {
|
|
'game_id': this_game.id,
|
|
'team_id': this_team['id'],
|
|
'player_id': pitchers['players'][0]['player_id'],
|
|
'card_id': card_id,
|
|
'position': 'P',
|
|
'batting_order': 10,
|
|
'after_play': 0
|
|
}
|
|
|
|
|
|
async def run_draft(interaction: discord.Interaction, main_team, this_event):
|
|
if this_event['id'] == 1:
|
|
draft_embed = helpers.get_team_embed(f'{this_event["name"]}')
|
|
draft_embed.description = f'{main_team["lname"]} - Team Draft - Round 1'
|
|
base_params = ai_manager.MAJOR_CARDSET_PARAMS
|
|
base_params.append(('limit', 8))
|
|
else:
|
|
logging.error(f'run_draft - Event ID {this_event["id"]} not recognized')
|
|
raise KeyError(f'Draft data not found for Gauntlet {this_event["id"]}')
|
|
|
|
# Post draft team linked to main team
|
|
draft_team = 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'],
|
|
'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']
|
|
}
|
|
)
|
|
|
|
all_players = []
|
|
p_names = []
|
|
counts = {
|
|
'SP': 0,
|
|
'RP': 0,
|
|
'CP': 0,
|
|
'C': 0,
|
|
'1B': 0,
|
|
'2B': 0,
|
|
'3B': 0,
|
|
'SS': 0,
|
|
'LF': 0,
|
|
'CF': 0,
|
|
'RF': 0,
|
|
'DH': 0,
|
|
'MVP': 0,
|
|
'All-Star': 0,
|
|
'Starter': 0,
|
|
'Reserve': 0,
|
|
'Replacement': 0,
|
|
}
|
|
round_num = 1
|
|
counter = 1
|
|
|
|
if this_event['id'] == 1:
|
|
while round_num <= 3 and counter < 50:
|
|
counter += 1
|
|
|
|
await interaction.edit_original_response(
|
|
content=f'**Round {round_num}**\n\n__Draft Picks__\n{p_names}'
|
|
)
|
|
# Set params based on current round
|
|
params = copy.deepcopy(base_params)
|
|
if round_num == 1:
|
|
params.extend([
|
|
('min_rarity', RARITY['MVP']), ('max_rarity', RARITY['MVP']), ('pos_exc', 'RP')
|
|
])
|
|
elif round_num == 2:
|
|
params.extend([
|
|
('min_rarity', RARITY['All-Star']), ('max_rarity', RARITY['All-Star'])
|
|
])
|
|
elif round_num <= 5:
|
|
params.extend([
|
|
('min_rarity', RARITY['Starter']), ('max_rarity', RARITY['Starter'])
|
|
])
|
|
elif round_num <= 10:
|
|
params.extend([
|
|
('min_rarity', RARITY['Reserve']), ('max_rarity', RARITY['Reserve'])
|
|
])
|
|
elif round_num == 11:
|
|
params.extend([
|
|
('min_rarity', RARITY['All-Star']), ('max_rarity', RARITY['All-Star'])
|
|
])
|
|
elif round_num == 12:
|
|
params.extend([
|
|
('min_rarity', RARITY['Starter']), ('max_rarity', RARITY['Starter'])
|
|
])
|
|
elif round_num <= 15:
|
|
params.extend([
|
|
('min_rarity', RARITY['Replacement']), ('max_rarity', RARITY['Replacement'])
|
|
])
|
|
elif round_num <= 18:
|
|
params.extend([
|
|
('min_rarity', RARITY['Starter']), ('max_rarity', RARITY['Starter'])
|
|
])
|
|
elif round_num == 19:
|
|
params.extend([
|
|
('min_rarity', RARITY['All-Star']), ('max_rarity', RARITY['All-Star'])
|
|
])
|
|
elif round_num <= 21:
|
|
params.extend([
|
|
('min_rarity', RARITY['Starter']), ('max_rarity', RARITY['Starter'])
|
|
])
|
|
elif round_num <= 24:
|
|
params.extend([
|
|
('min_rarity', RARITY['Reserve']), ('max_rarity', RARITY['Reserve'])
|
|
])
|
|
elif round_num <= 26:
|
|
params.extend([
|
|
('min_rarity', RARITY['Replacement']), ('max_rarity', RARITY['Replacement'])
|
|
])
|
|
|
|
# Any positional adjustments can be added as params here
|
|
for x in ['C', '1B', '2B', '3B', 'SS', 'LF', 'CF', 'RF']:
|
|
if counts[x] > 2:
|
|
logging.info(f'run_draft pos_check - {x} count up to {counts[x]}')
|
|
if 0 in [counts['C'], counts['1B'], counts['2B'], counts['3B'], counts['SS'], counts['LF'],
|
|
counts['CF'], counts['RF']]:
|
|
logging.info(f'0 exists in other positions; excluding {x}')
|
|
params.append(('pos_exc', x))
|
|
|
|
if counts['RP'] > 8:
|
|
params.append(('pos_exc', 'RP'))
|
|
if counts['SP'] > 6:
|
|
params.append(('pos_exc', 'SP'))
|
|
if counts['RP'] > counts['SP'] + 3:
|
|
params.append(('pos_exc', 'RP'))
|
|
if counts['SP'] > counts['RP'] + 3:
|
|
params.append(('pos_exc', 'RP'))
|
|
|
|
# Call /players/random to get eight cards
|
|
p_query = None
|
|
try:
|
|
p_query = db_get('players/random', params=params)
|
|
except requests.ReadTimeout as e:
|
|
logging.error(f'run_draft - timeout error pulling player with params {params}')
|
|
|
|
if p_query is not None:
|
|
test_player_list = ''
|
|
for x in p_query['players']:
|
|
test_player_list += f'{x["rarity"]["name"]} - {x["description"]} - {helpers.get_all_pos(x)}\n'
|
|
await interaction.channel.send(f'The Round {round_num} players drawn are:\n\n{test_player_list}')
|
|
|
|
# Collect 4 cards with no repeat player names
|
|
this_batch = []
|
|
for x in p_query['players']:
|
|
if x['p_name'] not in p_names:
|
|
this_batch.append(x)
|
|
if len(this_batch) == 4:
|
|
break
|
|
|
|
# Present choices and capture selection
|
|
p_choice = await helpers.get_choice_from_cards(interaction, this_batch)
|
|
|
|
# Add player to list and update counts
|
|
p_names.append(p_choice['p_name'])
|
|
all_players.append(p_choice)
|
|
for x in helpers.get_all_pos(p_choice):
|
|
if x in counts:
|
|
counts[x] += 1
|
|
|
|
# Update roster embed
|
|
round_num += 1
|
|
else:
|
|
logging.error(f'run_draft - No draft logic for Event ID {this_event["id"]}')
|
|
raise KeyError(f'Draft data not found for Gauntlet {this_event["id"]}')
|
|
|
|
raise EnvironmentError(f'End test run')
|
|
|
|
# Create team google sheet
|
|
this_pack = db_post(
|
|
'packs/one',
|
|
payload={
|
|
'team_id': draft_team['id'],
|
|
'pack_type_id': 2,
|
|
'open_time': datetime.datetime.timestamp(datetime.datetime.now()) * 1000
|
|
}
|
|
)
|
|
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
|
|
]}
|
|
)
|
|
db_post(
|
|
'gauntletruns',
|
|
payload={
|
|
'team_id': draft_team['id'],
|
|
'gauntlet_id': this_event['id']
|
|
}
|
|
)
|