Refactor creation to modules
This commit is contained in:
parent
7114d61ecc
commit
dae6b7e8df
@ -4,9 +4,10 @@ import urllib.parse
|
|||||||
import pandas as pd
|
import pandas as pd
|
||||||
|
|
||||||
from creation_helpers import get_all_pybaseball_ids, sanitize_name, CLUB_LIST, FRANCHISE_LIST, pd_players_df, \
|
from creation_helpers import get_all_pybaseball_ids, sanitize_name, CLUB_LIST, FRANCHISE_LIST, pd_players_df, \
|
||||||
mlbteam_and_franchise
|
mlbteam_and_franchise, get_hand
|
||||||
from db_calls import db_post, db_get, db_put, db_patch
|
from db_calls import db_post, db_get, db_put, db_patch
|
||||||
from . import calcs_batter as cba
|
from . import calcs_batter as cba
|
||||||
|
from defenders import calcs_defense as cde
|
||||||
|
|
||||||
|
|
||||||
async def pd_battingcards_df(cardset_id: int):
|
async def pd_battingcards_df(cardset_id: int):
|
||||||
@ -128,14 +129,7 @@ async def create_new_players(
|
|||||||
return len(new_players)
|
return len(new_players)
|
||||||
|
|
||||||
|
|
||||||
def get_offense_stats(final_batting: pd.DataFrame, input_path: str):
|
def get_stat_df(final_batting: pd.DataFrame, input_path: str):
|
||||||
def get_hand(df_data):
|
|
||||||
if df_data['Name'][-1] == '*':
|
|
||||||
return 'L'
|
|
||||||
elif df_data['Name'][-1] == '#':
|
|
||||||
return 'S'
|
|
||||||
else:
|
|
||||||
return 'R'
|
|
||||||
|
|
||||||
print(f'Reading baserunning stats...')
|
print(f'Reading baserunning stats...')
|
||||||
run_data = (pd.read_csv(f'{input_path}running.csv')
|
run_data = (pd.read_csv(f'{input_path}running.csv')
|
||||||
@ -147,7 +141,7 @@ def get_offense_stats(final_batting: pd.DataFrame, input_path: str):
|
|||||||
return offense_stats
|
return offense_stats
|
||||||
|
|
||||||
|
|
||||||
async def calculate_batting_cards(offense_stats: pd.DataFrame, cardset: dict, season_pct: float, to_post: bool):
|
async def calculate_batting_cards(offense_stats: pd.DataFrame, cardset: dict, season_pct: float, post_batters: bool):
|
||||||
batting_cards = []
|
batting_cards = []
|
||||||
|
|
||||||
def create_batting_card(df_data):
|
def create_batting_card(df_data):
|
||||||
@ -182,7 +176,7 @@ async def calculate_batting_cards(offense_stats: pd.DataFrame, cardset: dict, se
|
|||||||
print(f'Calculating batting cards...')
|
print(f'Calculating batting cards...')
|
||||||
offense_stats.apply(create_batting_card, axis=1)
|
offense_stats.apply(create_batting_card, axis=1)
|
||||||
print(f'Cards are complete.\n\nPosting cards now...')
|
print(f'Cards are complete.\n\nPosting cards now...')
|
||||||
if to_post:
|
if post_batters:
|
||||||
resp = await db_put('battingcards', payload={'cards': batting_cards}, timeout=30)
|
resp = await db_put('battingcards', payload={'cards': batting_cards}, timeout=30)
|
||||||
print(f'Response: {resp}\n\nMatching batting card database IDs to player stats...')
|
print(f'Response: {resp}\n\nMatching batting card database IDs to player stats...')
|
||||||
offense_stats = pd.merge(
|
offense_stats = pd.merge(
|
||||||
@ -384,3 +378,61 @@ async def post_player_updates(
|
|||||||
await db_patch('players', object_id=x, params=player_updates[x])
|
await db_patch('players', object_id=x, params=player_updates[x])
|
||||||
|
|
||||||
return len(player_updates)
|
return len(player_updates)
|
||||||
|
|
||||||
|
|
||||||
|
async def run_batters(
|
||||||
|
cardset: dict, input_path: str, post_players: bool, card_base_url: str, release_directory: str,
|
||||||
|
player_description: str, season_pct: float, post_batters: bool, pull_fielding: bool, season: int,
|
||||||
|
is_liveseries: bool):
|
||||||
|
print(f'Pulling PD player IDs...')
|
||||||
|
pd_players = await pd_players_df(cardset['id'])
|
||||||
|
|
||||||
|
print('Reading batting stats...')
|
||||||
|
all_stats = get_batting_stats(file_path=input_path)
|
||||||
|
print(f'Processed {len(all_stats.values)} batters\n')
|
||||||
|
bat_step1 = match_player_lines(all_stats, pd_players)
|
||||||
|
if post_players:
|
||||||
|
new_batters = await create_new_players(
|
||||||
|
bat_step1, cardset, card_base_url, release_directory, player_description
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
new_batters = 0
|
||||||
|
|
||||||
|
offense_stats = get_stat_df(bat_step1, input_path)
|
||||||
|
del bat_step1, all_stats
|
||||||
|
|
||||||
|
offense_stats = await calculate_batting_cards(offense_stats, cardset, season_pct, post_batters)
|
||||||
|
await calculate_batting_ratings(offense_stats, post_batters)
|
||||||
|
if pull_fielding:
|
||||||
|
print(f'Pulling catcher defense...')
|
||||||
|
df_c = cde.get_bbref_fielding_df('c', season)
|
||||||
|
print(f'Pulling first base defense...')
|
||||||
|
df_1b = cde.get_bbref_fielding_df('1b', season)
|
||||||
|
print(f'Pulling second base defense...')
|
||||||
|
df_2b = cde.get_bbref_fielding_df('2b', season)
|
||||||
|
print(f'Pulling third base defense...')
|
||||||
|
df_3b = cde.get_bbref_fielding_df('3b', season)
|
||||||
|
print(f'Pulling short stop defense...')
|
||||||
|
df_ss = cde.get_bbref_fielding_df('ss', season)
|
||||||
|
print(f'Pulling left field defense...')
|
||||||
|
df_lf = cde.get_bbref_fielding_df('lf', season)
|
||||||
|
print(f'Pulling center field defense...')
|
||||||
|
df_cf = cde.get_bbref_fielding_df('cf', season)
|
||||||
|
print(f'Pulling right field defense...')
|
||||||
|
df_rf = cde.get_bbref_fielding_df('rf', season)
|
||||||
|
print(f'Pulling outfield defense...')
|
||||||
|
df_of = cde.get_bbref_fielding_df('of', season)
|
||||||
|
print(f'Positions data is retrieved')
|
||||||
|
|
||||||
|
await cde.create_positions(
|
||||||
|
offense_stats, season_pct, post_batters, df_c, df_1b, df_2b, df_3b, df_ss, df_lf, df_cf, df_rf, df_of
|
||||||
|
)
|
||||||
|
|
||||||
|
await post_player_updates(
|
||||||
|
cardset, card_base_url, release_directory, player_description, is_liveseries, post_batters
|
||||||
|
)
|
||||||
|
|
||||||
|
return {
|
||||||
|
'tot_batters': len(offense_stats.index),
|
||||||
|
'new_batters': new_batters
|
||||||
|
}
|
||||||
|
|||||||
@ -524,34 +524,6 @@ async def pd_players_df(cardset_id: int):
|
|||||||
return pd.DataFrame(p_query['players'])
|
return pd.DataFrame(p_query['players'])
|
||||||
|
|
||||||
|
|
||||||
async def pd_pitchingcards_df(cardset_id: int):
|
|
||||||
bc_query = await db_get('pitchingcards', params=[('cardset_id', cardset_id), ('short_output', True)])
|
|
||||||
if bc_query['count'] == 0:
|
|
||||||
raise ValueError(f'No pitching cards returned from Paper Dynasty API')
|
|
||||||
return pd.DataFrame(bc_query['cards']).rename(columns={'id': 'pitchingcard_id', 'player': 'player_id'})
|
|
||||||
|
|
||||||
|
|
||||||
async def pd_pitchingcardratings_df(cardset_id: int):
|
|
||||||
vl_query = await db_get(
|
|
||||||
'pitchingcardratings', params=[('cardset_id', cardset_id), ('vs_hand', 'L'), ('short_output', True)])
|
|
||||||
vr_query = await db_get(
|
|
||||||
'pitchingcardratings', params=[('cardset_id', cardset_id), ('vs_hand', 'R'), ('short_output', True)])
|
|
||||||
if 0 in [vl_query['count'], vr_query['count']]:
|
|
||||||
raise ValueError(f'No pitching card ratings returned from Paper Dynasty API')
|
|
||||||
vl = pd.DataFrame(vl_query['ratings'])
|
|
||||||
vr = pd.DataFrame(vr_query['ratings'])
|
|
||||||
ratings = (pd.merge(vl, vr, on='pitchingcard', suffixes=('_vL', '_vR'))
|
|
||||||
.rename(columns={'pitchingcard': 'pitchingcard_id'}))
|
|
||||||
|
|
||||||
def get_total_ops(df_data):
|
|
||||||
ops_vl = df_data['obp_vL'] + df_data['slg_vL']
|
|
||||||
ops_vr = df_data['obp_vR'] + df_data['slg_vR']
|
|
||||||
return (ops_vr + ops_vl + max(ops_vl, ops_vr)) / 3
|
|
||||||
ratings['total_OPS'] = ratings.apply(get_total_ops, axis=1)
|
|
||||||
|
|
||||||
return ratings
|
|
||||||
|
|
||||||
|
|
||||||
async def pd_positions_df(cardset_id: int):
|
async def pd_positions_df(cardset_id: int):
|
||||||
pos_query = await db_get(
|
pos_query = await db_get(
|
||||||
'cardpositions', params=[('cardset_id', cardset_id), ('short_output', True), ('sort', 'innings-desc')])
|
'cardpositions', params=[('cardset_id', cardset_id), ('short_output', True), ('sort', 'innings-desc')])
|
||||||
@ -562,50 +534,6 @@ async def pd_positions_df(cardset_id: int):
|
|||||||
return all_pos
|
return all_pos
|
||||||
|
|
||||||
|
|
||||||
def get_pitching_stats(file_path: str = None, start_date: datetime.datetime = None, end_date: datetime.datetime = None):
|
|
||||||
if file_path is not None:
|
|
||||||
vl_basic = pd.read_csv(f'{file_path}vlhh-basic.csv').query('TBF >= 20')
|
|
||||||
vr_basic = pd.read_csv(f'{file_path}vrhh-basic.csv').query('TBF >= 40')
|
|
||||||
total_basic = pd.merge(vl_basic, vr_basic, on="playerId", suffixes=('_vL', '_vR'))
|
|
||||||
|
|
||||||
vl_rate = pd.read_csv(f'{file_path}vlhh-rate.csv').query('TBF >= 20')
|
|
||||||
vr_rate = pd.read_csv(f'{file_path}vrhh-rate.csv').query('TBF >= 40')
|
|
||||||
total_rate = pd.merge(vl_rate, vr_rate, on="playerId", suffixes=('_vL', '_vR'))
|
|
||||||
|
|
||||||
return pd.merge(total_basic, total_rate, on="playerId", suffixes=('', '_rate'))
|
|
||||||
|
|
||||||
else:
|
|
||||||
raise LookupError(f'Date-based stat pulls not implemented, yet. Please provide batting csv files.')
|
|
||||||
# vrb_url = f'https://www.fangraphs.com/leaders/splits-leaderboards?splitArr=6&splitArrPitch=&position=P' \
|
|
||||||
# f'&autoPt=false&splitTeams=false&statType=player&statgroup=1' \
|
|
||||||
# f'&startDate={start_date.year}-{start_date.month}-{start_date.day}' \
|
|
||||||
# f'&endDate={end_date.year}-{end_date.month}-{end_date.day}' \
|
|
||||||
# f'&players=&filter=&groupBy=season&sort=4,1&wxTemperature=&wxPressure=&wxAirDensity=' \
|
|
||||||
# f'&wxElevation=&wxWindSpeed='
|
|
||||||
# vrr_url = f'https://www.fangraphs.com/leaders/splits-leaderboards?splitArr=6&splitArrPitch=&position=P' \
|
|
||||||
# f'&autoPt=false&splitTeams=false&statType=player&statgroup=3' \
|
|
||||||
# f'&startDate={start_date.year}-{start_date.month}-{start_date.day}' \
|
|
||||||
# f'&endDate={end_date.year}-{end_date.month}-{end_date.day}' \
|
|
||||||
# f'&players=&filter=&groupBy=season&sort=4,1&wxTemperature=&wxPressure=&wxAirDensity=' \
|
|
||||||
# f'&wxElevation=&wxWindSpeed='
|
|
||||||
# vlb_url = f'https://www.fangraphs.com/leaders/splits-leaderboards?splitArr=5&splitArrPitch=&position=P' \
|
|
||||||
# f'&autoPt=false&splitTeams=false&statType=player&statgroup=1' \
|
|
||||||
# f'&startDate={start_date.year}-{start_date.month}-{start_date.day}' \
|
|
||||||
# f'&endDate={end_date.year}-{end_date.month}-{end_date.day}' \
|
|
||||||
# f'&players=&filter=&groupBy=season&sort=4,1&wxTemperature=&wxPressure=&wxAirDensity=' \
|
|
||||||
# f'&wxElevation=&wxWindSpeed='
|
|
||||||
# vlr_url = f'https://www.fangraphs.com/leaders/splits-leaderboards?splitArr=5&splitArrPitch=&position=P' \
|
|
||||||
# f'&autoPt=false&splitTeams=false&statType=player&statgroup=3' \
|
|
||||||
# f'&startDate={start_date.year}-{start_date.month}-{start_date.day}' \
|
|
||||||
# f'&endDate={end_date.year}-{end_date.month}-{end_date.day}' \
|
|
||||||
# f'&players=&filter=&groupBy=season&sort=4,1&wxTemperature=&wxPressure=&wxAirDensity=' \
|
|
||||||
# f'&wxElevation=&wxWindSpeed='
|
|
||||||
#
|
|
||||||
# soup = BeautifulSoup(requests.get(vrb_url).text, 'html.parser')
|
|
||||||
# time.sleep(3)
|
|
||||||
# table = soup.find('a', {'class': 'data-export'})
|
|
||||||
|
|
||||||
|
|
||||||
def get_pitching_peripherals(season: int):
|
def get_pitching_peripherals(season: int):
|
||||||
url = f'https://www.baseball-reference.com/leagues/majors/{season}-standard-pitching.shtml'
|
url = f'https://www.baseball-reference.com/leagues/majors/{season}-standard-pitching.shtml'
|
||||||
soup = BeautifulSoup(requests.get(url).text, 'html.parser')
|
soup = BeautifulSoup(requests.get(url).text, 'html.parser')
|
||||||
@ -1034,3 +962,13 @@ def sanitize_name(start_name: str) -> str:
|
|||||||
.replace("ú", "u")
|
.replace("ú", "u")
|
||||||
.replace("'", "")
|
.replace("'", "")
|
||||||
.replace('-', ' '))
|
.replace('-', ' '))
|
||||||
|
|
||||||
|
|
||||||
|
def get_hand(df_data):
|
||||||
|
if df_data['Name'][-1] == '*':
|
||||||
|
return 'L'
|
||||||
|
elif df_data['Name'][-1] == '#':
|
||||||
|
return 'S'
|
||||||
|
else:
|
||||||
|
return 'R'
|
||||||
|
|
||||||
|
|||||||
@ -301,13 +301,13 @@ def valid_error_ratings(err_num: int, position: str) -> int:
|
|||||||
def raw_error(errors: int, chances: int, season_pct: float, chance_max: int):
|
def raw_error(errors: int, chances: int, season_pct: float, chance_max: int):
|
||||||
if errors == 0 or chances == 0:
|
if errors == 0 or chances == 0:
|
||||||
return 0
|
return 0
|
||||||
# c_max = chance_max * season_pct
|
c_max = max(round(chance_max * season_pct), 1)
|
||||||
c_max = chance_max
|
# c_max = chance_max
|
||||||
return errors * c_max / chances
|
return errors * c_max / chances
|
||||||
|
|
||||||
|
|
||||||
def error_pitcher(errors: int, chances: int, season_pct: float):
|
def error_pitcher(errors: int, chances: int, season_pct: float):
|
||||||
return valid_error_ratings(int(raw_error(errors, chances, season_pct, 50)), 'p')
|
return valid_error_ratings(int(raw_error(errors, chances, season_pct, 300)), 'p')
|
||||||
|
|
||||||
|
|
||||||
def error_catcher(errors: int, chances: int, season_pct: float):
|
def error_catcher(errors: int, chances: int, season_pct: float):
|
||||||
@ -335,19 +335,19 @@ def error_outfield(errors: int, chances: int, season_pct: float):
|
|||||||
|
|
||||||
|
|
||||||
def get_any_error(pos_code: str, errors: int, chances: int, season_pct: float):
|
def get_any_error(pos_code: str, errors: int, chances: int, season_pct: float):
|
||||||
if pos_code == 'p':
|
if pos_code.lower() == 'p':
|
||||||
return error_pitcher(errors, chances, season_pct)
|
return error_pitcher(errors, chances, season_pct)
|
||||||
elif pos_code == 'c':
|
elif pos_code.lower() == 'c':
|
||||||
return error_catcher(errors, chances, season_pct)
|
return error_catcher(errors, chances, season_pct)
|
||||||
elif pos_code == '1b':
|
elif pos_code.lower() == '1b':
|
||||||
return error_first_base(errors, chances, season_pct)
|
return error_first_base(errors, chances, season_pct)
|
||||||
elif pos_code == '2b':
|
elif pos_code.lower() == '2b':
|
||||||
return error_second_base(errors, chances, season_pct)
|
return error_second_base(errors, chances, season_pct)
|
||||||
elif pos_code == '3b':
|
elif pos_code.lower() == '3b':
|
||||||
return error_third_base(errors, chances, season_pct)
|
return error_third_base(errors, chances, season_pct)
|
||||||
elif pos_code == 'ss':
|
elif pos_code.lower() == 'ss':
|
||||||
return error_shortstop(errors, chances, season_pct)
|
return error_shortstop(errors, chances, season_pct)
|
||||||
elif pos_code in ['lf', 'cf', 'rf', 'of']:
|
elif pos_code.lower() in ['lf', 'cf', 'rf', 'of']:
|
||||||
return error_outfield(errors, chances, season_pct)
|
return error_outfield(errors, chances, season_pct)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -1,19 +1,14 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
import datetime
|
import datetime
|
||||||
import logging
|
import logging
|
||||||
import urllib.parse
|
|
||||||
|
|
||||||
import batters.creation
|
import batters.creation
|
||||||
import defenders.calcs_defense
|
import pitchers.creation
|
||||||
from defenders import calcs_defense as cde
|
|
||||||
from pitchers import calcs_pitcher as cpi
|
|
||||||
import pandas as pd
|
import pandas as pd
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
from creation_helpers import pd_players_df, \
|
from creation_helpers import pd_players_df, pd_positions_df, get_args
|
||||||
get_pitching_stats, get_all_pybaseball_ids, pd_pitchingcards_df, pd_pitchingcardratings_df, pd_positions_df, \
|
from db_calls import db_get, db_patch, DB_URL
|
||||||
get_args, mlbteam_and_franchise, CLUB_LIST, FRANCHISE_LIST, sanitize_name
|
|
||||||
from db_calls import db_get, db_put, db_post, db_patch, DB_URL
|
|
||||||
|
|
||||||
date = f'{datetime.datetime.now().year}-{datetime.datetime.now().month}-{datetime.datetime.now().day}'
|
date = f'{datetime.datetime.now().year}-{datetime.datetime.now().month}-{datetime.datetime.now().day}'
|
||||||
log_level = logging.INFO
|
log_level = logging.INFO
|
||||||
@ -50,6 +45,7 @@ async def main(args):
|
|||||||
return
|
return
|
||||||
cardset = c_query['cardsets'][0]
|
cardset = c_query['cardsets'][0]
|
||||||
del c_query
|
del c_query
|
||||||
|
input_path = f'data-input/{cardset["name"]} Cardset/'
|
||||||
|
|
||||||
if 'season' in arg_data:
|
if 'season' in arg_data:
|
||||||
season = arg_data['season']
|
season = arg_data['season']
|
||||||
@ -71,454 +67,49 @@ async def main(args):
|
|||||||
player_description = f'{season}'
|
player_description = f'{season}'
|
||||||
|
|
||||||
post_batters = True if 'post_batters' not in arg_data or arg_data['post_batters'].lower() == 'true' else False
|
post_batters = True if 'post_batters' not in arg_data or arg_data['post_batters'].lower() == 'true' else False
|
||||||
|
post_pitchers = True if 'post_pitchers' not in arg_data or arg_data['post_pitchers'].lower() == 'true' else False
|
||||||
post_players = True if 'post_players' not in arg_data or arg_data['post_players'].lower() == 'true' else False
|
post_players = True if 'post_players' not in arg_data or arg_data['post_players'].lower() == 'true' else False
|
||||||
pull_fielding = True if 'pull_fielding' not in arg_data or arg_data['pull_fielding'].lower() == 'true' else False
|
pull_fielding = True if 'pull_fielding' not in arg_data or arg_data['pull_fielding'].lower() == 'true' else False
|
||||||
|
is_liveseries = True if 'is_liveseries' not in arg_data or arg_data['is_liveseries'].lower() == 'true' else False
|
||||||
|
|
||||||
start_time = datetime.datetime.now()
|
start_time = datetime.datetime.now()
|
||||||
release_directory = f'{start_time.year}-{start_time.month}-{start_time.day}'
|
release_directory = f'{start_time.year}-{start_time.month}-{start_time.day}'
|
||||||
input_path = f'data-input/{cardset["name"]} Cardset/'
|
|
||||||
|
|
||||||
print('Reading batting stats...')
|
# data = await batters.creation.run_batters(
|
||||||
all_batting = batters.creation.get_batting_stats(file_path=input_path)
|
# cardset, input_path, post_players, CARD_BASE_URL, release_directory, player_description, season_pct,
|
||||||
print(f'Processed {len(all_batting.values)} batters\n')
|
# post_batters, pull_fielding, season, is_liveseries
|
||||||
|
# )
|
||||||
print(f'Pulling PD player IDs...')
|
|
||||||
pd_players = await pd_players_df(cardset['id'])
|
|
||||||
bat_step1 = batters.creation.match_player_lines(all_batting, pd_players)
|
|
||||||
if post_players:
|
|
||||||
new_batters = await batters.creation.create_new_players(
|
|
||||||
bat_step1, cardset, CARD_BASE_URL, release_directory, player_description
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
new_batters = 0
|
|
||||||
offense_stats = batters.creation.get_offense_stats(bat_step1, input_path)
|
|
||||||
del bat_step1
|
|
||||||
|
|
||||||
offense_stats = await batters.creation.calculate_batting_cards(offense_stats, cardset, season_pct, post_batters)
|
|
||||||
await batters.creation.calculate_batting_ratings(offense_stats, post_batters)
|
|
||||||
|
|
||||||
print(f'Pulling pitcher defense...')
|
|
||||||
df_p = cde.get_bbref_fielding_df('p', season)
|
|
||||||
if pull_fielding:
|
|
||||||
print(f'Pulling catcher defense...')
|
|
||||||
df_c = cde.get_bbref_fielding_df('c', season)
|
|
||||||
print(f'Pulling first base defense...')
|
|
||||||
df_1b = cde.get_bbref_fielding_df('1b', season)
|
|
||||||
print(f'Pulling second base defense...')
|
|
||||||
df_2b = cde.get_bbref_fielding_df('2b', season)
|
|
||||||
print(f'Pulling third base defense...')
|
|
||||||
df_3b = cde.get_bbref_fielding_df('3b', season)
|
|
||||||
print(f'Pulling short stop defense...')
|
|
||||||
df_ss = cde.get_bbref_fielding_df('ss', season)
|
|
||||||
print(f'Pulling left field defense...')
|
|
||||||
df_lf = cde.get_bbref_fielding_df('lf', season)
|
|
||||||
print(f'Pulling center field defense...')
|
|
||||||
df_cf = cde.get_bbref_fielding_df('cf', season)
|
|
||||||
print(f'Pulling right field defense...')
|
|
||||||
df_rf = cde.get_bbref_fielding_df('rf', season)
|
|
||||||
print(f'Pulling outfield defense...')
|
|
||||||
df_of = cde.get_bbref_fielding_df('of', season)
|
|
||||||
print(f'Positions data is retrieved')
|
|
||||||
|
|
||||||
await defenders.calcs_defense.create_positions(
|
|
||||||
offense_stats, season_pct, post_batters, df_c, df_1b, df_2b, df_3b, df_ss, df_lf, df_cf, df_rf, df_of
|
|
||||||
)
|
|
||||||
|
|
||||||
is_liveseries = True if 'is_liveseries' not in arg_data or arg_data['is_liveseries'].lower() == 'true' else False
|
|
||||||
batter_updates = await batters.creation.post_player_updates(
|
|
||||||
cardset, CARD_BASE_URL, release_directory, player_description, is_liveseries, post_batters
|
|
||||||
)
|
|
||||||
|
|
||||||
print(f'Batter updates are complete')
|
print(f'Batter updates are complete')
|
||||||
start_time_two = datetime.datetime.now()
|
start_time_two = datetime.datetime.now()
|
||||||
run_time = start_time_two - start_time
|
run_time = start_time_two - start_time
|
||||||
print(f'Total batting cards: {len(offense_stats.index)}\nNew cardset batters: {new_batters}\n'
|
# print(f'Total batting cards: {data["tot_batters"]}\nNew cardset batters: {data["new_batters"]}\n'
|
||||||
f'Batter runtime: {round(run_time.total_seconds())} seconds\n')
|
# f'Batter runtime: {round(run_time.total_seconds())} seconds\n')
|
||||||
|
|
||||||
print('Reading pitching stats...')
|
data = await pitchers.creation.run_pitchers(
|
||||||
all_pitching = get_pitching_stats(file_path=input_path)
|
cardset, input_path, CARD_BASE_URL, season, release_directory, player_description, season_pct, post_players,
|
||||||
print(f'Processed {len(all_pitching.values)} pitchers\n')
|
post_pitchers, is_liveseries
|
||||||
|
|
||||||
def get_pids(df_data):
|
|
||||||
return get_all_pybaseball_ids([df_data["playerId"]], 'fangraphs')
|
|
||||||
|
|
||||||
print(f'Now pulling mlbam player IDs...')
|
|
||||||
ids_and_names = all_pitching.apply(get_pids, axis=1)
|
|
||||||
player_data = (ids_and_names
|
|
||||||
.merge(p_data, how='left', left_on='key_bbref', right_on='bbref_id')
|
|
||||||
.query('key_mlbam == key_mlbam')
|
|
||||||
.set_index('key_bbref', drop=False))
|
|
||||||
print(f'Matched mlbam to pd players.')
|
|
||||||
|
|
||||||
step_pitching = pd.merge(
|
|
||||||
player_data, all_pitching, left_on='key_fangraphs', right_on='playerId', sort=False
|
|
||||||
).set_index('key_bbref', drop=False)
|
|
||||||
final_pitching = step_pitching.join(df_p, rsuffix='_r')
|
|
||||||
|
|
||||||
new_players = []
|
|
||||||
|
|
||||||
def create_pitchers(df_data):
|
|
||||||
f_name = sanitize_name(df_data["name_first"]).title()
|
|
||||||
l_name = sanitize_name(df_data["name_last"]).title()
|
|
||||||
new_players.append({
|
|
||||||
'p_name': f'{f_name} {l_name}',
|
|
||||||
'cost': 99999,
|
|
||||||
'image': f'{CARD_BASE_URL}/{df_data["player_id"]}/'
|
|
||||||
f'pitchingcard{urllib.parse.quote("?d=")}{release_directory}',
|
|
||||||
'mlbclub': CLUB_LIST[df_data['Tm_vL']],
|
|
||||||
'franchise': FRANCHISE_LIST[df_data['Tm_vL']],
|
|
||||||
'cardset_id': cardset['id'],
|
|
||||||
'set_num': int(float(df_data['key_fangraphs'])),
|
|
||||||
'rarity_id': 99,
|
|
||||||
'pos_1': 'P',
|
|
||||||
'description': f'{player_description}',
|
|
||||||
'bbref_id': df_data.name,
|
|
||||||
'fangr_id': int(float(df_data['key_fangraphs'])),
|
|
||||||
'strat_code': int(float(df_data['key_mlbam']))
|
|
||||||
})
|
|
||||||
|
|
||||||
final_pitching[final_pitching['player_id'].isnull()].apply(create_pitchers, axis=1)
|
|
||||||
print(f'Creating {len(new_players)} new players...')
|
|
||||||
for x in new_players:
|
|
||||||
this_player = await db_post('players', payload=x)
|
|
||||||
final_pitching.at[x['bbref_id'], 'player_id'] = this_player['player_id']
|
|
||||||
final_pitching.at[x['bbref_id'], 'p_name'] = this_player['p_name']
|
|
||||||
del ids_and_names, all_pitching, p_data, step_pitching
|
|
||||||
print(f'Player IDs linked to pitching stats.\n{len(final_pitching.values)} players remain\n')
|
|
||||||
|
|
||||||
print(f'Reading pitching peripheral stats...')
|
|
||||||
pit_data = (pd.read_csv(f'{input_path}pitching.csv')
|
|
||||||
.drop_duplicates(subset=['Name-additional'], keep='first')
|
|
||||||
.set_index('Name-additional'))
|
|
||||||
pit_data['pitch_hand'] = pit_data.apply(get_hand, axis=1)
|
|
||||||
pitching_stats = final_pitching.join(pit_data, lsuffix='_l')
|
|
||||||
del final_pitching, pit_data
|
|
||||||
print(f'Stats are tallied\n{len(pitching_stats.values)} players remain\n')
|
|
||||||
|
|
||||||
pitching_cards = []
|
|
||||||
|
|
||||||
def create_pitching_card(df_data):
|
|
||||||
pow_data = cde.pow_ratings(float(df_data['Inn_def']), int(df_data['GS']), int(df_data['G']))
|
|
||||||
pitching_cards.append({
|
|
||||||
"player_id": int(float(df_data['player_id'])),
|
|
||||||
"key_bbref": df_data.name,
|
|
||||||
"key_fangraphs": int(float(df_data['key_fangraphs'])),
|
|
||||||
"key_mlbam": int(float(df_data['key_mlbam'])),
|
|
||||||
"key_retro": df_data['key_retro'],
|
|
||||||
"name_first": df_data["name_first"].title(),
|
|
||||||
"name_last": df_data["name_last"].title(),
|
|
||||||
"balk": cpi.balks(df_data['BK'], df_data['IP'], season_pct),
|
|
||||||
"wild_pitch": cpi.wild_pitches(df_data['WP'], df_data['IP'], season_pct),
|
|
||||||
"hold": cde.hold_pitcher(df_data['caught_stealing_perc'], int(df_data['pickoffs']), season_pct),
|
|
||||||
"starter_rating": pow_data[0],
|
|
||||||
"relief_rating": pow_data[1],
|
|
||||||
"closer_rating": cpi.closer_rating(int(df_data['GF']), int(df_data['SV']), int(df_data['G'])),
|
|
||||||
"hand": df_data['pitch_hand'],
|
|
||||||
"batting": f'#1W{df_data["pitch_hand"]}-C'
|
|
||||||
})
|
|
||||||
|
|
||||||
print(f'Calculating pitching cards...')
|
|
||||||
pitching_stats.apply(create_pitching_card, axis=1)
|
|
||||||
print(f'Cards are complete.\n\nPosting cards now...')
|
|
||||||
if 'post_pitchers' not in arg_data or arg_data['post_pitchers'].lower() == 'true':
|
|
||||||
resp = await db_put('pitchingcards', payload={'cards': pitching_cards}, timeout=30)
|
|
||||||
print(f'Response: {resp}\n\nMatching pitching card database IDs to player stats...')
|
|
||||||
# final_pitching_stats = pd.merge(
|
|
||||||
# pitching_stats, await pd_pitchingcards_df(cardset['id']), on='player_id')
|
|
||||||
# final_pitching_stats.set_index('key_bbref', drop=False, inplace=True)
|
|
||||||
# final_pitching_stats = final_pitching_stats.astype({'player_id': int})
|
|
||||||
|
|
||||||
pc_df = await pd_pitchingcards_df(cardset['id'])
|
|
||||||
pitching_stats = pitching_stats.merge(pc_df, how='left', on='player_id').set_index('key_bbref', drop=False)
|
|
||||||
|
|
||||||
pit_positions = []
|
|
||||||
|
|
||||||
def create_pit_position(df_data):
|
|
||||||
if df_data["key_bbref"] in df_p.index:
|
|
||||||
logging.debug(f'Running P stats for {df_data["p_name"]}')
|
|
||||||
pit_positions.append({
|
|
||||||
"player_id": int(df_data['player_id']),
|
|
||||||
"position": 'P',
|
|
||||||
"innings": float(df_p.at[df_data["key_bbref"], 'Inn_def']),
|
|
||||||
"range": cde.range_pitcher(
|
|
||||||
rs_value=int(df_p.at[df_data["key_bbref"], 'bis_runs_total']),
|
|
||||||
season_pct=season_pct
|
|
||||||
),
|
|
||||||
"error": cde.get_any_error(
|
|
||||||
pos_code='p',
|
|
||||||
errors=int(df_p.at[df_data["key_bbref"], 'E_def']),
|
|
||||||
chances=int(df_p.at[df_data["key_bbref"], 'chances']),
|
|
||||||
season_pct=season_pct
|
|
||||||
)
|
)
|
||||||
})
|
pitching_stats = data['pitching_stats']
|
||||||
else:
|
|
||||||
pit_positions.append({
|
|
||||||
"player_id": int(player_data.at[df_data["key_bbref"], 'player_id']),
|
|
||||||
"position": 'P',
|
|
||||||
"innings": 1,
|
|
||||||
"range": 5,
|
|
||||||
"error": 51
|
|
||||||
})
|
|
||||||
|
|
||||||
print(f'Calculating pitcher fielding lines now...')
|
|
||||||
pitching_stats.apply(create_pit_position, axis=1)
|
|
||||||
print(f'Fielding is complete.\n\nPosting positions now...')
|
|
||||||
if 'post_pitchers' not in arg_data or arg_data['post_pitchers'].lower() == 'true':
|
|
||||||
resp = await db_put('cardpositions', payload={'positions': pit_positions}, timeout=30)
|
|
||||||
print(f'Response: {resp}\n')
|
|
||||||
|
|
||||||
pitching_ratings = []
|
|
||||||
|
|
||||||
def create_pitching_card_ratings(df_data):
|
|
||||||
logging.info(f'Calculating pitching card ratings for {df_data.name}')
|
|
||||||
pitching_ratings.extend(cpi.get_pitcher_ratings(df_data))
|
|
||||||
|
|
||||||
print(f'Calculating card ratings...')
|
|
||||||
pitching_stats.apply(create_pitching_card_ratings, axis=1)
|
|
||||||
print(f'Ratings are complete\n\nPosting ratings now...')
|
|
||||||
if 'post_pitchers' not in arg_data or arg_data['post_pitchers'].lower() == 'true':
|
|
||||||
resp = await db_put('pitchingcardratings', payload={'ratings': pitching_ratings}, timeout=30)
|
|
||||||
print(f'Response: {resp}\n\nPulling all positions to set player positions...')
|
|
||||||
|
|
||||||
print(f'Pitcher updates are complete')
|
print(f'Pitcher updates are complete')
|
||||||
start_time_three = datetime.datetime.now()
|
start_time_three = datetime.datetime.now()
|
||||||
p_run_time = datetime.datetime.now() - start_time_two
|
p_run_time = datetime.datetime.now() - start_time_two
|
||||||
print(f'Total pitching cards: {len(pitching_cards)}\nNew cardset pitchers: {len(new_players)}\n'
|
print(f'Total pitching cards: {data["tot_pitchers"]}\nNew cardset pitchers: {data["new_pitchers"]}\n'
|
||||||
f'Pitcher runtime: {round(p_run_time.total_seconds())} seconds\n')
|
f'Pitcher runtime: {round(p_run_time.total_seconds())} seconds\n')
|
||||||
print(f'Checking for player updates...')
|
|
||||||
|
|
||||||
"""
|
|
||||||
Pull fresh pd_players and set_index to player_id
|
|
||||||
Pull fresh battingcards and set_index to player
|
|
||||||
Pull fresh battingcardratings one hand at a time and join on battingcard (suffixes _vl and vR)
|
|
||||||
|
|
||||||
Join battingcards (left) with battingcardratings (right) as total_ratings on id (left) and battingcard (right)
|
|
||||||
Join pd_players (left) with total_ratings (right) on indeces
|
|
||||||
Output: PD player list with batting card, ratings vL, and ratings vR
|
|
||||||
|
|
||||||
Calculate Total OPS as OPSvL + OPSvR + min(OPSvL, OPSvR) / 3 and assign rarity_id
|
|
||||||
For players with cost of 99999, set cost to <Rarity Base Cost> * Total OPS / <Rarity Avg OPS>
|
|
||||||
"""
|
|
||||||
|
|
||||||
def new_rarity_id(df_data):
|
|
||||||
if df_data['starter_rating'] > 3:
|
|
||||||
if df_data['total_OPS'] <= 0.4:
|
|
||||||
return 99
|
|
||||||
elif df_data['total_OPS'] <= 0.475:
|
|
||||||
return 1
|
|
||||||
elif df_data['total_OPS'] <= 0.53:
|
|
||||||
return 2
|
|
||||||
elif df_data['total_OPS'] <= 0.6:
|
|
||||||
return 3
|
|
||||||
elif df_data['total_OPS'] <= 0.675:
|
|
||||||
return 4
|
|
||||||
else:
|
|
||||||
return 5
|
|
||||||
else:
|
|
||||||
if df_data['total_OPS'] <= 0.325:
|
|
||||||
return 99
|
|
||||||
elif df_data['total_OPS'] <= 0.4:
|
|
||||||
return 1
|
|
||||||
elif df_data['total_OPS'] <= 0.475:
|
|
||||||
return 2
|
|
||||||
elif df_data['total_OPS'] <= 0.55:
|
|
||||||
return 3
|
|
||||||
elif df_data['total_OPS'] <= 0.625:
|
|
||||||
return 4
|
|
||||||
else:
|
|
||||||
return 5
|
|
||||||
p_data = await pd_players_df(cardset['id'])
|
|
||||||
p_data.set_index('player_id', drop=False)
|
|
||||||
total_ratings = pd.merge(
|
|
||||||
await pd_pitchingcards_df(cardset['id']),
|
|
||||||
await pd_pitchingcardratings_df(cardset['id']),
|
|
||||||
on='pitchingcard_id'
|
|
||||||
)
|
|
||||||
total_ratings['new_rarity_id'] = total_ratings.apply(new_rarity_id, axis=1)
|
|
||||||
|
|
||||||
player_data = pd.merge(
|
|
||||||
p_data,
|
|
||||||
total_ratings,
|
|
||||||
on='player_id'
|
|
||||||
).set_index('player_id', drop=False)
|
|
||||||
del total_ratings, pitching_stats
|
|
||||||
|
|
||||||
def get_pids(df_data):
|
|
||||||
return get_all_pybaseball_ids([df_data["bbref_id"]], 'bbref')
|
|
||||||
|
|
||||||
ids_and_names = player_data.apply(get_pids, axis=1)
|
|
||||||
player_data = (ids_and_names
|
|
||||||
.merge(player_data, how='left', left_on='key_bbref', right_on='bbref_id')
|
|
||||||
.query('key_mlbam == key_mlbam')
|
|
||||||
.set_index('key_bbref', drop=False))
|
|
||||||
|
|
||||||
player_updates = {} # { <player_id> : [ (param pairs) ] }
|
|
||||||
sp_rarity_group = player_data.query('rarity == new_rarity_id and starter_rating >= 4').groupby('rarity')
|
|
||||||
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
|
|
||||||
|
|
||||||
def get_player_updates(df_data):
|
|
||||||
base_costs = {
|
|
||||||
1: 810,
|
|
||||||
2: 270,
|
|
||||||
3: 90,
|
|
||||||
4: 30,
|
|
||||||
5: 10,
|
|
||||||
99: 2400
|
|
||||||
}
|
|
||||||
|
|
||||||
def avg_ops(rarity_id, starter_rating):
|
|
||||||
if starter_rating >= 4:
|
|
||||||
return sp_average_ops[rarity_id]
|
|
||||||
else:
|
|
||||||
return rp_average_ops[rarity_id]
|
|
||||||
|
|
||||||
params = []
|
|
||||||
|
|
||||||
if df_data['description'] != player_description:
|
|
||||||
params = [('description', f'{player_description}')]
|
|
||||||
|
|
||||||
if 'is_liveseries' in arg_data and arg_data['is_liveseries'].lower() == 'true':
|
|
||||||
team_data = mlbteam_and_franchise(int(float(df_data['key_mlbam'])))
|
|
||||||
|
|
||||||
if df_data['mlbclub'] != team_data['mlbclub'] and team_data['mlbclub'] is not None:
|
|
||||||
params.extend([('mlbclub', team_data['mlbclub'])])
|
|
||||||
if df_data['franchise'] != team_data['franchise'] and team_data['franchise'] is not None:
|
|
||||||
params.extend([('franchise', team_data['franchise'])])
|
|
||||||
|
|
||||||
# if release_directory not in df_data['image']:
|
|
||||||
params.extend([('image', f'{CARD_BASE_URL}/{df_data["player_id"]}/pitchingcard'
|
|
||||||
f'{urllib.parse.quote("?d=")}{release_directory}')])
|
|
||||||
|
|
||||||
if df_data['cost'] == 99999:
|
|
||||||
params.extend([
|
|
||||||
('cost',
|
|
||||||
round(base_costs[df_data['new_rarity_id']] * df_data['total_OPS'] /
|
|
||||||
avg_ops(df_data['new_rarity_id'], df_data['starter_rating']))),
|
|
||||||
('rarity_id', df_data['new_rarity_id'])
|
|
||||||
])
|
|
||||||
|
|
||||||
elif df_data['rarity'] != df_data['new_rarity_id']:
|
|
||||||
old_rarity = df_data['rarity']
|
|
||||||
new_rarity = df_data['new_rarity_id']
|
|
||||||
old_cost = df_data['cost']
|
|
||||||
new_cost = 0
|
|
||||||
|
|
||||||
if old_rarity == 1:
|
|
||||||
if new_rarity == 2:
|
|
||||||
new_cost = max(old_cost - 540, 100)
|
|
||||||
elif new_rarity == 3:
|
|
||||||
new_cost = max(old_cost - 720, 50)
|
|
||||||
elif new_rarity == 4:
|
|
||||||
new_cost = max(old_cost - 780, 15)
|
|
||||||
elif new_rarity == 5:
|
|
||||||
new_cost = max(old_cost - 800, 5)
|
|
||||||
elif new_rarity == 99:
|
|
||||||
new_cost = old_cost + 1600
|
|
||||||
elif old_rarity == 2:
|
|
||||||
if new_rarity == 1:
|
|
||||||
new_cost = old_cost + 540
|
|
||||||
elif new_rarity == 3:
|
|
||||||
new_cost = max(old_cost - 180, 50)
|
|
||||||
elif new_rarity == 4:
|
|
||||||
new_cost = max(old_cost - 240, 15)
|
|
||||||
elif new_rarity == 5:
|
|
||||||
new_cost = max(old_cost - 260, 5)
|
|
||||||
elif new_rarity == 99:
|
|
||||||
new_cost = old_cost + 2140
|
|
||||||
elif old_rarity == 3:
|
|
||||||
if new_rarity == 1:
|
|
||||||
new_cost = old_cost + 720
|
|
||||||
elif new_rarity == 2:
|
|
||||||
new_cost = old_cost + 180
|
|
||||||
elif new_rarity == 4:
|
|
||||||
new_cost = max(old_cost - 60, 15)
|
|
||||||
elif new_rarity == 5:
|
|
||||||
new_cost = max(old_cost - 80, 5)
|
|
||||||
elif new_rarity == 99:
|
|
||||||
new_cost = old_cost + 2320
|
|
||||||
elif old_rarity == 4:
|
|
||||||
if new_rarity == 1:
|
|
||||||
new_cost = old_cost + 780
|
|
||||||
elif new_rarity == 2:
|
|
||||||
new_cost = old_cost + 240
|
|
||||||
elif new_rarity == 3:
|
|
||||||
new_cost = old_cost + 60
|
|
||||||
elif new_rarity == 5:
|
|
||||||
new_cost = max(old_cost - 20, 5)
|
|
||||||
elif new_rarity == 99:
|
|
||||||
new_cost = old_cost + 2380
|
|
||||||
elif old_rarity == 5:
|
|
||||||
if new_rarity == 1:
|
|
||||||
new_cost = old_cost + 800
|
|
||||||
elif new_rarity == 2:
|
|
||||||
new_cost = old_cost + 260
|
|
||||||
elif new_rarity == 3:
|
|
||||||
new_cost = old_cost + 80
|
|
||||||
elif new_rarity == 4:
|
|
||||||
new_cost = old_cost + 20
|
|
||||||
elif new_rarity == 99:
|
|
||||||
new_cost = old_cost + 2400
|
|
||||||
elif old_rarity == 99:
|
|
||||||
if new_rarity == 1:
|
|
||||||
new_cost = max(old_cost - 1600, 800)
|
|
||||||
elif new_rarity == 2:
|
|
||||||
new_cost = max(old_cost - 2140, 100)
|
|
||||||
elif new_rarity == 3:
|
|
||||||
new_cost = max(old_cost - 2320, 50)
|
|
||||||
elif new_rarity == 4:
|
|
||||||
new_cost = max(old_cost - 2380, 15)
|
|
||||||
elif new_rarity == 5:
|
|
||||||
new_cost = max(old_cost - 2400, 5)
|
|
||||||
|
|
||||||
if new_cost != 0:
|
|
||||||
params.extend([('cost', new_cost), ('rarity_id', new_rarity)])
|
|
||||||
|
|
||||||
if len(params) > 0:
|
|
||||||
if df_data.player_id not in player_updates.keys():
|
|
||||||
player_updates[df_data.player_id] = params
|
|
||||||
else:
|
|
||||||
player_updates[df_data.player_id].extend(params)
|
|
||||||
|
|
||||||
player_data.apply(get_player_updates, axis=1)
|
|
||||||
|
|
||||||
print(f'Running player position updates..')
|
print(f'Running player position updates..')
|
||||||
all_pos = await pd_positions_df(cardset['id'])
|
all_pos = await pd_positions_df(cardset['id'])
|
||||||
|
|
||||||
|
player_updates = {}
|
||||||
|
|
||||||
def set_all_positions(df_data):
|
def set_all_positions(df_data):
|
||||||
pos_series = all_pos.query(f'player_id == {df_data["player_id"]}')['position']
|
pos_series = all_pos.query(f'player_id == {df_data["player_id"]}')['position']
|
||||||
pos_updates = []
|
pos_updates = []
|
||||||
count = 1
|
count = 1
|
||||||
for this_pos in pos_series:
|
for this_pos in pos_series:
|
||||||
if this_pos == 'P':
|
if this_pos == 'P':
|
||||||
this_pitcher = player_data.loc[df_data['bbref_id']]
|
this_pitcher = pitching_stats.loc[df_data['bbref_id']]
|
||||||
if this_pitcher['starter_rating'] > 3:
|
if this_pitcher['starter_rating'] > 3:
|
||||||
pos_updates.append((f'pos_{count}', 'SP'))
|
pos_updates.append((f'pos_{count}', 'SP'))
|
||||||
count += 1
|
count += 1
|
||||||
@ -550,13 +141,11 @@ async def main(args):
|
|||||||
else:
|
else:
|
||||||
player_updates[df_data.player_id].extend(pos_updates)
|
player_updates[df_data.player_id].extend(pos_updates)
|
||||||
|
|
||||||
p_data.apply(set_all_positions, axis=1)
|
all_players = await pd_players_df(cardset['id'])
|
||||||
# Get all positions from each player in p_data and send position updates
|
all_players.apply(set_all_positions, axis=1)
|
||||||
# Consider combining all player updates into one master call to keep from updating each player twice
|
|
||||||
# (once in batter/pitcher and then here)
|
|
||||||
|
|
||||||
print(f'Sending {len(player_updates)} player updates to PD database...')
|
print(f'Sending {len(player_updates)} player updates to PD database...')
|
||||||
if 'post_players' not in arg_data or arg_data['post_players'].lower() == 'true':
|
if post_players:
|
||||||
for x in player_updates:
|
for x in player_updates:
|
||||||
await db_patch('players', object_id=x, params=player_updates[x])
|
await db_patch('players', object_id=x, params=player_updates[x])
|
||||||
print(f'Player updates are complete\n')
|
print(f'Player updates are complete\n')
|
||||||
|
|||||||
@ -0,0 +1,506 @@
|
|||||||
|
import logging
|
||||||
|
import datetime
|
||||||
|
import urllib.parse
|
||||||
|
import pandas as pd
|
||||||
|
|
||||||
|
from creation_helpers import get_all_pybaseball_ids, sanitize_name, CLUB_LIST, FRANCHISE_LIST, pd_players_df, \
|
||||||
|
mlbteam_and_franchise
|
||||||
|
from db_calls import db_post, db_get, db_put, db_patch
|
||||||
|
from defenders import calcs_defense as cde
|
||||||
|
from . import calcs_pitcher as cpi
|
||||||
|
|
||||||
|
|
||||||
|
def get_pitching_stats(file_path: str = None, start_date: datetime.datetime = None, end_date: datetime.datetime = None):
|
||||||
|
print('Reading pitching stats...')
|
||||||
|
if file_path is not None:
|
||||||
|
vl_basic = pd.read_csv(f'{file_path}vlhh-basic.csv').query('TBF >= 20')
|
||||||
|
vr_basic = pd.read_csv(f'{file_path}vrhh-basic.csv').query('TBF >= 40')
|
||||||
|
total_basic = pd.merge(vl_basic, vr_basic, on="playerId", suffixes=('_vL', '_vR'))
|
||||||
|
|
||||||
|
vl_rate = pd.read_csv(f'{file_path}vlhh-rate.csv').query('TBF >= 20')
|
||||||
|
vr_rate = pd.read_csv(f'{file_path}vrhh-rate.csv').query('TBF >= 40')
|
||||||
|
total_rate = pd.merge(vl_rate, vr_rate, on="playerId", suffixes=('_vL', '_vR'))
|
||||||
|
|
||||||
|
return pd.merge(total_basic, total_rate, on="playerId", suffixes=('', '_rate'))
|
||||||
|
|
||||||
|
else:
|
||||||
|
raise LookupError(f'Date-based stat pulls not implemented, yet. Please provide batting csv files.')
|
||||||
|
# vrb_url = f'https://www.fangraphs.com/leaders/splits-leaderboards?splitArr=6&splitArrPitch=&position=P' \
|
||||||
|
# f'&autoPt=false&splitTeams=false&statType=player&statgroup=1' \
|
||||||
|
# f'&startDate={start_date.year}-{start_date.month}-{start_date.day}' \
|
||||||
|
# f'&endDate={end_date.year}-{end_date.month}-{end_date.day}' \
|
||||||
|
# f'&players=&filter=&groupBy=season&sort=4,1&wxTemperature=&wxPressure=&wxAirDensity=' \
|
||||||
|
# f'&wxElevation=&wxWindSpeed='
|
||||||
|
# vrr_url = f'https://www.fangraphs.com/leaders/splits-leaderboards?splitArr=6&splitArrPitch=&position=P' \
|
||||||
|
# f'&autoPt=false&splitTeams=false&statType=player&statgroup=3' \
|
||||||
|
# f'&startDate={start_date.year}-{start_date.month}-{start_date.day}' \
|
||||||
|
# f'&endDate={end_date.year}-{end_date.month}-{end_date.day}' \
|
||||||
|
# f'&players=&filter=&groupBy=season&sort=4,1&wxTemperature=&wxPressure=&wxAirDensity=' \
|
||||||
|
# f'&wxElevation=&wxWindSpeed='
|
||||||
|
# vlb_url = f'https://www.fangraphs.com/leaders/splits-leaderboards?splitArr=5&splitArrPitch=&position=P' \
|
||||||
|
# f'&autoPt=false&splitTeams=false&statType=player&statgroup=1' \
|
||||||
|
# f'&startDate={start_date.year}-{start_date.month}-{start_date.day}' \
|
||||||
|
# f'&endDate={end_date.year}-{end_date.month}-{end_date.day}' \
|
||||||
|
# f'&players=&filter=&groupBy=season&sort=4,1&wxTemperature=&wxPressure=&wxAirDensity=' \
|
||||||
|
# f'&wxElevation=&wxWindSpeed='
|
||||||
|
# vlr_url = f'https://www.fangraphs.com/leaders/splits-leaderboards?splitArr=5&splitArrPitch=&position=P' \
|
||||||
|
# f'&autoPt=false&splitTeams=false&statType=player&statgroup=3' \
|
||||||
|
# f'&startDate={start_date.year}-{start_date.month}-{start_date.day}' \
|
||||||
|
# f'&endDate={end_date.year}-{end_date.month}-{end_date.day}' \
|
||||||
|
# f'&players=&filter=&groupBy=season&sort=4,1&wxTemperature=&wxPressure=&wxAirDensity=' \
|
||||||
|
# f'&wxElevation=&wxWindSpeed='
|
||||||
|
#
|
||||||
|
# soup = BeautifulSoup(requests.get(vrb_url).text, 'html.parser')
|
||||||
|
# time.sleep(3)
|
||||||
|
# table = soup.find('a', {'class': 'data-export'})
|
||||||
|
|
||||||
|
|
||||||
|
async def pd_pitchingcards_df(cardset_id: int):
|
||||||
|
bc_query = await db_get('pitchingcards', params=[('cardset_id', cardset_id), ('short_output', True)])
|
||||||
|
if bc_query['count'] == 0:
|
||||||
|
raise ValueError(f'No pitching cards returned from Paper Dynasty API')
|
||||||
|
return pd.DataFrame(bc_query['cards']).rename(columns={'id': 'pitchingcard_id', 'player': 'player_id'})
|
||||||
|
|
||||||
|
|
||||||
|
async def pd_pitchingcardratings_df(cardset_id: int):
|
||||||
|
vl_query = await db_get(
|
||||||
|
'pitchingcardratings', params=[('cardset_id', cardset_id), ('vs_hand', 'L'), ('short_output', True)])
|
||||||
|
vr_query = await db_get(
|
||||||
|
'pitchingcardratings', params=[('cardset_id', cardset_id), ('vs_hand', 'R'), ('short_output', True)])
|
||||||
|
if 0 in [vl_query['count'], vr_query['count']]:
|
||||||
|
raise ValueError(f'No pitching card ratings returned from Paper Dynasty API')
|
||||||
|
vl = pd.DataFrame(vl_query['ratings'])
|
||||||
|
vr = pd.DataFrame(vr_query['ratings'])
|
||||||
|
ratings = (pd.merge(vl, vr, on='pitchingcard', suffixes=('_vL', '_vR'))
|
||||||
|
.rename(columns={'pitchingcard': 'pitchingcard_id'}))
|
||||||
|
|
||||||
|
def get_total_ops(df_data):
|
||||||
|
ops_vl = df_data['obp_vL'] + df_data['slg_vL']
|
||||||
|
ops_vr = df_data['obp_vR'] + df_data['slg_vR']
|
||||||
|
return (ops_vr + ops_vl + max(ops_vl, ops_vr)) / 3
|
||||||
|
ratings['total_OPS'] = ratings.apply(get_total_ops, axis=1)
|
||||||
|
|
||||||
|
return ratings
|
||||||
|
|
||||||
|
|
||||||
|
def match_player_lines(all_pitching: pd.DataFrame, all_players: pd.DataFrame, df_p: pd.DataFrame):
|
||||||
|
def get_pids(df_data):
|
||||||
|
return get_all_pybaseball_ids([df_data["playerId"]], 'fangraphs')
|
||||||
|
|
||||||
|
print(f'Now pulling mlbam player IDs...')
|
||||||
|
ids_and_names = all_pitching.apply(get_pids, axis=1)
|
||||||
|
player_data = (ids_and_names
|
||||||
|
.merge(all_players, how='left', left_on='key_bbref', right_on='bbref_id')
|
||||||
|
.query('key_mlbam == key_mlbam')
|
||||||
|
.set_index('key_bbref', drop=False))
|
||||||
|
print(f'Matched mlbam to pd players.')
|
||||||
|
|
||||||
|
step_pitching = pd.merge(
|
||||||
|
player_data, all_pitching, left_on='key_fangraphs', right_on='playerId', sort=False
|
||||||
|
).set_index('key_bbref', drop=False)
|
||||||
|
final_pitching = step_pitching.join(df_p, rsuffix='_r')
|
||||||
|
|
||||||
|
return final_pitching
|
||||||
|
|
||||||
|
|
||||||
|
async def create_new_players(
|
||||||
|
final_pitching: pd.DataFrame, cardset: dict, card_base_url: str, release_dir: str, player_desc: str):
|
||||||
|
new_players = []
|
||||||
|
|
||||||
|
def create_pitchers(df_data):
|
||||||
|
f_name = sanitize_name(df_data["name_first"]).title()
|
||||||
|
l_name = sanitize_name(df_data["name_last"]).title()
|
||||||
|
new_players.append({
|
||||||
|
'p_name': f'{f_name} {l_name}',
|
||||||
|
'cost': 99999,
|
||||||
|
'image': f'{card_base_url}/{df_data["player_id"]}/'
|
||||||
|
f'pitchingcard{urllib.parse.quote("?d=")}{release_dir}',
|
||||||
|
'mlbclub': CLUB_LIST[df_data['Tm_vL']],
|
||||||
|
'franchise': FRANCHISE_LIST[df_data['Tm_vL']],
|
||||||
|
'cardset_id': cardset['id'],
|
||||||
|
'set_num': int(float(df_data['key_fangraphs'])),
|
||||||
|
'rarity_id': 99,
|
||||||
|
'pos_1': 'P',
|
||||||
|
'description': f'{player_desc}',
|
||||||
|
'bbref_id': df_data.name,
|
||||||
|
'fangr_id': int(float(df_data['key_fangraphs'])),
|
||||||
|
'strat_code': int(float(df_data['key_mlbam']))
|
||||||
|
})
|
||||||
|
|
||||||
|
final_pitching[final_pitching['player_id'].isnull()].apply(create_pitchers, axis=1)
|
||||||
|
print(f'Creating {len(new_players)} new players...')
|
||||||
|
for x in new_players:
|
||||||
|
this_player = await db_post('players', payload=x)
|
||||||
|
final_pitching.at[x['bbref_id'], 'player_id'] = this_player['player_id']
|
||||||
|
final_pitching.at[x['bbref_id'], 'p_name'] = this_player['p_name']
|
||||||
|
print(f'Player IDs linked to pitching stats.\n{len(final_pitching.values)} players remain\n')
|
||||||
|
|
||||||
|
return len(new_players)
|
||||||
|
|
||||||
|
|
||||||
|
def get_stat_df(input_path: str, final_pitching: pd.DataFrame):
|
||||||
|
def get_hand(df_data):
|
||||||
|
if df_data['Name'][-1] == '*':
|
||||||
|
return 'L'
|
||||||
|
else:
|
||||||
|
return 'R'
|
||||||
|
|
||||||
|
print(f'Reading pitching peripheral stats...')
|
||||||
|
pit_data = (pd.read_csv(f'{input_path}pitching.csv')
|
||||||
|
.drop_duplicates(subset=['Name-additional'], keep='first')
|
||||||
|
.set_index('Name-additional'))
|
||||||
|
pit_data['pitch_hand'] = pit_data.apply(get_hand, axis=1)
|
||||||
|
pitching_stats = final_pitching.join(pit_data, lsuffix='_l')
|
||||||
|
print(f'Stats are tallied\n{len(pitching_stats.values)} players remain\n')
|
||||||
|
|
||||||
|
return pitching_stats
|
||||||
|
|
||||||
|
|
||||||
|
async def calculate_pitching_cards(pitching_stats: pd.DataFrame, cardset: dict, season_pct: float, post_pitchers: bool):
|
||||||
|
pitching_cards = []
|
||||||
|
|
||||||
|
def create_pitching_card(df_data):
|
||||||
|
pow_data = cde.pow_ratings(float(df_data['Inn_def']), int(df_data['GS']), int(df_data['G']))
|
||||||
|
pitching_cards.append({
|
||||||
|
"player_id": int(float(df_data['player_id'])),
|
||||||
|
"key_bbref": df_data.name,
|
||||||
|
"key_fangraphs": int(float(df_data['key_fangraphs'])),
|
||||||
|
"key_mlbam": int(float(df_data['key_mlbam'])),
|
||||||
|
"key_retro": df_data['key_retro'],
|
||||||
|
"name_first": df_data["name_first"].title(),
|
||||||
|
"name_last": df_data["name_last"].title(),
|
||||||
|
"balk": cpi.balks(df_data['BK'], df_data['IP'], season_pct),
|
||||||
|
"wild_pitch": cpi.wild_pitches(df_data['WP'], df_data['IP'], season_pct),
|
||||||
|
"hold": cde.hold_pitcher(df_data['caught_stealing_perc'], int(df_data['pickoffs']), season_pct),
|
||||||
|
"starter_rating": pow_data[0],
|
||||||
|
"relief_rating": pow_data[1],
|
||||||
|
"closer_rating": cpi.closer_rating(int(df_data['GF']), int(df_data['SV']), int(df_data['G'])),
|
||||||
|
"hand": df_data['pitch_hand'],
|
||||||
|
"batting": f'#1W{df_data["pitch_hand"]}-C'
|
||||||
|
})
|
||||||
|
|
||||||
|
print(f'Calculating pitching cards...')
|
||||||
|
pitching_stats.apply(create_pitching_card, axis=1)
|
||||||
|
print(f'Cards are complete.\n\nPosting cards now...')
|
||||||
|
if post_pitchers:
|
||||||
|
resp = await db_put('pitchingcards', payload={'cards': pitching_cards}, timeout=30)
|
||||||
|
print(f'Response: {resp}\n\nMatching pitching card database IDs to player stats...')
|
||||||
|
|
||||||
|
pc_df = await pd_pitchingcards_df(cardset['id'])
|
||||||
|
pitching_stats = pitching_stats.merge(pc_df, how='left', on='player_id').set_index('key_bbref', drop=False)
|
||||||
|
|
||||||
|
return pitching_stats
|
||||||
|
|
||||||
|
|
||||||
|
async def create_position(season_pct: float, pitching_stats: pd.DataFrame, post_pitchers: bool, df_p: pd.DataFrame):
|
||||||
|
pit_positions = []
|
||||||
|
|
||||||
|
def create_pit_position(df_data):
|
||||||
|
if df_data["key_bbref"] in df_p.index:
|
||||||
|
logging.debug(f'Running P stats for {df_data["p_name"]}')
|
||||||
|
pit_positions.append({
|
||||||
|
"player_id": int(df_data['player_id']),
|
||||||
|
"position": 'P',
|
||||||
|
"innings": float(df_p.at[df_data["key_bbref"], 'Inn_def']),
|
||||||
|
"range": cde.range_pitcher(
|
||||||
|
rs_value=int(df_p.at[df_data["key_bbref"], 'bis_runs_total']),
|
||||||
|
season_pct=season_pct
|
||||||
|
),
|
||||||
|
"error": cde.get_any_error(
|
||||||
|
pos_code='p',
|
||||||
|
errors=int(df_p.at[df_data["key_bbref"], 'E_def']),
|
||||||
|
chances=int(df_p.at[df_data["key_bbref"], 'chances']),
|
||||||
|
season_pct=season_pct
|
||||||
|
)
|
||||||
|
})
|
||||||
|
else:
|
||||||
|
pit_positions.append({
|
||||||
|
"player_id": int(df_data['key_bbref']),
|
||||||
|
"position": 'P',
|
||||||
|
"innings": 1,
|
||||||
|
"range": 5,
|
||||||
|
"error": 51
|
||||||
|
})
|
||||||
|
|
||||||
|
print(f'Calculating pitcher fielding lines now...')
|
||||||
|
pitching_stats.apply(create_pit_position, axis=1)
|
||||||
|
print(f'Fielding is complete.\n\nPosting positions now...')
|
||||||
|
if post_pitchers:
|
||||||
|
resp = await db_put('cardpositions', payload={'positions': pit_positions}, timeout=30)
|
||||||
|
print(f'Response: {resp}\n')
|
||||||
|
|
||||||
|
|
||||||
|
async def calculate_pitcher_ratings(pitching_stats: pd.DataFrame, post_pitchers: bool):
|
||||||
|
pitching_ratings = []
|
||||||
|
|
||||||
|
def create_pitching_card_ratings(df_data):
|
||||||
|
logging.info(f'Calculating pitching card ratings for {df_data.name}')
|
||||||
|
pitching_ratings.extend(cpi.get_pitcher_ratings(df_data))
|
||||||
|
|
||||||
|
print(f'Calculating card ratings...')
|
||||||
|
pitching_stats.apply(create_pitching_card_ratings, axis=1)
|
||||||
|
print(f'Ratings are complete\n\nPosting ratings now...')
|
||||||
|
if post_pitchers:
|
||||||
|
resp = await db_put('pitchingcardratings', payload={'ratings': pitching_ratings}, timeout=30)
|
||||||
|
print(f'Response: {resp}\n\nPulling all positions to set player positions...')
|
||||||
|
|
||||||
|
|
||||||
|
async def post_player_updates(
|
||||||
|
cardset: dict, player_description: str, card_base_url: str, release_dir: str, is_liveseries: bool,
|
||||||
|
post_players: bool):
|
||||||
|
def new_rarity_id(df_data):
|
||||||
|
if df_data['starter_rating'] > 3:
|
||||||
|
if df_data['total_OPS'] <= 0.4:
|
||||||
|
return 99
|
||||||
|
elif df_data['total_OPS'] <= 0.475:
|
||||||
|
return 1
|
||||||
|
elif df_data['total_OPS'] <= 0.53:
|
||||||
|
return 2
|
||||||
|
elif df_data['total_OPS'] <= 0.6:
|
||||||
|
return 3
|
||||||
|
elif df_data['total_OPS'] <= 0.675:
|
||||||
|
return 4
|
||||||
|
else:
|
||||||
|
return 5
|
||||||
|
else:
|
||||||
|
if df_data['total_OPS'] <= 0.325:
|
||||||
|
return 99
|
||||||
|
elif df_data['total_OPS'] <= 0.4:
|
||||||
|
return 1
|
||||||
|
elif df_data['total_OPS'] <= 0.475:
|
||||||
|
return 2
|
||||||
|
elif df_data['total_OPS'] <= 0.55:
|
||||||
|
return 3
|
||||||
|
elif df_data['total_OPS'] <= 0.625:
|
||||||
|
return 4
|
||||||
|
else:
|
||||||
|
return 5
|
||||||
|
|
||||||
|
p_data = await pd_players_df(cardset['id'])
|
||||||
|
p_data.set_index('player_id', drop=False)
|
||||||
|
total_ratings = pd.merge(
|
||||||
|
await pd_pitchingcards_df(cardset['id']),
|
||||||
|
await pd_pitchingcardratings_df(cardset['id']),
|
||||||
|
on='pitchingcard_id'
|
||||||
|
)
|
||||||
|
total_ratings['new_rarity_id'] = total_ratings.apply(new_rarity_id, axis=1)
|
||||||
|
|
||||||
|
player_data = pd.merge(
|
||||||
|
p_data,
|
||||||
|
total_ratings,
|
||||||
|
on='player_id'
|
||||||
|
).set_index('player_id', drop=False)
|
||||||
|
del total_ratings
|
||||||
|
|
||||||
|
def get_pids(df_data):
|
||||||
|
return get_all_pybaseball_ids([df_data["bbref_id"]], 'bbref')
|
||||||
|
|
||||||
|
ids_and_names = player_data.apply(get_pids, axis=1)
|
||||||
|
player_data = (ids_and_names
|
||||||
|
.merge(player_data, how='left', left_on='key_bbref', right_on='bbref_id')
|
||||||
|
.query('key_mlbam == key_mlbam')
|
||||||
|
.set_index('key_bbref', drop=False))
|
||||||
|
|
||||||
|
player_updates = {} # { <player_id> : [ (param pairs) ] }
|
||||||
|
sp_rarity_group = player_data.query('rarity == new_rarity_id and starter_rating >= 4').groupby('rarity')
|
||||||
|
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
|
||||||
|
|
||||||
|
def get_player_updates(df_data):
|
||||||
|
base_costs = {
|
||||||
|
1: 810,
|
||||||
|
2: 270,
|
||||||
|
3: 90,
|
||||||
|
4: 30,
|
||||||
|
5: 10,
|
||||||
|
99: 2400
|
||||||
|
}
|
||||||
|
|
||||||
|
def avg_ops(rarity_id, starter_rating):
|
||||||
|
if starter_rating >= 4:
|
||||||
|
return sp_average_ops[rarity_id]
|
||||||
|
else:
|
||||||
|
return rp_average_ops[rarity_id]
|
||||||
|
|
||||||
|
params = []
|
||||||
|
|
||||||
|
if df_data['description'] != player_description:
|
||||||
|
params = [('description', f'{player_description}')]
|
||||||
|
|
||||||
|
if is_liveseries:
|
||||||
|
team_data = mlbteam_and_franchise(int(float(df_data['key_mlbam'])))
|
||||||
|
|
||||||
|
if df_data['mlbclub'] != team_data['mlbclub'] and team_data['mlbclub'] is not None:
|
||||||
|
params.extend([('mlbclub', team_data['mlbclub'])])
|
||||||
|
if df_data['franchise'] != team_data['franchise'] and team_data['franchise'] is not None:
|
||||||
|
params.extend([('franchise', team_data['franchise'])])
|
||||||
|
|
||||||
|
# if release_directory not in df_data['image']:
|
||||||
|
params.extend([('image', f'{card_base_url}/{df_data["player_id"]}/pitchingcard'
|
||||||
|
f'{urllib.parse.quote("?d=")}{release_dir}')])
|
||||||
|
|
||||||
|
if df_data['cost'] == 99999:
|
||||||
|
params.extend([
|
||||||
|
('cost',
|
||||||
|
round(base_costs[df_data['new_rarity_id']] * df_data['total_OPS'] /
|
||||||
|
avg_ops(df_data['new_rarity_id'], df_data['starter_rating']))),
|
||||||
|
('rarity_id', df_data['new_rarity_id'])
|
||||||
|
])
|
||||||
|
|
||||||
|
elif df_data['rarity'] != df_data['new_rarity_id']:
|
||||||
|
old_rarity = df_data['rarity']
|
||||||
|
new_rarity = df_data['new_rarity_id']
|
||||||
|
old_cost = df_data['cost']
|
||||||
|
new_cost = 0
|
||||||
|
|
||||||
|
if old_rarity == 1:
|
||||||
|
if new_rarity == 2:
|
||||||
|
new_cost = max(old_cost - 540, 100)
|
||||||
|
elif new_rarity == 3:
|
||||||
|
new_cost = max(old_cost - 720, 50)
|
||||||
|
elif new_rarity == 4:
|
||||||
|
new_cost = max(old_cost - 780, 15)
|
||||||
|
elif new_rarity == 5:
|
||||||
|
new_cost = max(old_cost - 800, 5)
|
||||||
|
elif new_rarity == 99:
|
||||||
|
new_cost = old_cost + 1600
|
||||||
|
elif old_rarity == 2:
|
||||||
|
if new_rarity == 1:
|
||||||
|
new_cost = old_cost + 540
|
||||||
|
elif new_rarity == 3:
|
||||||
|
new_cost = max(old_cost - 180, 50)
|
||||||
|
elif new_rarity == 4:
|
||||||
|
new_cost = max(old_cost - 240, 15)
|
||||||
|
elif new_rarity == 5:
|
||||||
|
new_cost = max(old_cost - 260, 5)
|
||||||
|
elif new_rarity == 99:
|
||||||
|
new_cost = old_cost + 2140
|
||||||
|
elif old_rarity == 3:
|
||||||
|
if new_rarity == 1:
|
||||||
|
new_cost = old_cost + 720
|
||||||
|
elif new_rarity == 2:
|
||||||
|
new_cost = old_cost + 180
|
||||||
|
elif new_rarity == 4:
|
||||||
|
new_cost = max(old_cost - 60, 15)
|
||||||
|
elif new_rarity == 5:
|
||||||
|
new_cost = max(old_cost - 80, 5)
|
||||||
|
elif new_rarity == 99:
|
||||||
|
new_cost = old_cost + 2320
|
||||||
|
elif old_rarity == 4:
|
||||||
|
if new_rarity == 1:
|
||||||
|
new_cost = old_cost + 780
|
||||||
|
elif new_rarity == 2:
|
||||||
|
new_cost = old_cost + 240
|
||||||
|
elif new_rarity == 3:
|
||||||
|
new_cost = old_cost + 60
|
||||||
|
elif new_rarity == 5:
|
||||||
|
new_cost = max(old_cost - 20, 5)
|
||||||
|
elif new_rarity == 99:
|
||||||
|
new_cost = old_cost + 2380
|
||||||
|
elif old_rarity == 5:
|
||||||
|
if new_rarity == 1:
|
||||||
|
new_cost = old_cost + 800
|
||||||
|
elif new_rarity == 2:
|
||||||
|
new_cost = old_cost + 260
|
||||||
|
elif new_rarity == 3:
|
||||||
|
new_cost = old_cost + 80
|
||||||
|
elif new_rarity == 4:
|
||||||
|
new_cost = old_cost + 20
|
||||||
|
elif new_rarity == 99:
|
||||||
|
new_cost = old_cost + 2400
|
||||||
|
elif old_rarity == 99:
|
||||||
|
if new_rarity == 1:
|
||||||
|
new_cost = max(old_cost - 1600, 800)
|
||||||
|
elif new_rarity == 2:
|
||||||
|
new_cost = max(old_cost - 2140, 100)
|
||||||
|
elif new_rarity == 3:
|
||||||
|
new_cost = max(old_cost - 2320, 50)
|
||||||
|
elif new_rarity == 4:
|
||||||
|
new_cost = max(old_cost - 2380, 15)
|
||||||
|
elif new_rarity == 5:
|
||||||
|
new_cost = max(old_cost - 2400, 5)
|
||||||
|
|
||||||
|
if new_cost != 0:
|
||||||
|
params.extend([('cost', new_cost), ('rarity_id', new_rarity)])
|
||||||
|
|
||||||
|
if len(params) > 0:
|
||||||
|
if df_data.player_id not in player_updates.keys():
|
||||||
|
player_updates[df_data.player_id] = params
|
||||||
|
else:
|
||||||
|
player_updates[df_data.player_id].extend(params)
|
||||||
|
|
||||||
|
player_data.apply(get_player_updates, axis=1)
|
||||||
|
|
||||||
|
print(f'Sending {len(player_updates)} player updates to PD database...')
|
||||||
|
if post_players:
|
||||||
|
for x in player_updates:
|
||||||
|
await db_patch('players', object_id=x, params=player_updates[x])
|
||||||
|
|
||||||
|
return len(player_updates)
|
||||||
|
|
||||||
|
|
||||||
|
async def run_pitchers(
|
||||||
|
cardset: dict, input_path: str, card_base_url: str, season: int, release_directory: str,
|
||||||
|
player_description: str, season_pct: float, post_players: bool, post_pitchers: bool, is_liveseries: bool):
|
||||||
|
print(f'Pulling PD player IDs...')
|
||||||
|
pd_players = await pd_players_df(cardset['id'])
|
||||||
|
|
||||||
|
all_stats = get_pitching_stats(file_path=input_path)
|
||||||
|
print(f'Processed {len(all_stats.values)} pitchers\n')
|
||||||
|
|
||||||
|
print(f'Pulling pitcher defense...')
|
||||||
|
df_p = cde.get_bbref_fielding_df('p', season)
|
||||||
|
|
||||||
|
pit_step1 = match_player_lines(all_stats, pd_players, df_p)
|
||||||
|
if post_players:
|
||||||
|
new_pitchers = await create_new_players(
|
||||||
|
pit_step1, cardset, card_base_url, release_directory, player_description
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
new_pitchers = 0
|
||||||
|
|
||||||
|
pitching_stats = get_stat_df(input_path, pit_step1)
|
||||||
|
del all_stats, pit_step1
|
||||||
|
|
||||||
|
pitching_stats = await calculate_pitching_cards(pitching_stats, cardset, season_pct, post_pitchers)
|
||||||
|
|
||||||
|
await create_position(season_pct, pitching_stats, post_pitchers, df_p)
|
||||||
|
await calculate_pitcher_ratings(pitching_stats, post_pitchers)
|
||||||
|
await post_player_updates(
|
||||||
|
cardset, player_description, card_base_url, release_directory, is_liveseries, post_players)
|
||||||
|
|
||||||
|
return {
|
||||||
|
'tot_pitchers': len(pitching_stats.index),
|
||||||
|
'new_pitchers': new_pitchers,
|
||||||
|
'pitching_stats': pitching_stats
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Loading…
Reference in New Issue
Block a user