---@class Chat
---@field data string[]
---@field task function|nil
---@field rate integer
local Chat = {
data = {},
task = nil,
rate = 1000, -- 1000 = 1 sec
}
Chat.__index = Chat
function Chat:isUpdated()
return #self.data ~= 0
end
function Chat:getDataAndRemove()
local data = self.data
self.data = {}
return data
end
function Chat:start()
if self.task then
return
end
self.task = newTask(function()
repeat
local data = Chat:getDataAndRemove()
for _, str in ipairs(data) do
printf('CHAT: Sended chat... (%s)', str)
sendInput(str)
printf('CHAT: Waiting >> (%d)', self.rate)
wait(self.rate)
print('CHAT: << Waited!')
end
until not Chat:isUpdated()
self.task = nil
end)
end
function Chat:input(str)
table.insert(self.data, str)
if not self.task then
self:start()
end
end
function Chat:inputf(pattern, ...)
local fmt = string.format(pattern, ...)
table.insert(self.data, fmt)
if not self.task then
self:start()
end
end
---@class TimerData
---@field unix integer
---@field callback function
---@class Timer
---@field data TimerData[]
---@field task any
local Timer = {
data = {},
task = nil,
}
Timer.__index = Timer
function Timer:get()
return self.data
end
---@param unix integer
---@param callback function
function Timer:add(unix, callback)
table.insert(self.data, {
unix = unix,
callback = callback
})
end
---@param unix integer
function Timer:tick(unix)
for _, t in ipairs(self:get()) do
if unix % t.unix == 0 then
t.callback()
end
end
end
function Timer:start()
if self.task then
return
end
local unix = os.time()
self.task = newTask(function()
while true do
wait(300)
local currentUnix = os.time()
if unix ~= currentUnix then
unix = currentUnix
Timer:tick(currentUnix)
print('Ticked!')
end
end
end)
end
function Timer:stop()
self.task = nil
end