core refactoring
This commit is contained in:
		
							parent
							
								
									49fa684f47
								
							
						
					
					
						commit
						f15c139502
					
				
					 19 changed files with 114 additions and 91 deletions
				
			
		|  | @ -1 +0,0 @@ | ||||||
| return require 'core.core' |  | ||||||
|  | @ -3,9 +3,9 @@ | ||||||
|   -- Zlib License |   -- Zlib License | ||||||
| --]] | --]] | ||||||
| 
 | 
 | ||||||
| local tools =require 'core.tools' | local tools =require 'etc.api.tools' | ||||||
| local json = require 'core.json' | local json = require 'etc.json' | ||||||
| local events=require 'core.events' | local events=require 'etc.events' | ||||||
| local api = { _ev_ = {} } | local api = { _ev_ = {} } | ||||||
| api.__index = api -- Make class | api.__index = api -- Make class | ||||||
| 
 | 
 | ||||||
|  | @ -3,8 +3,8 @@ | ||||||
|   -- Zlib License |   -- Zlib License | ||||||
| --]] | --]] | ||||||
| 
 | 
 | ||||||
| local tools = require 'core.tools' | local tools = require 'etc.api.tools' | ||||||
| local api = require 'core.api' | local api = require 'etc.api.api' | ||||||
| api.__index = api | api.__index = api | ||||||
| 
 | 
 | ||||||
| function api:_ev(t, i, name, ...) | function api:_ev(t, i, name, ...) | ||||||
|  | @ -116,10 +116,10 @@ return function(opts) | ||||||
|   if not token or type(token) ~= 'string' then token = nil end |   if not token or type(token) ~= 'string' then token = nil end | ||||||
| 
 | 
 | ||||||
|   local self = setmetatable({}, api) |   local self = setmetatable({}, api) | ||||||
|   if not opts then return self end |   if type(opts) == 'table' then | ||||||
| 
 |  | ||||||
|     if opts.token then self.token = opts.token end |     if opts.token then self.token = opts.token end | ||||||
|     if opts.norun then self.nr = true end |     if opts.norun then self.nr = true end | ||||||
|  |   end | ||||||
| 
 | 
 | ||||||
|   return self |   return self | ||||||
| end | end | ||||||
							
								
								
									
										1
									
								
								etc/api/init.lua
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								etc/api/init.lua
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1 @@ | ||||||
|  | return require 'etc.api.core' | ||||||
|  | @ -4,7 +4,7 @@ | ||||||
| --]] | --]] | ||||||
| 
 | 
 | ||||||
| local tools = { | local tools = { | ||||||
|   json = require 'core.json', |   json = require 'etc.json', | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| local json = tools.json | local json = tools.json | ||||||
							
								
								
									
										0
									
								
								db/.gitignore → etc/db/.gitignore
									
										
									
									
										vendored
									
									
								
							
							
						
						
									
										0
									
								
								db/.gitignore → etc/db/.gitignore
									
										
									
									
										vendored
									
									
								
							|  | @ -1,4 +1,4 @@ | ||||||
| local mp = require 'db.mp' | local mp = require 'etc.db.mp' | ||||||
| 
 | 
 | ||||||
| local tools = { | local tools = { | ||||||
|   isFile = function(path) |   isFile = function(path) | ||||||
|  | @ -30,6 +30,7 @@ function events:emit(name, ...) | ||||||
|   for i = 1, #t do |   for i = 1, #t do | ||||||
|     local v = t[i] or {} |     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.type) == 'string' | ||||||
|     and type(v.fn)   == 'function' |     and type(v.fn)   == 'function' | ||||||
|     then self:_ev(t, i, name, ...) end |     then self:_ev(t, i, name, ...) end | ||||||
							
								
								
									
										76
									
								
								init.lua
									
										
									
									
									
								
							
							
						
						
									
										76
									
								
								init.lua
									
										
									
									
									
								
							|  | @ -1,75 +1 @@ | ||||||
| local config = require 'config' | require 'src.core.core' | ||||||
| 
 |  | ||||||
| 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() |  | ||||||
|  |  | ||||||
|  | @ -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=' |   url = 'https://api.factmaven.com/xml-to-json/?xml=' | ||||||
|     .. 'https://www.cbr.ru/scripts/XML_daily.asp', |     .. 'https://www.cbr.ru/scripts/XML_daily.asp', | ||||||
|   fmt = function(v, fmt) |   fmt = function(v, fmt) | ||||||
|  | @ -8,7 +12,7 @@ | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| function rub:course(wants, fmt) | 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 |   if not succ then | ||||||
|     return {}, '[ошибка]', {} |     return {}, '[ошибка]', {} | ||||||
|   end |   end | ||||||
							
								
								
									
										76
									
								
								src/core/core.lua
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										76
									
								
								src/core/core.lua
									
										
									
									
									
										Normal 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
									
								
							
							
						
						
									
										16
									
								
								src/core/db.lua
									
										
									
									
									
										Normal 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 | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue