Merge pull request #1 from calcorum/pack-customization

Pack customization
This commit is contained in:
Cal Corum 2023-03-11 18:00:51 -06:00 committed by GitHub
commit 079bff99c8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 90 additions and 5 deletions

View File

@ -248,6 +248,8 @@ db.create_tables([PackType])
class Pack(BaseModel):
team = ForeignKeyField(Team)
pack_type = ForeignKeyField(PackType)
pack_team = ForeignKeyField(Team, null=True)
pack_cardset = ForeignKeyField(Cardset, null=True)
open_time = DateTimeField(null=True)

42
db_migrations.py Normal file
View File

@ -0,0 +1,42 @@
from playhouse.migrate import *
import db_engine
# db = SqliteDatabase('pd_database.db')
migrator = SqliteMigrator(db_engine.db)
# db_engine.db.create_tables([db_engine.PackTheme])
# pubdate_field = DateTimeField(null=True)
# comment_field = TextField(default='')
# pitcher_injury = IntegerField(null=True)
# pos_1 = CharField(default='None')
# pos_2 = CharField(null=True)
# last_game = CharField(null=True)
# game_type = CharField(null=True)
# pack_type = ForeignKeyField(PackType, default=1, to_field='id', field_type=int)
# active_theme = ForeignKeyField(PackTheme, to_field='id', field_type=int, null=True)
# active_theme = ForeignKeyField(db_engine.PackTheme, field=db_engine.PackTheme.id, null=True) # for careers
# game_type = CharField(null=True)
# pack_team = ForeignKeyField(db_engine.Team, field=db_engine.Team.id, null=True)
pack_cardset = ForeignKeyField(db_engine.Cardset, field=db_engine.Cardset.id, null=True)
migrate(
# migrator.add_column('current', 'active_theme_id', active_theme),
# migrator.add_column('pack', 'pack_team_id', pack_team),
migrator.add_column('pack', 'pack_cardset_id', pack_cardset),
# migrator.rename_column('cardset', 'available', 'for_purchase')
# migrator.add_column('player', 'pos_1', pos_1),
# migrator.add_column('comment_tbl', 'comment', comment_field),
# migrator.rename_column('story', 'pub_date', 'publish_date'),
# migrator.drop_column('story', 'some_old_field'),
# migrator.drop_not_null('team', 'abbrev'),
# migrator.add_not_null('story', 'modified_date'),
# migrator.rename_table('story', 'stories_tbl'),
# migrator.drop_index('team', 'team_abbrev'),
# migrator.drop_index('player', 'player_name')
# migrator.add_column('pack', 'pack_type', pack_type),
)
db_engine.db.close()

51
main.py
View File

@ -1298,7 +1298,8 @@ async def v1_players_get_random(
min_cost: Optional[int] = None, max_cost: Optional[int] = None, in_packs: Optional[bool] = True,
min_rarity: Optional[int] = None, max_rarity: Optional[int] = None, limit: Optional[int] = None,
pos_include: Optional[str] = None, pos_exclude: Optional[str] = None, franchise: Optional[str] = None,
mlbclub: Optional[str] = None, csv: Optional[bool] = None):
mlbclub: Optional[str] = None, cardset_id: list = Query(default=None), pos_inc: list = Query(default=None),
pos_exc: list = Query(default=None), csv: Optional[bool] = None):
all_players = (Player
.select()
.join(Cardset)
@ -1328,11 +1329,26 @@ async def v1_players_get_random(
all_players = all_players.where(fn.Lower(Player.franchise) == franchise.lower())
if mlbclub is not None:
all_players = all_players.where(fn.Lower(Player.mlbclub) == mlbclub.lower())
if cardset_id is not None:
all_players = all_players.where(Player.cardset_id << cardset_id)
if pos_inc is not None:
p_list = [x.upper() for x in pos_inc]
all_players = all_players.where(
(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_exclude:
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()]
else:
final_players = [x for x in all_players]
final_players = all_players
if limit is not None:
final_players = final_players[:limit]
@ -1880,6 +1896,8 @@ PACK ENDPOINTS
class PackPydantic(pydantic.BaseModel):
team_id: int
pack_type_id: int
pack_team_id: Optional[int] = None
pack_cardset_id: Optional[int] = None
open_time: Optional[str] = None
@ -1890,7 +1908,8 @@ class PackModel(pydantic.BaseModel):
@app.get('/api/v1/packs')
async def v1_packs_get(
team_id: Optional[int] = None, pack_type_id: Optional[int] = None, opened: Optional[bool] = None,
limit: Optional[int] = None, new_to_old: Optional[bool] = None, csv: Optional[bool] = None):
limit: Optional[int] = None, new_to_old: Optional[bool] = None, pack_team_id: Optional[int] = None,
pack_cardset_id: Optional[int] = None, csv: Optional[bool] = None):
all_packs = Pack.select()
if all_packs.count() == 0:
@ -1911,6 +1930,20 @@ async def v1_packs_get(
db.close()
raise HTTPException(status_code=404, detail=f'No pack type found with id {pack_type_id}')
all_packs = all_packs.where(Pack.pack_type == this_pack_type)
if pack_team_id is not None:
try:
this_pack_team = Team.get_by_id(pack_team_id)
except Exception:
db.close()
raise HTTPException(status_code=404, detail=f'No team found with id {pack_team_id}')
all_packs = all_packs.where(Pack.pack_team == this_pack_team)
if pack_cardset_id is not None:
try:
this_pack_cardset = Cardset.get_by_id(pack_cardset_id)
except Exception:
db.close()
raise HTTPException(status_code=404, detail=f'No cardset found with id {pack_cardset_id}')
all_packs = all_packs.where(Pack.pack_cardset == this_pack_cardset)
if opened is not None:
all_packs = all_packs.where(Pack.open_time.is_null(not opened))
if limit is not None:
@ -1985,6 +2018,8 @@ async def v1_packs_post(packs: PackModel, token: str = Depends(oauth2_scheme)):
this_player = Pack(
team_id=x.team_id,
pack_type_id=x.pack_type_id,
pack_team_id=x.pack_team_id,
pack_cardset_id=x.pack_cardset_id,
open_time=x.open_time if x.open_time != "" else None
)
new_packs.append(this_player)
@ -2009,6 +2044,8 @@ async def v1_packs_post_one(pack: PackPydantic, token: str = Depends(oauth2_sche
this_pack = Pack(
team_id=pack.team_id,
pack_type_id=pack.pack_type_id,
pack_team_id=pack.pack_team_id,
pack_cardset_id=pack.pack_cardset_id,
open_time=pack.open_time
)
@ -2027,7 +2064,7 @@ async def v1_packs_post_one(pack: PackPydantic, token: str = Depends(oauth2_sche
@app.patch('/api/v1/packs/{pack_id}')
async def v1_packs_patch(
pack_id, team_id: Optional[int] = None, pack_type_id: Optional[int] = None, open_time: Optional[int] = None,
token: str = Depends(oauth2_scheme)):
pack_team_id: Optional[int] = None, pack_cardset_id: Optional[int] = None, token: str = Depends(oauth2_scheme)):
if not valid_token(token):
logging.warning(f'Bad Token: {token}')
db.close()
@ -2045,6 +2082,10 @@ async def v1_packs_patch(
this_pack.team_id = team_id
if pack_type_id is not None:
this_pack.pack_type_id = pack_type_id
if pack_team_id is not None:
this_pack.pack_team_id = pack_team_id
if pack_cardset_id is not None:
this_pack.pack_cardset_id = pack_cardset_id
if open_time is not None:
this_pack.open_time = open_time