Batting Card Generation Complete
This commit is contained in:
parent
485a046855
commit
a44250803a
1517
app/card_creation.py
1517
app/card_creation.py
File diff suppressed because one or more lines are too long
@ -1,10 +1,11 @@
|
|||||||
from fastapi import APIRouter, Depends, HTTPException, Query, Response
|
from fastapi import APIRouter, Depends, HTTPException, Query, Response
|
||||||
from typing import Literal, Optional, List
|
from typing import Literal, Optional, List
|
||||||
import logging
|
import logging
|
||||||
|
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, BattingCardRatings, model_to_dict, chunked, BattingCard, Player, query_to_csv
|
from ..db_engine import db, BattingCardRatings, model_to_dict, chunked, BattingCard, Player, query_to_csv, Team
|
||||||
from ..dependencies import oauth2_scheme, valid_token, LOG_DATA
|
from ..dependencies import oauth2_scheme, valid_token, LOG_DATA
|
||||||
|
|
||||||
logging.basicConfig(
|
logging.basicConfig(
|
||||||
@ -85,16 +86,24 @@ class RatingsList(pydantic.BaseModel):
|
|||||||
|
|
||||||
@router.get('')
|
@router.get('')
|
||||||
async def get_card_ratings(
|
async def get_card_ratings(
|
||||||
battingcard_id: list = Query(default=None), vs_hand: Literal['R', 'L', 'vR', 'vL'] = None,
|
team_id: int, ts: str, battingcard_id: list = Query(default=None), cardset_id: list = Query(default=None),
|
||||||
short_output: bool = False, csv: bool = False, cardset_id: list = Query(default=None),
|
vs_hand: Literal['R', 'L', 'vR', 'vL'] = None, short_output: bool = False, csv: bool = False):
|
||||||
token: str = Depends(oauth2_scheme)):
|
this_team = Team.get_or_none(Team.id == team_id)
|
||||||
if not valid_token(token):
|
logging.debug(f'Team: {this_team} / has_guide: {this_team.has_guide}')
|
||||||
logging.warning(f'Bad Token: {token}')
|
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()
|
db.close()
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=401,
|
status_code=401,
|
||||||
detail='You are not authorized to pull card ratings.'
|
detail='You are not authorized to pull card ratings.'
|
||||||
)
|
)
|
||||||
|
# elif not valid_token(token):
|
||||||
|
# logging.warning(f'Bad Token: {token}')
|
||||||
|
# db.close()
|
||||||
|
# raise HTTPException(
|
||||||
|
# status_code=401,
|
||||||
|
# detail='You are not authorized to pull card ratings.'
|
||||||
|
# )
|
||||||
|
|
||||||
all_ratings = BattingCardRatings.select()
|
all_ratings = BattingCardRatings.select()
|
||||||
|
|
||||||
@ -108,9 +117,16 @@ async def get_card_ratings(
|
|||||||
all_ratings = all_ratings.where(BattingCardRatings.battingcard << set_cards)
|
all_ratings = all_ratings.where(BattingCardRatings.battingcard << set_cards)
|
||||||
|
|
||||||
if csv:
|
if csv:
|
||||||
return_val = query_to_csv(all_ratings)
|
# return_val = query_to_csv(all_ratings)
|
||||||
|
return_vals = [model_to_dict(x) for x in all_ratings]
|
||||||
|
for x in return_vals:
|
||||||
|
x.update(x['battingcard'])
|
||||||
|
x['player_id'] = x['battingcard']['player']['player_id']
|
||||||
|
del x['battingcard']
|
||||||
|
del x['player']
|
||||||
|
|
||||||
db.close()
|
db.close()
|
||||||
return Response(content=return_val, media_type='text/csv')
|
return Response(content=pd.DataFrame(return_vals).to_csv(index=False), media_type='text/csv')
|
||||||
|
|
||||||
else:
|
else:
|
||||||
return_val = {'count': all_ratings.count(), 'ratings': [
|
return_val = {'count': all_ratings.count(), 'ratings': [
|
||||||
@ -120,6 +136,52 @@ async def get_card_ratings(
|
|||||||
return return_val
|
return return_val
|
||||||
|
|
||||||
|
|
||||||
|
@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()
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=401,
|
||||||
|
detail='You are not authorized to pull card ratings.'
|
||||||
|
)
|
||||||
|
|
||||||
|
all_ratings = BattingCardRatings.select()
|
||||||
|
if cardset_id is not None:
|
||||||
|
set_players = Player.select(Player.player_id).where(Player.cardset_id << cardset_id)
|
||||||
|
set_cards = BattingCard.select(BattingCard.id).where(BattingCard.player << set_players)
|
||||||
|
all_ratings = all_ratings.where(BattingCardRatings.battingcard << set_cards)
|
||||||
|
|
||||||
|
vl_query = all_ratings.where(BattingCardRatings.vs_hand == 'L')
|
||||||
|
vr_query = all_ratings.where(BattingCardRatings.vs_hand == 'R')
|
||||||
|
|
||||||
|
vl_vals = [model_to_dict(x) for x in vl_query]
|
||||||
|
for x in vl_vals:
|
||||||
|
x.update(x['battingcard'])
|
||||||
|
x['player_id'] = x['battingcard']['player']['player_id']
|
||||||
|
x['player_name'] = x['battingcard']['player']['p_name']
|
||||||
|
x['rarity'] = x['battingcard']['player']['rarity']['name']
|
||||||
|
del x['battingcard']
|
||||||
|
del x['player']
|
||||||
|
|
||||||
|
vr_vals = [model_to_dict(x) for x in vr_query]
|
||||||
|
for x in vr_vals:
|
||||||
|
x['player_id'] = x['battingcard']['player']['player_id']
|
||||||
|
del x['battingcard']
|
||||||
|
|
||||||
|
vl = pd.DataFrame(vl_vals)
|
||||||
|
vr = pd.DataFrame(vr_vals)
|
||||||
|
|
||||||
|
output = pd.merge(vl, vr, on='player_id', suffixes=('_vl', '_vr'))
|
||||||
|
first = ['player_id', 'player_name', 'rarity', 'hand', 'variant']
|
||||||
|
exclude = first + ['id_vl', 'id_vr']
|
||||||
|
output = output[first + [col for col in output.columns if col not in exclude]]
|
||||||
|
db.close()
|
||||||
|
return Response(content=pd.DataFrame(output).to_csv(index=False), media_type='text/csv')
|
||||||
|
|
||||||
|
|
||||||
@router.get('/{ratings_id}')
|
@router.get('/{ratings_id}')
|
||||||
async def get_one_rating(ratings_id: int, token: str = Depends(oauth2_scheme)):
|
async def get_one_rating(ratings_id: int, token: str = Depends(oauth2_scheme)):
|
||||||
if not valid_token(token):
|
if not valid_token(token):
|
||||||
@ -141,7 +203,17 @@ async def get_one_rating(ratings_id: int, token: str = Depends(oauth2_scheme)):
|
|||||||
|
|
||||||
|
|
||||||
@router.get('/player/{player_id}')
|
@router.get('/player/{player_id}')
|
||||||
async def get_player_ratings(player_id: int, variant: list = Query(default=None), short_output: bool = False):
|
async def get_player_ratings(
|
||||||
|
player_id: int, variant: list = Query(default=None), short_output: bool = False,
|
||||||
|
token: str = Depends(oauth2_scheme)):
|
||||||
|
if not valid_token(token):
|
||||||
|
logging.warning(f'Bad Token: {token}')
|
||||||
|
db.close()
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=401,
|
||||||
|
detail='You are not authorized to pull card ratings.'
|
||||||
|
)
|
||||||
|
|
||||||
all_cards = BattingCard.select().where(BattingCard.player_id == player_id).order_by(BattingCard.variant)
|
all_cards = BattingCard.select().where(BattingCard.player_id == player_id).order_by(BattingCard.variant)
|
||||||
if variant is not None:
|
if variant is not None:
|
||||||
all_cards = all_cards.where(BattingCard.variant << variant)
|
all_cards = all_cards.where(BattingCard.variant << variant)
|
||||||
|
|||||||
@ -386,6 +386,24 @@ async def get_batter_card(
|
|||||||
db.close()
|
db.close()
|
||||||
return html_response
|
return html_response
|
||||||
|
|
||||||
|
updates = 0
|
||||||
|
if card_type == 'batting':
|
||||||
|
updates += BattingCardRatings.update(card_data['new_ratings_vl'].dict()).where(
|
||||||
|
(BattingCardRatings.id == rating_vl.id)
|
||||||
|
).execute()
|
||||||
|
updates += BattingCardRatings.update(card_data['new_ratings_vr'].dict()).where(
|
||||||
|
(BattingCardRatings.id == rating_vr.id)
|
||||||
|
).execute()
|
||||||
|
else:
|
||||||
|
updates += PitchingCardRatings.update(card_data['new_ratings_vl'].dict()).where(
|
||||||
|
(PitchingCardRatings.id == rating_vl.id)
|
||||||
|
).execute()
|
||||||
|
updates += PitchingCardRatings.update(card_data['new_ratings_vr'].dict()).where(
|
||||||
|
(PitchingCardRatings.id == rating_vr.id)
|
||||||
|
).execute()
|
||||||
|
|
||||||
|
logging.info(f'Rating updates: {updates}')
|
||||||
|
|
||||||
hti = Html2Image(
|
hti = Html2Image(
|
||||||
browser='chromium',
|
browser='chromium',
|
||||||
size=(1200, 600),
|
size=(1200, 600),
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user