def process(self, game): if self.status is not EventStatus.WAITING: for rule in self.rules["prepare"]: rule.prepare(self, game) if self.status is EventStatus.ABORTED: log.d("{} aborted on prepare by rule {}".format(self, rule)) break if self.status is not EventStatus.OK: return self.status self.prepare() self.should_wait() if self.status is not EventStatus.OK: return self.status for rule in self.rules["resolve"]: rule.resolve(self, game) if self.status is EventStatus.ABORTED: break if self.status is not EventStatus.OK: return self.status self.resolve() for rule in self.rules["done"]: rule.done(self, game) self.status = EventStatus.DONE; return self.status
def apply(self, event): log.d("Apply Rule: {}".format(self)) if self.prepare is not None: event.rules["prepare"].append(self) if self.resolve is not None: event.rules["resolve"].append(self) if self.done is not None: event.rules["done"].append(self)
def process_event(self): if len(self.event_queue) is 0: return None event = self.event_queue[-1] log.i("EventQueue: {}".format(event)) status = event.process(self.game) log.d("EventQueue: {} - {}".format(event, status)) if status is not EventStatus.WAITING: self.event_queue.remove(event) if status is EventStatus.DONE: self.game.event_history.append(event) return status
def queue(self, event, priority=False): if event is None: log.w("NULL event queued") return if Event not in event.__class__.__bases__: log.w("Cannot queue {} as it is a class, not an object. Missing ()?".format(event)) return log.d("Queueing event: {}".format(event)) for rule in self.game.rules: if rule.check_filter(event, self.game): rule.apply(event) if priority: self.event_queue.append(event) else: self.event_queue.insert(0, event) if self.status == EventQueueStatus.IDLE: self.status = EventQueueStatus.BUSY
def __init__(self, filename=None): self.id = uuid.uuid4() self.filename = filename self.main = [] self.side = [] if filename is None: return with open(filename) as f: data = f.read() in_sideboard = False for line in data.split('\n'): if not line.strip(): in_sideboard = True continue exp = re.search("([0-9]+) (.+)", line.strip()) amount = int(exp.group(1)) cardname = exp.group(2) filename = Deck.get_file_name(cardname) classname = Deck.get_class_name(cardname) try: card_module = importlib.import_module("cards.{}".format(filename)) card_class = getattr(card_module, classname) except: log.e("Card not found: {}".format(cardname)) log.d("Card Loaded: {}".format(card_class)) for i in range(amount): if not in_sideboard: self.main.append(card_class()) else: self.side.append(card)
def advance(self): if self.step is None: log.d("Turn: Advance to UNTAP STEP") self.phase = TurnPhase.BEGINNING self.step = TurnStep.UNTAP self.game.event_queue.queue(EventBeginningPhase(self.game, status=EventStatus.OK)) elif self.step is TurnStep.UNTAP: log.d("Turn: Advance to UPKEEP STEP") self.step = TurnStep.UPKEEP self.game.event_queue.queue(EventUpkeepStep(self.game, status=EventStatus.OK), True) elif self.step is TurnStep.UPKEEP: log.d("Turn: Advance to DRAW STEP") self.step = TurnStep.DRAW self.game.event_queue.queue(EventDrawStep(self.game, status=EventStatus.OK)) elif self.step is TurnStep.DRAW: log.d("Turn: Advance to FIRST MAIN PHASE") self.phase = TurnPhase.MAIN self.step = TurnStep.FIRST_MAIN self.game.event_queue.queue(EventMainPhase(self.game, status=EventStatus.OK)) elif self.step is TurnStep.FIRST_MAIN: log.d("Turn: Advance to COMBAT PHASE") self.phase = TurnPhase.COMBAT self.step = TurnStep.BEGINNING_OF_COMBAT self.game.event_queue.queue(EventCombatPhase(self.game, status=EventStatus.OK)) elif self.step is TurnStep.BEGINNING_OF_COMBAT: log.d("Turn: Advance to DECLARE ATTACKERS") self.step = TurnStep.DECLARE_ATTACKERS self.game.event_queue.queue(EventDeclareAttackersStep(self.game, status=EventStatus.OK))