-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGuild.lua
More file actions
374 lines (338 loc) · 10.5 KB
/
Copy pathGuild.lua
File metadata and controls
374 lines (338 loc) · 10.5 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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
-- Tracks guild info
local addonName, addonTable = ...
local guild = {}
addonTable.guild = guild
local utils = addonTable.utilities
local ADDON_MSG_PREFIX = addonName .. "Guild"
local defaults = {
profile = {
cache = {}
}
}
LibStub("AceEvent-3.0"):Embed(guild)
LibStub("AceComm-3.0"):Embed(guild)
LibStub("AceSerializer-3.0"):Embed(guild)
LibStub("AceTimer-3.0"):Embed(guild)
function guild:OnInitialize()
self.db = LibStub("AceDB-3.0"):New("JitterDKPDB_Data",defaults)
self.guild_info = {}
self.cachedPlayers = {}
self.lastUpdate = 0
self.pendingChanges = {public = {}, officer = {}, history = {player = {}, item = {} }}
self.pendingTimer = nil
hooksecurefunc("GuildRosterSetPublicNote", function (...)
self:Hook_GuildRosterSetPublicNote(...)
end)
hooksecurefunc("GuildRosterSetOfficerNote", function (...)
self:Hook_GuildRosterSetOfficerNote(...)
end)
end
function guild:OnEnable()
self:RegisterEvent("GUILD_ROSTER_UPDATE")
self:RegisterComm(ADDON_MSG_PREFIX)
if IsInGuild() and GetGuildInfo("player") then
self:ScanGuild()
end
end
function guild:Hook_GuildRosterSetPublicNote(idx, note)
local fullname = GetGuildRosterInfo(idx)
local words = {}
if not fullname then return end -- wtf?
for word in fullname:gmatch("([^-]+)") do
table.insert(words,word)
end
name = words[1]
realm = words[2]
local info = self.guild_info[name]
if info then
info.note.note = note
info.note.cached = true
table.insert(self.cachedPlayers, name)
end
self.pendingChanges.public[name] = note
self:setupPendingTimer()
end
function guild:Hook_GuildRosterSetOfficerNote(idx, note)
local fullname = GetGuildRosterInfo(idx)
local words = {}
if not fullname then return end -- wtf?
for word in fullname:gmatch("([^-]+)") do
table.insert(words,word)
end
name = words[1]
realm = words[2]
local info = self.guild_info[name]
if info then
info.officernote.note = note
info.officernote.cached = true
table.insert(self.cachedPlayers, name)
end
self.pendingChanges.officer[name] = note
self:setupPendingTimer()
end
function guild:setupPendingTimer()
if not self.pendingTimer then
self.pendingTimer = self:ScheduleTimer(function ()
if next(self.pendingChanges.public) then
local names, notes = {}, {}
for k,v in pairs(self.pendingChanges.public) do
table.insert(names, k)
table.insert(notes, v)
end
self:broadcastChanges("N", names, notes)
table.wipe(self.pendingChanges.public)
end
if next(self.pendingChanges.officer) then
local names, notes = {}, {}
for k,v in pairs(self.pendingChanges.officer) do
table.insert(names, k)
table.insert(notes, v)
end
self:broadcastChanges("O", names, notes)
table.wipe(self.pendingChanges.officer)
end
self.pendingTimer = nil
end, 0.1)
end
end
local eventIdxs = {}
function guild:OnCommReceived(prefix, message, channel, sender)
if sender == UnitName("player") then return end -- ignore messages from self
local command, idx, rest = strsplit(" ", message, 3)
if command == "Event" then
local tbl = eventIdxs[sender] or {} -- reuse tables if we can
tbl[idx] = GetTime()
eventIdxs[sender] = tbl
else
-- Assuming messages always arrive in the same order they were broadcast, we can
-- safely process all events, even if multiple events are broadcast before we
-- process any. However, we should not process any events that were fired after
-- we last updated the guild roster.
local eventTime
if eventIdxs[sender] then
eventTime = eventIdxs[sender][idx]
eventIdxs[sender][idx] = nil
end
if (eventTime or -1) < self.lastUpdate then
return
end
if command == "N" or command == "O" then
--message to update note/officer note
local key = command == "N" and "note" or "officernote"
local success, names, notes = self:Deserialize(rest)
if success then
for _,name,note in utils.ipairs(names, notes) do
local info = self.guild_info[name]
if info then
info[key].note = note
info[key].cached = true
end
end
end
GuildRoster()
end -- N or O
if command == "H" then
--message to update history
local success, winners, points, item, time = self:Deserialize(rest)
local Auction = JitterDKP:GetModule("Auction")
--("%s wins %s for %d dkp."):format(table.concat(winners, ", "), link, points))
Auction:AddHistory(winners,points,item,time)
--JitterDKP:printConsoleMessage("Item history comm [%s %d %s %s] received from %s"):format(table.concat(winners, ","), points, item, sender)
JitterDKP:printConsoleMessage("Item history" .. item .." received from " .. sender)
end -- H
end
end
function guild:GUILD_ROSTER_UPDATE(event, update)
local name, rank, rankIndex = GetGuildInfo("player")
if update or name ~= self.guild_info.name then
self:ScanGuild()
elseif #self.cachedPlayers > 0 then
-- scan just those players
for _,name in self.cachedPlayers do
local info = self.guild_info[name]
if info then
local fullname, rank, rankIndex, level, class, zone, note, officernote, online, status, classFileName, achievementPoints, achievementRank, isMobile = GetGuildRosterInfo(info.index)
for word in fullname:gmatch("([^-]+)") do
table.insert(words,word)
end
name = words[1]
realm = words[2]
if name ~= info.name then
-- indexes changed? Weird. Rescan the guild
self:ScanGuild()
break
end
info.note.note = note
info.note.cached = false
info.officernote.note = officernote
info.officernote.cached = false
end
end
table.wipe(self.cachedPlayers)
end
self.lastUpdate = GetTime()
end
function guild:ScanGuild()
if not IsInGuild() then
-- we're not in a guild at this point
table.wipe(self.guild_info)
return
end
for i = 1, GetNumGuildMembers() do
local words = {}
local fullname, rank, rankIndex, level, class, zone, note, officernote, online, status, classFileName, achievementPoints, achievementRank, isMobile = GetGuildRosterInfo(i)
-- re-use tables if we can, to help the GC
for word in fullname:gmatch("([^-]+)") do
table.insert(words,word)
end
name = words[1]
realm = words[2]
local entry = self.guild_info[name] or {note={}, officernote={}}
entry.index = i
entry.rank = rank
entry.rankIndex = rankIndex
entry.level = level
entry.class = class
entry.note.note = note
entry.note.cached = false
entry.officernote.note = officernote
entry.officernote.cached = false
entry.mark = true
self.guild_info[name] = entry
end
for k,v in pairs(self.guild_info) do
if v.mark then
v.mark = nil
else
self.guild_info[k] = nil
end
end
end
local broadcastIdx = 0
function guild:broadcastChanges(type, names, notes)
-- send an alert with an integer first, before sending the bulk event
-- this lets the receiver ignore the bulk event if it finishes arriving after they get a guild update
broadcastIdx = broadcastIdx + 1
self:SendCommMessage(ADDON_MSG_PREFIX, "Event " .. broadcastIdx, "GUILD", nil, "ALERT")
self:SendCommMessage(ADDON_MSG_PREFIX, type .. " " .. broadcastIdx .. " " .. self:Serialize(names, notes), "GUILD")
end
function guild:broadcastHistory(type,winners,points,item,time)
-- send an alert with an integer first, before sending the bulk event
-- this lets the receiver ignore the bulk event if it finishes arriving after they get a guild update
broadcastIdx = broadcastIdx + 1
self:SendCommMessage(ADDON_MSG_PREFIX, "Event " .. broadcastIdx, "GUILD", nil, "ALERT")
self:SendCommMessage(ADDON_MSG_PREFIX, type .. " " .. broadcastIdx .. " " .. self:Serialize(winners,points,item,time), "GUILD")
end
-- validates that the local info has the correct index for each name
function guild:validateIndexes(names)
for i,name in ipairs(names) do
local info = self.guild_info[name]
if info then
local fullname = GetGuildRosterInfo(info.index)
local words = {}
for word in fullname:gmatch("([^-]+)") do
table.insert(words,word)
end
gname = words[1]
realm = words[2]
if gname ~= name then
self:ScanGuild()
break
end
end
end
end
function guild:OfficerNote(name)
assert(type(name) == "string")
return self.guild_info[name] and self.guild_info[name].officernote.note or nil
end
function guild:SetOfficerNote(name, note)
assert(type(name) == "string")
assert(type(note) == "string")
self:validateIndexes({name})
local info = self.guild_info[name]
if info then
info.officernote.note = note
info.officernote.cached = true
table.insert(self.cachedPlayers, name)
GuildRosterSetOfficerNote(info.index, note)
end
self:CacheNote(name,info.note.note, note)
end
function guild:CacheNote(name,note,dkp)
assert(type(name) == "string")
assert(type(note) == "string")
local cache_note = {}
cache_note["note"] = note
cache_note["dkp"] = dkp
cache_note["last_seen"] = date("%m/%d/%y")
self.db.profile.cache[name] = cache_note
end
-- bulk-set officer notes
-- input is 2 parallel arrays
function guild:SetOfficerNotes(names, notes)
assert(type(notes) == "table")
assert(type(names) == "table")
assert(#names == #notes)
self:validateIndexes(names)
for i,name,note in utils.ipairs(names, notes) do
self:SetOfficerNote(name, note)
end
end
function guild:GuildNote(name)
assert(type(name) == "string")
return self.guild_info[name] and self.guild_info[name].note.note or nil
end
function guild:SetGuildNote(name, note)
assert(type(name) == "string")
assert(type(note) == "string")
self:validateIndexes({name})
local info = self.guild_info[name]
if info then
info.note.note = note
info.note.cached = true
table.insert(self.cachedPlayers, name)
GuildRosterSetPublicNote(info.index, note)
end
end
-- bulk-set notes
-- input is 2 parallel arrays
function guild:SetGuildNotes(names, notes)
assert(type(notes) == "table")
assert(type(names) == "table")
assert(#names == #notes)
self:validateIndexes(names)
for i,name,note in utils.ipairs(names, notes) do
self:SetGuildNote(name, note)
end
end
function guild:IsInGuild(name)
assert(type(name) == "string")
return not not self.guild_info[name]
end
local function makeIter(key)
return function (t, obj)
local k,v = next(t, obj)
if k == nil then return nil end
return k, v[key].note
end
end
local function makeIterRaidGroup(key)
return function (t, obj)
local raider = {}
local k,v = next(t, obj)
if k == nil then return nil end
raider.group = v.note.note
raider.note = v[key].note
return k, raider
end
end
function guild:GuildNotePairs()
return makeIter("note"), self.guild_info, nil
end
function guild:OfficerNotePairs()
return makeIter("officernote"), self.guild_info, nil
end
function guild:OfficerNotePairsRaidGroup(raid_group)
return makeIterRaidGroup("officernote"), self.guild_info, nil
end