-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Timer.lua
166 lines (148 loc) · 4.54 KB
/
Timer.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
--[[
* Copyright (c) 2011-2020 by Adam Hellberg.
*
* This file is part of KillTrack.
*
* KillTrack is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* KillTrack is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with KillTrack. If not, see <http://www.gnu.org/licenses/>.
--]]
---@class KillTrack
local KT = select(2, ...)
---@class KillTrackTimer
local T = {
Time = {
Start = 0,
Stop = 0
},
Running = false,
---@enum KillTrackTimerState
State = {
START = 0,
UPDATE = 1,
STOP = 2
}
}
KT.Timer = T
local KTT = KT.Tools
---@class KillTrackTimerData
---@field Last integer
---@field Current integer
---@field Start integer
---@field Stop integer
---@field Total integer
---@field Left integer
---@field LeftFormat string
---@field Progress number
---@field __DATA__ { [any]: any }
local TimerData = {}
---@alias KillTrackTimerCallback fun(data: KillTrackTimerData, state: KillTrackTimerState)
T.Frame = CreateFrame("Frame")
local function TimeCheck(_, _)
if not T.Running then T.Frame:SetScript("OnUpdate", nil) return end
local now = time()
TimerData.Last = now
TimerData.Current = now - T.Time.Start
TimerData.Start = T.Time.Start
TimerData.Stop = T.Time.Stop
TimerData.Total = TimerData.Stop - TimerData.Start
TimerData.Left = TimerData.Total - TimerData.Current
TimerData.LeftFormat = KTT:FormatSeconds(TimerData.Left)
TimerData.Progress = TimerData.Current / TimerData.Total
T:RunCallback(T:GetAllData(), T.State.UPDATE)
if now >= T.Time.Stop then T:Stop() end
end
---@return KillTrackTimerData
function T:GetAllData()
return KTT:TableCopy(TimerData)
end
---@param key any
---@param failsafe boolean
---@return any
function T:GetData(key, failsafe)
local r
if failsafe then r = 0 end
if not TimerData.__DATA__ then if failsafe then return 0 else return nil end end
return TimerData.__DATA__[key] or r
end
---@param key any
---@param value any
function T:SetData(key, value)
if type(TimerData.__DATA__) ~= "table" then TimerData.__DATA__ = {} end
TimerData.__DATA__[key] = value
end
---@return boolean
function T:IsRunning()
return self.Running
end
---@param seconds integer?
---@param minutes integer?
---@param hours integer?
---@param callback KillTrackTimerCallback?
---@param data { [any]: any }?
---@return boolean
function T:Start(seconds, minutes, hours, callback, data)
if self.Running then return false end
self.Running = true
self:Reset()
seconds = tonumber(seconds) or 0
minutes = tonumber(minutes) or 0
hours = tonumber(hours) or 0
seconds = seconds + minutes * 60 + hours * 60 ^ 2
if seconds <= 0 then
self.Running = false
KT:Msg("Time must be greater than zero.")
return false
end
if type(callback) == "function" then
self:SetCallback(callback)
end
if type(data) == "table" then
for k,v in pairs(data) do
self:SetData(k, v)
end
end
self.Time.Start = time()
self.Time.Stop = self.Time.Start + seconds
self:RunCallback(self:GetAllData(), self.State.START)
self.Frame:SetScript("OnUpdate", TimeCheck)
return true
end
function T:Stop()
if not self.Running then return end
self.Frame:SetScript("OnUpdate", nil)
self:RunCallback(self:GetAllData(), self.State.STOP)
self.Running = false
self.Time.Diff = self.Time.Stop - self.Time.Start
return self.Time.Diff
end
function T:Reset()
wipe(TimerData)
self.Time.Start = 0
self.Time.Stop = 0
end
---@return KillTrackTimerCallback
function T:GetCallback()
return self.Callback
end
---@param func KillTrackTimerCallback
function T:SetCallback(func)
if type(func) ~= "function" then error("Argument 'func' must be of type 'function'.") end
self.Callback = func
end
---@param data KillTrackTimerData
---@param state KillTrackTimerState
function T:RunCallback(data, state)
if type(data) ~= "table" then error("Argument 'data' must be of type 'table'.") end
local callback = self:GetCallback()
if callback then callback(data, state) end
end