From 64bacccb8b10f40289c25009108064c6aecec971 Mon Sep 17 00:00:00 2001 From: Marrub Date: Fri, 11 Aug 2017 18:16:16 -0400 Subject: [PATCH] Add minor code clean-up --- source/common.rb | 9 ++++++--- source/main.rb | 8 +++----- source/mod_base.rb | 2 +- source/mod_doomrla.rb | 7 +++++-- source/mod_fun.rb | 5 +++-- source/module.rb | 20 ++++++++++---------- source/server.rb | 8 ++++---- source/sv_discord.rb | 4 ++-- 8 files changed, 34 insertions(+), 29 deletions(-) diff --git a/source/common.rb b/source/common.rb index 0e9f1a9..7e44f9c 100644 --- a/source/common.rb +++ b/source/common.rb @@ -1,5 +1,5 @@ module Vrobot4 - Version = '4.00'.freeze + Version = "4.00".freeze @@debug = 0 def self.debug= set; @@debug = set; end @@ -9,11 +9,14 @@ module Vrobot4 if (lv != :DEBUG || @@debug >= 1) && (lv != :DEBUGV || @@debug >= 2) puts "[" + lv.to_s.ljust(6) + "] " + text.join(" ") + true + else + false end end - def self.is_num? s - /\A[-+]?[0-9]*\.?[0-9]+\Z/ === s + def self.is_num? str + /\A[-+]?[0-9]*\.?[0-9]+\Z/ === str end end diff --git a/source/main.rb b/source/main.rb index d80ae2e..e9985f1 100644 --- a/source/main.rb +++ b/source/main.rb @@ -30,13 +30,11 @@ module Vrobot4 thrds.each {|th| th.join} end - def self.main cfgname - cfg = nil - + def self.main cfg log :INFO, "vrobot version", Version begin - cfg = YAML.load IO.read(cfgname) + cfg = YAML.load cfg.read rescue log :ERROR, "error reading bot config:", $! return @@ -59,6 +57,6 @@ module Vrobot4 end end -Vrobot4.main "bot.yml" +Vrobot4.main open("bot.yml") ## EOF diff --git a/source/mod_base.rb b/source/mod_base.rb index 948c72a..7aff276 100644 --- a/source/mod_base.rb +++ b/source/mod_base.rb @@ -64,7 +64,7 @@ module Vrobot4::Server Vrobot4.debug = argv[0].to_i end - def onCommand m, cnam, argv + def on_command m, cnam, argv Vrobot4.log :DEBUGV, "command", cnam.to_s, argv.to_s super end diff --git a/source/mod_doomrla.rb b/source/mod_doomrla.rb index d80ca8a..3154123 100644 --- a/source/mod_doomrla.rb +++ b/source/mod_doomrla.rb @@ -33,6 +33,7 @@ class Mod_DoomRLA < Vrobot4::Module::Module def c_weaponinfo m, argv name = argv.join(" ") w = @winfo.select {|wep| wep if wep["Name"].casecmp? name}[0] + raise ArgumentError, "Weapon not found" if w == nil t = "%s %s damage\n" % [w["Damage"], w["DamageType"]] t << "%s shots\n" % [w["Shots"]] if w["Shots"].to_i > 1 t << "Uses %s %s\n" % [w["AmmoUsage"], w["AmmoType"]] \ @@ -53,10 +54,12 @@ class Mod_DoomRLA < Vrobot4::Module::Module def c_monsterinfo m, argv name = argv.join(" ") - @minfo.select do |mon| + mi = @minfo.select do |mon| mon if mon["Name"].casecmp? name or mon["Tags"].split(", ").include? name - end .first(3).each do |w| + end + raise ArgumentError, "No monsters found" if mi == nil + mi.first(3).each do |w| t = "%i Health\n" % [w["Health"]] t << "Resistances: %s\n" % [w["Resistances"]] t << "Drops: %s\n" % [w["ItemDrops"]] if w["ItemDrops"] diff --git a/source/mod_fun.rb b/source/mod_fun.rb index d7db477..af148ee 100644 --- a/source/mod_fun.rb +++ b/source/mod_fun.rb @@ -64,7 +64,8 @@ class Mod_Fun < Vrobot4::Module::Module argv = argv.join(" ") if argv.length == 0 max = JSON.parse(open(QDB + "/interface.cgi").read)["numQuotes"] - m.reply_b open(QDB + "/q/" + (rand(max) + 1).to_s).read + num = rand(max) + 1 + m.reply_b "Quote %i:\n" % [num] + open(QDB + "/q/" + num.to_s).read elsif Vrobot4.is_num? argv m.reply_b open(QDB + "/q/" + argv.to_i.to_s).read else @@ -73,7 +74,7 @@ class Mod_Fun < Vrobot4::Module::Module info = JSON.parse(open(QDB + "/interface.cgi?query=" + q).read) res = info["searchResults"].sample raise ArgumentError, "no results found" unless res - m.reply_b open(QDB + "/q/" + res.to_s).read + m.reply_b "Quote %i:\n" % [res] + open(QDB + "/q/" + res.to_s).read end end diff --git a/source/module.rb b/source/module.rb index b226f6e..46ae249 100644 --- a/source/module.rb +++ b/source/module.rb @@ -8,12 +8,11 @@ module Vrobot4::Module @roles = /[#{roles}]/ end - def usable_in? m, type + def usable_in? m + type = @function.owner role = m.user.roles mprm = m.serv.mprm - retm = true - retc = false - retr = false + retm, retc, retr = true, false, false retm = mprm[:glob][type] if mprm[:glob].key? type retc = mprm[:chan][type][m.chan] if mprm[:chan].key? type retr = mprm[:role][type].scan /[#{role}]/ if mprm[:role].key? type @@ -32,26 +31,27 @@ module Vrobot4::Module Vrobot4.log :DEBUG, "initialized", self.to_s end - def register fn, cnam, help, roles: "v" + def register fn, cnam, help = nil, roles: "v" + help = "No help available for this command." if help == nil @commands[cnam] = Command.new(self.method(fn), help, roles).freeze end - def onMessage m + def on_message m false end - def onCommand m, cnam, argv + def on_command m, cnam, argv if (cmd = get_cmd m, cnam) begin; cmd.run(m, argv) rescue; m.reply "Error:", $!.to_s; end - return true + true else - return false + false end end def all_cmds m - @commands.select {|name, cmd| cmd.usable_in? m, self.class} + @commands.select {|name, cmd| cmd.usable_in? m} end def get_cmd m, name diff --git a/source/server.rb b/source/server.rb index 10d8fc8..10205d4 100644 --- a/source/server.rb +++ b/source/server.rb @@ -59,12 +59,12 @@ module Vrobot4::Server @modules.each {|mod| yield mod} end - def onMessage m - @modules.each {|mod| break if mod.onMessage m} + def on_message m + @modules.each {|mod| break if mod.on_message m} end - def onCommand m, cnam, argv - @modules.each {|mod| break if mod.onCommand m, cnam, argv} + def on_command m, cnam, argv + @modules.each {|mod| break if mod.on_command m, cnam, argv} end def type diff --git a/source/sv_discord.rb b/source/sv_discord.rb index 4320ace..7d00d88 100644 --- a/source/sv_discord.rb +++ b/source/sv_discord.rb @@ -58,9 +58,9 @@ class Sv_Discord < Vrobot4::Server::AudioServer if m.msg.start_with? '.' argv = m.msg.split cnam = argv.shift[1..-1] - onCommand m, cnam, argv + on_command m, cnam, argv else - onMessage m + on_message m end end end