--[[
 -- @Description:
 -- Hello , DiaoSi ~ ( or not ? ) ,glad you saw my comment
 -- If you think my mod is okay , you can sponsor my breakfast by a tiny amount donation  ($_$)
 -- paypal:td1madao@163.com     alipay:td1madao@163.com
 -- If any question ,Please email me
 -- What's more ,don't modify and republish my mods . 3Q for cooperation
 -- What's more ,don't modify and republish my mods . 3Q for cooperation
 -- What's more ,don't modify and republish my mods . 3Q for cooperation
 -- @version 1.0.0
 -- @author td1madao
 -- @email td1madao@163.com
 -- @qq 360810498
 -- @date 16/8/6
 ]]
local TD = TD.td1madao.rivendst
local Rivendst_common_perioder = Class(function(self, inst)
    TD.see()
    self.inst = inst
    self.taskList = {}
    self.time = 0
    self.buffer = {}
    self.bak = {}
    self.task = self.inst:DoPeriodicTask(1, function()
        for i, v in pairs(self.taskList) do
            local myTask = self.taskList[i]
            if myTask and myTask.run and myTask.period and self.time % myTask.period == 0 then
                myTask.run()
            end
        end
        self.bak = self.buffer
        self.buffer = {}
        self.time = self.time + 1
        if self.time > 86400 then
            self.time = 0
        end
    end)
end)

function Rivendst_common_perioder:common_addTask(name, run, period)
    TD.see()
    self:common_removeTask(name)
    self:common_removeTask(name)
    if self.taskList then
        self.taskList[name] = {}
        self.taskList[name].run = run
        self.taskList[name].period = period
    end
end

function Rivendst_common_perioder:common_removeTask(name)
    TD.see()
    if self.taskList and self.taskList[name] then
        self.taskList[name].run = nil
        self.taskList[name].period = nil
        self.taskList[name] = nil
    end
end

function Rivendst_common_perioder:common_hasTask(name)
    TD.see()
    return self.taskList and self.taskList[name] and self.taskList[name].run
end

function Rivendst_common_perioder:player_addTask(name, run, period)
    TD.see()
    self:common_addTask(name, run, period)
end

function Rivendst_common_perioder:server_addTask(name, run, period)
    TD.see()
    self:common_addTask(name, run, period)
end

function Rivendst_common_perioder:player_hasTask(name)
    TD.see()
    return self:common_hasTask(name)
end

function Rivendst_common_perioder:server_hasTask(name)
    TD.see()
    return self:common_hasTask(name)
end

function Rivendst_common_perioder:player_removeTask(name)
    TD.see()
    self:common_removeTask(name)
end

function Rivendst_common_perioder:server_removeTask(name)
    TD.see()
    self:common_removeTask(name)
end

return Rivendst_common_perioder