Пример #1
0
def filter_sort_cards(cards, options):

    # Filter out cards by edition
    if options.edition and options.edition != "all":
        keep_sets = []
        for set_tag in Card.sets:
            for edition in Card.sets[set_tag]["edition"]:
                if options.edition == edition:
                    keep_sets.append(set_tag)

        keep_cards = []  # holds the cards that are to be kept
        for card in cards:
            if card.cardset_tag in keep_sets:
                keep_cards.append(card)

        cards = keep_cards

    # Combine upgrade cards with their expansion
    if options.upgrade_with_expansion:
        for card in cards:
            if card.cardset_tag == 'dominion2ndEditionUpgrade':
                card.cardset_tag = 'dominion1stEdition'
            elif card.cardset_tag == 'intrigue2ndEditionUpgrade':
                card.cardset_tag = 'intrigue1stEdition'

    # Combine all Events across all expansions
    if options.exclude_events:
        cards = combine_cards(cards,
                              old_card_type="Event",
                              new_type="Events",
                              new_card_tag='events',
                              new_cardset_tag='extras'
                              )
        if options.expansions:
            options.expansions.append("extras")

    # Combine all Landmarks across all expansions
    if options.exclude_landmarks:
        cards = combine_cards(cards,
                              old_card_type="Landmark",
                              new_type="Landmarks",
                              new_card_tag='landmarks',
                              new_cardset_tag='extras'
                              )
        if options.expansions:
            options.expansions.append("extras")

    # FIX THIS: Combine all Prizes across all expansions
    # if options.exclude_prizes:
    #    cards = combine_cards(cards, 'Prize', 'prizes')

    # Group all the special cards together
    if options.special_card_groups:
        keep_cards = []   # holds the cards that are to be kept
        group_cards = {}  # holds the cards for each group
        for card in cards:
            if not card.group_tag:
                keep_cards.append(card)  # not part of a group, so just keep the card
            else:
                # have a card in a group
                if card.group_tag not in group_cards:
                    # First card of a group
                    group_cards[card.group_tag] = card  # save to update cost later
                    # this card becomes the card holder for the whole group.
                    card.card_tag = card.group_tag
                    # These text fields should be updated later if there is a translation for this group_tag.
                    error_msg = "ERROR: Missing language entry for group_tab '%s'." % card.group_tag
                    card.name = card.group_tag  # For now, change the name to the group_tab
                    card.description = error_msg
                    card.extra = error_msg
                    if card.isEvent():
                        card.cost = "*"
                    if card.isLandmark():
                        card.cost = ""
                    # now save the card
                    keep_cards.append(card)
                else:
                    # subsequent cards in the group. Update group info, but don't keep the card.
                    if card.group_top:
                        # this is a designated card to represent the group, so update important data
                        group_cards[card.group_tag].cost = card.cost
                        group_cards[card.group_tag].potcost = card.potcost
                        group_cards[card.group_tag].debtcost = card.debtcost
                        group_cards[card.group_tag].types = card.types
                        group_cards[card.group_tag].image = card.image

                    group_cards[card.group_tag].addCardCount(card.count)    # increase the count
                    # group_cards[card.group_tag].set_lowest_cost(card)  # set holder to lowest cost of the two cards

        cards = keep_cards

        # Now fix up card costs
        for card in cards:
            if card.card_tag in group_cards:
                if group_cards[card.group_tag].isEvent():
                        group_cards[card.group_tag].cost = "*"
                        group_cards[card.group_tag].debtcost = 0
                        group_cards[card.group_tag].potcost = 0
                if group_cards[card.group_tag].isLandmark():
                        group_cards[card.group_tag].cost = ""
                        group_cards[card.group_tag].debtcost = 0
                        group_cards[card.group_tag].potcost = 0

    # Get the final type names in the requested language
    Card.type_names = add_type_text(options, Card.type_names, LANGUAGE_DEFAULT)
    if options.language != LANGUAGE_DEFAULT:
        Card.type_names = add_type_text(options, Card.type_names, options.language)
    for card in cards:
        card.types_name = ' - '.join([Card.type_names[t] for t in card.types]).upper()

    # Get the card bonus keywords in the requested language
    bonus = add_bonus_regex(options, LANGUAGE_DEFAULT)
    Card.addBonusRegex(bonus)
    if options.language != LANGUAGE_DEFAULT:
        bonus = add_bonus_regex(options, options.language)
        Card.addBonusRegex(bonus)

    # Fix up cardset text.  Waited as long as possible.
    Card.sets = add_set_text(options, Card.sets, LANGUAGE_DEFAULT)
    if options.language != LANGUAGE_DEFAULT:
        Card.sets = add_set_text(options, Card.sets, options.language)

    for card in cards:
        if card.cardset_tag in Card.sets:
            if 'set_name' in Card.sets[card.cardset_tag].keys():
                card.cardset = Card.sets[card.cardset_tag]['set_name']

    # If expansion names given, then remove any cards not in those expansions
    # Expansion names can be the names from the language or the cardset_tag
    if options.expansions:
        options.expansions = set([e.lower() for e in options.expansions])
        wantedExpansions = set()
        knownExpansions = set()
        # Match sets that either start with the expansion set key (used by cardset_tag)
        # or the actual name of the set/expansion in the specified language.
        for e in options.expansions:
            for s in Card.sets:
                if (s.lower().startswith(e) or
                        Card.sets[s].get('set_name', "").lower().startswith(e)):
                    wantedExpansions.add(s)
                    knownExpansions.add(e)

        # Give indication if an imput did not match anything
        unknownExpansions = options.expansions - knownExpansions
        if unknownExpansions:
            print "Error - unknown expansion(s): %s" % ", ".join(unknownExpansions)

        # Now keep only the cards that were in the expansions requested
        filteredCards = []
        for c in cards:
            if c.cardset_tag in wantedExpansions:
                filteredCards.append(c)
        cards = filteredCards

    # Now add text to the cards.  Waited as long as possible to catch all groupings
    cards = add_card_text(options, cards, LANGUAGE_DEFAULT)
    if options.language != LANGUAGE_DEFAULT:
        cards = add_card_text(options, cards, options.language)

    # Get list of cards from a file
    if options.cardlist:
        cardlist = set()
        with open(options.cardlist) as cardfile:
            for line in cardfile:
                cardlist.add(line.strip())
        if cardlist:
            cards = [card for card in cards if card.name in cardlist]

    # Set up the card sorter
    cardSorter = CardSorter(
        options.order,
        [card.name for card in cards if card.cardset.lower() == 'base'])
    if options.base_cards_with_expansion:
        cards = [card for card in cards if card.cardset.lower() != 'base']
    else:
        cards = [card for card in cards
                 if not cardSorter.isBaseExpansionCard(card)]

    # Add expansion divider
    if options.expansion_dividers:

        cardnamesByExpansion = {}
        for c in cards:
            if cardSorter.isBaseExpansionCard(c):
                continue
            cardnamesByExpansion.setdefault(c.cardset, []).append(c.name.strip().replace(' ', ' '))

        for set_tag, set_values in Card.sets.iteritems():
            exp = set_values["set_name"]
            if exp in cardnamesByExpansion:
                exp_name = exp

                count = len(cardnamesByExpansion[exp])
                if 'no_randomizer' in set_values:
                    if set_values['no_randomizer']:
                        count = 0

                if not options.expansion_dividers_long_name:
                    if 'short_name' in set_values:
                        exp_name = set_values['short_name']

                c = Card(name=exp_name,
                         cardset=exp,
                         cardset_tag=set_tag,
                         types=("Expansion", ),
                         cost=None,
                         description=' | '.join(sorted(cardnamesByExpansion[exp])),
                         count=count,
                         card_tag=set_tag)
                cards.append(c)

    # Now sort what is left
    cards.sort(key=cardSorter)

    return cards
