Create simulations.py

This commit is contained in:
Cal Corum 2024-06-27 00:32:24 -05:00
parent 93075519a3
commit 6baa989aae

66
in_game/simulations.py Normal file
View File

@ -0,0 +1,66 @@
import discord
import helpers
import logging
import random
from in_game import data_cache
from dataclasses import asdict, fields
def get_result(pitcher: data_cache.PitchingWrapper, batter: data_cache.BattingWrapper):
which = random.choice(['pitcher', 'batter'])
logging.info(f'in_game.simulations - get_result - which: {which}')
unused_fields = ['avg', 'obp', 'slg', 'pull_rate', 'center_rate', 'slap_rate']
pit_hand = pitcher.card.hand
bat_hand = batter.card.hand
if which == 'pitcher':
logging.info(f'in_game.simulations - get_result - grabbing pitcher card chances')
ch_data = pitcher.ratings_vl if bat_hand.upper() == 'L' else pitcher.ratings_vr
logging.info(f'ch_data: {ch_data}')
# for field in fields(ch_data):
# if field.name not in unused_fields:
# ch_results.append(field.name)
# ch_probs.append(getattr(data_cache.PitchingRatings, field.name))
else:
logging.info(f'in_game.simulations - get_result - grabbing batter card chances')
ch_data = batter.ratings_vl if pit_hand.upper() == 'L' else batter.ratings_vr
logging.info(f'ch_data: {ch_data}')
# for field in fields(ch_data):
# if field.name not in unused_fields:
# ch_results.append(field.name)
# ch_probs.append(getattr(data_cache.BattingRatings, field.name))
ch_dict = {k: v for k, v in asdict(ch_data).items()}
for x in unused_fields:
if x in ch_dict:
del ch_dict[x]
ch_results = list(ch_dict.keys())
ch_probs = list(ch_dict.values())
logging.info(f'ch_results: {ch_results}')
logging.info(f'ch_probs: {ch_probs}')
result = random.choices(ch_results, ch_probs)
logging.info(f'result: {result}')
return result[0]
def get_pos_embeds(this_pitcher, this_batter, pit_card, bat_card):
pit_desc = helpers.player_desc(this_pitcher)
bat_desc = helpers.player_desc(this_batter)
embed = discord.Embed(color=int(helpers.SBA_COLOR, 16), title=f'{pit_desc} vs. {bat_desc}')
embed.set_image(url=helpers.player_pcard(this_pitcher))
embed2 = discord.Embed(color=int(helpers.SBA_COLOR, 16))
embed2.set_image(url=helpers.player_bcard(this_batter))
embed3 = discord.Embed(color=int(helpers.SBA_COLOR, 16))
result = get_result(pit_card, bat_card)
embed3.add_field(name='Pitcher', value=pit_desc)
embed3.add_field(name='Batter', value=bat_desc)
embed3.add_field(name='Result', value=result, inline=False)
return [embed, embed2, embed3]