Added team record query & poopers

This commit is contained in:
Cal Corum 2023-10-26 00:35:40 -05:00
parent f0cd9b814f
commit 4e54e36bc9
2 changed files with 83 additions and 7 deletions

View File

@ -863,7 +863,7 @@ async def get_game_summary(
all_sb = steal_att.where(StratPlay.sb == 1)
all_cs = steal_att.where(StratPlay.cs == 1)
top_batters = (
all_batters = (
StratPlay
.select(StratPlay.batter, fn.SUM(StratPlay.re24).alias('sum_re24'), fn.SUM(StratPlay.ab).alias('sum_ab'),
fn.SUM(StratPlay.run).alias('sum_run'),
@ -872,10 +872,8 @@ async def get_game_summary(
fn.SUM(StratPlay.homerun).alias('sum_hr'), fn.SUM(StratPlay.bphr).alias('sum_bphr'))
.where(StratPlay.game_id == game_id)
.group_by(StratPlay.batter)
.order_by(SQL('sum_re24').desc())
.limit(tp_max)
)
top_pitchers = (
all_pitchers = (
StratPlay
.select(StratPlay.pitcher, fn.SUM(StratPlay.re24).alias('sum_re24'), fn.SUM(StratPlay.pa).alias('sum_pa'),
fn.SUM(StratPlay.outs).alias('sum_outs'), fn.SUM(StratPlay.e_run).alias('sum_erun'),
@ -883,9 +881,13 @@ async def get_game_summary(
fn.SUM(StratPlay.hit).alias('sum_hit'))
.where(StratPlay.game_id == game_id)
.group_by(StratPlay.pitcher)
.order_by(SQL('sum_re24').asc())
.limit(tp_max)
)
top_pitchers = all_pitchers.order_by(SQL('sum_re24').asc()).limit(tp_max)
top_batters = all_batters.order_by(SQL('sum_re24').desc()).limit(tp_max)
bot_pitcher = all_pitchers.order_by(SQL('sum_re24').desc()).get()
bot_batter = all_batters.order_by(SQL('sum_re24').asc()).get()
top_b = [{
'player': model_to_dict(x.batter, recurse=not short_output),
'ab': x.sum_ab,
@ -910,6 +912,30 @@ async def get_game_summary(
top_players = [*top_b, *top_p]
logging.info(f'top_players: {top_players}')
bot_players = [
{
'player': model_to_dict(bot_pitcher.pitcher, recurse=not short_output),
'tbf': bot_pitcher.sum_pa,
'ip': math.floor(bot_pitcher.sum_outs / 3) + ((bot_pitcher.sum_outs % 3) * .1),
'run': bot_pitcher.sum_run,
'e_run': bot_pitcher.sum_erun,
'hit': bot_pitcher.sum_hit,
'so': bot_pitcher.sum_so,
're24': bot_pitcher.sum_re24 * -1
},
{
'player': model_to_dict(bot_batter.batter, recurse=not short_output),
'ab': bot_batter.sum_ab,
'run': bot_batter.sum_run,
'hit': bot_batter.sum_hit,
'rbi': bot_batter.sum_rbi,
'double': bot_batter.sum_double,
'triple': bot_batter.sum_triple,
'hr': bot_batter.sum_hr,
're24': bot_batter.sum_re24
}
]
return {
'game': model_to_dict(this_game, recurse=not short_output),
'teams': {
@ -929,6 +955,7 @@ async def get_game_summary(
'home': all_errors.where(StratPlay.defender_team == this_game.home_team).count()
},
'top-players': sorted(top_players, key=lambda x: x['re24'], reverse=True)[:tp_max],
'pooper': sorted(bot_players, key=lambda x: x['re24'])[0],
'pitchers': {
'win': model_to_dict(winner.get().pitcher, recurse=not short_output),
'loss': model_to_dict(loser.get().pitcher, recurse=not short_output),

View File

@ -1,3 +1,4 @@
import copy
from datetime import datetime
import pandas as pd
@ -9,7 +10,7 @@ from pandas import DataFrame
from ..db_engine import db, Team, model_to_dict, fn, Pack, Card, Player, Paperdex, Notification, PackType, \
Rarity, Current, query_to_csv, complex_data_to_csv, CARDSETS, CardPosition, BattingCardRatings, BattingCard, \
PitchingCard, PitchingCardRatings
PitchingCard, PitchingCardRatings, StratGame
from ..dependencies import oauth2_scheme, valid_token, LOG_DATA, int_timestamp
logging.basicConfig(
@ -557,6 +558,54 @@ async def get_team_rp(
return this_player
@router.get('/{team_id}/season-record/{season}')
async def get_team_record(team_id: int, season: int):
all_games = StratGame.select().where(
((StratGame.away_team_id == team_id) | (StratGame.home_team_id == team_id)) & (StratGame.season == season)
)
template = {
'ARI': [0, 0, 0], 'ATL': [0, 0, 0], 'BAL': [0, 0, 0], 'BOS': [0, 0, 0], 'CHC': [0, 0, 0], 'CHW': [0, 0, 0],
'CIN': [0, 0, 0], 'CLE': [0, 0, 0], 'COL': [0, 0, 0], 'DET': [0, 0, 0],
'NYY': [0, 0, 0], 'TBR': [0, 0, 0], 'TOR': [0, 0, 0], 'PHI': [0, 0, 0], 'MIA': [0, 0, 0], 'NYM': [0, 0, 0],
'WSN': [0, 0, 0], 'MIN': [0, 0, 0], 'KCR': [0, 0, 0], 'HOU': [0, 0, 0],
'TEX': [0, 0, 0], 'SEA': [0, 0, 0], 'LAA': [0, 0, 0], 'OAK': [0, 0, 0], 'MIL': [0, 0, 0], 'PIT': [0, 0, 0],
'STL': [0, 0, 0], 'LAD': [0, 0, 0], 'SDP': [0, 0, 0], 'SFG': [0, 0, 0]
}
standings = {
'minor-league': copy.deepcopy(template),
'major-league': copy.deepcopy(template),
'hall-of-fame': copy.deepcopy(template)
}
for game in all_games:
run_diff = game.away_score - game.home_score
if run_diff > 0:
if game.away_team_id == team_id:
standings[game.game_type][game.home_team.abbrev][0] += 1
standings[game.game_type][game.home_team.abbrev][2] += run_diff
else:
standings[game.game_type][game.home_team.abbrev][1] += 1
standings[game.game_type][game.home_team.abbrev][2] += run_diff
else:
if game.away_team_id == team_id:
standings[game.game_type][game.home_team.abbrev][1] += 1
standings[game.game_type][game.home_team.abbrev][2] += run_diff
else:
standings[game.game_type][game.home_team.abbrev][0] += 1
standings[game.game_type][game.home_team.abbrev][2] += run_diff
# for lg_query in [minor_games, major_games, hof_games]:
# this_lg = copy.deepcopy(template)
# for x in range(1, 30):
# team_games = lg_query.where((StratGame.away_team_id == x) | (StratGame.home_team_id == x))
# for game in team_games:
db.close()
return standings
@router.get('/{team_id}/buy/players')
async def team_buy_players(team_id: int, ids: str, ts: str):
try: