-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLibGuildStorage-1.0.lua
More file actions
335 lines (300 loc) · 9.09 KB
/
Copy pathLibGuildStorage-1.0.lua
File metadata and controls
335 lines (300 loc) · 9.09 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
-- This library handles storing information in officer notes. It
-- streamlines and optimizes access to these notes. It should be noted
-- that the library does not have correct information until
-- PLAYER_ENTERING_WORLD is fired (for Ace authors this is after OnInitialize
-- is called). The API is as follows:
--
-- GetNote(name): Returns the officer note of member 'name'
--
-- SetNote(name, note): Sets the officer note of member 'name' to
-- 'note'
--
-- GetClass(name): Returns the class of member 'name'
--
-- GetGuildInfo(): Returns the guild info text
--
-- IsCurrentState(): Return true if the state of the library is current.
--
-- Snapshot(table) -- DEPRECATED: Write out snapshot in the table
-- provided. table.guild_info will contain the epgp clause in guild
-- info and table.notes a table of {name, class, note}.
--
-- The library also fires the following messages, which you can
-- register for through RegisterCallback and unregister through
-- UnregisterCallback. You can also unregister all messages through
-- UnregisterAllCallbacks.
--
-- GuildInfoChanged(info): Fired when guild info has changed since its
-- previous state. The info is the new guild info.
--
-- GuildNoteChanged(name, note): Fired when a guild note changes. The
-- name is the name of the member of which the note changed and the
-- note is the new note.
--
-- StateChanged(): Fired when the state of the guild storage cache has
-- changed.
--
local MAJOR_VERSION = "LibGuildStorage-1.0"
local MINOR_VERSION = tonumber(("$Revision: 1399 $"):match("%d+")) or 0
local lib, oldMinor = LibStub:NewLibrary(MAJOR_VERSION, MINOR_VERSION)
if not lib then return end
local Debug = LibStub("LibDebug-1.0")
local CallbackHandler = LibStub("CallbackHandler-1.0")
if not lib.callbacks then
lib.callbacks = CallbackHandler:New(lib)
end
local callbacks = lib.callbacks
if lib.frame then
lib.frame:UnregisterAllEvents()
lib.frame:SetScript("OnEvent", nil)
lib.frame:SetScript("OnUpdate", nil)
else
lib.frame = CreateFrame("Frame", MAJOR_VERSION .. "_Frame")
end
local frame = lib.frame
frame:Show()
frame:SetScript("OnEvent",
function(self, event, ...)
lib[event](lib, ...)
end)
local SendAddonMessage = _G.SendAddonMessage
if ChatThrottleLib then
SendAddonMessage = function(...)
ChatThrottleLib:SendAddonMessage(
"ALERT", MAJOR_VERSION, ...)
end
end
local SetState
-- state of the cache: UNINITIALIZED, STALE,
-- STALE_WAITING_FOR_ROSTER_UPDATE, CURRENT, FLUSHING, REMOTE_FLUSHING
--
-- A complete graph of state changes is found in LibGuildStorage-1.0.dot
local state = "STALE_WAITING_FOR_ROSTER_UPDATE"
local initialized
local index
-- name -> {note=, seen=, class=}
local cache = {}
-- pending notes to write out
local pending_note = {}
local guild_info = ""
function lib:GetNote(name)
local e = cache[name]
if e then return e.note end
end
function lib:SetNote(name, note)
local e = cache[name]
if e then
if pending_note[name] then
DEFAULT_CHAT_FRAME:AddMessage(
string.format("Ignoring attempt to set note before flushing pending "..
"note for %s! "..
"current=[%s] pending=[%s] new[%s]. "..
"Please report this bug along with the actions that "..
"lead to this on http://epgp.googlecode.com",
tostring(name),
tostring(e.note),
tostring(pending_note[name]),
tostring(note)))
else
pending_note[name] = note
SetState("FLUSHING")
end
return e.note
end
end
function lib:GetClass(name)
local e = cache[name]
if e then return e.class end
end
function lib:GetRank(name)
local e = cache[name]
if e then return e.rank end
end
function lib:GetGuildInfo()
return guild_info
end
function lib:IsCurrentState()
return state == "CURRENT"
end
-- This is kept for historical reasons. See:
-- http://code.google.com/p/epgp/issues/detail?id=350.
function lib:Snapshot(t)
assert(type(t) == "table")
t.guild_info = guild_info:match("%-EPGP%-\n(.*)\n\%-EPGP%-")
t.roster_info = {}
for name,info in pairs(cache) do
table.insert(t.roster_info, {name, info.class, info.note})
end
end
--
-- Event handlers
--
frame:RegisterEvent("PLAYER_GUILD_UPDATE")
frame:RegisterEvent("GUILD_ROSTER_UPDATE")
frame:RegisterEvent("CHAT_MSG_ADDON")
frame:RegisterEvent("PLAYER_ENTERING_WORLD")
function lib:CHAT_MSG_ADDON(prefix, msg, type, sender)
Debug("CHAT_MSG_ADDON: %s, %s, %s, %s", prefix, msg, type, sender)
if prefix ~= MAJOR_VERSION or sender == UnitName("player") then return end
if msg == "CHANGES_PENDING" then
SetState("REMOTE_FLUSHING")
elseif msg == "CHANGES_FLUSHED" then
SetState("STALE_WAITING_FOR_ROSTER_UPDATE")
end
end
function lib:PLAYER_GUILD_UPDATE()
if IsInGuild() then
frame:Show()
else
frame:Hide()
end
SetState("STALE_WAITING_FOR_ROSTER_UPDATE")
end
function lib:PLAYER_ENTERING_WORLD()
lib:PLAYER_GUILD_UPDATE()
end
function lib:GUILD_ROSTER_UPDATE(loc)
Debug("GUILD_ROSTER_UPDATE(%s)", tostring(loc))
if loc then
SetState("FLUSHING") -- SetState("STALE_WAITING_FOR_ROSTER_UPDATE")
else
if state ~= "UNINITIALIZED" then
SetState("STALE")
index = nil
end
end
end
--
-- Locally defined functions
--
local valid_transitions = {
UNINITIALIZED = {
CURRENT = true,
},
STALE = {
CURRENT = true,
REMOTE_FLUSHING = true,
STALE_WAITING_FOR_ROSTER_UPDATE = true,
},
STALE_WAITING_FOR_ROSTER_UPDATE = {
STALE = true,
FLUSHING = true,
},
CURRENT = {
FLUSHING = true,
REMOTE_FLUSHING = true,
STALE = true,
},
FLUSHING = {
STALE_WAITING_FOR_ROSTER_UPDATE = true,
},
REMOTE_FLUSHING = {
STALE_WAITING_FOR_ROSTER_UPDATE = true,
},
}
function SetState(new_state)
if state == new_state then return end
if not valid_transitions[state][new_state] then
Debug("Ignoring state change %s -> %s", state, new_state)
return
else
Debug("StateChanged: %s -> %s", state, new_state)
state = new_state
if new_state == FLUSHING then
SendAddonMessage("CHANGES_PENDING", "GUILD")
end
callbacks:Fire("StateChanged")
end
end
local function Frame_OnUpdate(self, elapsed)
debugprofilestart()
if state == "CURRENT" then
return
end
if state == "STALE_WAITING_FOR_ROSTER_UPDATE" then
GuildRoster()
return
end
-- Sometimes GetNumGuildMembers returns 0. In this case return now,
-- so that we call it again and get a proper value.
if GetNumGuildMembers(true) == 0 then return end
if not index or index >= GetNumGuildMembers(true) then
index = 1
end
-- Check guild info for changes.
if index == 1 then
local new_guild_info = GetGuildInfoText() or ""
if new_guild_info ~= guild_info then
guild_info = new_guild_info
callbacks:Fire("GuildInfoChanged", guild_info)
end
end
-- Read up to 100 members at a time.
local last_index = math.min(index + 100, GetNumGuildMembers(true))
Debug("Processing from %d to %d members", index, last_index)
for i = index, last_index do
local name, rank, _, _, _, _, _, note, _, _, class = GetGuildRosterInfo(i)
if name then
local entry = cache[name]
local pending = pending_note[name]
if not entry then
entry = {}
cache[name] = entry
end
entry.rank = rank
entry.class = class
-- Mark this note as seen
entry.seen = true
if entry.note ~= note then
entry.note = note
-- We want to delay all GuildNoteChanged calls until we have a
-- complete view of the guild, otherwise alts might not be
-- rejected (we read alts note before we even know about the
-- main).
if initialized then
callbacks:Fire("GuildNoteChanged", name, note)
end
if pending then
callbacks:Fire("InconsistentNote", name, note, entry.note, pending)
end
end
if pending then
GuildRosterSetOfficerNote(i, pending)
pending_note[name] = nil
end
end
end
index = last_index
if index >= GetNumGuildMembers(true) then
-- We are done, we need to clear the seen marks and delete the
-- unmarked entries. We also fire events for removed members now.
for name, t in pairs(cache) do
if t.seen then
t.seen = nil
else
cache[name] = nil
callbacks:Fire("GuildNoteDeleted", name)
end
end
if not initialized then
-- Now make all GuildNoteChanged calls because we have a full
-- state.
for name, t in pairs(cache) do
callbacks:Fire("GuildNoteChanged", name, t.note)
end
initialized = true
callbacks:Fire("StateChanged")
end
if state == "STALE" then
SetState("CURRENT")
elseif state == "FLUSHING" then
if not next(pending_note) then
SetState("STALE_WAITING_FOR_ROSTER_UPDATE")
SendAddonMessage("CHANGES_FLUSHED", "GUILD")
end
end
end
Debug(tostring(debugprofilestop()).."ms for LibGuildStorage:OnUpdate")
end
frame:SetScript("OnUpdate", Frame_OnUpdate)
GuildRoster()