Пример #1
0
    def ingest_config(self, game_info):
        """
        Take in config dicts and instantiate in-game Things and Actions.
        This function may be called multiple time in order to ingest multiple
        config files.
        """
        # Create game rooms.
        for info in game_info['rooms']:
            room = things.Room(verbose=self.verbose, **info)
            self._rooms.append(room)

            if self.verbose:
                print('room: {:s}, {:s}'.format(room.name, room.description))

        # Create game items.
        for info in game_info['items']:
            item = things.Item(verbose=self.verbose, **info)

            # Place item in a room.
            room_start = things.find_thing(self._rooms, info['start'])
            room_start.add(item)

            self._items.append(item)

            if self.verbose:
                print('item: {:s}, {:s}'.format(item.name, item.description))

        # Create user object(s).
        for info in game_info['user']:
            self.user = things.User(verbose=self.verbose, **info)

            # Place user in a room.
            room_start = things.find_thing(self._rooms, info['start'])
            room_start.add(self.user)

            if self.verbose:
                print('user: {:s}, {:s}'.format(self.user.name,
                                                self.user.description))

        # Create game actions.
        for action_info in game_info['actions']:
            # Get action sub-class from actions module.
            name = action_info['name'].lower().title()
            action_class = getattr(actions_module, name)

            # Instantiate Action class.
            action = action_class(self.user,  action_info['description'],
                                  action_info['aliases'])
            self.actions.append(action)

            if self.verbose:
                print('action: {:s}, {:s}'.format(action.names,
                      action.description))
Пример #2
0
    def apply(self, *args):
        """
        Look at something nearby.
        Take first argument as action target.
        Accept a string name or an Item instance.
        Default to looking at current room if no args supplied.
        """
        if not args:
            # Default is to look at the room the user is inside.
            args = [self.user.parent.name]

        # Consider first argument as target for looking.
        target = args[0]
        if isinstance(target, things.Item):
            thing_targ = target
        elif isinstance(target, basestring):
            # Search local area for Item with matching name.
            try:
                thing_targ = things.find_thing(self.user.local_things, target)
            except errors.FindThingError:
                msg = "There isn't a '{:s}' nearby ".format(target)
                raise errors.ApplyActionError(msg)
        else:
            msg = "I don't know what to do with: {:s}".format(target)
            raise errors.ApplyActionError(msg)

        # Gather up Items that inside the target Thing.
        response = 'You see {:s}.'.format(thing_targ.description)

        # Build up nice string of the extra stuff also found here.
        extra = None
        num_inside = len(thing_targ.container)
        if num_inside > 2:
            names = [t.name for t in thing_targ.container[:-1]]
            extra = ', '.join(names) + ' and ' + thing_targ.container[-1].name
        elif num_inside == 2:
            n1 = thing_targ.container[0].name
            n2 = thing_targ.container[1].name
            extra = n1 + ' and ' + n2

        elif num_inside == 1:
            extra = thing_targ.container[0].name

        if extra:
            response += '\nThe room also contains ' + extra + '.'

        return response