---------------------------------------------------------
-- Helper
---------------------------------------------------------
Helper = class()
function Helper:ctor()
self.isPrint = false
self.cmds = {}
end
function Helper:Print()
if self.isPrint then
Note()
end
end
function Helper:AddTimer(name, interval)
local hours = math.floor(interval / 3600)
interval = interval - (hours * 3600)
local minutes = math.floor(interval / 60)
local seconds = interval - (minutes * 60)
local status = AddTimer (name, hours, minutes, seconds, "dispatcher:SendTimeout(\"" .. name .. "\")", timer_flag.OneShot + timer_flag.Temporary + timer_flag.Replace, "")
assert(status == error_code.eOK, "fail to create timer:" .. name)
SetTimerOption(name, "send_to", 12)
EnableTimer(name, true)
ResetTimer(name)
end
function Helper:ResetTimer(name, interval)
assert(IsTimer(name), "timer doesn't exist")
EnableTimer(name, false)
local hours = math.floor(interval / 3600)
interval = interval - (hours * 3600)
local minutes = math.floor(interval / 60)
local seconds = interval - (minutes * 60)
SetTimerOption(name, "hour", hours)
SetTimerOption(name, "minute", minutes)
SetTimerOption(name, "second", seconds)
EnableTimer(name, true)
ResetTimer(name)
end
function Helper:RemoveTimer(name)
EnableTimer(name, false)
DeleteTimer(name)
end
-- only one instance
helper = Helper.new()
---------------------------------------------------------
-- Command
--- Repeat: #4 xx (repeat 4 times for command xx)
--- Delay: @3 (delay 3 seconds)
---------------------------------------------------------
Command = class()
function Command:ctor()
self.cmds = {}
self.isRunning = false
self.thread = nil
end
function Command:ToTable(cmds)
assert(type(cmds) == "string", "commands must be string type")
local retVal = {}
for k, v in pairs(utils.split(cmds, ";")) do
if (string.sub(v, 1, 1) == "#") then -- convert repeat command
local sb, se = string.find(v, "%s+")
assert(sb ~= nil and se ~= nil, "wrong repeat command format")
local times = tonumber(string.sub(v, 2, sb - 1))
local cmd = string.sub(v, se + 1)
for i = 1, times, 1 do
retVal[#retVal + 1] = cmd
end
else
retVal[#retVal + 1] = v
end
end
return retVal
end
function Command:Add(cmds)
if (type(cmds) == "string") then
cmds = self:ToTable(cmds)
end
assert(type(cmds) == "table", "commands must be string or table type")
-- add cmds
for k, v in pairs (cmds) do
self.cmds[#self.cmds + 1] = v
end
-- wakeup to process
self:Wakeup()
end
function Command:Clear()
self.cmds = {}
end
function Command:Wakeup()
if (self.thread == nil) then
cmdSender.thread = coroutine.create(cmdSender.Do)
end
if (not self.isRunning) then
coroutine.resume(self.thread)
end
end
function Command:Do()
while true do
local cmd = nil
if (#cmdSender.cmds ~= 0) then
cmd = cmdSender.cmds[1] -- pick cmd in queue
table.remove (cmdSender.cmds, 1) -- remove cmd in queue
end
if (cmd ~= nil) then
cmdSender.isRunning = true
if (string.sub(cmd, 1, 1) == "@") then
local interval = tonumber(string.sub(cmd, 2))
if (interval > 0) then
helper:Print("delay:", interval)
DoAfterSpecial(interval, "coroutine.resume(cmdSender.thread)", 12)
coroutine.yield()
end
else
cmdSender:Send(cmd)
end
else
cmdSender.isRunning = false
coroutine.yield()
end
end
end
function Command:Send(cmd)
helper:Print("cmd:", cmd)
if (IsAlias(cmd) == error_code.eOK) then
SendImmediate(GetAliasInfo(cmd, 2))
else
SendImmediate(cmd)
end
end
cmdSender = Command.new()
|