-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathUtility.lua
285 lines (233 loc) · 9.23 KB
/
Utility.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
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
--[[-----------------------------------------------------------------------
The MIT License (MIT)
Copyright (c) 2010-2020 Mark Rogaski
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
--]]-----------------------------------------------------------------------
--[[-----------------------------------------------------------------------
Imported Libraries
--]]-----------------------------------------------------------------------
local crc = LibStub:GetLibrary("Hash:CRC:16ccitt-1.0")
---------------------------------------------------------------------------
-- Logic functions
---------------------------------------------------------------------------
--- Case insensitive string comparison.
-- @param a A string
-- @param b A string
-- @return True if the strings match in all respects except case, false otherwise.
function gw.iCmp(a, b)
if string.lower(a) == string.lower(b) then
return true
else
return false
end
end
---------------------------------------------------------------------------
-- Logging functions
---------------------------------------------------------------------------
--- Add a message to the log file
-- @param msg A string to write to the log.
function gw.Log(msg)
if gw.settings and gw.settings:get('log') then
local ts = date('%Y-%m-%d %H:%M:%S')
tinsert(GreenWallLog, format('%s -- %s', ts, msg))
while #GreenWallLog > gw.settings:get('logsize') do
tremove(GreenWallLog, 1)
end
end
end
--- Write a message to the default chat frame.
-- @param ... A list of the string and arguments for substitution using the syntax of string.format.
function gw.Write(...)
local msg = string.format(unpack({ ... }))
DEFAULT_CHAT_FRAME:AddMessage('|cff0bda51GreenWall:|r ' .. msg)
gw.Log(msg)
end
--- Write an error message to the default chat frame.
-- @param ... A list of the string and arguments for substitution using the syntax of string.format.
function gw.Error(...)
local msg = string.format(unpack({ ... }))
DEFAULT_CHAT_FRAME:AddMessage('|cffabd473GreenWall:|r |cffff6000[ERROR] ' .. msg)
gw.Log('[ERROR] ' .. msg)
end
--- Write a debugging message to the default chat frame with a detail level.
-- Messages will be filtered with the "/greenwall debug <level>" command.
-- @param level A positive integer specifying the debug level to display this under.
-- @param ... A list of the string and arguments for substitution using the syntax of string.format.
function gw.Debug(level, ...)
local function get_caller()
local s = debugstack(3, 1, 0)
local file, loc = strmatch(s, '%[string "(.+)"%]:(%d+): in function')
local f = strmatch(s, 'in function \`([%a_-]+)\'')
file = file == nil and "" or file
loc = loc == nil and "" or loc
f = f == nil and "?" or f
return format('%s:%s:%s', file, loc, f)
end
if gw.settings then
if level <= gw.settings:get('debug') then
local msg = string.format(unpack({ ... }))
local trace = format('debug/%d [%s] %s', level, get_caller(), msg)
gw.Log(trace)
if gw.settings:get('verbose') then
DEFAULT_CHAT_FRAME:AddMessage(format('|cff009a7dGreenWall:|r |cff778899%s|r', trace))
end
end
end
end
--- Print an obfuscated string if the redaction option is set.
-- @param msg The input string
-- @return The string with redaction applied, if necessary.
function gw.Redact(msg)
if gw.settings:get('redact') then
return string.format('<<%04X>>', crc.Hash(msg))
else
return msg
end
end
---------------------------------------------------------------------------
-- Game functions
---------------------------------------------------------------------------
--- Format name for cross-realm addressing.
-- @param name Character name or guild name.
-- @param realm Name of the realm.
-- @return A formatted cross-realm address.
function gw.GlobalName(name, realm)
if name == nil then
return
end
-- Pass formatted names without modification.
if name:match("[^-]+-[^-]+$") then
return name
end
-- Use local realm as the default.
if realm == nil then
realm = GetRealmName()
end
return name .. '-' .. realm:gsub("%s+", "")
end
--- Get the player's fully-qualified guild name.
-- @param target (optional) unit ID, default is 'Player'.
-- @return A qualified guild name or nil if the player is not in a guild.
function gw.GetGuildName(target)
if target == nil then
target = 'Player'
end
local name, _, _, realm = GetGuildInfo(target)
if name == nil then
return
end
return gw.GlobalName(name, realm)
end
--- Get a string with the player's fully-qualified guild name and numeric rank.
-- @return A string identifier, the empty string if not in a guild.
function gw.GetGuildStatus()
local name, _, rank, realm = GetGuildInfo('Player')
if name == nil then
return ''
else
return string.format('%s-%d', gw.GlobalName(name, realm), rank)
end
end
--- Check a target player for officer status in the same container guild.
-- @param target The name of the player to check.
-- @return True if the target has at least read access to officer chat and officer notes, false otherwise.
function gw.IsOfficer(target)
local function get_rank(target)
local guild, name, rank
if target == nil or gw.GlobalName(target) == gw.player then
guild, name, rank = GetGuildInfo('player')
if guild then
gw.Debug(GW_LOG_DEBUG, 'target=%s, rank=%s (%s)', gw.player, rank, name)
return rank + 1
else
gw.Debug(GW_LOG_DEBUG, 'target=%s not in a guild', gw.player)
return
end
else
local n = GetNumGuildMembers()
for i = 1, n do
local candidate
candidate, name, rank = GetGuildRosterInfo(i)
if gw.GlobalName(candidate) == gw.GlobalName(target) then
gw.Debug(GW_LOG_DEBUG, 'target=%s, rank=%s (%s)', target, rank, name)
return rank + 1
end
end
end
gw.Debug(GW_LOG_DEBUG, 'target=%s not in a guild', target)
return
end
-- Workaround for 7.3.0, where GuildControlSetRank() is protected.
local note = gw.GetGMOfficerNote()
local result = false
if note ~= nil and note ~= '' then
result = true
end
gw.Debug(GW_LOG_INFO, 'is_officer: %s', tostring(result))
return result
end
--- Get the officer note for the GM, if possible.
-- @return The officer note of the GM as a string, or nil.
function gw.GetGMOfficerNote()
local n = GetNumGuildMembers();
local name, rank, note
for i = 1, n do
name, _, rank, _, _, _, _, note = GetGuildRosterInfo(i);
if rank == 0 then
return note;
end
end
return
end
--- Check if player is currently in any world channels.
-- @return True is the player has joined any world channels, false otherwise.
function gw.WorldChannelFound()
gw.Debug(GW_LOG_DEBUG, 'scanning for world channels')
for i, v in pairs({ GetChannelList() }) do
local name, header, _, _, _, _, category = GetChannelDisplayInfo(i)
if not header then
if category == 'CHANNEL_CATEGORY_WORLD' then
gw.Debug(GW_LOG_DEBUG, 'world channel found: %s', name)
return true
end
end
end
return false
end
---------------------------------------------------------------------------
-- Item functions
---------------------------------------------------------------------------
--- Extract an embedded itemString from a chat message.
-- @param message A chat message.
-- @return An itemString.
function gw.GetItemString(message)
return string.match(message, '|H(item[0-9:]+)|h')
end
--- Extract an embedded itemString from a chat message.
-- @param item An itemString.
-- @return True is the item is legendary.
function gw.IsLegendary(item)
_, _, rarity = GetItemInfo(item)
if rarity == 5 then
return true
else
return false
end
end
--[[-----------------------------------------------------------------------
END
--]]-----------------------------------------------------------------------