def __do_input(self, sender: CommandSender, inp: str): input_object = CommandInput(inp) if sender.output.on_input(sender, input_object): # since the on_input method returned True, it must have done something, so we don't need to send a message return if input_object.is_empty(): # since the player's OutputSender didn't handle this, we should do it sender.send_message( "You must enter a command. Normally, pressing enter with a blank line won't trigger this.") return input_handles = [] # Note this is a list of InputHandles for input_handler in self.get_input_handlers(): handle = input_handler.on_input(self, sender, input_object) # call on_input for each handler if handle is not None: input_handles.append(handle) input_handles.sort(key=lambda k: k.priority) # sort by priority already_handled = [] for input_handle in list(input_handles): # copy list so we can delete stuff handle_type = input_handle.handle(already_handled) # note not method # let it decide to take assert handle_type is not None, "An InputHandle's handle callable cannot return None. {} broke this rule" \ .format(type(input_handle.input_handler)) # cannot be None because it said it would handle it already_handled.append(handle_type) if handle_type is InputHandleType.REMOVE_HANDLER or handle_type is \ InputHandleType.REMOVE_HANDLER_ALLOW_RESPONSE: self.input_handlers.remove(input_handle.input_handler) elif handle_type is InputHandleType.HANDLED_AND_DONE: break # we don't care what others have to say. We're done handling this input # player.send_line() # for the debug if len(already_handled) == 0 or has_only(already_handled, [InputHandleType.NOT_HANDLED, InputHandleType.UNNOTICEABLE]): sender.send_message("Command: \"" + input_object.get_command() + "\" not recognized.")
def _handle_command(self, handler: Handler, sender: CommandSender, command_input: CommandInput) -> InputHandleType: if not isinstance(sender, Player): sender.send_message("This must be an error. A {} shouldn't be trying to attack.".format(type(sender))) player = sender manager = handler.get_managers(BattleManager, 1)[0] # get the BattleManager, there should be 1 battles = manager.get_battles(player) assert len(battles) <= 1, "The player can only by in one battle" battle = None if len(battles) > 0: battle = battles[0] # now we have the battle that the player is currently in which could be None if battle is None: # the player isn't in a battle first_arg = command_input.get_arg(0, False) # remember this is a list containing the first arg at [0] # ignore_unimportant_before is False (^)because we want to get everything the player typed after the command if not first_arg: # there isn't a first argument player.send_message("Who would you like to battle?") return InputHandleType.HANDLED reference = " ".join(first_arg) challenged = player.location.get_referenced_entity(handler, reference) if challenged is None: player.send_message(Message("Cannot find entity named: '{}' in your current location.", named_variables=[reference])) return InputHandleType.HANDLED challenge_action = EntityChallengeAction(player, challenged) handler.do_action(challenge_action) challenge_action.try_action(handler) return InputHandleType.HANDLED # now we know the player is in a battle since Battle isn't None assert not battle.has_ended, "For whatever reason, we received at battle that was ended." if not battle.has_started: player.send_message("Sorry, for whatever reason the battle hasn't started yet. This could be an error.") return InputHandleType.HANDLED turn = battle.current_turn assert turn is not None, "The battle is started and there's no current_turn ??? That would be an error." user = turn.get_target(player) assert isinstance(user.move_chooser, SetMoveChooser), "We need the player's MoveChooser to be a SetMoveChooser." first_arg = command_input.get_arg(0) if not first_arg: return self.__class__.send_options(player, user) try: number = int(first_arg[0]) # now that we know the first argument is a number, the player wants to choose a move return self.__class__.choose_option_from_number(battle, number, player, user) except ValueError: pass # the first argument was not a number so lets test for something else now if first_arg[0].lower() == "members": return self.__class__.send_members(battle, player) self.send_help(player) return InputHandleType.HANDLED
def on_player_input(self, handler: Handler, player: Player, command_input: CommandInput): def handle_function(already_handled: List[InputHandleType]) -> InputHandleType: if not self._should_handle_input(already_handled): return InputHandleType.NOT_HANDLED return GoCommandHandler.player_go(handler, player, command_input.get_command()) if not command_input.get_arg(0): # make sure the length is always 1 (there should be no args cuz command) command_name = command_input.get_command() if get_point(handler, player, command_name) is not None: # we'll let the GoCommandHandler do what it wants return InputHandle(InputHandle.PRIORITY_COMMAND_ALIAS, handle_function, self) return None
def on_yell(self, handler: Handler, player: Player, command_input: CommandInput, is_there_response=False) -> None: """ There is a default player implementation for this is_there_response is only meant to be changed by inheriting classes (It shouldn't be changed by YellCommandHa..) :param handler: The handler object :param player: The player :param command_input: The player input object :param is_there_response: By default, False. If set to True, the default implementation won't send a message saying no one responded. :return: None """ first_arg = command_input.get_arg(0, False) player.send_message( Message("You (Yelling): {}".format(" ".join(first_arg)), MessageType.IMMEDIATE)) player.send_message(Message("....", message_type=MessageType.TYPE_SLOW)) if not is_there_response: player.send_message(self.__class__.NO_YELL_RESPONSE)
def _handle_command(self, handler: Handler, sender: CommandSender, command_input: CommandInput): if not isinstance(sender, Player): return InputHandleType.UNSUPPORTED_SENDER player = sender first_arg = command_input.get_arg(0) if not first_arg: self.send_help(player) return InputHandleType.HANDLED item = get_reference(player, " ".join(first_arg)) if item is None: player.send_message(Item.CANNOT_SEE[1]) return InputHandleType.HANDLED can_ref = item.can_reference(player) if can_ref[0] is False: player.send_message(can_ref[1]) return InputHandleType.HANDLED can_put = item.can_put(player) if can_put[0] is False: player.send_message(can_put[1]) return InputHandleType.HANDLED if item in player.items: if item.change_holder(player, player.location): player.location.on_place(handler, item, player) player.send_message(Message("You placed {}.", named_variables=[item])) else: player.send_message("Apparently, that item that you tried to place isn't in your inventory.\n" "Please tell someone what you did so it can be fixed. (This message shouldn't appear)") return InputHandleType.HANDLED
def _handle_command(self, handler: Handler, sender: CommandSender, command_input: CommandInput) -> InputHandleType: if not isinstance(sender, Player): return InputHandleType.UNSUPPORTED_SENDER player = sender first_arg = command_input.get_arg(0) if not first_arg: self.send_help(player) return InputHandleType.HANDLED rest = " ".join(first_arg) return self.__class__.player_go(handler, player, rest)
def _handle_command(self, handler: Handler, sender: CommandSender, command_input: CommandInput): if not isinstance(sender, Player): return InputHandleType.UNSUPPORTED_SENDER player = sender first_arg = command_input.get_arg(0, False) if not first_arg: self.send_help(player) return InputHandleType.HANDLED if self.can_yell(player): player.location.on_yell(handler, player, command_input) else: player.send_message("You can't yell right now.") return InputHandleType.HANDLED
def _handle_command(self, handler: Handler, sender: CommandSender, command_input: CommandInput): if not isinstance(sender, Player): return InputHandleType.UNSUPPORTED_SENDER player = sender first_arg = command_input.get_arg(0) if not first_arg: self.send_help(player) return InputHandleType.HANDLED item = get_reference(player, " ".join(first_arg)) can_ref = Item.CANNOT_SEE # by default, make it so they can't see the item if item is not None: can_ref = item.can_reference(player) # check if they can reference/see the item if can_ref[0] is False: player.send_message(can_ref[1]) return InputHandleType.HANDLED player.location.on_item_use(handler, player, item) return InputHandleType.HANDLED
def on_input(self, handler: Handler, player: Player, command_input: CommandInput): command = command_input.get_command().lower() if player != self.allowed_player or (command != "setting" and not command.startswith(":")): # or type(player.output) is not RichStreamOutput: return None output = player.output def handle_function( already_handled: List[InputHandleType]) -> InputHandleType: arg_index = 1 arg = None if command.startswith( ":"): # the player is typing the argument as the command arg = command.replace(":", "") arg_index = 0 elif len(command_input.get_arg(1)) != 0: arg = command_input.get_arg(0)[0].lower() # TODO, fix this code with : and setting and this is terrible if arg is not None: if arg == "speed": speed = command_input.get_arg( arg_index + 0)[0].lower() # remember, arg 0 is the second word if speed == "fast": output.wait_multiplier = 0.4 player.send_message("Set speed to fast.") return InputHandleType.HANDLED_AND_DONE elif speed == "normal": output.wait_multiplier = 1 player.send_message("Set speed to normal") return InputHandleType.HANDLED_AND_DONE player.send_message( Message( "Help for setting command: \n" "\tspeed: setting speed <fast:normal>", MessageType.IMMEDIATE)) return InputHandleType.HANDLED_AND_DONE return InputHandle(InputHandle.PRIORITY_CLIENT, handle_function, self)
def _handle_command(self, handler: Handler, sender: CommandSender, command_input: CommandInput) -> InputHandleType: if not isinstance(sender, Player): return InputHandleType.NOT_HANDLED first_arg = command_input.get_arg(0) if len(first_arg) != 0: # thing stuff # here to referenced_item = get_reference(sender, " ".join(first_arg)) if referenced_item is None: sender.send_message(Item.CANNOT_SEE[1]) return InputHandleType.HANDLED can_ref = referenced_item.can_reference(sender) if can_ref[0] is False: sender.send_message(can_ref[1]) else: # here is how you should use get_reference can_do_sense = self.can_sense(referenced_item, sender) if can_do_sense[0]: self.sense(referenced_item, handler, sender) else: sender.send_message(can_do_sense[1]) else: # location stuff self.sense(sender.location, handler, sender) return InputHandleType.HANDLED
def _handle_command(self, handler: Handler, sender: CommandSender, command_input: CommandInput): if not isinstance(sender, Player): return InputHandleType.UNSUPPORTED_SENDER player = sender first_arg = command_input.get_arg(0) if not first_arg: self.send_help(player) return InputHandleType.HANDLED item = get_reference(player, " ".join(first_arg)) if item is None: player.send_message(Item.CANNOT_SEE[1]) return InputHandleType.HANDLED can_ref = item.can_reference(player) if can_ref[0] is False: player.send_message(can_ref[1]) return InputHandleType.HANDLED can_take = item.can_take(player) if can_take[0] is False: player.send_message(can_take[1]) return InputHandleType.HANDLED # if we are here, the player is allowed to take this item if item in player.location.items: if item.change_holder(player.location, player): player.location.on_take(handler, item, player) player.send_message(Message("You took {}.", named_variables=[item])) else: player.send_message("You were unable to take the item because we couldn't find who had the item.\n" "Obviously, this is kind of embarrassing so please tell someone what you did " "so it can be fixed.") # previous_holder = item.holder # if item.change_holder(previous_holder, player) and isinstance(previous_holder, Location): # previous_holder.on_take(handler, item) return InputHandleType.HANDLED
def auto_flag_setup(): command = CommandInput(CommandInput.join(sys.argv)) options = { ("rest", "r"): 1, ("simple", "windows", "dos"): 0, ("file", "f", "save", "path"): 1, ("clean", "no_load"): 0, ("user", "u", "player", "name"): 1 } flag_data = FlagData(command, options) rest = 0.001 string_rest = flag_data.get_flag("rest") if string_rest is not None: try: rest = float(string_rest) except ValueError: print("'{}' is not a valid number for rest.".format(string_rest)) sys.exit(1) if rest > 1: print("Rest cannot be greater than 1. Rest is the amount of time in seconds it waits to update.") print("Making this greater than 1 makes the game unresponsive for too long of a period.") sys.exit(1) string_file = flag_data.get_flag("file") save_path = SavePath(Path("./save.dat.d")) if string_file: save_path = SavePath(Path(string_file)) is_clean = flag_data.get_flag("clean") # should we load data? player_handler = PlayerHandler(save_path) player_savable = None result = player_handler.load_player_savables() # load these anyway player_name = flag_data.get_flag("user") if player_name is not None: if is_clean: print("Error: Using --clean flag but also specifying a player to load.") sys.exit(1) if not result[0]: print(result[1]) sys.exit(1) player_savable = player_handler.get_player_savable(player_name) if player_savable is None: print("Unable to find player with name: '{}'".format(player_name)) sys.exit(1) else: print("Successfully loaded player: '{}'".format(player_savable.name)) if flag_data.get_flag("simple"): # setup_simple(player_savable) information = create_simple_player(player_savable) else: try: import curses import pyparsing except ModuleNotFoundError: print("Unable to load curses or pyparsing library. Initializing simple instead of fancy") # setup_simple(player_savable) information = create_simple_player(player_savable) else: # setup_fancy(player_savable) information = create_fancy_player(curses.initscr(), player_savable) player, custom_managers, end_function = information try: main_instance = ClientSideMain(NinjaGame(), custom_managers, player, save_path, rest=rest, player_handler=player_handler) main_instance.start() while True: main_instance.update() time.sleep(rest) finally: end_function()
def _should_handle_command(self, command_input: CommandInput): return command_input.get_command().lower() in self.command_names