module MimudServer # parse_input will check the data received with receve_data # and check it against a list of commands or situations that # are dependant on the player's current state. A new state can # be added just by expanding the case statement here, just make # sure each section passes through the states as required. def parse_input(data) data.chomp! case @player.state when :get_name get_name(data) when :playing cmd = get_command(data) # Get the first word of player input. args = get_args(data) # Get the rest of the players input. if @@command_table.include?(cmd) # Is it a command? send(@@command_table[cmd], args) # Then execute it! else send_to_player("That command is not recognized.") # Not today pal. end end end # get_name is very simple right now. All it does # is check to make sure that the information being # input is between MIN_NAME and MAX_NAME, which are # defined in constants.rb. If they are, it's set's the # player's name to the data input and then changes their # state to :playing. While this is very basic right now it # should explain how a character creation process could be # done. def get_name(data) if data.size > MIN_NAME && data.size < MAX_NAME @player.name = data @player.state = :playing else send_to_player("Your name must be between #{MIN_NAME} and #{MAX_NAME} characters.\r\n") send_to_player("Please enter your name: ") end end # This is a little helper method I put together to # keep the state case above as uncluttered as possible. # It will accept a string (the player's input) and pull # out the very first word, in lowercase and return it as # a string. def get_command(text) return text.split(' ')[0].downcase end # This is another helper method I wrote for the same reason # as get_command. It will go through a string (the player's # input), and return everything but the first word as a string. def get_args(text) return text.split(' ')[1..text.size].join(' ') end # Another helper method to include color parsing and a newline # for sending data to a socket. It accepts a string of text # that may include any ansi codes included in the ansi_table # defined in server.handler.ansi and change them into the # appropriate color. def send_to_player(text) send_data("#{parse_color(text)}\r\n") end # Yet another simple helper method. It accepts a string that # may include color, and passes it everyone player except the # one in scope. def send_to_all_but(text) @@client_list.each do |c| next if c == self c.send_to_player(text) end end end