-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathShame.lua
321 lines (265 loc) · 8.02 KB
/
Shame.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
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
--[[
Shame (C) Kruithne <[email protected]>
Licensed under GNU General Public Licence version 3.
https://github.com/Kruithne/Shame
Shame.lua - Core of the addon.
]]--
do
-- [[ Optimization ]] --
local wipe = wipe;
local type = type;
local pairs = pairs;
local select = select;
local string_format = string.format;
local string_sub = string.sub;
local string_len = string.len;
local SendChatMessage = SendChatMessage;
local UnitIsPlayer = UnitIsPlayer;
local GetInstanceInfo = GetInstanceInfo;
-- [[ Initiate ]] --
local Shame = {
tracking = false, -- Flag for tracking state.
boardGroup = {},
instances = {},
combatListeners = {},
strings = {}, -- Localized strings.
modeChannel = "party", -- Real-time shaming channel.
};
-- Add feed-through to strings table for localization.
setmetatable(Shame, { __index = function(t, k) return t.strings[k]; end });
_G["Shame"] = Shame; -- Expose addon container.
--[[
Shame.ApplyLocalization
Copies all given localization into the string table.
self - Reference to the addon container.
locale - Localization table.
]]--
Shame.ApplyLocalization = function(self, locale)
local strings = self.strings;
for key, str in pairs(locale) do
strings[key] = str;
end
end
--[[
Shame.Message
Send a text message to a specified (or default) output.
self - Reference to the addon container.
text - Message to be sent.
channel - Output channel, leave blank for default.
]]--
Shame.Message = function(self, text, channel, ...)
text = text:format(...);
if not channel then
-- Print message to users chat.
DEFAULT_CHAT_FRAME:AddMessage(self.CHAT_PREFIX:format(text));
else
-- Print message to specified channel.
SendChatMessage(text, self.validChannels[channel]);
end
end
--[[
Shame.OnLoad
Invoked when the addon is loaded.
self - Reference to the addon container.
]]--
Shame.OnLoad = function(self)
-- Assign default values.
self.currentMode = self.L_MODE_SELF;
-- Create chat command.
_G["SLASH_SHAME1"] = "/" .. self.ADDON_NAME:lower();
SlashCmdList[self.ADDON_NAME:upper()] = self.OnCommand;
-- Create table containing valid channels.
self.validChannels = {
["guild"] = "GUILD",
["instance"] = "INSTANCE_CHAT",
["officer"] = "OFFICER",
["party"] = "PARTY",
["raid"] = "RAID"
};
-- Create table containing valid modes.
self.validModes = {
[self.L_MODE_ALL] = true,
[self.L_MODE_SILENT] = true,
[self.L_MODE_SELF] = true
};
-- Create command table.
self.commandList = {
[self.L_CMD_START] = { desc = self.L_CMD_DESC_START, func = self.Command_Enable },
[self.L_CMD_STOP] = { desc = self.L_CMD_DESC_STOP, func = self.Command_Disable },
[self.L_CMD_MODE] = { desc = self.L_CMD_DESC_MODE, usage = self.L_CMD_MODE_HELP, func = self.Command_SetMode },
[self.L_CMD_PRINT] = { desc = self.L_CMD_DESC_PRINT, usage = self.L_CMD_PRINT_HELP, func = self.Command_Print },
[self.L_CMD_HELP] = { desc = self.L_CMD_DESC_HELP, func = self.ListCommands },
["?"] = { hidden = true, func = self.ListCommands },
};
-- Begin monitoring for map changes.
self:SetEventHandler("ZONE_CHANGED_NEW_AREA", self.OnZoneChange);
self:SetEventHandler("PLAYER_ENTERING_WORLD", self.OnZoneChange);
-- Print loaded message.
self:Message(self.L_LOADED:format(GetAddOnMetadata(self.ADDON_NAME, "Version")));
end
--[[
Shame.OnCombatEvent
Invoked when a combat log event occurs.
self - Reference to the addon container.
timestamp - Integer timestamp for when the event occurred.
event - Identifier for the event.
... - Event arguments.
]]--
Shame.OnCombatEvent = function(self, timestamp, event, ...)
local listeners = self.combatListeners[event];
if listeners then
for i = 1, #listeners do
local tracker = listeners[i];
local func = tracker.func or self.CombatGeneric_SpellDamage;
func(self, tracker, timestamp, event, ...);
end
end
end
--[[
Shame.OnZoneChange
Invoked when the player changes zone.
self - Reference to the addon container.
]]--
Shame.OnZoneChange = function(self)
local currentMapID = select(8, GetInstanceInfo());
local instance = self.instances[currentMapID];
local currentInstance = self.currentInstance;
-- Disable the active tracking module if there is one.
if currentInstance and currentInstance.zoneID ~= currentMapID then
self:ResetCombatListeners();
end
-- Enable a new tracker module if needed.
if instance then
self:RegisterCombatNodes(instance);
self.currentInstance = instance;
end
end
--[[
Shame.RegisterCombatNodes
Register all combat nodes for a tracker.
self - Reference to the addon container.
data - Table containing tracker nodes.
]]--
Shame.RegisterCombatNodes = function(self, data)
local trackers = data.trackers;
for i = 1, #trackers do
local tracker = trackers[i];
if type(tracker) ~= "table" then
tracker = { spellID = tracker };
trackers[i] = tracker;
end
local event = tracker.event or self.COMBAT_SPELL_DAMAGE;
local nodes = self.combatListeners[event];
-- No listener table, create one.
if not nodes then
nodes = {};
self.combatListeners[event] = nodes;
end
nodes[#nodes + 1] = tracker;
end
end
--[[
Shame.ResetCombatListeners
Remove all existing combat listeners.
self - Reference to the addon container.
]]--
Shame.ResetCombatListeners = function(self)
self.combatListeners = {};
end
--[[
Shame.RegisterMistake
Register a player mistake.
self - Reference to the addon container.
actor - Name of the actor.
damage - Avoidable damage this mistake cost.
message - Message to display for this mistake.
... - String formatting arguments.
]]--
Shame.RegisterMistake = function(self, actor, damage, message, ...)
if not self.tracking then
-- Prevent mistakes being registered outside a session.
return;
end
if not UnitIsPlayer(actor) then
-- Prevent non-players being flagged for mistakes.
return;
end
local node = self.boardGroup[actor];
if not node then
node = { name = actor };
self.boardGroup[actor] = node;
end
node.mistakes = (node.mistakes or 0) + 1;
node.damage = (node.damage or 0) + (damage or 0);
if self.currentMode == self.L_MODE_ALL or self.currentMode == self.L_MODE_SELF then
local target = nil;
if self.currentMode == self.L_MODE_ALL then
target = self.modeChannel;
end
self:Message(message, target, ...);
end
end
--[[
Shame.RegisterInstance
Register an instance module for tracking.
self - Reference to the addon container.
instance - Table containing tracking data.
]]--
Shame.RegisterInstance = function(self, instance)
self.instances[instance.instanceID] = instance;
end
--[[
Shame.Enable
Enable the shaming.
self - Reference to the addon container.
]]--
Shame.Enable = function(self)
self.tracking = true;
wipe(self.boardGroup); -- Reset the score board.
-- Enable combat log monitoring.
self:SetEventHandler("COMBAT_LOG_EVENT_UNFILTERED", self.OnCombatEvent);
end
--[[
Shame.Disable
Disable the shaming.
self - Reference to the addon container.
]]--
Shame.Disable = function(self)
self.tracking = false;
-- Disable combat log monitoring.
self:RemoveEventHandler("COMBAT_LOG_EVENT_UNFILTERED", self.OnCombatEvent);
end
--[[
Shame.Validate
Check if the input is contained within the pool.
input - Value to check for.
pool - Table to check inside.
]]--
Shame.Validate = function(input, pool)
if not input then
return false;
end
input = input:lower();
if pool[input] then
return input;
end
for check, _ in pairs(pool) do
if string_sub(check, 1, string_len(input)) == input then
return check;
end
end
return false;
end
--[[
Shame.PrintCurrentMode
Print the current output mode to chat.
self - Reference to the addon container.
]]--
Shame.PrintCurrentMode = function(self)
if self.currentMode == self.L_MODE_ALL then
self:Message(self.L_MODE_SET, nil, self.currentMode, self.modeChannel);
else
self:Message(self.L_MODE_SET_SIMPLE, nil, self.currentMode);
end
end
end