示例#1
0
    def take_action(self,action,input=user_input.default_input):
        """
        This function takes an action specified by a string
         and completes that action.
        """
        # NOTE: currently there is no check to ensure that each action is available
        # TODO: check to see if action is available before trying... like:
        # if action in self.actions_available:

        # === Base Actions: ===
        room_dict = self.generate_rooms_dict()
        if action == "exit":
            sys.exit()
        elif action == "look":
            print self.player.current_location.description
        elif action == "enter":
            raise NotImplementedError('action_main call needs to be fixed') #action_main()
        elif action == "go":
            print self.player.current_location.exits
            travel_location = input("Which Room?")
            try:
                self.player.current_location = self.config_loader.get_by_type_and_name('room', self.player.current_location.exits[int(travel_location)-1])
            except ValueError:
                try:
                    self.player.current_location = self.config_loader.get_by_type_and_name('room', travel_location)
                except ValueError:
                    print 'Place not recognized.'
        elif action == "stats":
            self.player.player_status()
            return True
        elif action == "help":
            user_input.help_info()
            return True
        elif action == "location":
            self.player.player_location()
            return True

        # === iteminteractions ===
        elif action == "grab":
            self.player.inventory.add_item(user_input.choose_object(self.keys))

        # === monster interactions
        elif action == "fight":
            opponent_engine()
            return True
        else:
            print "That's not a valid command!!!"
 def base_help_test(self):
     '''tests out the help for all base actions'''
     game = GameInstance(input_func=randomString)
     
     for action in BASE_ACTIONS:
         class pickAction(object):
             def __init__(self):
                 self.choice = action
             def __call__(self,prompt):
                 if self.choice == action:
                     self.choice = 'back'
                     return action
                 elif self.choice == 'back':
                     self.choice == None
                     return 'back'
                 else:
                     raise Error('WAT')
         chooser = pickAction()
         help_info(input=chooser)
    def take_action(self, command, input=user_input.default_input):

        # This method now uses the Command object returned from the Parser
        # This means you can call commands with 2 words, e.g. 'look desk'

        # TODO:
        #   - Move this code to user_input maybe
        #     ^ Any thoughts on this? This code will become very large as we implement more verbs.
        #       Unless we can devise a smart way to handle them all.

        if command:
            if command.verb.name == 'exit':
                self.exit_game()

            if command.verb.name == 'look':
                # call look function of object of command
                if command.object == None:
                    print self.player.current_location.description
                    
                elif command.object.type == 'error':
                    invalid_input("I don't understand %s" % command.object.name,
                        input_string=command.raw,
                        tag='unknown object error',
                        game=self)
                elif 'in' in command.object.prepositional_modifiers:

                    self.config_loader.get_by_type_and_name('item', command.object.name).look_in()

                else:    # If there is no object of look it will print the current room's description
                    self.config_loader.get_by_type_and_name('item', command.object.name).look()

            if command.verb.name == 'go':
                if command.object != None:
                    try:
                        target_room = self.config_loader.get_by_type_and_name('room', command.object.name)
                        if target_room.name in self.player.current_location.exits:
                            self.player.current_location = target_room
                        else:
                            print 'Cannot go to '+Fore.GREEN+"{}".format(target_room.name)
                    except ValueError as err:
                        if err.message[0:16] == 'Cannot find room':
                            invalid_input(err.message,
                                input_string=command.raw,
                                tag='room not found',
                                game=self)
                        else:
                            raise
                else:
                    print self.player.current_location.exits
                    travel_location = input("Which Room?")
                    try:
                        self.player.current_location = self.config_loader.get_by_type_and_name('room', self.player.current_location.exits[int(travel_location)-1])
                    except ValueError:
                        try:
                            self.player.current_location = self.config_loader.get_by_type_and_name('room', travel_location)
                        except ValueError:
                            invalid_input('Place not recognized.',
                                input_string=travel_location,
                                tag='room specified not found',
                                game=self)
                            
            if command.verb.name == 'stats':
                print ' ### USER STATS ### '
                self.player.player_status()
                print ' ### GAME STATS ### '
                print 'game started : ', self.GAME_START
                print 'commands entered : ', self.commands_entered

            if command.verb.name == 'quests':
                user_input.get_events_list(self)

            if command.verb.name == 'help':
                user_input.help_info()

            if command.verb.name == 'location':
                self.player.player_location()

            if command.verb.name == 'inventory' or command.verb.name == 'bag':
                print self.player.inventory.list_of_items()

            if command.verb.name == 'save':
                self.save_game()

            if command.verb.name == 'name':
                print self.player.name

            else:
                print "I'm not sure what you mean."

            self.commands_entered += 1
        else:
            invalid_input('Command not recognized.',
                input_string=command.raw,
                tag='unknown command',
                game=self)