2017-08-06 05:36:15 -07:00
|
|
|
require './common.rb'
|
|
|
|
require './module.rb'
|
2017-08-08 01:35:20 -07:00
|
|
|
require './server.rb'
|
2019-02-23 22:43:50 -08:00
|
|
|
require './robots.rb'
|
2017-08-06 05:36:15 -07:00
|
|
|
|
|
|
|
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
|
2017-08-06 05:36:15 -07:00
|
|
|
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
|
2017-08-06 05:36:15 -07:00
|
|
|
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}"
|
2017-08-06 05:36:15 -07:00
|
|
|
|
2019-02-24 03:00:41 -08:00
|
|
|
load cfg
|
|
|
|
cfg = Config.get
|
2017-08-06 05:36:15 -07:00
|
|
|
|
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
|
2017-08-06 16:28:17 -07:00
|
|
|
|
2017-08-06 05:36:15 -07:00
|
|
|
begin
|
2017-08-06 16:28:17 -07:00
|
|
|
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}"
|
2017-08-06 05:36:15 -07:00
|
|
|
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
|
2017-08-06 05:36:15 -07:00
|
|
|
end
|
2017-08-09 00:52:34 -07:00
|
|
|
|
2019-02-23 20:46:40 -08:00
|
|
|
run_bots bots
|
2017-08-06 05:36:15 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-02-23 20:46:40 -08:00
|
|
|
Vrobot4.main open ARGV[0]
|
2017-08-06 05:36:15 -07:00
|
|
|
|
|
|
|
## EOF
|