diff --git a/batters/creation.py b/batters/creation.py index 9f81dfc..d077fb6 100644 --- a/batters/creation.py +++ b/batters/creation.py @@ -3,10 +3,12 @@ import urllib.parse import pandas as pd import numpy as np +from typing import Dict from creation_helpers import ( get_all_pybaseball_ids, sanitize_name, CLUB_LIST, FRANCHISE_LIST, pd_players_df, mlbteam_and_franchise, get_hand, NEW_PLAYER_COST, RARITY_BASE_COSTS, - should_update_player_description, calculate_rarity_cost_adjustment + should_update_player_description, calculate_rarity_cost_adjustment, + DEFAULT_BATTER_OPS ) from db_calls import db_post, db_get, db_put, db_patch from . import calcs_batter as cba @@ -226,8 +228,15 @@ async def calculate_batting_ratings(offense_stats: pd.DataFrame, to_post: bool): async def post_player_updates( - cardset: dict, card_base_url: str, release_dir: str, player_desc: str, is_liveseries: bool, to_post: bool, - is_custom: bool, season: int): + cardset: Dict[str, any], + card_base_url: str, + release_dir: str, + player_desc: str, + is_liveseries: bool, + to_post: bool, + is_custom: bool, + season: int +) -> int: """ Update player metadata after card creation (costs, rarities, descriptions, teams, images). @@ -300,16 +309,11 @@ async def post_player_updates( player_updates = {} # { : [ (param pairs) ] } rarity_group = player_data.query('rarity == new_rarity_id').groupby('rarity') average_ops = rarity_group['total_OPS'].mean().to_dict() - if 1 not in average_ops: - average_ops[1] = 1.066 - if 2 not in average_ops: - average_ops[2] = 0.938 - if 3 not in average_ops: - average_ops[3] = 0.844 - if 4 not in average_ops: - average_ops[4] = 0.752 - if 5 not in average_ops: - average_ops[5] = 0.612 + + # Fill in missing rarity averages with defaults + for rarity, default_ops in DEFAULT_BATTER_OPS.items(): + if rarity not in average_ops: + average_ops[rarity] = default_ops def get_player_updates(df_data): params = [] diff --git a/creation_helpers.py b/creation_helpers.py index 9e93081..375223b 100644 --- a/creation_helpers.py +++ b/creation_helpers.py @@ -3,6 +3,7 @@ import datetime import math from decimal import ROUND_HALF_EVEN, Decimal from exceptions import logger +from typing import Dict, List, Tuple, Optional import pandas as pd import pybaseball as pb @@ -73,6 +74,39 @@ RARITY_COST_ADJUSTMENTS = { (99, 5): (-2400, 5), } +# Default OPS Values (fallbacks when actual averages unavailable) +# These are used to calculate player costs when we don't have enough data +# to calculate rarity-specific averages from the cardset + +# Batter default OPS by rarity +DEFAULT_BATTER_OPS = { + 1: 1.066, # Diamond + 2: 0.938, # Gold + 3: 0.844, # Silver + 4: 0.752, # Bronze + 5: 0.612, # Common +} + +# Starting Pitcher default OPS-against by rarity +DEFAULT_STARTER_OPS = { + 99: 0.388, # Special/Legend + 1: 0.445, # Diamond + 2: 0.504, # Gold + 3: 0.568, # Silver + 4: 0.634, # Bronze + 5: 0.737, # Common +} + +# Relief Pitcher default OPS-against by rarity +DEFAULT_RELIEVER_OPS = { + 99: 0.282, # Special/Legend + 1: 0.375, # Diamond + 2: 0.442, # Gold + 3: 0.516, # Silver + 4: 0.591, # Bronze + 5: 0.702, # Common +} + D20_CHANCES = { '2': { 'chances': 1, diff --git a/pitchers/creation.py b/pitchers/creation.py index 84b89bf..dc71fd4 100644 --- a/pitchers/creation.py +++ b/pitchers/creation.py @@ -1,11 +1,13 @@ import datetime import urllib.parse import pandas as pd +from typing import Dict from creation_helpers import ( get_all_pybaseball_ids, sanitize_name, CLUB_LIST, FRANCHISE_LIST, pd_players_df, mlbteam_and_franchise, NEW_PLAYER_COST, RARITY_BASE_COSTS, - should_update_player_description, calculate_rarity_cost_adjustment + should_update_player_description, calculate_rarity_cost_adjustment, + DEFAULT_STARTER_OPS, DEFAULT_RELIEVER_OPS ) from db_calls import db_post, db_get, db_put, db_patch from defenders import calcs_defense as cde @@ -306,8 +308,14 @@ async def calculate_pitcher_ratings(pitching_stats: pd.DataFrame, post_pitchers: async def post_player_updates( - cardset: dict, player_description: str, card_base_url: str, release_dir: str, is_liveseries: bool, - post_players: bool, season: int): + cardset: Dict[str, any], + player_description: str, + card_base_url: str, + release_dir: str, + is_liveseries: bool, + post_players: bool, + season: int +) -> int: p_data = await pd_players_df(cardset['id']) p_data.set_index('player_id', drop=False) @@ -360,32 +368,15 @@ async def post_player_updates( sp_average_ops = sp_rarity_group['total_OPS'].mean().to_dict() rp_rarity_group = player_data.query('rarity == new_rarity_id and starter_rating < 4').groupby('rarity') rp_average_ops = rp_rarity_group['total_OPS'].mean().to_dict() - # cost_groups = rarity_group['cost'].mean() - if 99 not in sp_average_ops: - sp_average_ops[99] = 0.388 - if 1 not in sp_average_ops: - sp_average_ops[1] = 0.445 - if 2 not in sp_average_ops: - sp_average_ops[2] = 0.504 - if 3 not in sp_average_ops: - sp_average_ops[3] = 0.568 - if 4 not in sp_average_ops: - sp_average_ops[4] = 0.634 - if 5 not in sp_average_ops: - sp_average_ops[5] = 0.737 - if 99 not in rp_average_ops: - rp_average_ops[99] = 0.282 - if 1 not in rp_average_ops: - rp_average_ops[1] = 0.375 - if 2 not in rp_average_ops: - rp_average_ops[2] = 0.442 - if 3 not in rp_average_ops: - rp_average_ops[3] = 0.516 - if 4 not in rp_average_ops: - rp_average_ops[4] = 0.591 - if 5 not in rp_average_ops: - rp_average_ops[5] = 0.702 + # Fill in missing rarity averages with defaults + for rarity, default_ops in DEFAULT_STARTER_OPS.items(): + if rarity not in sp_average_ops: + sp_average_ops[rarity] = default_ops + + for rarity, default_ops in DEFAULT_RELIEVER_OPS.items(): + if rarity not in rp_average_ops: + rp_average_ops[rarity] = default_ops def get_player_updates(df_data): def avg_ops(rarity_id, starter_rating):