-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.lua
192 lines (169 loc) · 4.54 KB
/
helpers.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
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
local _, NS = ...
local LibStub = LibStub
local IsInRaid = IsInRaid
local IsInGroup = IsInGroup
local UnitInRange = UnitInRange
local GetNumGroupMembers = GetNumGroupMembers
local GetNumSubgroupMembers = GetNumSubgroupMembers
local UnitGroupRolesAssigned = UnitGroupRolesAssigned
local UnitIsDeadOrGhost = UnitIsDeadOrGhost
local pairs = pairs
local type = type
local next = next
local setmetatable = setmetatable
local getmetatable = getmetatable
local print = print
local IsInInstance = IsInInstance
-- local wipe = table.wipe
local sformat = string.format
local SharedMedia = LibStub("LibSharedMedia-3.0")
NS.Debug = function(...)
if NS.db.global.debug then
print(...)
end
end
NS.isDead = function()
return UnitIsDeadOrGhost("player")
end
NS.isInGroup = function()
return IsInRaid() or IsInGroup()
end
NS.isHealer = function(unit)
return UnitGroupRolesAssigned(unit) == "HEALER"
end
-- Function to assist iterating group members whether in a party or raid.
NS.IterateGroupMembers = function(reversed, forceParty)
local unit = (not forceParty and IsInRaid()) and "raid" or "party"
local numGroupMembers = unit == "party" and GetNumSubgroupMembers() or GetNumGroupMembers()
local i = reversed and numGroupMembers or (unit == "party" and 0 or 1)
return function()
local ret
if i == 0 and unit == "party" then
ret = "player"
elseif i <= numGroupMembers and i > 0 then
ret = unit .. i
end
i = i + (reversed and -1 or 1)
return ret
end
end
NS.isHealerInRange = function()
if NS.isHealer("player") then
return true
else
local count = 0
for unit in NS.IterateGroupMembers() do
if unit and NS.isHealer(unit) and UnitInRange(unit) then
count = count + 1
end
end
if count > 0 then
return true
end
return false
end
end
NS.noHealersInGroup = function()
if NS.isHealer("player") then
return false
else
for unit in NS.IterateGroupMembers() do
if unit and NS.isHealer(unit) then
return false
end
end
return true
end
end
NS.UpdateText = function(frame, reverse)
local displayText = sformat("HEALER %s RANGE", reverse == true and "OUT OF" or "IN")
frame:SetText(displayText)
end
NS.UpdateFont = function(frame)
frame:SetFont(SharedMedia:Fetch("font", NS.db.global.font), NS.db.global.fontsize, "OUTLINE")
end
NS.ToggleVisibility = function(inRange, reverse)
if IsInInstance() then
if inRange then
if reverse then
NS.Interface:ShowText(false)
else
NS.Interface:ShowText(true)
end
else
if reverse then
NS.Interface:ShowText(true)
else
NS.Interface:ShowText(false)
end
end
else
if NS.db.global.showOutside then
if inRange then
if reverse then
NS.Interface:ShowText(false)
else
NS.Interface:ShowText(true)
end
else
if reverse then
NS.Interface:ShowText(true)
else
NS.Interface:ShowText(false)
end
end
end
end
end
-- Copies table values from src to dst if they don't exist in dst
NS.CopyDefaults = function(src, dst)
if type(src) ~= "table" then
return {}
end
if type(dst) ~= "table" then
dst = {}
end
for k, v in pairs(src) do
if type(v) == "table" then
dst[k] = NS.CopyDefaults(v, dst[k])
elseif type(v) ~= type(dst[k]) then
dst[k] = v
end
end
return dst
end
NS.CopyTable = function(src, dest)
-- Handle non-tables and previously-seen tables.
if type(src) ~= "table" then
return src
end
if dest and dest[src] then
return dest[src]
end
-- New table; mark it as seen an copy recursively.
local s = dest or {}
local res = {}
s[src] = res
for k, v in next, src do
res[NS.CopyTable(k, s)] = NS.CopyTable(v, s)
end
return setmetatable(res, getmetatable(src))
end
-- Cleanup savedvariables by removing table values in src that no longer
-- exists in table dst (default settings)
NS.CleanupDB = function(src, dst)
for key, value in pairs(src) do
if dst[key] == nil then
-- HACK: offsetsXY are not set in DEFAULT_SETTINGS but sat on demand instead to save memory,
-- which causes nil comparison to always be true here, so always ignore these for now
if key ~= "offsetsX" and key ~= "offsetsY" and key ~= "version" then
src[key] = nil
end
elseif type(value) == "table" then
if key ~= "disabledCategories" and key ~= "categoryTextures" then -- also sat on demand
dst[key] = NS.CleanupDB(value, dst[key])
end
end
end
return dst
end