def gain_card(self, source_entity: Optional[EntityState], target_entity: PlayerEntityState, card_name: str): if not target_entity.is_player: raise GameError('Only players can gain cards: {0}'.format( target_entity.get_state()), crucial=False) if card_name not in cards: raise GameError('Now such card: {0}'.format(card_name)) target_entity.hand.append(CardState(card_name))
def remove_card(self, source_entity: Optional[EntityState], target_entity: PlayerEntityState, card_name: str): if not target_entity.is_player: raise GameError('Only players can gain cards: {0}'.format( target_entity.get_state()), crucial=False) if card_name not in cards: raise GameError('Now such card: {0}'.format(card_name)) for card_state in target_entity.hand: if card_state.name == card_name: target_entity.hand.remove(card_state)
def _apply_effect(self, source_entity: EntityState, target_entity: EntityState, effect: Dict): ef_type = effect.get(Effect.TYPE, None) if ef_type is None: raise GameError('Effect has no type: {0}'.format(effect)) if ef_type not in self.handlers: raise GameError('No handler for effect: {0}'.format(ef_type)) handler, args_def = self.handlers[ef_type] args = tuple([ effect.get(ef_prop, ef_default) for ef_prop, ef_default in args_def ]) handler(self, source_entity, target_entity, *args)
def draw_card(self, source_entity: Optional[EntityState], target_entity: EntityState): if not self.game.is_player(target_entity): raise GameError('Non-player entities cannot draw card') if isinstance(target_entity, PlayerEntityState): if len(target_entity.deck) > 0: card = random.sample(target_entity.deck, 1)[0] target_entity.deck.remove(card) self.gain_card(source_entity, target_entity, card)
def destroy(self, source_entity: Optional[EntityState], target_entity: EntityState): if self.game.is_player(target_entity): raise GameError('Player entity can not be destroyed: {0}'.format( target_entity.id), crucial=False) self.game.objects.remove(target_entity) self.game.invoke_case(target_entity, Case.DESTROYED, target_entity.name)
def __init__(self, name): if name not in cards: raise GameError('No such card: {0}'.format(name)) self.name = name card = get_card(self.name) card_offense_core = card.get(Card.ACTION_OFFENSE, card.get(Card.ACTION_SAME)) card_defense_core = card.get(Card.ACTION_OFFENSE, card.get(Card.ACTION_SAME)) self.cost_offense = card_offense_core.get(Card.COST) self.cost_defense = card_defense_core.get(Card.COST)
def _change_card_cost(self, entity: EntityState, card_type: str, amount: int): if not isinstance(entity, PlayerEntityState): raise GameError( 'Non-player entities does not have cards: {0}'.format( card_type), crucial=False) for card_state in entity.hand: c = get_card(card_state.name) if c.get(Card.TYPE, None) == card_type: card_state.cost_defense = max(card_state.cost_defense + amount, 0) card_state.cost_offense = max(card_state.cost_offense + amount, 0)
def drop_card(self, source_entity: Optional[EntityState], target_entity: EntityState, card_type: str): if not self.game.is_player(target_entity): raise GameError( 'Non-player entities cannot have cards: {0}'.format( target_entity.name), crucial=False) ''' if isinstance(target_entity, PlayerState): if len(target_entity.hand) > 0: card_to_drop = random.sample(target_entity.hand, 1)[0] target_entity.hand.remove(card_to_drop) self.game.invoke_case(target_entity, Case.DROP_CARD, card_to_drop) ''' raise NotImplementedError()
def spawn(self, source_entity: Optional[EntityState], target_entity: EntityState, name: str, spawn_position: int): if name not in objects: raise GameError('No such entity: {0}'.format(name)) e = EntityState() e.hp = objects[name].get(Entity.HP, e.hp) e.name = name e.side = target_entity.side e.position = max( min(target_entity.position + spawn_position, self.game.board_size - 1), 0) self.game._last_entity_id += 1 e.id = self.game._last_entity_id self.game.objects.append(e) self.game.invoke_case(e, Case.SPAWNED, None)