From e8fc2791e04f514227ff3263f03faf9822d6f12d Mon Sep 17 00:00:00 2001 From: Er2 Date: Wed, 21 Jul 2021 11:40:31 +0300 Subject: [PATCH] upd readme, some small changes --- README.md | 8 ++++ etc/api/api.lua | 6 +-- etc/events.lua | 5 ++- init.lua | 2 +- src/core/core.lua | 76 -------------------------------------- src/parts/client.lua | 35 ++++++++++++++++++ src/parts/core.lua | 44 ++++++++++++++++++++++ src/{core => parts}/db.lua | 0 8 files changed, 95 insertions(+), 81 deletions(-) delete mode 100644 src/core/core.lua create mode 100644 src/parts/client.lua create mode 100644 src/parts/core.lua rename src/{core => parts}/db.lua (100%) diff --git a/README.md b/README.md index 249c96c..1ac8714 100644 --- a/README.md +++ b/README.md @@ -13,13 +13,21 @@ and C is faster. [Alpine Linux](https://alpinelinux.org), root: * Enable community repo (in wiki) + * Install: `apk add sudo git lua5.3 luarocks openssl-dev` + * Install dependencies: `luarocks-5.3 install luasec` + * Create user: `adduser user` + setup sudo and login to user * Get repo: `git clone https://github.com/Er2ch/comp-tg` + and `cd comp-tg` + * Change token and owner in `config.lua` + TODO: Use env instaed of config + * Run: `lua5.3 init.lua` diff --git a/etc/api/api.lua b/etc/api/api.lua index ce537dd..e72bc79 100644 --- a/etc/api/api.lua +++ b/etc/api/api.lua @@ -6,13 +6,13 @@ local tools =require 'etc.api.tools' local json = require 'etc.json' local events=require 'etc.events' -local api = { _ev_ = {} } +local api = { + request = function(s, ...) return tools.request(s.token, ...) end +} api.__index = api -- Make class events(api) -- inheritance -function api:request(...) return tools.request(self.token, ...) end - -- Getters without params function api:getMe() return self:request 'getMe' end diff --git a/etc/events.lua b/etc/events.lua index a91e1bf..ee1876a 100644 --- a/etc/events.lua +++ b/etc/events.lua @@ -37,4 +37,7 @@ function events:emit(name, ...) end end -return function(t) return setmetatable(t, events) end +return function(t) + t._ev_ = {} + return setmetatable(t, events) +end diff --git a/init.lua b/init.lua index 8ba5bb7..49637ba 100644 --- a/init.lua +++ b/init.lua @@ -1 +1 @@ -require 'src.core.core' +require 'src.parts.core' diff --git a/src/core/core.lua b/src/core/core.lua deleted file mode 100644 index b2f2dbb..0000000 --- a/src/core/core.lua +++ /dev/null @@ -1,76 +0,0 @@ -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/parts/client.lua b/src/parts/client.lua new file mode 100644 index 0000000..d544e89 --- /dev/null +++ b/src/parts/client.lua @@ -0,0 +1,35 @@ +local tg = require 'etc.api' + +return function(Core) + local self = Core + + self.api = tg { norun = true } + self.cmds = {} + + print 'Client initialization...' + + function Core._ev(ev, ...) self:ev(...) end + function self.api._ev(_, t, i, n, ...) + self._ev(_, t, i, n, self.api, ...) + end + + self:load 'events' + + self.api:login(self.config.token, function() + print('Logged on as @' .. self.api.info.username) + self.config.token = nil + self.api:emit 'ready' + end) + + local offs, o = 0 + self.api.runs = true + self:on('ready', function() + 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) +end diff --git a/src/parts/core.lua b/src/parts/core.lua new file mode 100644 index 0000000..58938ba --- /dev/null +++ b/src/parts/core.lua @@ -0,0 +1,44 @@ +local config = require 'config' + +local Core = { + config = config, +} +(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 true then + --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 == 'parts' 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:load 'parts' + + print 'Done!' + self:emit 'ready' +end + +Core:init() diff --git a/src/core/db.lua b/src/parts/db.lua similarity index 100% rename from src/core/db.lua rename to src/parts/db.lua