require "Command.rb"

module RubyMud
class Commands
	private_class_method :new
	
	@@commands = []
	
	def Commands.getCommands()
		return @@commands
	end	
	
	def Commands.add(command)
		@@commands.push(command)
	end
	
	def Commands.doCommand(clientConnection,string)
		tokens = string.split(' ')
	
		# Loop through all the active commands
		if (tokens.length > 0)
			for command in @@commands
				# If this command doesnt have words, just invoke it			
				if command.words == nil
					next if !command.canUse?(clientConnection.player)
					if command.execute(clientConnection,string)
						return
					end
					next
				end
				# Check the word in this command
				for word in command.words				
					# If one of the words match, let the command handle the rest
					if word == tokens[0]
						if !command.canUse?(clientConnection.player)
							clientConnection.send("<red>You are not allowed to do that!\r\n")
							return
						end
						command.execute(clientConnection,string)
						return
					end
				end
			end
			clientConnection.send("I dont know what you want me to do "+clientConnection.player.name+":"+string+"\r\n")		
		end
		
		# Should the prompt be printed here ?
		
	end
	
end
end
# Dont need to add your commands manually anymore
# This now happens automatically
d = Dir.new("Commands")
d.each do |x|
	if (x.include?(".rb"))
		require x
	end
end