-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathepgp_recurring.lua
150 lines (132 loc) · 4.08 KB
/
epgp_recurring.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
local L = LibStub("AceLocale-3.0"):GetLocale("EPGP")
local GS = LibStub("LibGuildStorage-1.2")
local Debug = LibStub("LibDebug-1.0")
local DLG = LibStub("LibDialog-1.0")
local callbacks = EPGP.callbacks
local frame = CreateFrame("Frame", "EPGP_RecurringAwardFrame")
local timeout = 0
local function RecurringTicker(self, elapsed)
-- EPGP's db is available after GUILD_ROSTER_UPDATE. So we have a
-- guard.
if not EPGP.db then return end
local vars = EPGP.db.profile
local now = GetTime()
if now > vars.next_award and GS:IsCurrentState() then
EPGP:IncMassEPBy(vars.next_award_reason, vars.next_award_amount)
vars.next_award =
vars.next_award + vars.recurring_ep_period_mins * 60
end
timeout = timeout + elapsed
if timeout > 0.5 then
callbacks:Fire("RecurringAwardUpdate",
vars.next_award_reason,
vars.next_award_amount,
vars.next_award - now)
timeout = 0
end
end
frame:SetScript("OnUpdate", RecurringTicker)
frame:Hide()
function EPGP:StartRecurringEP(reason, amount)
local vars = EPGP.db.profile
if vars.next_award then
return false
end
vars.next_award_reason = reason
vars.next_award_amount = amount
vars.next_award = GetTime() + vars.recurring_ep_period_mins * 60
frame:Show()
callbacks:Fire("StartRecurringAward",
vars.next_award_reason,
vars.next_award_amount,
vars.recurring_ep_period_mins)
return true
end
DLG:Register("EPGP_RECURRING_RESUME", {
buttons = {
{
text = _G.YES,
on_click = function(self, data, reason)
callbacks:Fire("ResumeRecurringAward",
EPGP.db.profile.next_award_reason,
EPGP.db.profile.next_award_amount,
EPGP.db.profile.recurring_ep_period_mins)
frame:Show()
end,
},
{
text = _G.NO,
on_click = function(self, data, reason)
EPGP:StopRecurringEP()
end,
},
},
on_show = function(self, data)
self.text:SetText(data.text)
self.time_remaining = data.timeout
self.close_button:Hide()
end,
on_cancel = function(self, data, reason)
if reason ~= "override" then
EPGP:StopRecurringEP()
end
end,
on_hide = function(self, data)
self.close_button:Show()
end,
hide_on_escape = true,
show_while_dead = true,
})
function EPGP:ResumeRecurringEP()
local vars = EPGP.db.profile
local period_secs = vars.recurring_ep_period_mins * 60
local timeout = vars.next_award + period_secs - GetTime()
-- We need to do the formatting here because static popups do
-- not allow for 3 arguments to the formatting function.
local text = L["Do you want to resume recurring award (%s) %d EP/%s?"]:format(
vars.next_award_reason,
vars.next_award_amount,
EPGP:RecurringEPPeriodString())
DLG:Spawn("EPGP_RECURRING_RESUME", {text = text, timeout = timeout})
end
function EPGP:CanResumeRecurringEP()
local vars = EPGP.db.profile
local now = GetTime()
if not vars.next_award then return false end
local period_secs = vars.recurring_ep_period_mins * 60
local last_award = vars.next_award - period_secs
local next_next_award = vars.next_award + period_secs
if last_award < now and now < next_next_award then
return true
end
return false
end
function EPGP:CancelRecurringEP()
DLG:Dismiss("EPGP_RECURRING_RESUME")
local vars = EPGP.db.profile
vars.next_award_reason = nil
vars.next_award_amount = nil
vars.next_award = nil
frame:Hide()
end
function EPGP:StopRecurringEP()
self:CancelRecurringEP()
callbacks:Fire("StopRecurringAward")
return true
end
function EPGP:RunningRecurringEP()
local vars = EPGP.db.profile
return not not vars.next_award
end
function EPGP:RecurringEPPeriodMinutes(val)
local vars = EPGP.db.profile
if val == nil then
return vars.recurring_ep_period_mins
end
vars.recurring_ep_period_mins = val
end
function EPGP:RecurringEPPeriodString()
local vars = EPGP.db.profile
local fmt, val = SecondsToTimeAbbrev(vars.recurring_ep_period_mins * 60)
return fmt:format(val)
end