Updated /plays/fielding
This commit is contained in:
parent
b8c1096b86
commit
ff27abd408
@ -710,7 +710,8 @@ async def get_fielding_totals(
|
||||
season: list = Query(default=None), week: list = Query(default=None),
|
||||
s_type: Literal['regular', 'post', 'total', None] = None, position: list = Query(default=None),
|
||||
player_id: list = Query(default=None),
|
||||
group_by: Literal['team', 'player', 'playerteam', 'playerposition', 'teamposition'] = 'player',
|
||||
group_by: Literal['team', 'player', 'playerteam', 'playerposition', 'teamposition', 'playerpositiongame',
|
||||
'playergame'] = 'player',
|
||||
min_ch: Optional[int] = 1, team_id: list = Query(default=None), manager_id: list = Query(default=None),
|
||||
sort: Optional[str] = None, limit: Optional[int] = None, short_output: Optional[bool] = False):
|
||||
season_games = StratGame.select()
|
||||
@ -732,7 +733,8 @@ async def get_fielding_totals(
|
||||
|
||||
def_plays = (
|
||||
StratPlay
|
||||
.select(StratPlay.defender, StratPlay.defender_team, fn.SUM(StratPlay.error).alias('sum_error'),
|
||||
.select(StratPlay.defender, StratPlay.defender_team, StratPlay.game,
|
||||
fn.SUM(StratPlay.error).alias('sum_error'),
|
||||
fn.SUM(StratPlay.hit).alias('sum_hit'), fn.SUM(StratPlay.pa).alias('sum_chances'),
|
||||
fn.SUM(StratPlay.wpa).alias('sum_wpa'), StratPlay.check_pos)
|
||||
.where((StratPlay.game << season_games) & (StratPlay.defender.is_null(False)))
|
||||
@ -740,9 +742,9 @@ async def get_fielding_totals(
|
||||
)
|
||||
cat_plays = (
|
||||
StratPlay
|
||||
.select(StratPlay.catcher, StratPlay.catcher_team, fn.SUM(StratPlay.sb).alias('sum_sb'),
|
||||
.select(StratPlay.catcher, StratPlay.catcher_team, StratPlay.game, fn.SUM(StratPlay.sb).alias('sum_sb'),
|
||||
fn.SUM(StratPlay.cs).alias('sum_cs'), fn.SUM(StratPlay.wpa).alias('sum_wpa'),
|
||||
fn.SUM(StratPlay.passed_ball).alias('sum_pb'))
|
||||
fn.SUM(StratPlay.passed_ball).alias('sum_pb'), fn.SUM(StratPlay.error).alias('sum_error'))
|
||||
.where((StratPlay.game << season_games) & (StratPlay.catcher.is_null(False)))
|
||||
)
|
||||
|
||||
@ -773,6 +775,12 @@ async def get_fielding_totals(
|
||||
elif group_by == 'teamposition':
|
||||
def_plays = def_plays.group_by(StratPlay.defender_team, StratPlay.check_pos)
|
||||
cat_plays = cat_plays.group_by(StratPlay.catcher_team)
|
||||
elif group_by == 'playergame':
|
||||
def_plays = def_plays.group_by(StratPlay.defender, StratPlay.game)
|
||||
cat_plays = cat_plays.group_by(StratPlay.catcher, StratPlay.game)
|
||||
elif group_by == 'playerpositiongame':
|
||||
def_plays = def_plays.group_by(StratPlay.defender, StratPlay.check_pos, StratPlay.game)
|
||||
cat_plays = cat_plays.group_by(StratPlay.catcher, StratPlay.game)
|
||||
if sort is not None:
|
||||
if sort == 'player':
|
||||
def_plays = def_plays.order_by(StratPlay.defender)
|
||||
@ -786,6 +794,10 @@ async def get_fielding_totals(
|
||||
def_plays = def_plays.order_by(SQL('sum_chances').desc())
|
||||
elif sort == 'ch-asc':
|
||||
def_plays = def_plays.order_by(SQL('sum_chances').asc())
|
||||
elif sort == 'newest':
|
||||
def_plays = def_plays.order_by(StratPlay.game_id.desc(), StratPlay.play_num.desc())
|
||||
elif sort == 'oldest':
|
||||
def_plays = def_plays.order_by(StratPlay.game_id, StratPlay.play_num)
|
||||
if limit is not None:
|
||||
if limit < 1:
|
||||
limit = 1
|
||||
@ -800,39 +812,66 @@ async def get_fielding_totals(
|
||||
|
||||
for x in def_plays:
|
||||
logging.info(f'this_play: {x}')
|
||||
this_cat = cat_plays.where(StratPlay.catcher == x.defender)
|
||||
# this_cat = cat_plays.where(StratPlay.catcher == x.defender)
|
||||
# if this_cat.count() > 0:
|
||||
# sum_sb = this_cat[0].sum_sb
|
||||
# sum_cs = this_cat[0].sum_cs
|
||||
# sum_wpa = this_cat[0].sum_wpa
|
||||
# sum_pb = this_cat[0].sum_pb
|
||||
# sum_error = this_cat[0].sum_error + x.sum_error
|
||||
# else:
|
||||
# sum_sb = 0
|
||||
# sum_cs = 0
|
||||
# sum_wpa = 0
|
||||
# sum_pb = 0
|
||||
# sum_error = x.sum_error
|
||||
|
||||
this_pos = 'TOT'
|
||||
this_cat = StratPlay.select().where(StratPlay.play_num < 0)
|
||||
if 'position' in group_by:
|
||||
this_pos = x.check_pos
|
||||
if this_pos == 'C' and 'player' in group_by:
|
||||
if 'player' in group_by:
|
||||
this_cat = cat_plays.where(StratPlay.catcher == x.defender)
|
||||
if 'game' in group_by:
|
||||
this_cat = cat_plays.where(StratPlay.game == x.game)
|
||||
|
||||
if this_cat.count() > 0:
|
||||
sum_sb = this_cat[0].sum_sb
|
||||
sum_cs = this_cat[0].sum_cs
|
||||
sum_wpa = this_cat[0].sum_wpa
|
||||
sum_pb = this_cat[0].sum_pb
|
||||
sum_error = this_cat[0].sum_error + x.sum_error
|
||||
else:
|
||||
sum_sb = 0
|
||||
sum_cs = 0
|
||||
sum_wpa = 0
|
||||
sum_pb = 0
|
||||
sum_error = x.sum_error
|
||||
|
||||
this_pos = 'TOT'
|
||||
if group_by in ['playerposition', 'teamposition']:
|
||||
this_pos = x.check_pos
|
||||
this_player = x.defender_id if short_output else model_to_dict(x.defender, recurse=False)
|
||||
if group_by == 'teamposition':
|
||||
this_player = 'TOT'
|
||||
|
||||
this_game = 'TOT'
|
||||
if 'game' in group_by:
|
||||
this_game = x.game_id if short_output else model_to_dict(x.game, recurse=False)
|
||||
|
||||
return_stats['stats'].append({
|
||||
'player': this_player,
|
||||
'team': x.defender_team_id if short_output else model_to_dict(x.defender_team, recurse=False),
|
||||
'pos': this_pos,
|
||||
'x-ch': x.sum_chances,
|
||||
'hit': x.sum_hit,
|
||||
'error': x.sum_error,
|
||||
'error': sum_error,
|
||||
'sb-ch': sum_sb + sum_cs,
|
||||
'sb': sum_sb,
|
||||
'cs': sum_cs,
|
||||
'pb': sum_pb,
|
||||
'wpa': (x.sum_wpa + sum_wpa) * -1,
|
||||
'wf%': (x.sum_chances - (x.sum_error * .5) - (x.sum_hit * .75)) / x.sum_chances,
|
||||
'cs%': sum_cs / (sum_sb + sum_cs) if (sum_sb + sum_cs) > 0 else ''
|
||||
'cs%': sum_cs / (sum_sb + sum_cs) if (sum_sb + sum_cs) > 0 else None,
|
||||
'game': this_game
|
||||
})
|
||||
db.close()
|
||||
return return_stats
|
||||
|
||||
Loading…
Reference in New Issue
Block a user