Add minor code clean-up

master
Marrub 2017-08-11 18:16:16 -04:00
parent a52e788a3f
commit 64bacccb8b
8 changed files with 34 additions and 29 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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"]

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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