def check_syntax(self, world, *args): """ Makes sure that the command has been called with the correct number of arguments and that the arguments are the right type. """ if len(args) < len(self.syntax): raise LogicError(Messages.TOO_FEW_ARGS.format(self.name)) elif len(args) > len(self.syntax): raise LogicError(Messages.TOO_MANY_ARGS) for arg, expected in zip(args, self.syntax): expected(world, arg)
def _has_thing_of_type_in_inventory(player, equipment_type): if not [ thing for thing in player.inventory if (hasattr(thing, 'equipment_type') and thing.equipment_type == equipment_type) ]: raise LogicError( "{name} {verb} not carrying a {equipment_type}".format( equipment_type=equipment_type, name=player.name, verb=("are" if player.name == "you" else "is")))
def change_state(self, world, player, *args): """ Manages changes in world state. Wraps the state-changing methods in a transaction and will roll state back if something goes wrong. """ # world.start_transaction() try: for state_change in self.state_changes: state_change(world, player, *args) except StateError: # world.rollback() raise LogicError(Messages.BAD_STATE_CHANGE)
def _get_item_of_type(player, equipment_type): items = [ thing for thing in player.inventory if (hasattr(thing, 'equipment_type') and thing.equipment_type == equipment_type) ] if len(items) > 1: raise LogicError( "{name} {verb} carrying more than one {equipment_type}s".format( equipment_type=equipment_type, name=player.name, verb=("are" if player.name == "you" else "is"))) return items[0]
def __call__(self, world, player, *args): """ Called by LogicHandler. Does three things: * makes sure command syntax is correct * checks if command can be run * returns the correct response """ if args and not self.syntax: raise LogicError(Messages.TOO_MANY_ARGS) if self.syntax: self.check_syntax(world, *args) if self.rules: self.check_rules(world, player, *args) return self.calculate_response(world, player)
def _path_exists(world, player, direction): if direction not in player.location.paths: raise LogicError(PATH_ERROR)
def _is_a_direction(world, entity): if not entity.has_component(Direction): raise LogicError(DIRECTION_ERROR)
def _has_a_wand(world, player, other_player): try: other_player.wand except AttributeError: raise LogicError( "Nothing happens. Your opponent is not carrying their wand!")
def _is_in_same_room(world, player, other_player): if player.location != other_player.location: raise LogicError("You're too far away to do that!")
def _is_equipment_type(world, word): word = re.sub('s$', '', word) if word != 'wand': raise LogicError("You can't trade that away")
def _is_valid_number(world, word): try: int(word) except (TypeError, ValueError): raise LogicError("You must use an integer skill level.")
def _is_expelliarmus(world, word): if word != 'expelliarmus': raise LogicError("You can only set your skill for expelliarmus.")
def _is_a_player(world, entity): if entity not in player_aspect: raise LogicError("You can only perform that action on other people!")
def _is_equippable(world, entity): if not entity.has_component(duel.Equipment): raise LogicError("You cannot equip that.")
def _player_can_successfully_cast_expelliarmus(world, player, other_player): player_skill = _get_expelliarmus_skill(player) if player_skill < random.randint(1, 20): raise LogicError("Nothing happens.")
def _is_in_inventory(world, player, thing): if thing not in player.inventory: raise LogicError("You are not carrying that.")
def _legal_skill_level(world, player, skill): skill = int(skill) if not 0 < skill <= 15: raise LogicError("Skill levels range from 0 to 15.")