Refactor scouting df pull

This commit is contained in:
Cal Corum 2023-10-29 00:50:32 -05:00
parent 6dfbd33605
commit 07df617ae0

View File

@ -5,7 +5,8 @@ import pandas as pd
import pydantic import pydantic
from pydantic import validator, root_validator from pydantic import validator, root_validator
from ..db_engine import db, PitchingCardRatings, model_to_dict, chunked, PitchingCard, Player, query_to_csv, Team from ..db_engine import db, PitchingCardRatings, model_to_dict, chunked, PitchingCard, Player, query_to_csv, Team, \
CardPosition
from ..dependencies import oauth2_scheme, valid_token, LOG_DATA from ..dependencies import oauth2_scheme, valid_token, LOG_DATA
logging.basicConfig( logging.basicConfig(
@ -127,17 +128,7 @@ async def get_card_ratings(
return return_val return return_val
@router.get('/scouting') def get_scouting_dfs(cardset_id: list = None):
async def get_card_scouting(team_id: int, ts: str, cardset_id: list = Query(default=None)):
this_team = Team.get_or_none(Team.id == team_id)
logging.debug(f'Team: {this_team} / has_guide: {this_team.has_guide}')
if this_team is None or ts != this_team.team_hash() or this_team.has_guide != 1:
logging.warning(f'Team_id {team_id} attempted to pull ratings')
db.close()
return 'Your team does not have the ratings guide enabled. If you have purchased a copy ping Cal to ' \
'make sure it is enabled on your team. If you are interested you can pick it up here (thank you!): ' \
'https://ko-fi.com/manticorum/shop'
all_ratings = PitchingCardRatings.select() all_ratings = PitchingCardRatings.select()
if cardset_id is not None: if cardset_id is not None:
set_players = Player.select(Player.player_id).where(Player.cardset_id << cardset_id) set_players = Player.select(Player.player_id).where(Player.cardset_id << cardset_id)
@ -155,8 +146,10 @@ async def get_card_scouting(team_id: int, ts: str, cardset_id: list = Query(defa
x['rarity'] = x['pitchingcard']['player']['rarity']['name'] x['rarity'] = x['pitchingcard']['player']['rarity']['name']
x['cardset_id'] = x['pitchingcard']['player']['cardset']['id'] x['cardset_id'] = x['pitchingcard']['player']['cardset']['id']
x['cardset_name'] = x['pitchingcard']['player']['cardset']['name'] x['cardset_name'] = x['pitchingcard']['player']['cardset']['name']
del x['pitchingcard'] x['starter_rating'] = x['pitchingcard']['starter_rating']
del x['player'] x['relief_rating'] = x['pitchingcard']['relief_rating']
x['closer_rating'] = x['pitchingcard']['closer_rating']
del x['pitchingcard'], x['player']
vr_vals = [model_to_dict(x) for x in vr_query] vr_vals = [model_to_dict(x) for x in vr_query]
for x in vr_vals: for x in vr_vals:
@ -165,13 +158,43 @@ async def get_card_scouting(team_id: int, ts: str, cardset_id: list = Query(defa
vl = pd.DataFrame(vl_vals) vl = pd.DataFrame(vl_vals)
vr = pd.DataFrame(vr_vals) vr = pd.DataFrame(vr_vals)
db.close()
output = pd.merge(vl, vr, on='player_id', suffixes=('_vl', '_vr')) pit_df = pd.merge(vl, vr, on='player_id', suffixes=('_vl', '_vr')).set_index('player_id', drop=False)
logging.info(f'pit_df: {pit_df}')
positions = CardPosition.select().where(CardPosition.position == 'P')
if cardset_id is not None:
set_players = Player.select(Player.player_id).where(Player.cardset_id << cardset_id)
positions = positions.where(CardPosition.player << set_players)
series_list = [pd.Series(
dict([(x.player.player_id, x.range) for x in positions]),
name=f'Range P'
), pd.Series(
dict([(x.player.player_id, x.error) for x in positions]),
name=f'Error P'
)]
db.close()
logging.info(f'series_list: {series_list}')
return pit_df.join(series_list)
@router.get('/scouting')
async def get_card_scouting(team_id: int, ts: str, cardset_id: list = Query(default=None)):
this_team = Team.get_or_none(Team.id == team_id)
logging.debug(f'Team: {this_team} / has_guide: {this_team.has_guide}')
if this_team is None or ts != this_team.team_hash() or this_team.has_guide != 1:
logging.warning(f'Team_id {team_id} attempted to pull ratings')
db.close()
return 'Your team does not have the ratings guide enabled. If you have purchased a copy ping Cal to ' \
'make sure it is enabled on your team. If you are interested you can pick it up here (thank you!): ' \
'https://ko-fi.com/manticorum/shop'
output = get_scouting_dfs(cardset_id)
first = ['player_id', 'player_name', 'cardset_name', 'rarity', 'hand', 'variant'] first = ['player_id', 'player_name', 'cardset_name', 'rarity', 'hand', 'variant']
exclude = first + ['id_vl', 'id_vr', 'vs_hand_vl', 'vs_hand_vr'] exclude = first + ['id_vl', 'id_vr', 'vs_hand_vl', 'vs_hand_vr']
output = output[first + [col for col in output.columns if col not in exclude]].sort_values(by=['player_id']) output = output[first + [col for col in output.columns if col not in exclude]]
# output = output.sort_values(by=['player_id'])
return Response(content=pd.DataFrame(output).to_csv(index=False), media_type='text/csv') return Response(content=pd.DataFrame(output).to_csv(index=False), media_type='text/csv')