166 lines
6.4 KiB
Python
166 lines
6.4 KiB
Python
import logging
|
|
import random
|
|
|
|
import db_calls_gameplay
|
|
from db_calls_gameplay import StratGame, StratPlay, StratLineup, StratManagerAi, patch_play, advance_runners, \
|
|
complete_play, get_team_lineups, get_or_create_bullpen, get_player, get_sheets, make_sub
|
|
from db_calls import db_get
|
|
|
|
|
|
def single_onestar(this_play: StratPlay, comp_play: bool = True):
|
|
patch_play(this_play.id, locked=True)
|
|
advance_runners(this_play.id, num_bases=1)
|
|
patch_play(this_play.id, pa=1, ab=1, hit=1)
|
|
if comp_play:
|
|
complete_play(this_play.id, batter_to_base=1)
|
|
|
|
|
|
def single_wellhit(this_play: StratPlay, comp_play: bool = True):
|
|
patch_play(this_play.id, locked=True)
|
|
advance_runners(this_play.id, num_bases=2)
|
|
patch_play(this_play.id, pa=1, ab=1, hit=1)
|
|
if comp_play:
|
|
complete_play(this_play.id, batter_to_base=1)
|
|
|
|
|
|
def double_twostar(this_play: StratPlay, comp_play: bool = True):
|
|
patch_play(this_play.id, locked=True)
|
|
advance_runners(this_play.id, num_bases=2)
|
|
patch_play(this_play.id, pa=1, ab=1, hit=1, double=1)
|
|
if comp_play:
|
|
complete_play(this_play.id, batter_to_base=2)
|
|
|
|
|
|
def double_threestar(this_play: StratPlay, comp_play: bool = True):
|
|
patch_play(this_play.id, locked=True)
|
|
advance_runners(this_play.id, num_bases=3)
|
|
patch_play(this_play.id, pa=1, ab=1, hit=1, double=1)
|
|
if comp_play:
|
|
complete_play(this_play.id, batter_to_base=2)
|
|
|
|
|
|
def triple(this_play: StratPlay, comp_play: bool = True):
|
|
patch_play(this_play.id, locked=True)
|
|
advance_runners(this_play.id, num_bases=3)
|
|
patch_play(this_play.id, pa=1, ab=1, hit=1, triple=1)
|
|
if comp_play:
|
|
complete_play(this_play.id, batter_to_base=3)
|
|
|
|
|
|
async def next_pitcher(this_play: StratPlay, ai_team, bot):
|
|
used_pitchers = await get_team_lineups(
|
|
game_id=this_play.game.id, team_id=ai_team['id'], inc_inactive=True, pitchers_only=True, as_string=False
|
|
)
|
|
logging.debug(f'\n\nused_pitchers: {used_pitchers}')
|
|
|
|
used_ids = [x.card_id for x in used_pitchers]
|
|
logging.debug(f'used_ids: {used_ids}')
|
|
bullpen = get_or_create_bullpen(ai_team, bot)
|
|
logging.debug(f'bullpen: {bullpen}')
|
|
|
|
if this_play.game.short_game:
|
|
if this_play.inning_num == 1:
|
|
relievers = [bullpen.middle_two_id, bullpen.middle_one_id, bullpen.middle_three_id]
|
|
elif this_play.inning_num == 2:
|
|
relievers = [bullpen.setup_id, bullpen.middle_one_id, bullpen.middle_two_id, bullpen.middle_three_id]
|
|
elif this_play.inning_num == 3:
|
|
relievers = [
|
|
bullpen.closer_id, bullpen.setup_id, bullpen.middle_one_id, bullpen.middle_two_id,
|
|
bullpen.middle_three_id
|
|
]
|
|
else:
|
|
relievers = [
|
|
bullpen.long_one_id, bullpen.long_two_id, bullpen.long_three_id, bullpen.long_four_id,
|
|
bullpen.middle_three_id, bullpen.middle_two_id, bullpen.middle_one_id, bullpen.setup_id,
|
|
bullpen.closer_id
|
|
]
|
|
else:
|
|
if this_play.inning_num < 6:
|
|
relievers = [bullpen.long_one_id, bullpen.long_two_id, bullpen.long_three_id, bullpen.long_four_id]
|
|
elif this_play.inning_num == 6:
|
|
relievers = [bullpen.middle_two_id, bullpen.middle_one_id, bullpen.middle_three_id]
|
|
elif this_play.inning_num == 7:
|
|
relievers = [bullpen.middle_one_id, bullpen.middle_two_id, bullpen.middle_three_id]
|
|
elif this_play.inning_num == 8:
|
|
relievers = [bullpen.setup_id, bullpen.middle_one_id, bullpen.middle_two_id, bullpen.middle_three_id]
|
|
elif this_play.inning_num == 9:
|
|
relievers = [
|
|
bullpen.closer_id, bullpen.setup_id, bullpen.middle_one_id, bullpen.middle_two_id, bullpen.middle_three_id
|
|
]
|
|
else:
|
|
relievers = [
|
|
bullpen.long_one_id, bullpen.long_two_id, bullpen.long_three_id, bullpen.long_four_id,
|
|
bullpen.middle_three_id, bullpen.middle_two_id, bullpen.middle_one_id, bullpen.setup_id, bullpen.closer_id
|
|
]
|
|
|
|
for arm in relievers:
|
|
if arm and arm not in used_ids:
|
|
return {'player': await get_player(this_play.game, {'card_id': arm}), 'card_id': arm}
|
|
|
|
relievers = [
|
|
bullpen.closer_id, bullpen.setup_id, bullpen.middle_one_id, bullpen.middle_two_id, bullpen.middle_three_id,
|
|
bullpen.long_one_id, bullpen.long_two_id, bullpen.long_three_id, bullpen.long_four_id
|
|
]
|
|
|
|
for arm in relievers:
|
|
if arm and arm not in used_ids:
|
|
return {'player': await get_player(this_play.game, {'card_id': arm}), 'card_id': arm}
|
|
|
|
return {"p_name": "Backup Infielder"}
|
|
|
|
|
|
def starting_pitcher(ai_team, bot, is_home):
|
|
sheets = get_sheets(bot)
|
|
this_sheet = sheets.open_by_key(ai_team['gsheet'])
|
|
r_sheet = this_sheet.worksheet_by_title('My Rosters')
|
|
|
|
rotation_range = f'I4:I8'
|
|
raw_cells = r_sheet.range(rotation_range)
|
|
d_100 = random.randint(1, 100)
|
|
logging.info(f'raw_cells: {raw_cells} / d_100: {d_100}')
|
|
|
|
if is_home:
|
|
if d_100 <= 30:
|
|
return raw_cells[0][0].value
|
|
elif d_100 <= 55:
|
|
return raw_cells[1][0].value
|
|
elif d_100 <= 75:
|
|
return raw_cells[2][0].value
|
|
elif d_100 <= 90:
|
|
return raw_cells[3][0].value
|
|
else:
|
|
return raw_cells[4][0].value
|
|
else:
|
|
if d_100 <= 50:
|
|
return raw_cells[0][0].value
|
|
elif d_100 <= 75:
|
|
return raw_cells[1][0].value
|
|
elif d_100 <= 85:
|
|
return raw_cells[2][0].value
|
|
elif d_100 <= 95:
|
|
return raw_cells[3][0].value
|
|
else:
|
|
return raw_cells[4][0].value
|
|
|
|
|
|
def replace_pitcher(ai_team, this_play, pitcher_card_id):
|
|
this_card = db_get(f'cards', object_id=int(pitcher_card_id))
|
|
new_lineup = {
|
|
'game_id': this_play.game.id,
|
|
'team_id': ai_team['id'],
|
|
'player_id': this_card['player']['player_id'],
|
|
'card_id': pitcher_card_id,
|
|
'position': 'P',
|
|
'batting_order': 10,
|
|
'after_play': this_play.play_num - 1
|
|
}
|
|
make_sub(new_lineup)
|
|
|
|
|
|
def get_pitcher(this_game: StratGame, this_play: StratPlay):
|
|
p_team_id = this_game.away_team_id
|
|
if this_play.inning_half == 'top':
|
|
p_team_id = this_game.home_team_id
|
|
return db_calls_gameplay.get_one_lineup(this_game.id, team_id=p_team_id, position='P', active=True)
|
|
|