-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGridStatusHealth.lua
More file actions
335 lines (291 loc) · 8.44 KB
/
GridStatusHealth.lua
File metadata and controls
335 lines (291 loc) · 8.44 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
--{{{ Libraries
local RL = AceLibrary("RosterLib-2.0")
local Aura = AceLibrary("SpecialEvents-Aura-2.0")
local L = AceLibrary("AceLocale-2.2"):new("Grid")
local Compost = AceLibrary("Compost-2.0")
--}}}
GridStatusHealth = GridStatus:NewModule("GridStatusHealth")
GridStatusHealth.menuName = L["Health"]
--{{{ AceDB defaults
GridStatusHealth.defaultDB = {
debug = false,
unit_health = {
enable = true,
color = { r = 1, g = 1, b = 1, a = 1 },
priority = 30,
range = false,
deadAsFullHealth = true,
useClassColors = true,
},
unit_healthDeficit = {
enable = true,
color = { r = 1, g = 1, b = 1, a = 1 },
priority = 30,
threshold = 100,
range = false,
useClassColors = true,
},
alert_lowHealth = {
text = "Low HP",
enable = true,
color = { r = 1, g = 1, b = 1, a = 1 },
priority = 30,
threshold = 80,
range = true,
},
alert_death = {
text = "DEAD",
enable = true,
color = { r = 0.5, g = 0.5, b = 0.5, a = 1 },
priority = 50,
range = false,
},
alert_offline = {
text = "Offline",
enable = true,
color = { r = 0.5, g = 0.5, g = 0.5, a = 0.6 },
priority = 0,
range = false,
},
}
--}}}
--{{{ AceOptions table
--GridStatusHealth.extraOptions = {}
--}}}
local healthOptions = {
["deadAsFullHealth"] = {
type = "toggle",
name = L["Show dead as full health"],
desc = L["Treat dead units as being full health."],
get = function ()
return GridStatusHealth.db.profile.unit_health.deadAsFullHealth
end,
set = function (v)
GridStatusHealth.db.profile.unit_health.deadAsFullHealth = v
GridStatusHealth:UpdateAllUnits()
end,
},
["useClassColors"] = {
type = "toggle",
name = L["Use class color"],
desc = L["Color health based on class."],
get = function ()
return GridStatusHealth.db.profile.unit_health.useClassColors
end,
set = function (v)
GridStatusHealth.db.profile.unit_health.useClassColors = v
GridStatusHealth:UpdateAllUnits()
end,
},
}
local healthDeficitOptions = {
["threshold"] = {
type = "range",
name = L["Health threshold"],
desc = L["Only show deficit above % damage."],
max = 100,
min = 0,
step = 1,
get = function ()
return GridStatusHealth.db.profile.unit_healthDeficit.threshold
end,
set = function (v)
GridStatusHealth.db.profile.unit_healthDeficit.threshold = v
GridStatusHealth:UpdateAllUnits()
end,
},
["useClassColors"] = {
type = "toggle",
name = L["Use class color"],
desc = L["Color deficit based on class."],
get = function ()
return GridStatusHealth.db.profile.unit_healthDeficit.useClassColors
end,
set = function (v)
GridStatusHealth.db.profile.unit_healthDeficit.useClassColors = v
GridStatusHealth:UpdateAllUnits()
end,
},
}
local low_healthOptions = {
["threshold"] = {
type = "range",
name = L["Low HP threshold"],
desc = L["Set the HP % for the low HP warning."],
max = 100,
min = 0,
step = 1,
get = function ()
return GridStatusHealth.db.profile.alert_lowHealth.threshold
end,
set = function (v)
GridStatusHealth.db.profile.alert_lowHealth.threshold = v
GridStatusHealth:UpdateAllUnits()
end,
},
}
function GridStatusHealth:OnInitialize()
self.super.OnInitialize(self)
self.deathCache = Compost:Acquire()
self:RegisterStatus("unit_health", L["Unit health"], healthOptions)
self:RegisterStatus("unit_healthDeficit", L["Health deficit"], healthDeficitOptions)
self:RegisterStatus("alert_lowHealth", L["Low HP warning"], low_healthOptions)
self:RegisterStatus("alert_death", L["Death warning"], nil, true)
self:RegisterStatus("alert_offline", L["Offline warning"], nil, true)
end
function GridStatusHealth:OnEnable()
self:RegisterEvent("Grid_UnitJoined")
self:RegisterBucketEvent("UNIT_HEALTH", 0.2)
end
function GridStatusHealth:Reset()
self.super.Reset(self)
self:UpdateAllUnits()
end
function GridStatusHealth:UpdateAllUnits()
local name, status, statusTbl
self.deathCache = Compost:Erase(self.deathCache)
for name, status, statusTbl in self.core:CachedStatusIterator("unit_health") do
self:Grid_UnitJoined(name)
end
end
function GridStatusHealth:UNIT_HEALTH(units)
local unitid
local settings = self.db.profile.unit_health
for unitid in pairs(units) do
self:UpdateUnit(unitid)
end
end
function GridStatusHealth:Grid_UnitJoined(name)
local unitid = RL:GetUnitIDFromName(name)
if unitid then
self:UpdateUnit(unitid, true)
self:UpdateUnit(unitid)
end
end
function GridStatusHealth:UpdateUnit(unitid, ignoreRange)
local cur, max = UnitHealth(unitid), UnitHealthMax(unitid)
local name = UnitName(unitid)
local settings = self.db.profile.unit_health
local deficitSettings = self.db.profile.unit_healthDeficit
local healthText
local priority = settings.priority
if not name then return end
if UnitIsDeadOrGhost(unitid) and not self.deathCache[unitid] and not Aura:UnitHasBuff(unitid, "Feign Death") then
self:StatusDeath(unitid, true)
self.deathCache[unitid] = true
elseif not UnitIsDeadOrGhost(unitid) and self.deathCache[unitid] then
self:StatusDeath(unitid, true)
self.deathCache[unitid] = nil
end
self:StatusDeath(unitid, UnitIsDeadOrGhost(unitid))
self:StatusOffline(unitid, not UnitIsConnected(unitid))
if settings.deadAsFullHealth and UnitIsDeadOrGhost(unitid) then
cur = max
end
if cur < max then
healthText = self:FormatHealthText(cur,max)
else
priority = 1
end
if self:IsLowHealth(name, cur, max) and
not UnitIsDeadOrGhost(unitid) then
self:StatusLowHealth(unitid, true)
else
self:StatusLowHealth(unitid, false)
end
if (cur / max * 100) <= deficitSettings.threshold then
self.core:SendStatusGained(name, "unit_healthDeficit",
deficitSettings.priority,
(deficitSettings.range and 40),
(deficitSettings.useClassColors and self:UnitClassColor(name) or
deficitSettings.color),
healthText,
cur, max,
nil)
else
self.core:SendStatusLost(name, "unit_healthDeficit")
end
self.core:SendStatusGained(name, "unit_health",
priority,
(not ignoreRange and settings.range and 40),
(settings.useClassColors and self:UnitClassColor(name) or
settings.color),
nil,
cur, max,
nil)
end
function GridStatusHealth:FormatHealthText(cur, max)
local healthText
local deficit = max - cur
if deficit > 999 then
healthText = string.format("-%.1fk", deficit/1000.0)
else
healthText = string.format("-%d", deficit)
end
return healthText
end
function GridStatusHealth:UnitClassColor(name)
local u = RL:GetUnitObjectFromName(name)
return (u and RAID_CLASS_COLORS[u.class])
end
function GridStatusHealth:IsLowHealth(name, cur, max)
return (cur / max * 100) <= self.db.profile.alert_lowHealth.threshold
end
function GridStatusHealth:StatusLowHealth(unitid, gained)
local name = UnitName(unitid)
local settings = self.db.profile.alert_lowHealth
-- return if this option isnt enabled
if not settings.enable then return end
if gained then
self.core:SendStatusGained(name, "alert_lowHealth",
settings.priority,
(settings.range and 40),
settings.color,
settings.text,
nil,
nil,
nil)
else
self.core:SendStatusLost(name, "alert_lowHealth")
end
end
function GridStatusHealth:StatusDeath(unitid, gained)
local name = UnitName(unitid)
local settings = self.db.profile.alert_death
if not name then return end
-- return if this option isnt enabled
if not settings.enable then return end
if gained then
-- trigger death event for other modules as wow isnt firing a death event
self:TriggerEvent("Grid_UnitDeath", name)
self.core:SendStatusGained(name, "alert_death",
settings.priority,
(settings.range and 40),
settings.color,
settings.text,
(self.db.profile.unit_health.deadAsFullHealth and 100 or 0),
100,
nil)
else
self.core:SendStatusLost(name, "alert_death")
end
end
function GridStatusHealth:StatusOffline(unitid, gained)
local name = UnitName(unitid)
local settings = self.db.profile.alert_offline
if not name then return end
if gained then
-- trigger offline event for other modules
self:TriggerEvent("Grid_UnitOffline", name)
self.core:SendStatusGained(name, "alert_offline",
settings.priority,
(settings.range and 40),
settings.color,
settings.text,
nil,
nil,
nil)
else
self.core:SendStatusLost(name, "alert_offline")
end
end