vrobot4/source/main.rb

75 lines
1.5 KiB
Ruby

require './common.rb'
require './module.rb'
require './server.rb'
require './robots.rb'
module Vrobot4
private
# Loads a bot from its configuration +info+.
# @param info [Hash] arbitrary bot information
# ["type"] The type by name of the bot.
# @return [Vrobot4::Robots::Bot] the loaded bot
def self.load_bot info
type = info[:type]
Robots::get_bot_type(type).new info
end
# Runs all bots in +bots+ in separate threads.
# @param bots [Array<Vrobot4::Robots::Bot>] all bots to run
# @return [void]
def self.run_bots bots
threads = []
bots.each do |bot|
threads << Thread.new do
bot.connect
end
end
threads.each do |thread|
thread.join
end
end
public
# Runs the program.
# @param cfg [String] configuration file to read
# @return [void]
def self.main cfg
Thread.abort_on_exception = true
log :INFO, "vrobot version #{Version}"
load cfg
cfg = Config.get
Vrobot4.set_debug(cfg[:debug] || 0)
cfg[:load].each do |mod|
load mod, true
end
begin
bots = []
cfg[:bots].each do |info|
bots << load_bot(info)
end
log :DEBUGV, "bots: #{bots.to_s}"
rescue
err = $!.to_s + ?\n + $!.backtrace.first(3).join(?\n)
log :ERROR, "error loading bot config: #{err}"
return
end
run_bots bots
end
end
Vrobot4.main open ARGV[0]
## EOF