vrobot4/source/main.rb

75 lines
1.5 KiB
Ruby
Raw Normal View History

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