Skip to content

Commit

Permalink
Add search decks by card to mv api
Browse files Browse the repository at this point in the history
  • Loading branch information
saluk committed Jul 31, 2023
1 parent 7a871f1 commit 271ba6e
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 3 deletions.
64 changes: 64 additions & 0 deletions mastervault/mv_fastapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,70 @@ def makedeck(d):
]}


@mvapi.get("/deck_query_by_card", tags=["aa-api"])
def deck_query_by_card(
cards:str="",
houses:Optional[str]=None,
expansions:Optional[str]=None,
page:Optional[int]=0,
loadcards:Optional[bool]=False,
page_size:Optional[int]=15
):
from models import deck_card_search
with session_scope() as session:
found_decks = []
card_indexes_and_counts = {}
for card_name in [x.strip() for x in cards.split(",")]:
index = deck_card_search.create_card_index(session, card_name, new=False)
if not index:
return {"error": f"{card_name} is not a valid card name"}
if index not in card_indexes_and_counts:
card_indexes_and_counts[index] = ''
card_indexes_and_counts[index] += index

# Find decks from t_deck_cards that match cards
deckq = session.query(mv_model.Deck).join(mv_model.T_DECK_CARDS, mv_model.Deck.key==mv_model.T_DECK_CARDS.deck_key)
for index_set in card_indexes_and_counts.values():
deckq = deckq.filter(mv_model.T_DECK_CARDS.cards.like('%'+index_set+'%'))

if houses:
houses = [x.strip() for x in houses.split(',')]
for h in houses:
deckq = deckq.filter(mv_model.Deck.data['_links']['houses'].astext.like('%'+h+'%'))
if expansions:
expansions = [int(x.strip()) for x in expansions.split(',')]
deckq = deckq.filter(or_(
*[mv_model.Deck.expansion == expansion for expansion in expansions]
))
page_size = min(page_size, 50)
deckq = deckq.limit(page_size)
deckq = deckq.offset(page*page_size)
from sqlalchemy.dialects import postgresql
print(deckq.statement.compile(dialect=postgresql.dialect(), compile_kwargs={"literal_binds": True}))
decks = deckq.all()
print("finish")
def makedeck(d):
deck = [
d.key,
d.name,
", ".join(d.houses),
d.data["expansion"],
d.page
]
if loadcards:
deck.append([{
"key": card.key,
"data": card.data
} for card in d.get_cards()])
return deck
return {
"count": len(decks),
"decks":
[
makedeck(d) for d in decks
]}


@mvapi.get("/deck_count", tags=["aa-api"])
def deck_count():
resp = {}
Expand Down
6 changes: 3 additions & 3 deletions models/deck_card_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@
indexes = {}

# If there isn't an index for the card, create one by doing chr(ord(last_row)+1)
def create_card_index(session, card_name):
def create_card_index(session, card_name, new=True):
if not indexes:
for card_index in session.query(mv_model.T_CARD_INDEX).all():
indexes[card_index.name] = card_index.index
found = indexes.get(card_name, None)
if not found:
if not found and new:
count = len(indexes.keys())
print(count)
index = chr(count+1)
Expand Down Expand Up @@ -93,4 +93,4 @@ def card_search(card_list=[]):
print(deck.deck_key, deck.cards)

#create_deck_indexes()
#card_search([('Murmook', 1)])
#card_search([('Murmook', 1)])

0 comments on commit 271ba6e

Please sign in to comment.