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'
|
2017-08-06 05:36:15 -07:00
|
|
|
|
|
|
|
require 'yaml'
|
|
|
|
|
|
|
|
module Vrobot4
|
2017-08-11 19:40:43 -07:00
|
|
|
private
|
2019-02-23 20:46:40 -08:00
|
|
|
|
|
|
|
# Loads a bot from its configuration +botinfo+.
|
|
|
|
# @param botinfo [Hash] arbitrary bot information
|
|
|
|
# ["type"] The type by name of the bot.
|
|
|
|
# @return [Vrobot4::Server] the loaded bot
|
|
|
|
def self.load_bot botinfo
|
2017-08-11 20:07:24 -07:00
|
|
|
type = botinfo["type"]
|
2019-02-23 20:46:40 -08:00
|
|
|
serv = Server::get_server_type(type).new botinfo
|
2017-08-06 05:36:15 -07:00
|
|
|
|
2019-02-23 20:46:40 -08:00
|
|
|
for mod in botinfo["modules"] do
|
2017-08-12 16:22:31 -07:00
|
|
|
serv.load_mod mod
|
2019-02-23 20:46:40 -08:00
|
|
|
end
|
2017-08-06 05:36:15 -07:00
|
|
|
|
|
|
|
serv
|
|
|
|
end
|
|
|
|
|
2019-02-23 20:46:40 -08:00
|
|
|
# Runs all bots in +bots+ in separate threads.
|
|
|
|
# @param bots [Array<Vrobot4::Server>] all bots to run
|
|
|
|
# @return [void]
|
|
|
|
def self.run_bots bots
|
|
|
|
threads = []
|
|
|
|
|
|
|
|
bots.each do |server|
|
|
|
|
threads << Thread.new do
|
|
|
|
server.connect
|
|
|
|
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-23 20:46:40 -08:00
|
|
|
# @param cfg [IO] configuration file to read
|
|
|
|
# @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
|
|
|
|
2017-08-09 00:52:34 -07:00
|
|
|
begin
|
2017-08-11 15:16:16 -07:00
|
|
|
cfg = YAML.load cfg.read
|
2017-08-09 00:52:34 -07:00
|
|
|
rescue
|
2019-02-23 20:46:40 -08:00
|
|
|
log :ERROR, "error reading bot config: #{$!}"
|
2017-08-09 00:52:34 -07:00
|
|
|
return
|
|
|
|
end
|
2017-08-06 05:36:15 -07:00
|
|
|
|
2019-02-23 20:46:40 -08:00
|
|
|
Vrobot4.set_debug(cfg["debug"] || 0)
|
|
|
|
|
|
|
|
cfg["load"].each do |mod|
|
|
|
|
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
|
|
|
|
|
|
|
cfg["bots"].each do |botinfo|
|
|
|
|
bots << load_bot(botinfo)
|
|
|
|
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
|