core refactoring

This commit is contained in:
Er2 2021-07-20 09:56:14 +03:00
parent 49fa684f47
commit f15c139502
19 changed files with 114 additions and 91 deletions

View File

@ -1 +0,0 @@
return require 'core.core'

View File

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

View File

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

1
etc/api/init.lua Normal file
View File

@ -0,0 +1 @@
return require 'etc.api.core'

View File

@ -4,7 +4,7 @@
--]]
local tools = {
json = require 'core.json',
json = require 'etc.json',
}
local json = tools.json

View File

View File

@ -1,4 +1,4 @@
local mp = require 'db.mp'
local mp = require 'etc.db.mp'
local tools = {
isFile = function(path)

View File

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

View File

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

View File

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

76
src/core/core.lua Normal file
View File

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

16
src/core/db.lua Normal file
View File

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