def demultiplex(iterable): for t in iterable: if isiterable(t): for t2 in t: # It's only one level of nesting yield t2 else: yield t
def __init__(self, triggers, effects, zone="battlefield", txt='', keyword=''): if not isiterable(triggers): triggers=(triggers,) self.triggers = triggers self.effect_generator = effects self.zone = zone self.keyword = keyword if keyword and not txt: txt = keyword.capitalize() super(TriggeredAbility, self).__init__(txt)
def delay(source, ability): '''Ability should be defined for stack abilities. Setup a one time delayed triggered ability''' triggers, effects = ability() if not isiterable(triggers): triggers=(triggers,) controller = source.controller # in case controller changes before it triggers def playAbility(**trigger_keys): delayed = TriggeredStackAbility(effects, trigger_keys, source, controller) controller.stack.add_triggered(delayed) for t in triggers: t.clear_trigger() for t in triggers: t.setup_trigger(source, playAbility, weak=False)
def __init__(self, target_types, number=1, up_to=False, distribute=0, distribute_type='', msg='', selector="controller", player=None, zone="battlefield"): self.number = number self.distribute = distribute self.distribute_type = distribute_type # When you can distribute N among any number of targets if self.distribute > 0 and self.number == -1: self.number = self.distribute self.target = [] self.up_to = up_to if msg and not isiterable(msg): self.msg = (msg,) else: self.msg = msg self.selector = selector self.player = player if not isiterable(target_types): self.target_types = (target_types,) else: self.target_types = tuple(target_types) def match_type(card): for match in self.target_types: if match(card): return True else: return False self.match_type = match_type self.zone = zone
def __init__(self, triggers, effects, zone="battlefield", txt='', keyword=''): if not isiterable(triggers): triggers = (triggers, ) self.triggers = triggers self.effect_generator = effects self.zone = zone self.keyword = keyword if keyword and not txt: txt = keyword.capitalize() super(TriggeredAbility, self).__init__(txt)
def __init__(self, target_types, number=1, up_to=False, distribute=0, distribute_type='', msg='', selector="controller", player=None, zone="battlefield", min=0): self.number = number self.distribute = distribute self.distribute_type = distribute_type # When you can distribute N among any number of targets if self.distribute > 0 and self.number == -1: self.number = self.distribute self.target = [] self.up_to = up_to if msg and not isiterable(msg): self.msg = (msg,) else: self.msg = msg self.selector = selector self.player = player if not isiterable(target_types): self.target_types = (target_types,) else: self.target_types = tuple(target_types) def match_type(card): for match in self.target_types: if match(card): return True else: return False self.match_type = match_type self.zone = zone self.min = min # Sometimes you get "up to three target blahs", and sometimes you get "between two or three target blahs". Hopefully this should cover both.
def convertToTxt(card_dict): tmpl = """\ name = %(name)s %(char)s %(extra)s %(abilities)s""" fields = {} fields["char"] = "" for attr in ["types", "supertypes", "subtypes", "color"]: char = card_dict.get(attr, None) if char: if not isiterable(char): char = (char,) fields["char"] += "%s = %s\n" % (attr, ", ".join(map(str, char))) if "P/T" in card_dict: power, toughness = card_dict["P/T"] fields["extra"] = "power = %d\ntoughness = %d\n" % (power, toughness) elif "loyalty" in card_dict: fields["extra"] = "loyalty = %d\n" % card_dict["loyalty"] else: fields["extra"] = "" name = card_dict.get("name", None) if not name: subtypes = card_dict.get("subtypes", ()) if not isiterable(subtypes): subtypes = (subtypes,) name = " ".join(map(str, subtypes)) fields["name"] = repr(name) # Now process abilities (should only be simple keyword abilities) abilities = card_dict.get("abilities", "") if abilities: if not isiterable(abilities): abilities = (abilities,) abilities = "\n".join(["abilities.add(%s())" % a for a in abilities]) fields["abilities"] = abilities return tmpl % fields
def delay(source, ability): '''Ability should be defined for stack abilities. Setup a one time delayed triggered ability''' triggers, effects = ability() if not isiterable(triggers): triggers = (triggers, ) controller = source.controller # in case controller changes before it triggers def playAbility(**trigger_keys): delayed = TriggeredStackAbility(effects, trigger_keys, source, controller) controller.stack.add_triggered(delayed) for t in triggers: t.clear_trigger() for t in triggers: t.setup_trigger(source, playAbility, weak=False)
def convertToTxt(card_dict): tmpl = '''\ name = %(name)s %(char)s %(extra)s %(abilities)s''' fields = {} fields["char"] = '' for attr in ["types", "supertypes", "subtypes", "color"]: char = card_dict.get(attr, None) if char: if not isiterable(char): char = (char, ) fields["char"] += "%s = %s\n" % (attr, ', '.join(map(str, char))) if "P/T" in card_dict: power, toughness = card_dict["P/T"] fields["extra"] = "power = %d\ntoughness = %d\n" % (power, toughness) elif "loyalty" in card_dict: fields["extra"] = "loyalty = %d\n" % card_dict["loyalty"] else: fields["extra"] = '' name = card_dict.get("name", None) if not name: subtypes = card_dict.get("subtypes", ()) if not isiterable(subtypes): subtypes = (subtypes, ) name = " ".join(map(str, subtypes)) fields["name"] = repr(name) # Now process abilities (should only be simple keyword abilities) abilities = card_dict.get("abilities", '') if abilities: if not isiterable(abilities): abilities = (abilities, ) abilities = "\n".join(["abilities.add(%s())" % a for a in abilities]) fields["abilities"] = abilities return tmpl % fields
def __init__(self, target_types, msg='', selector="controller", zone="battlefield", player=None): self.is_valid = False self.target = None self.zone = zone self.player = player if not isiterable(target_types): self.target_types = (target_types,) else: self.target_types = tuple(target_types) # It should still be able to match any target types in the spell # See oracle for Putrefy (http://ww2.wizards.com/Gatherer/CardDetails.aspx?name=Putrefy) def match_types(card): for match in self.target_types: if match(card): return True else: return False self.match_types = match_types self.targeting_player = any(isinstance(ttype, match) for match in (PlayerMatch, OpponentMatch, PlayerOrCreatureMatch) for ttype in self.target_types) self.msg = msg self.selector = selector self.required = True
def demultiplex(iterable): for t in iterable: if isiterable(t): for t2 in t: # It's only one level of nesting yield t2 else: yield t
def count_nested(iterable): for t in iterable: if isiterable(t): yield len(t) else: yield 1
def chroma(selection, mana_color): if not isiterable(selection): selection = (selection, ) return sum([ sum([1 for symbol in obj.cost if symbol == mana_color]) for obj in selection if obj.cost.is_mana_cost() ])
def chroma(selection, mana_color): if not isiterable(selection): selection = (selection,) return sum([sum([1 for symbol in obj.cost if symbol == mana_color]) for obj in selection if obj.cost.is_mana_cost()])
def __init__(self, target_types=None, msg=''): self.is_valid = False self.target = None if not isiterable(target_types): self.target_types = (target_types,) else: self.target_types = tuple(target_types) self.msg = msg
def count_nested(iterable): for t in iterable: if isiterable(t): yield len(t) else: yield 1