vrobot4/source/main.rb

86 lines
1.7 KiB
Ruby
Raw Normal View History

require './common.rb'
require './module.rb'
require './server.rb'
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
type = botinfo["type"]
2019-02-23 20:46:40 -08:00
serv = Server::get_server_type(type).new botinfo
2019-02-23 20:46:40 -08:00
for mod in botinfo["modules"] do
serv.load_mod mod
2019-02-23 20:46:40 -08:00
end
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
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-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
2019-02-23 20:46:40 -08:00
Vrobot4.set_debug(cfg["debug"] || 0)
cfg["load"].each do |mod|
load mod, true
end
begin
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}"
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