Пример #2
0
def filter_sort_cards(cards, options):

    # Filter out cards by edition
    if options.edition and options.edition != "all":
        keep_sets = []
        for set_tag in Card.sets:
            for edition in Card.sets[set_tag]["edition"]:
                if options.edition == edition:
                    keep_sets.append(set_tag)

        keep_cards = []  # holds the cards that are to be kept
        for card in cards:
            if card.cardset_tag in keep_sets:
                keep_cards.append(card)

        cards = keep_cards

    # Combine upgrade cards with their expansion
    if options.upgrade_with_expansion:
        for card in cards:
            if card.cardset_tag == 'dominion2ndEditionUpgrade':
                card.cardset_tag = 'dominion1stEdition'
            elif card.cardset_tag == 'intrigue2ndEditionUpgrade':
                card.cardset_tag = 'intrigue1stEdition'

    # Combine all Events across all expansions
    if options.exclude_events:
        cards = combine_cards(cards,
                              old_card_type="Event",
                              new_type="Events",
                              new_card_tag='events',
                              new_cardset_tag='extras')
        if options.expansions:
            options.expansions.append("extras")

    # Combine all Landmarks across all expansions
    if options.exclude_landmarks:
        cards = combine_cards(cards,
                              old_card_type="Landmark",
                              new_type="Landmarks",
                              new_card_tag='landmarks',
                              new_cardset_tag='extras')
        if options.expansions:
            options.expansions.append("extras")

    # Group all the special cards together
    if options.special_card_groups:
        keep_cards = []  # holds the cards that are to be kept
        group_cards = {}  # holds the cards for each group
        for card in cards:
            if not card.group_tag:
                keep_cards.append(
                    card)  # not part of a group, so just keep the card
            else:
                # have a card in a group
                if card.group_tag not in group_cards:
                    # First card of a group
                    group_cards[
                        card.group_tag] = card  # save to update cost later
                    # this card becomes the card holder for the whole group.
                    card.card_tag = card.group_tag
                    # These text fields should be updated later if there is a translation for this group_tag.
                    error_msg = "ERROR: Missing language entry for group_tab '%s'." % card.group_tag
                    card.name = card.group_tag  # For now, change the name to the group_tab
                    card.description = error_msg
                    card.extra = error_msg
                    if card.isEvent():
                        card.cost = "*"
                    if card.isLandmark():
                        card.cost = ""
                    # now save the card
                    keep_cards.append(card)
                else:
                    # subsequent cards in the group. Update group info, but don't keep the card.
                    if card.group_top:
                        # this is a designated card to represent the group, so update important data
                        group_cards[card.group_tag].cost = card.cost
                        group_cards[card.group_tag].potcost = card.potcost
                        group_cards[card.group_tag].debtcost = card.debtcost
                        group_cards[card.group_tag].types = card.types
                        group_cards[card.group_tag].image = card.image

                    group_cards[card.group_tag].addCardCount(
                        card.count)  # increase the count
                    # group_cards[card.group_tag].set_lowest_cost(card)  # set holder to lowest cost of the two cards

        cards = keep_cards

        # Now fix up card costs
        for card in cards:
            if card.card_tag in group_cards:
                if group_cards[card.group_tag].isEvent():
                    group_cards[card.group_tag].cost = "*"
                    group_cards[card.group_tag].debtcost = 0
                    group_cards[card.group_tag].potcost = 0
                if group_cards[card.group_tag].isLandmark():
                    group_cards[card.group_tag].cost = ""
                    group_cards[card.group_tag].debtcost = 0
                    group_cards[card.group_tag].potcost = 0

    # Get the final type names in the requested language
    Card.type_names = add_type_text(options, Card.type_names, LANGUAGE_DEFAULT)
    if options.language != LANGUAGE_DEFAULT:
        Card.type_names = add_type_text(options, Card.type_names,
                                        options.language)
    for card in cards:
        card.types_name = ' - '.join([Card.type_names[t]
                                      for t in card.types]).upper()

    # Get the card bonus keywords in the requested language
    bonus = add_bonus_regex(options, LANGUAGE_DEFAULT)
    Card.addBonusRegex(bonus)
    if options.language != LANGUAGE_DEFAULT:
        bonus = add_bonus_regex(options, options.language)
        Card.addBonusRegex(bonus)

    # Fix up cardset text.  Waited as long as possible.
    Card.sets = add_set_text(options, Card.sets, LANGUAGE_DEFAULT)
    if options.language != LANGUAGE_DEFAULT:
        Card.sets = add_set_text(options, Card.sets, options.language)

    # Split out Official and Fan set information
    Official_sets = set()  # Will hold official sets
    Official_search = [
    ]  # Will hold official sets for searching, both set key and set_name
    Fan_sets = set()  # Will hold fan sets
    Fan_search = [
    ]  # Will hold fan sets for searching, both set key and set_name
    wantedSets = set()  # Will hold all the sets requested for printing
    for s in Card.sets:
        if Card.sets[s].get("fan", False):
            # Fan Expansion
            Fan_sets.add(s)
            Fan_search.extend(
                [s.lower(), Card.sets[s].get('set_name', None).lower()])
        else:
            # Official Expansion
            Official_sets.add(s)
            Official_search.extend(
                [s.lower(), Card.sets[s].get('set_name', None).lower()])

    # If expansion names given, then find out which expansions are requested
    # Expansion names can be the names from the language or the cardset_tag
    if options.expansions:
        # Expand out any wildcards, matching set key or set name in the given language
        expanded_expansions = []
        for e in options.expansions:
            matches = fnmatch.filter(Official_search, e)
            if matches:
                expanded_expansions.extend(matches)
            else:
                expanded_expansions.append(e)

        # Now get the actual sets that are matched above
        options.expansions = set([e for e in expanded_expansions
                                  ])  # Remove duplicates
        knownExpansions = set()
        for e in options.expansions:
            for s in Official_sets:
                if (s.lower() == e
                        or Card.sets[s].get('set_name', "").lower() == e):
                    wantedSets.add(s)
                    knownExpansions.add(e)
        # Give indication if an imput did not match anything
        unknownExpansions = options.expansions - knownExpansions
        if unknownExpansions:
            print "Error - unknown expansion(s): %s" % ", ".join(
                unknownExpansions)

    # Take care of fan expansions.  Fan expansions must be explicitly named to be added.
    # If no --fan is given, then no fan cards are added.
    # Fan expansion names can be the names from the language or the cardset_tag
    if options.fan:
        # Expand out any wildcards, matching set key or set name in the given language
        expanded_expansions = []
        for e in options.fan:
            matches = fnmatch.filter(Fan_search, e)
            if matches:
                expanded_expansions.extend(matches)
            else:
                expanded_expansions.append(e)

        # Now get the actual sets that are matched above
        options.fan = set([e
                           for e in expanded_expansions])  # Remove duplicates
        knownExpansions = set()
        for e in options.fan:
            for s in Fan_sets:
                if (s.lower() == e
                        or Card.sets[s].get('set_name', "").lower() == e):
                    wantedSets.add(s)
                    knownExpansions.add(e)
        # Give indication if an imput did not match anything
        unknownExpansions = options.fan - knownExpansions
        if unknownExpansions:
            print "Error - unknown fan expansion(s): %s" % ", ".join(
                unknownExpansions)

    # Now keep only the cards that are in the sets that have been requested
    keep_cards = []
    for c in cards:
        if c.cardset_tag in wantedSets:
            # Add the cardset informaiton to the card and add it to the list of cards to use
            c.cardset = Card.sets[c.cardset_tag].get('set_name', c.cardset_tag)
            keep_cards.append(c)
    cards = keep_cards

    # Now add text to the cards.  Waited as long as possible to catch all groupings
    cards = add_card_text(options, cards, LANGUAGE_DEFAULT)
    if options.language != LANGUAGE_DEFAULT:
        cards = add_card_text(options, cards, options.language)

    # Get list of cards from a file
    if options.cardlist:
        cardlist = set()
        with open(options.cardlist) as cardfile:
            for line in cardfile:
                cardlist.add(line.strip())
        if cardlist:
            cards = [card for card in cards if card.name in cardlist]

    # Set up the card sorter
    cardSorter = CardSorter(
        options.order, {
            card.card_tag: card.name
            for card in cards
            if 'base' in [set_name.lower() for set_name in card.cardset_tags]
        })

    # Optionally remove base cards from expansions that have them
    if not options.base_cards_with_expansion:
        cards = [
            card for card in cards if not cardSorter.isBaseExpansionCard(card)
        ]

    # Add expansion divider
    if options.expansion_dividers:

        cardnamesByExpansion = {}
        for c in cards:
            if cardSorter.isBaseExpansionCard(c):
                continue
            cardnamesByExpansion.setdefault(c.cardset,
                                            []).append(c.name.strip().replace(
                                                ' ', ' '))

        for set_tag, set_values in Card.sets.iteritems():
            exp = set_values["set_name"]
            if exp in cardnamesByExpansion:
                exp_name = exp

                count = len(cardnamesByExpansion[exp])
                Card.sets[set_tag]['count'] = count
                if 'no_randomizer' in set_values:
                    if set_values['no_randomizer']:
                        count = 0

                if not options.expansion_dividers_long_name:
                    if 'short_name' in set_values:
                        exp_name = set_values['short_name']

                c = Card(name=exp_name,
                         cardset=exp,
                         cardset_tag=set_tag,
                         types=("Expansion", ),
                         cost=None,
                         description=' | '.join(
                             sorted(cardnamesByExpansion[exp])),
                         extra=set_values.get("set_text", ""),
                         count=count,
                         card_tag=set_tag)
                cards.append(c)

    # Now sort what is left
    cards.sort(key=cardSorter)

    return cards