Added GauntletRun and GauntletReward
This commit is contained in:
parent
079bff99c8
commit
12e1988322
25
db_engine.py
25
db_engine.py
@ -458,7 +458,26 @@ class Notification(BaseModel):
|
||||
ack = BooleanField(default=False)
|
||||
|
||||
|
||||
db.create_tables(
|
||||
[Roster, BattingStat, PitchingStat, Result, Award, Paperdex, Reward, GameRewards, Notification]
|
||||
)
|
||||
class GauntletReward(BaseModel):
|
||||
name = CharField()
|
||||
gauntlet = ForeignKeyField(Event)
|
||||
reward = ForeignKeyField(GameRewards)
|
||||
win_num = IntegerField()
|
||||
loss_max = IntegerField(default=1)
|
||||
|
||||
|
||||
class GauntletRun(BaseModel):
|
||||
team = ForeignKeyField(Team)
|
||||
gauntlet = ForeignKeyField(Event)
|
||||
wins = IntegerField(default=0)
|
||||
losses = IntegerField(default=0)
|
||||
gsheet = CharField(null=True)
|
||||
created = DateTimeField(default=int(datetime.timestamp(datetime.now())*1000))
|
||||
ended = DateTimeField(null=True)
|
||||
|
||||
|
||||
db.create_tables([
|
||||
Roster, BattingStat, PitchingStat, Result, Award, Paperdex, Reward, GameRewards, Notification, GauntletReward,
|
||||
GauntletRun
|
||||
])
|
||||
db.close()
|
||||
|
||||
274
main.py
274
main.py
@ -4498,3 +4498,277 @@ async def v1_gamerewards_delete(gamereward_id, token: str = Depends(oauth2_schem
|
||||
raise HTTPException(status_code=200, detail=f'Game Reward {gamereward_id} has been deleted')
|
||||
else:
|
||||
raise HTTPException(status_code=500, detail=f'Game Reward {gamereward_id} was not deleted')
|
||||
|
||||
|
||||
"""
|
||||
GAUNTLET REWARD ENDPOINTS
|
||||
"""
|
||||
|
||||
|
||||
class GauntletRewardModel(pydantic.BaseModel):
|
||||
name: str
|
||||
gauntlet_id: Optional[int] = 0
|
||||
reward_id: Optional[int] = 0
|
||||
win_num: Optional[int] = 0
|
||||
loss_max: Optional[int] = 1
|
||||
|
||||
|
||||
class GauntletRewardList(pydantic.BaseModel):
|
||||
rewards: List[GauntletRewardModel]
|
||||
|
||||
|
||||
@app.get('/api/v1/gauntletreward')
|
||||
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):
|
||||
all_rewards = GauntletReward.select()
|
||||
|
||||
if name is not None:
|
||||
all_rewards = all_rewards.where(GauntletReward.name == name)
|
||||
if gauntlet_id is not None:
|
||||
all_rewards = all_rewards.where(GauntletReward.gauntlet_id == gauntlet_id)
|
||||
if reward_id is not None:
|
||||
all_rewards = all_rewards.where(GauntletReward.reward_id << reward_id)
|
||||
if win_num is not None:
|
||||
all_rewards = all_rewards.where(GauntletReward.win_num == win_num)
|
||||
if loss_max is not None:
|
||||
all_rewards = all_rewards.where(GauntletReward.loss_max == loss_max)
|
||||
|
||||
return_val = {'count': all_rewards.count(), 'rewards': []}
|
||||
for x in all_rewards:
|
||||
return_val['rewards'].append(model_to_dict(x))
|
||||
|
||||
db.close()
|
||||
return return_val
|
||||
|
||||
|
||||
@app.get('/api/v1/gauntletreward/{gauntletreward_id}')
|
||||
async def v1_gauntletreward_get_one(gauntletreward_id):
|
||||
try:
|
||||
this_reward = GauntletReward.get_by_id(gauntletreward_id)
|
||||
except Exception:
|
||||
db.close()
|
||||
raise HTTPException(status_code=404, detail=f'No gauntlet reward found with id {gauntletreward_id}')
|
||||
|
||||
return_val = model_to_dict(this_reward)
|
||||
db.close()
|
||||
return return_val
|
||||
|
||||
|
||||
@app.patch('/api/v1/gauntletreward/{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,
|
||||
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 patch gauntlet rewards. This event has been logged.'
|
||||
)
|
||||
|
||||
this_reward = GauntletReward.get_or_none(GauntletReward.id == gauntletreward_id)
|
||||
if this_reward is None:
|
||||
db.close()
|
||||
raise KeyError(f'Gauntlet Reward ID {gauntletreward_id} not found')
|
||||
|
||||
if gauntlet_id is not None:
|
||||
this_reward.gauntlet_id = gauntlet_id
|
||||
if reward_id is not None:
|
||||
this_reward.reward_id = reward_id
|
||||
if win_num is not None:
|
||||
this_reward.win_num = win_num
|
||||
if loss_max is not None:
|
||||
this_reward.loss_max = loss_max
|
||||
if name is not None:
|
||||
this_reward.name = name
|
||||
|
||||
if this_reward.save():
|
||||
r_curr = model_to_dict(this_reward)
|
||||
db.close()
|
||||
return r_curr
|
||||
else:
|
||||
db.close()
|
||||
raise DatabaseError(f'Unable to patch gauntlet reward {gauntletreward_id}')
|
||||
|
||||
|
||||
@app.post('/api/v1/gauntletreward')
|
||||
async def v1_gauntletreward_post(gauntletreward: GauntletRewardList, 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 post gauntlets. This event has been logged.'
|
||||
)
|
||||
|
||||
all_rewards = []
|
||||
for x in gauntletreward.rewards:
|
||||
all_rewards.append(x.dict())
|
||||
|
||||
with db.atomic():
|
||||
for batch in chunked(all_rewards, 15):
|
||||
GauntletReward.insert_many(batch).on_conflict_replace().execute()
|
||||
db.close()
|
||||
|
||||
return f'Inserted {len(all_rewards)} gauntlet rewards'
|
||||
|
||||
|
||||
@app.delete('/api/v1/gauntletreward/{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}'
|
||||
|
||||
raise DatabaseError(f'Unable to delete gauntlet run {gauntletreward_id}')
|
||||
|
||||
|
||||
"""
|
||||
GAUNTLET ENDPOINTS
|
||||
"""
|
||||
|
||||
|
||||
class GauntletRunModel(pydantic.BaseModel):
|
||||
team_id: int
|
||||
wins: Optional[int] = 0
|
||||
losses: Optional[int] = 0
|
||||
gsheet: Optional[str] = None
|
||||
created: Optional[int] = int(datetime.timestamp(datetime.now())*1000)
|
||||
ended: Optional[int] = 0
|
||||
|
||||
|
||||
@app.get('/api/v1/gauntletrun')
|
||||
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)):
|
||||
all_gauntlets = GauntletRun.select()
|
||||
|
||||
if team_id is not None:
|
||||
all_gauntlets = all_gauntlets.where(GauntletRun.team_id << team_id)
|
||||
if wins is not None:
|
||||
all_gauntlets = all_gauntlets.where(GauntletRun.wins == wins)
|
||||
if wins_min is not None:
|
||||
all_gauntlets = all_gauntlets.where(GauntletRun.wins >= wins_min)
|
||||
if wins_max is not None:
|
||||
all_gauntlets = all_gauntlets.where(GauntletRun.wins <= wins_max)
|
||||
if losses is not None:
|
||||
all_gauntlets = all_gauntlets.where(GauntletRun.losses == losses)
|
||||
if losses_min is not None:
|
||||
all_gauntlets = all_gauntlets.where(GauntletRun.losses >= losses_min)
|
||||
if losses_max is not None:
|
||||
all_gauntlets = all_gauntlets.where(GauntletRun.losses <= losses_max)
|
||||
if gsheet is not None:
|
||||
all_gauntlets = all_gauntlets.where(GauntletRun.gsheet == gsheet)
|
||||
if created_after is not None:
|
||||
all_gauntlets = all_gauntlets.where(GauntletRun.created >= created_after)
|
||||
if created_before is not None:
|
||||
all_gauntlets = all_gauntlets.where(GauntletRun.created <= created_before)
|
||||
if ended_after is not None:
|
||||
all_gauntlets = all_gauntlets.where(GauntletRun.ended >= ended_after)
|
||||
if ended_before is not None:
|
||||
all_gauntlets = all_gauntlets.where(GauntletRun.ended <= ended_before)
|
||||
if is_active is not None:
|
||||
if is_active is True:
|
||||
all_gauntlets = all_gauntlets.where(GauntletRun.ended.is_null())
|
||||
else:
|
||||
all_gauntlets = all_gauntlets.where(GauntletRun.ended.is_null(False))
|
||||
if season is not None:
|
||||
all_gauntlets = all_gauntlets.where(GauntletRun.team.season << season)
|
||||
|
||||
return_val = {'count': all_gauntlets.count(), 'runs': []}
|
||||
for x in all_gauntlets:
|
||||
return_val['runs'].append(model_to_dict(x))
|
||||
|
||||
db.close()
|
||||
return return_val
|
||||
|
||||
|
||||
@app.get('/api/v1/gauntletrun/{gauntletrun_id}')
|
||||
async def v1_gauntletrun_get_one(gauntletrun_id):
|
||||
try:
|
||||
this_gauntlet = GauntletRun.get_by_id(gauntletrun_id)
|
||||
except Exception:
|
||||
db.close()
|
||||
raise HTTPException(status_code=404, detail=f'No gauntlet found with id {gauntletrun_id}')
|
||||
|
||||
return_val = model_to_dict(this_gauntlet)
|
||||
db.close()
|
||||
return return_val
|
||||
|
||||
|
||||
@app.patch('/api/v1/gauntletrun/{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,
|
||||
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 patch gauntlet runs. This event has been logged.'
|
||||
)
|
||||
|
||||
this_run = GauntletRun.get_or_none(GauntletRun.id == gauntletrun_id)
|
||||
if this_run is None:
|
||||
db.close()
|
||||
raise KeyError(f'Gauntlet Run ID {gauntletrun_id} not found')
|
||||
|
||||
if team_id is not None:
|
||||
this_run.team_id = team_id
|
||||
if wins is not None:
|
||||
this_run.wins = wins
|
||||
if losses is not None:
|
||||
this_run.losses = losses
|
||||
if gsheet is not None:
|
||||
this_run.gsheet = gsheet
|
||||
if created is not None:
|
||||
if created is True:
|
||||
this_run.created = int(datetime.timestamp(datetime.now())*1000)
|
||||
else:
|
||||
this_run.created = None
|
||||
if ended is not None:
|
||||
if ended is True:
|
||||
this_run.ended = int(datetime.timestamp(datetime.now())*1000)
|
||||
else:
|
||||
this_run.ended = None
|
||||
|
||||
if this_run.save():
|
||||
r_curr = model_to_dict(this_run)
|
||||
db.close()
|
||||
return r_curr
|
||||
else:
|
||||
db.close()
|
||||
raise DatabaseError(f'Unable to patch gauntlet run {gauntletrun_id}')
|
||||
|
||||
|
||||
@app.post('/api/v1/gauntletrun')
|
||||
async def v1_gauntletrun_post(gauntletrun: GauntletRunModel, 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 post gauntlets. This event has been logged.'
|
||||
)
|
||||
|
||||
this_run = GauntletRun(**gauntletrun.dict())
|
||||
|
||||
if this_run.save():
|
||||
r_run = model_to_dict(this_run)
|
||||
db.close()
|
||||
return r_run
|
||||
else:
|
||||
db.close()
|
||||
raise DatabaseError(f'Unable to post gauntlet run')
|
||||
|
||||
|
||||
@app.delete('/api/v1/gauntletrun/{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}'
|
||||
|
||||
raise DatabaseError(f'Unable to delete gauntlet run {gauntletrun_id}')
|
||||
|
||||
Loading…
Reference in New Issue
Block a user