module RubyMud

require "Player.rb"

require "Commands.rb"
class ClientQuitError < RuntimeError; end

require "InputHandler.rb"

class DefaultInputHandler  < InputHandler
    def initialize(clientconnection)
        super(clientconnection)
        @theline = ""
    end

    def recieve(line)
        tokens = line.split(' ')	
		if ((@clientconnection.player == nil) and (tokens[0] != "login"))
			@clientconnection.send("You have to login first, type <bright white>login new<grey> or <bright white>login username password\r\n")
			return
		end

        Commands.doCommand(@clientconnection,line)
    end

end

class ClientConnection

	attr_accessor :player

	def initialize(session)
		@theline = ""
		@session = session
		@player = nil

        @inputhandler = []
        @inputhandler.push(DefaultInputHandler.new(self))
        
		send("<red>Welcome <grey>to the RubyMud development centre\r\n")
		send("Type <white>login new<grey> or <white>login username password<grey> to continue\r\n")
	end
	
	def receive(line)
	end
	
	def receiveChar(char)
        @inputhandler[-1].recieveChar(char)
    end	
	
	def send(line)
		@session.print(color(line))
	end
	
	# Use this function to add an input handler to the top of
    # the stac
    def addInputHandler(ih)
        @inputhandler.push(ih)
    end
    
    # Use this function to remove an input handler from the stack
    # Note: at the moment this only works if the given input
    # handler is the top one. Later this can be extended, though
    def removeInputHandler(ih)
        if @inputhandler[-1] == ih
            @inputhandler.pop()
        end
    end
end
end