예제 #1
0
def insert_deck(competition_id: int, date: datetime.datetime,
                d: GatherlingDeck, fs: FinalStandings,
                players: List[Player]) -> deck.Deck:
    finish = fuzzy_get(fs, d.playername)
    if not finish:
        raise InvalidDataException(
            f"I don't have a finish for `{d.playername}`")
    mtgo_username = find_mtgo_username(d.playername, players)
    if not mtgo_username:
        raise InvalidDataException(
            f"I don't have an MTGO username for `{d.playername}`")
    raw: deck.RawDeckDescription = {
        'name': d.name,
        'source': 'Gatherling',
        'competition_id': competition_id,
        'created_date': dtutil.dt2ts(date),
        'mtgo_username': mtgo_username,
        'finish': finish,
        'url': gatherling_url(f'/deck.php?mode=view&id={d.id}'),
        'archetype': d.archetype.value,
        'identifier': str(d.id),
        'cards': {
            'maindeck': d.maindeck,
            'sideboard': d.sideboard
        },
    }
    if len(raw['cards']['maindeck']) + len(raw['cards']['sideboard']) == 0:
        raise InvalidDataException(
            f'Unable to add deck with no cards `{d.id}`')
    decklist.vivify(raw['cards'])
    if deck.get_deck_id(raw['source'], raw['identifier']):
        raise InvalidArgumentException(
            "You asked me to insert a deck that already exists `{raw['source']}`, `{raw['identifier']}`"
        )
    return deck.add_deck(raw)
예제 #2
0
def vivify_or_error(d: Container) -> str:
    try:
        vivified = decklist.vivify(d.cards)
    # MTGG doesn't do any validation of cards so some decks with fail here with card names like 'Stroke of Genuineness'.
    except InvalidDataException as e:
        return 'Rejecting decklist of deck with identifier {identifier} because of {e}'.format(
            identifier=d.identifier, e=e)
    if len(
        [f for f in legality.legal_formats(vivified)
         if 'Penny Dreadful' in f]) == 0:
        return 'Rejecting deck with identifier {identifier} because it is not legal in any PD formats.'.format(
            identifier=d.identifier)
    if len(d.cards) == 0:
        return 'Rejecting deck with identifier {identifier} because it has no cards.'.format(
            identifier=d.identifier)
    return ''
예제 #3
0
def scrape_url(url: str) -> deck.Deck:
    if not url.endswith('/'):
        url += '/'
    path = urllib.parse.urlparse(url).path
    slug = path.split('/')[2]
    raw_deck: RawDeckType = {}
    raw_deck['slug'] = slug
    raw_deck['url'] = url
    if is_authorised():
        raw_deck.update(fetch_deck_details(raw_deck)) # type: ignore
    else:
        raw_deck.update(parse_printable(raw_deck)) # type: ignore
    raw_deck = set_values(raw_deck)
    vivified = decklist.vivify(raw_deck['cards'])
    errors: Dict[str, Dict[str, Set[str]]] = {}
    if 'Penny Dreadful' not in legality.legal_formats(vivified, None, errors):
        raise InvalidDataException('Deck is not legal in Penny Dreadful - {error}'.format(error=errors.get('Penny Dreadful')))
    return deck.add_deck(raw_deck)
예제 #4
0
 def vivify_deck(self) -> None:
     try:
         self.deck = decklist.vivify(self.cards)
     except InvalidDataException as e:
         self.errors['decklist'] = str(e)