def deserialize_deck(sdeck: Container) -> Deck: deck = Deck(sdeck) deck.active_date = dtutil.ts2dt(deck.active_date) deck.created_date = dtutil.ts2dt(deck.created_date) deck.updated_date = dtutil.ts2dt(deck.updated_date) if deck.competition_end_date is not None: deck.competition_end_date = dtutil.ts2dt(deck.competition_end_date) deck.wins = int(deck.wins) deck.losses = int(deck.losses) deck.draws = int(deck.draws) if deck.get('omw') is not None: deck.omw = float(deck.omw) deck.maindeck = [CardRef(ref['name'], ref['n']) for ref in deck.maindeck] deck.sideboard = [CardRef(ref['name'], ref['n']) for ref in deck.sideboard] return deck
def load_cards(decks: List[Deck]) -> None: if len(decks) == 0: return decks_by_id = {d.id: d for d in decks} sql = """ SELECT deck_id, card, n, sideboard FROM deck_card WHERE deck_id IN ({deck_ids}) """.format(deck_ids=', '.join(map(sqlescape, map(str, decks_by_id.keys())))) rs = db().select(sql) for row in rs: location = 'sideboard' if row['sideboard'] else 'maindeck' name = row['card'] d = decks_by_id[row['deck_id']] d[location] = d.get(location, []) d[location].append(CardRef(name, row['n']))
def vivify(decklist: DecklistType) -> Deck: validated: DecklistType = {'maindeck': {}, 'sideboard': {}} invalid_names = set() for section in ['maindeck', 'sideboard']: for name, n in decklist.get(section, {}).items(): try: validated[section][oracle.valid_name(name)] = n except InvalidDataException: invalid_names.add(name) if invalid_names: raise InvalidDataException('Invalid cards: {invalid_names}'.format(invalid_names='; '.join(invalid_names))) d = Deck({'maindeck': [], 'sideboard': []}) for section in ['maindeck', 'sideboard']: for name, n in validated.get(section, {}).items(): d[section].append(CardRef(name, n)) return d
def test_legal_formats() -> None: d = Deck({'id': 0}) d.maindeck = [CardRef('Swamp', 59)] d.sideboard = [] assert len(d.all_cards()) == 59 formats = legality.legal_formats(d) assert len(formats) == 0 d.maindeck = [CardRef('Swamp', 60)] formats = legality.legal_formats(d) assert 'Penny Dreadful' in formats assert 'Legacy' in formats assert 'Penny Dreadful EMN' in formats formats = legality.legal_formats(d, {'Penny Dreadful'}) assert len(formats) == 1 assert 'Penny Dreadful' in formats assert 'Legacy' not in formats d.maindeck = [CardRef('Swamp', 55), CardRef('Think Twice', 5)] formats = legality.legal_formats(d) assert len(d.all_cards()) == 60 assert len(legality.legal_formats(d)) == 0 d.maindeck = [CardRef('Swamp', 56), CardRef('Think Twice', 4)] formats = legality.legal_formats(d) assert 'Legacy' in formats assert 'Modern' in formats d.sideboard = [CardRef('Swamp', 15), CardRef('Think Twice', 1)] formats = legality.legal_formats(d) assert len(legality.legal_formats(d)) == 0 d.maindeck = [CardRef('Swamp', 56), CardRef('Fork', 4)] d.sideboard = [CardRef('Swamp', 15)] formats = legality.legal_formats(d) assert 'Legacy' in formats assert 'Modern' not in formats d.maindeck = [CardRef('Swamp', 60)] d.sideboard = [CardRef('Swamp', 15)] formats = legality.legal_formats(d) assert 'Standard' in formats assert 'Modern' in formats assert 'Legacy' in formats assert 'Vintage' in formats assert 'Penny Dreadful' in formats assert 'Penny Dreadful EMN' in formats