From f15c139502cd8f9bee8f4f5a2eaaa4c1e840037e Mon Sep 17 00:00:00 2001 From: Er2 Date: Tue, 20 Jul 2021 09:56:14 +0300 Subject: [PATCH] core refactoring --- core/init.lua | 1 - {core => etc/api}/api.lua | 6 +-- {core => etc/api}/core.lua | 12 ++--- etc/api/init.lua | 1 + {core => etc/api}/tools.lua | 2 +- {db => etc/db}/.gitignore | 0 {db => etc/db}/init.lua | 2 +- {db => etc/db}/mp.lua | 0 {core => etc}/events.lua | 5 +- {core => etc}/json.lua | 0 init.lua | 76 +----------------------------- {cmds => src/cmds}/eval.lua | 0 {cmds => src/cmds}/ping.lua | 0 {cmds => src/cmds}/rub.lua | 8 +++- src/core/core.lua | 76 ++++++++++++++++++++++++++++++ src/core/db.lua | 16 +++++++ {events => src/events}/command.lua | 0 {events => src/events}/message.lua | 0 {events => src/events}/ready.lua | 0 19 files changed, 114 insertions(+), 91 deletions(-) delete mode 100644 core/init.lua rename {core => etc/api}/api.lua (96%) rename {core => etc/api}/core.lua (94%) create mode 100644 etc/api/init.lua rename {core => etc/api}/tools.lua (98%) rename {db => etc/db}/.gitignore (100%) rename {db => etc/db}/init.lua (98%) rename {db => etc/db}/mp.lua (100%) rename {core => etc}/events.lua (87%) rename {core => etc}/json.lua (100%) rename {cmds => src/cmds}/eval.lua (100%) rename {cmds => src/cmds}/ping.lua (100%) rename {cmds => src/cmds}/rub.lua (88%) create mode 100644 src/core/core.lua create mode 100644 src/core/db.lua rename {events => src/events}/command.lua (100%) rename {events => src/events}/message.lua (100%) rename {events => src/events}/ready.lua (100%) diff --git a/core/init.lua b/core/init.lua deleted file mode 100644 index 9855539..0000000 --- a/core/init.lua +++ /dev/null @@ -1 +0,0 @@ -return require 'core.core' diff --git a/core/api.lua b/etc/api/api.lua similarity index 96% rename from core/api.lua rename to etc/api/api.lua index 4ff7594..ce537dd 100644 --- a/core/api.lua +++ b/etc/api/api.lua @@ -3,9 +3,9 @@ -- Zlib License --]] -local tools =require 'core.tools' -local json = require 'core.json' -local events=require 'core.events' +local tools =require 'etc.api.tools' +local json = require 'etc.json' +local events=require 'etc.events' local api = { _ev_ = {} } api.__index = api -- Make class diff --git a/core/core.lua b/etc/api/core.lua similarity index 94% rename from core/core.lua rename to etc/api/core.lua index 23db15c..2db4bba 100644 --- a/core/core.lua +++ b/etc/api/core.lua @@ -3,8 +3,8 @@ -- Zlib License --]] -local tools = require 'core.tools' -local api = require 'core.api' +local tools = require 'etc.api.tools' +local api = require 'etc.api.api' api.__index = api function api:_ev(t, i, name, ...) @@ -116,10 +116,10 @@ return function(opts) if not token or type(token) ~= 'string' then token = nil end local self = setmetatable({}, api) - if not opts then return self end - - if opts.token then self.token = opts.token end - if opts.norun then self.nr = true end + if type(opts) == 'table' then + if opts.token then self.token = opts.token end + if opts.norun then self.nr = true end + end return self end diff --git a/etc/api/init.lua b/etc/api/init.lua new file mode 100644 index 0000000..51454ae --- /dev/null +++ b/etc/api/init.lua @@ -0,0 +1 @@ +return require 'etc.api.core' diff --git a/core/tools.lua b/etc/api/tools.lua similarity index 98% rename from core/tools.lua rename to etc/api/tools.lua index c877d6e..56b2db5 100644 --- a/core/tools.lua +++ b/etc/api/tools.lua @@ -4,7 +4,7 @@ --]] local tools = { - json = require 'core.json', + json = require 'etc.json', } local json = tools.json diff --git a/db/.gitignore b/etc/db/.gitignore similarity index 100% rename from db/.gitignore rename to etc/db/.gitignore diff --git a/db/init.lua b/etc/db/init.lua similarity index 98% rename from db/init.lua rename to etc/db/init.lua index 8073f22..24d936b 100644 --- a/db/init.lua +++ b/etc/db/init.lua @@ -1,4 +1,4 @@ -local mp = require 'db.mp' +local mp = require 'etc.db.mp' local tools = { isFile = function(path) diff --git a/db/mp.lua b/etc/db/mp.lua similarity index 100% rename from db/mp.lua rename to etc/db/mp.lua diff --git a/core/events.lua b/etc/events.lua similarity index 87% rename from core/events.lua rename to etc/events.lua index a44e09d..a91e1bf 100644 --- a/core/events.lua +++ b/etc/events.lua @@ -29,9 +29,10 @@ function events:emit(name, ...) local t = self._ev_ for i = 1, #t do local v = t[i] or {} - if type(v) == 'table' + if type(v) == 'table' + and type(v.name) == 'string' and type(v.type) == 'string' - and type(v.fn) == 'function' + and type(v.fn) == 'function' then self:_ev(t, i, name, ...) end end end diff --git a/core/json.lua b/etc/json.lua similarity index 100% rename from core/json.lua rename to etc/json.lua diff --git a/init.lua b/init.lua index a4788be..8ba5bb7 100644 --- a/init.lua +++ b/init.lua @@ -1,75 +1 @@ -local config = require 'config' - -local Core = { - db = require 'db' ('db'), -- db with name db - tg = require 'core', - tools = tools, - config = config, - cmds = {}, -} -local tg = Core.tg - -function Core:load(what) - local c = config[what] - local s = #c - for i = 1, s do - local v = c[i] - - print(('Loading %s (%d / %d) %s...'):format(what:sub(0, -2), i, s, v)) - -- Lint - if pcall(require, what ..'.'.. v) then - local a=require(what ..'.'.. v) - if what == 'events' then self.api:on(v, a) - elseif what == 'cmds' then self.cmds[v] = a - end - else print 'fail' end - end - print(('Loaded %d %s'):format(s, what)) -end - -function Core:ev(t, i, name, ...) - local v = t[i] - if v.name == name then - local succ = pcall(v.fn, self, ...) - if not succ then print('event "' .. name .. '" was failed') end - if v.type == 'once' then table.remove(t, i) end - end -end - -function Core:init() - self.api = tg {norun = true} - - print 'Client initialization...' - - self.api._ev = function(s, t, i, name, ...) - -- print(s, t, i, name) - self:ev(t, i, name, s, ...) - end - - self:load 'events' - - self.api:login(config.token, function() - print('Logged on as @' .. self.api.info.username) - self.config.token = nil - self.api:emit('ready') - end) - - print 'Done!' - - local offs, o = 0 - self.t = os.time() - self.api.runs = true - while self.api.runs do - o = self.api:_getUpd(1, offs, 0) - offs = o and o or offs - - if os.time() - self.t >= 60 * 5 then - self.t = os.time() - print 'saving...' - self.db:save() - end - end - self.api:getUpdates(1, offs, 0) -end - -Core:init() +require 'src.core.core' diff --git a/cmds/eval.lua b/src/cmds/eval.lua similarity index 100% rename from cmds/eval.lua rename to src/cmds/eval.lua diff --git a/cmds/ping.lua b/src/cmds/ping.lua similarity index 100% rename from cmds/ping.lua rename to src/cmds/ping.lua diff --git a/cmds/rub.lua b/src/cmds/rub.lua similarity index 88% rename from cmds/rub.lua rename to src/cmds/rub.lua index 7bf2508..113c74e 100644 --- a/cmds/rub.lua +++ b/src/cmds/rub.lua @@ -1,4 +1,8 @@ -local rub = { +-- It uses data from central bank of Russia +--- and external service to get json from xml +-- Privacy and security is unknown + +local rub = { url = 'https://api.factmaven.com/xml-to-json/?xml=' .. 'https://www.cbr.ru/scripts/XML_daily.asp', fmt = function(v, fmt) @@ -8,7 +12,7 @@ } function rub:course(wants, fmt) - local resp, succ = (require'core.tools').requ(self.url) + local resp, succ = (require 'etc.api.tools').requ(self.url) if not succ then return {}, '[ошибка]', {} end diff --git a/src/core/core.lua b/src/core/core.lua new file mode 100644 index 0000000..b2f2dbb --- /dev/null +++ b/src/core/core.lua @@ -0,0 +1,76 @@ +local config = require 'config' + +local Core = { + tg = require 'etc.api', + config = config, + cmds = {}, + + _ev_ = {}, -- :evil: +} +local tg = Core.tg + +Core = (require 'etc.events')(Core) -- add events + +function Core:load(what) + local c = config[what] + local s = #c + for i = 1, s do + local v = c[i] + + print(('Loading %s (%d / %d) %s...'):format(what:sub(0, -2), i, s, v)) + -- Lint + if pcall(require, 'src.'.. what ..'.'.. v) then + local a=require('src.'.. what ..'.'.. v) + if what == 'events' then self.api:on(v, a) + elseif what == 'cmds' then self.cmds[v] = a + elseif what == 'core' then a(self) + end + else print 'fail' end + end + print(('Loaded %d %s'):format(s, what)) +end + +function Core:ev(t, i, name, ...) + local v = t[i] + if v.name == name then + local succ = pcall(v.fn, self, ...) + if not succ then print('event "' .. name .. '" was failed') end + if v.type == 'once' then table.remove(t, i) end + end +end + +function Core:init() + self.api = tg {norun = true} + + print 'Client initialization...' + + function Core._ev(ev, ...) self:ev(...) end + function self.api._ev(ev, t, i, n, ...) + self._ev(ev, t, i, n, self.api, ...) + end + + self:load 'events' + self:load 'core' + + self.api:login(config.token, function() + print('Logged on as @' .. self.api.info.username) + self.config.token = nil + self.api:emit 'ready' + end) + + self:emit 'init' + print 'Done!' + + local offs, o = 0 + self.t = os.time() + self.api.runs = true + while self.api.runs do + self:emit 'tick' + + o = self.api:_getUpd(1, offs, 0) + offs = o and o or offs + end + self.api:getUpdates(1, offs, 0) +end + +Core:init() diff --git a/src/core/db.lua b/src/core/db.lua new file mode 100644 index 0000000..92c9aa5 --- /dev/null +++ b/src/core/db.lua @@ -0,0 +1,16 @@ +return function(Core) +local path = 'etc/db' -- from root +local time = 60 * 5 -- 5min + +Core.db = require 'etc.db' (path) + +local t = os.time() +Core:on('tick', function() + if os.time() - t >= time then + t = os.time() + print 'saving...' + Core.db:save() + end +end) + +end diff --git a/events/command.lua b/src/events/command.lua similarity index 100% rename from events/command.lua rename to src/events/command.lua diff --git a/events/message.lua b/src/events/message.lua similarity index 100% rename from events/message.lua rename to src/events/message.lua diff --git a/events/ready.lua b/src/events/ready.lua similarity index 100% rename from events/ready.lua rename to src/events/ready.lua