Add initial module/server implementation

master
Marrub 2017-08-06 08:36:15 -04:00
parent a3164706e5
commit 74d468c3ba
7 changed files with 200 additions and 0 deletions

3
.gitignore vendored
View File

@ -1 +1,4 @@
working
*.yml
*.swp
*.vim

15
source/common.rb Normal file
View File

@ -0,0 +1,15 @@
module Vrobot4
Version = '4.00'.freeze
@@debug = false
def self.debug= set; @@debug = !!set; end
def self.debug ; @@debug; end
def self.log lv, *text
if lv != :DEBUG || @@debug
puts "[" + lv.to_s.center(8) + "] " + text.join(" ")
end
end
end
## EOF

56
source/main.rb Normal file
View File

@ -0,0 +1,56 @@
require './common.rb'
require './server.rb'
require './module.rb'
require 'yaml'
module Vrobot4
def self.loadServer servinfo
name = servinfo["name"]
type = servinfo["type"]
log :INFO, "loadServer: loading configuration for", name
serv = Server::get_server_type(type).new(servinfo)
servinfo.has_key?("modules") && servinfo["modules"].each \
{|mod| serv.loadMod Module::get_module_type(mod).new}
serv
end
def self.loadBot botinfo
servers = []
log :INFO, "loadBot: loading configuration for", botinfo["name"]
botinfo["servers"].each {|servinfo| servers << loadServer(servinfo)}
servers
end
def self.main cfgname
cfg = nil
log :INFO, "vrobot version", Version
begin; cfg = YAML.load IO.read(cfgname)
rescue; log :ERROR, "error reading bot config:", $!; exit; end
begin
bots = []
thrds = []
cfg["loadmods"].each {|mod| load mod, true}
cfg["bots"] .each {|botinfo| bots << loadBot(botinfo)}
log :DEBUG, "bots:", bots.to_s
bots.each do |servs|
servs.each {|serv| thrds << Thread.new {serv.connect}}
end
thrds.each {|th| th.join}
rescue
log :ERROR, "error loading bot configuration:", $!
exit
end
end
end
Vrobot4.main "bot.yml"
## EOF

14
source/mod_util.rb Normal file
View File

@ -0,0 +1,14 @@
class Mod_Util < Vrobot4::Module::Module
Vrobot4::Module.add_module_type self, "Utilities"
def initialize
super()
register :testf, "Test function."
end
def testf msg, argv
puts "test function run", msg, argv
end
end
## EOF

45
source/module.rb Normal file
View File

@ -0,0 +1,45 @@
module Vrobot4::Module
class Command
attr_reader :help_str
def initialize fn, help
@function = fn
@help_str = help
end
def run msg, argv
@function.call(msg, argv)
end
end
class Module
def initialize
@commands = {}
end
def register fn, help
@commands[fn.to_s] = Command.new(self.method(fn), help)
end
def eachCmd
@commands.each {|cmd| yield cmd}
end
def onCommand msg, cmdname, argv
@commands[cmdname].run(msg, argv) if @commands.has_key? cmdname
end
end
@@module_types = {}
def self.add_module_type t, name
@@module_types[name] = t
Vrobot4.log :INFO, "added module type:", name
end
def self.get_module_type s
@@module_types[s]
end
end
## EOF

53
source/server.rb Normal file
View File

@ -0,0 +1,53 @@
module Vrobot4::Server
class User
end
class Message
attr_reader :msg
def initialize msg
@msg = msg
end
def to_s
@msg
end
end
class Channel
end
class Server
def initialize
@modules = []
end
def loadMod mod
@modules << mod
end
def onMessage msg
if msg.start_with? '.'
onCommand msg, nil, nil
end
log :MSG, msg.to_s
end
def onCommand msg, cmdname, argv
@modules.each {|mod| mod.onCommand msg, cmdname, argv}
end
end
@@server_types = {}
def self.add_server_type t, name
@@server_types[name] = t
Vrobot4.log :INFO, "added server type:", name
end
def self.get_server_type s
@@server_types[s]
end
end
## EOF

14
source/sv_discord.rb Normal file
View File

@ -0,0 +1,14 @@
class Sv_Discord < Vrobot4::Server::Server
Vrobot4::Server.add_server_type self, "Discord"
def initialize info
super()
Vrobot4.log :INFO, "Sv_Discord:", info["serverid"]
end
def connect
puts "connected"
end
end
## EOF