paper-dynasty-card-creation/calcs_defense.py
2023-06-21 10:06:00 -05:00

331 lines
8.5 KiB
Python

import math
def range_pitcher(rs_value: int, season_pct: float):
if rs_value >= (3 * season_pct):
return 1
elif rs_value >= (0 * season_pct):
return 2
elif rs_value >= (-1 * season_pct):
return 3
elif rs_value >= (-100 * season_pct):
return 4
else:
return 5
def range_catcher(rs_value: int, season_pct: float):
if rs_value >= 7 * season_pct:
return 1
elif rs_value >= 3 * season_pct:
return 2
elif rs_value >= -1 * season_pct:
return 3
elif rs_value >= -5 * season_pct:
return 4
else:
return 5
def range_first_base(drs: int, r_dp: int, season_pct: float):
if (drs + r_dp) >= (4 * season_pct):
return 1
elif (drs + r_dp) >= (1 * season_pct):
return 2
elif (drs + r_dp) >= (-1 * season_pct):
return 3
elif (drs + r_dp) >= (-3 * season_pct):
return 4
else:
return 5
def range_second_base(drs: int, r_dp: int, season_pct: float):
if (drs + r_dp) >= (8 * season_pct):
return 1
elif (drs + r_dp) >= (2 * season_pct):
return 2
elif (drs + r_dp) >= (0 * season_pct):
return 3
elif (drs + r_dp) >= (-3 * season_pct):
return 4
else:
return 5
def range_third_base(drs: int, r_dp: int, season_pct: float):
if (drs + r_dp) >= (5 * season_pct):
return 1
elif (drs + r_dp) >= (2 * season_pct):
return 2
elif (drs + r_dp) >= (0 * season_pct):
return 3
elif (drs + r_dp) >= (-3 * season_pct):
return 4
else:
return 5
def range_shortstop(drs: int, r_dp: int, season_pct: float):
if (drs + r_dp) >= (9 * season_pct):
return 1
elif (drs + r_dp) >= (2 * season_pct):
return 2
elif (drs + r_dp) >= (0 * season_pct):
return 3
elif (drs + r_dp) >= (-3 * season_pct):
return 4
else:
return 5
def range_center_field(drs: int, season_pct: float):
if drs >= 9 * season_pct:
return 1
elif drs >= 2 * season_pct:
return 2
elif drs >= -1 * season_pct:
return 3
elif drs >= -4 * season_pct:
return 4
else:
return 5
def range_left_field(drs: int, season_pct: float):
return range_center_field(drs, season_pct)
def range_right_field(drs: int, season_pct: float):
return range_center_field(drs, season_pct)
def valid_error_ratings(err_num: int, position: str) -> int:
if position.lower() == 'p':
valid_err = [
0, 4, 6, 7, 8, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 26, 27, 28, 30, 31, 33, 34,
35, 36, 38, 39, 40, 42, 43, 44, 46, 47, 48, 50, 51
]
elif position.lower() == 'c':
valid_err = list(range(17))
elif position.lower() == '1b':
valid_err = list(range(31))
elif position.lower() == '2b':
valid_err = [
0, 1, 2, 3, 4, 5, 6, 8, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
32, 34, 37, 39, 41, 44, 47, 50, 53, 56, 59, 62, 65, 68, 71
]
elif position.lower() == '3b':
valid_err = [
0, 1, 2, 3, 4, 5, 6, 8, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
31, 32, 33, 34, 35, 37, 39, 41, 44, 47, 50, 53, 56, 59, 62, 65
]
elif position.lower() == 'ss':
valid_err = [
0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32,
33, 34, 36, 38, 40, 42, 44, 48, 52, 56, 60, 64, 68, 72
]
# Outfielders
else:
valid_err = list(range(26))
if err_num in valid_err:
return err_num
elif err_num > valid_err[len(valid_err) - 1]:
return valid_err[len(valid_err) - 1]
else:
for x in valid_err:
if err_num <= x:
return x
def raw_error(errors: int, chances: int, season_pct: float, chance_max: int):
if errors == 0 or chances == 0:
return 0
# c_max = chance_max * season_pct
c_max = chance_max
return errors * c_max / chances
def error_pitcher(errors: int, chances: int, season_pct: float):
return valid_error_ratings(int(raw_error(errors, chances, season_pct, 50)), 'p')
def error_catcher(errors: int, chances: int, season_pct: float):
return valid_error_ratings(int(raw_error(errors, chances, season_pct, 500)), 'c')
def error_first_base(errors: int, chances: int, season_pct: float):
return valid_error_ratings(int(raw_error(errors, chances, season_pct, 1300)), '1b')
def error_second_base(errors: int, chances: int, season_pct: float):
return valid_error_ratings(int(raw_error(errors, chances, season_pct, 700)), '2b')
def error_third_base(errors: int, chances: int, season_pct: float):
return valid_error_ratings(int(raw_error(errors, chances, season_pct, 500)), '3b')
def error_shortstop(errors: int, chances: int, season_pct: float):
return valid_error_ratings(int(raw_error(errors, chances, season_pct, 700)), 'ss')
def error_outfield(errors: int, chances: int, season_pct: float):
return valid_error_ratings(int(raw_error(errors, chances, season_pct, 250)), 'of')
def arm_outfield(all_arms: list):
if not all_arms:
return '+5'
if max(all_arms) > 8:
return '-6'
elif max(all_arms) > 4:
return '-5'
elif max(all_arms) < -4:
return '+5'
else:
final_arm = max(all_arms) * -1
return f'{"+" if final_arm >= 0 else ""}{final_arm}'
def arm_catcher(cs_pct: str, raa: int, season_pct: float) -> str:
if cs_pct == '':
return '+3'
cs_pct = float(cs_pct.strip("%")) / 100
if raa > 5 * season_pct:
max_arm = -4
elif raa > 2 * season_pct:
max_arm = -2
elif raa > -1 * season_pct:
max_arm = 0
elif raa > -2 * season_pct:
max_arm = 3
else:
max_arm = 5
if cs_pct > .6:
raw_arm = -5
elif cs_pct > .5:
raw_arm = -4
elif cs_pct > .4:
raw_arm = -3
elif cs_pct > .3:
raw_arm = -2
elif cs_pct > .25:
raw_arm = -1
elif cs_pct > .2:
raw_arm = 0
elif cs_pct > .16:
raw_arm = 1
elif cs_pct > .12:
raw_arm = 2
elif cs_pct > .1:
raw_arm = 3
elif cs_pct > .05:
raw_arm = 4
else:
raw_arm = 5
final_arm = min(max_arm, raw_arm)
return f'{"+" if final_arm >= 0 else ""}{final_arm}'
def pb_catcher(pb: int, innings: int, season_pct: float):
if pb == 0 or innings == 0:
return 0
return abs(min(pb * 1000 * season_pct / innings, 20))
def ot_catcher(errors: int, chances: int, season_pct: float):
if errors == 0 or chances == 0:
return 0
c_max = 3000 * season_pct
return min(errors * c_max / chances / 3, 20)
def hold_pitcher(raw_cs: str, picks: int, season_pct: float) -> str:
if raw_cs == '':
return '+9'
cs_pct = float(raw_cs.strip("%")) / 100
if picks > 5 * season_pct:
pick_cap = -3
elif picks > 3 * season_pct:
pick_cap = -2
elif picks > 0 * season_pct:
pick_cap = 5
else:
pick_cap = 9
if cs_pct > .667:
hold_num = -5
elif cs_pct > .6:
hold_num = -4
elif cs_pct > .48:
hold_num = -3
elif cs_pct > .34:
hold_num = -2
elif cs_pct > .26:
hold_num = -1
elif cs_pct > .22:
hold_num = 0
elif cs_pct > .2:
hold_num = 1
elif cs_pct > .18:
hold_num = 3
elif cs_pct > .16:
hold_num = 4
elif cs_pct > .14:
hold_num = 5
elif cs_pct > .12:
hold_num = 6
elif cs_pct > .1:
hold_num = 7
elif cs_pct > .06:
hold_num = 8
else:
hold_num = 9
final_hold = min(pick_cap, hold_num)
return f'{"+" if final_hold >= 0 else ""}{final_hold}'
def pow_ratings(innings: float, gs: int, games: int) -> (int, int):
if innings <= 1 or games <= 1:
return 1, 1
s_innings = int(innings * gs / games)
r_innings = int(innings * (games - gs) / games)
if gs == 0:
s_pow = 1
else:
s_pow = round(s_innings / gs)
if r_innings == 0:
r_pow = 1
else:
r_pow = round(r_innings / (games - gs))
if r_innings / max(s_innings, 1) < .1:
r_pow = 1
elif r_pow >= s_pow > 1:
r_pow = s_pow - 1
return s_pow, r_pow
def innings_float(innings: str) -> float:
if '.' in innings:
whole, decimal = innings.split('.')
else:
whole = innings
decimal = "0"
return float(int(whole) + int(decimal) * .333)