Permalink
Browse files

Implement RandomCardGenerator for use in Give/Summon actions

  • Loading branch information...
jleclanche committed Jun 20, 2015
1 parent cdece24 commit 48f46fd2d68064504afbcc53138344c33c7eb427
Showing with 32 additions and 6 deletions.
  1. +32 −6 fireplace/actions.py
@@ -5,6 +5,35 @@
from .entity import Entity
class RandomCardGenerator(object):
"""
Store filters and generate a random card matching the filters on pick()
"""
def __init__(self, **filters):
self.filters = filters
def pick(self) -> str:
from . import cards
return random.choice(cards.filter(**self.filters))
def _eval_card(game, card):
"""
Return a Card instance from \a card
The card argument can be:
- A Card instance (nothing is done)
- The string ID of the card (the card is created)
- A RandomCardGenerator instance (a random card is picked)
Also returns True if the card was created
"""
if isinstance(card, str):
return game.card(card), True
elif isinstance(card, RandomCardGenerator):
return game.card(card.pick()), True
return card, False
class EventListener:
ON = 1
AFTER = 2
@@ -358,9 +387,7 @@ class Give(TargetedAction):
args = ("targets", "card")
def get_args(self, source, game, target):
card = self.card
if isinstance(card, str):
card = game.card(self.card)
card, _ = _eval_card(game, self.card)
return (target, card)
def do(self, source, game, target, card):
@@ -503,9 +530,8 @@ class Summon(TargetedAction):
args = ("targets", "card")
def get_args(self, source, game, target):
card = self.card
if isinstance(card, str):
card = game.card(self.card)
card, created = _eval_card(game, self.card)
if created:
card.controller = target
return (target, card)

0 comments on commit 48f46fd

Please sign in to comment.