From 1c689d3194dc38cf6440776d9acb85e22b675796 Mon Sep 17 00:00:00 2001 From: Cal Corum Date: Mon, 20 Mar 2023 01:45:14 -0500 Subject: [PATCH] Gauntlets functional --- db_engine.py | 2 +- main.py | 60 +++++++++++++++++++++++++++++++++------------------- 2 files changed, 39 insertions(+), 23 deletions(-) diff --git a/db_engine.py b/db_engine.py index bf00ed7..2550ac7 100644 --- a/db_engine.py +++ b/db_engine.py @@ -473,7 +473,7 @@ class GauntletRun(BaseModel): losses = IntegerField(default=0) gsheet = CharField(null=True) created = DateTimeField(default=int(datetime.timestamp(datetime.now())*1000)) - ended = DateTimeField(null=True) + ended = DateTimeField(default=0) db.create_tables([ diff --git a/main.py b/main.py index 9620cf6..2fdc248 100644 --- a/main.py +++ b/main.py @@ -229,7 +229,7 @@ async def v1_teams_get( cv_max: Optional[int] = None, ps_shiny_min: Optional[int] = None, ps_shiny_max: Optional[int] = None, ranking_min: Optional[int] = None, ranking_max: Optional[int] = None, has_guide: Optional[bool] = None, sname: Optional[str] = None, lname: Optional[str] = None, is_ai: Optional[bool] = None, - limit: Optional[int] = None, csv: Optional[bool] = False): + event_id: Optional[int] = None, limit: Optional[int] = None, csv: Optional[bool] = False): """ Param: season: int Param: team_abbrev: string @@ -292,6 +292,9 @@ async def v1_teams_get( if is_ai is not None: all_teams = all_teams.where(Team.is_ai) + if event_id is not None: + all_teams = all_teams.where(Team.event_id == event_id) + if limit is not None: all_teams = all_teams.limit(limit) @@ -1337,16 +1340,24 @@ async def v1_players_get_random( (Player.pos_1 << p_list) | (Player.pos_2 << p_list) | (Player.pos_3 << p_list) | (Player.pos_4 << p_list) | (Player.pos_5 << p_list) | (Player.pos_6 << p_list) | (Player.pos_7 << p_list) | (Player.pos_8 << p_list) ) - if pos_exc is not None: - p_list = [x.upper() for x in pos_exc] - all_players = all_players.where( - (Player.pos_1.not_in(p_list)) | (Player.pos_2.not_in(p_list)) | (Player.pos_3.not_in(p_list)) | - (Player.pos_4.not_in(p_list)) | (Player.pos_5.not_in(p_list)) | (Player.pos_6.not_in(p_list)) | - (Player.pos_7.not_in(p_list)) | (Player.pos_8.not_in(p_list)) - ) + # if pos_exc is not None: + # p_list = [x.upper() for x in pos_exc] + # logging.info(f'starting query: {all_players}\n\np_list: {p_list}\n\n') + # all_players = all_players.where( + # Player.pos_1.not_in(p_list) & Player.pos_2.not_in(p_list) & Player.pos_3.not_in(p_list) & + # Player.pos_4.not_in(p_list) & Player.pos_5.not_in(p_list) & Player.pos_6.not_in(p_list) & + # Player.pos_7.not_in(p_list) & Player.pos_8.not_in(p_list) + # ) + # logging.info(f'post pos query: {all_players}') if pos_exclude is not None and pos_exc is None: final_players = [x for x in all_players if pos_exclude not in x.get_all_pos()] + elif pos_exc is not None and pos_exclude is None: + final_players = [] + p_list = [x.upper() for x in pos_exc] + for x in all_players: + if not set(p_list).intersection(x.get_all_pos()): + final_players.append(x) else: final_players = all_players @@ -4517,7 +4528,7 @@ class GauntletRewardList(pydantic.BaseModel): rewards: List[GauntletRewardModel] -@app.get('/api/v1/gauntletreward') +@app.get('/api/v1/gauntletrewards') async def v1_gauntletreward_get( name: Optional[str] = None, gauntlet_id: Optional[int] = None, reward_id: list = Query(default=None), win_num: Optional[int] = None, loss_max: Optional[int] = None): @@ -4542,7 +4553,7 @@ async def v1_gauntletreward_get( return return_val -@app.get('/api/v1/gauntletreward/{gauntletreward_id}') +@app.get('/api/v1/gauntletrewards/{gauntletreward_id}') async def v1_gauntletreward_get_one(gauntletreward_id): try: this_reward = GauntletReward.get_by_id(gauntletreward_id) @@ -4555,7 +4566,7 @@ async def v1_gauntletreward_get_one(gauntletreward_id): return return_val -@app.patch('/api/v1/gauntletreward/{gauntletreward_id}') +@app.patch('/api/v1/gauntletrewards/{gauntletreward_id}') async def v1_gauntletreward_patch( gauntletreward_id, name: Optional[str] = None, gauntlet_id: Optional[int] = None, reward_id: Optional[int] = None, win_num: Optional[int] = None, loss_max: Optional[int] = None, @@ -4593,7 +4604,7 @@ async def v1_gauntletreward_patch( raise DatabaseError(f'Unable to patch gauntlet reward {gauntletreward_id}') -@app.post('/api/v1/gauntletreward') +@app.post('/api/v1/gauntletrewards') async def v1_gauntletreward_post(gauntletreward: GauntletRewardList, token: str = Depends(oauth2_scheme)): if not valid_token(token): logging.warning(f'Bad Token: {token}') @@ -4615,7 +4626,7 @@ async def v1_gauntletreward_post(gauntletreward: GauntletRewardList, token: str return f'Inserted {len(all_rewards)} gauntlet rewards' -@app.delete('/api/v1/gauntletreward/{gauntletreward_id}') +@app.delete('/api/v1/gauntletrewards/{gauntletreward_id}') async def v1_gauntletreward_delete(gauntletreward_id): if GauntletReward.delete_by_id(gauntletreward_id) == 1: return f'Deleted gauntlet reward ID {gauntletreward_id}' @@ -4630,6 +4641,7 @@ GAUNTLET ENDPOINTS class GauntletRunModel(pydantic.BaseModel): team_id: int + gauntlet_id: int wins: Optional[int] = 0 losses: Optional[int] = 0 gsheet: Optional[str] = None @@ -4637,13 +4649,13 @@ class GauntletRunModel(pydantic.BaseModel): ended: Optional[int] = 0 -@app.get('/api/v1/gauntletrun') +@app.get('/api/v1/gauntletruns') async def v1_gauntletrun_get( team_id: list = Query(default=None), wins: Optional[int] = None, wins_min: Optional[int] = None, wins_max: Optional[int] = None, losses: Optional[int] = None, losses_min: Optional[int] = None, losses_max: Optional[int] = None, gsheet: Optional[str] = None, created_after: Optional[int] = None, created_before: Optional[int] = None, ended_after: Optional[int] = None, ended_before: Optional[int] = None, - is_active: Optional[bool] = None, season: list = Query(default=None)): + is_active: Optional[bool] = None, gauntlet_id: list = Query(default=None), season: list = Query(default=None)): all_gauntlets = GauntletRun.select() if team_id is not None: @@ -4671,10 +4683,14 @@ async def v1_gauntletrun_get( if ended_before is not None: all_gauntlets = all_gauntlets.where(GauntletRun.ended <= ended_before) if is_active is not None: + logging.info(f'get_gauntletrun - is_active: {is_active}') if is_active is True: - all_gauntlets = all_gauntlets.where(GauntletRun.ended.is_null()) + all_gauntlets = all_gauntlets.where(GauntletRun.ended == 0) else: - all_gauntlets = all_gauntlets.where(GauntletRun.ended.is_null(False)) + all_gauntlets = all_gauntlets.where(GauntletRun.ended != 0) + logging.info(f'get_gauntletrun - query: {all_gauntlets}') + if gauntlet_id is not None: + all_gauntlets = all_gauntlets.where(GauntletRun.gauntlet_id << gauntlet_id) if season is not None: all_gauntlets = all_gauntlets.where(GauntletRun.team.season << season) @@ -4686,7 +4702,7 @@ async def v1_gauntletrun_get( return return_val -@app.get('/api/v1/gauntletrun/{gauntletrun_id}') +@app.get('/api/v1/gauntletruns/{gauntletrun_id}') async def v1_gauntletrun_get_one(gauntletrun_id): try: this_gauntlet = GauntletRun.get_by_id(gauntletrun_id) @@ -4699,7 +4715,7 @@ async def v1_gauntletrun_get_one(gauntletrun_id): return return_val -@app.patch('/api/v1/gauntletrun/{gauntletrun_id}') +@app.patch('/api/v1/gauntletruns/{gauntletrun_id}') async def v1_gauntletrun_patch( gauntletrun_id, team_id: Optional[int] = None, wins: Optional[int] = None, losses: Optional[int] = None, gsheet: Optional[str] = None, created: Optional[bool] = None, ended: Optional[bool] = None, @@ -4734,7 +4750,7 @@ async def v1_gauntletrun_patch( if ended is True: this_run.ended = int(datetime.timestamp(datetime.now())*1000) else: - this_run.ended = None + this_run.ended = 0 if this_run.save(): r_curr = model_to_dict(this_run) @@ -4745,7 +4761,7 @@ async def v1_gauntletrun_patch( raise DatabaseError(f'Unable to patch gauntlet run {gauntletrun_id}') -@app.post('/api/v1/gauntletrun') +@app.post('/api/v1/gauntletruns') async def v1_gauntletrun_post(gauntletrun: GauntletRunModel, token: str = Depends(oauth2_scheme)): if not valid_token(token): logging.warning(f'Bad Token: {token}') @@ -4766,7 +4782,7 @@ async def v1_gauntletrun_post(gauntletrun: GauntletRunModel, token: str = Depend raise DatabaseError(f'Unable to post gauntlet run') -@app.delete('/api/v1/gauntletrun/{gauntletrun_id}') +@app.delete('/api/v1/gauntletruns/{gauntletrun_id}') async def v1_gauntletrun_delete(gauntletrun_id): if GauntletRun.delete_by_id(gauntletrun_id) == 1: return f'Deleted gauntlet run ID {gauntletrun_id}'