This repository has been archived by the owner on Sep 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathiGuild.lua
529 lines (442 loc) · 16 KB
/
iGuild.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
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
-----------------------------------
-- Setting up scope, upvalues and libs
-----------------------------------
local AddonName, iGuild = ...;
LibStub("AceEvent-3.0"):Embed(iGuild);
LibStub("AceTimer-3.0"):Embed(iGuild);
LibStub("AceBucket-3.0"):Embed(iGuild);
local L = LibStub("AceLocale-3.0"):GetLocale(AddonName);
local _G = _G; -- I always use _G.FUNC when I call a Global. Upvalueing done here.
local format = string.format;
_G.iGuild = iGuild; -- support for iSocial
-------------------------------
-- Registering with iLib
-------------------------------
LibStub("iLib"):Register(AddonName, nil, iGuild);
-----------------------------------------
-- Variables, functions and colors
-----------------------------------------
local Bucket;
local RosterTimer; -- timer when the roster is fetched again
local COLOR_GOLD = "|cfffed100%s|r";
-- the Roster table, which is the basic data storage of iGuild. Every index is a guild member array.
iGuild.Roster = {};
local TradeSkillDB; -- table for the tradeskill database, f.e. mining, skinning, etc. Indexes are named as well, see below.
iGuild.TradeSkills = {}; -- table[charname] => { [1] = TradeSkillDB-Object, [2] = TradeSkillDB-Object }
local ClassTranslate = {};
for k, v in pairs(_G.LOCALIZED_CLASS_NAMES_MALE) do
ClassTranslate[v] = k;
end
for k, v in pairs(_G.LOCALIZED_CLASS_NAMES_FEMALE) do
ClassTranslate[v] = k;
end
-----------------------------
-- Setting up the LDB
-----------------------------
iGuild.ldb = LibStub("LibDataBroker-1.1"):NewDataObject(AddonName, {
type = "data source",
text = "",
icon = "Interface\\Addons\\iGuild\\Images\\iGuild",
});
iGuild.ldb.OnClick = function(_, button)
if( button == "LeftButton" ) then
if( _G.IsModifierKeyDown() ) then
if( _G.IsAltKeyDown() and _G.CanGuildInvite() ) then
_G.StaticPopup_Show("ADD_GUILDMEMBER");
end
else
_G.ToggleGuildFrame(1);
end
elseif( button == "RightButton" ) then
if( not _G.IsModifierKeyDown() ) then
iGuild:OpenOptions();
end
--@do-not-package@
elseif( button == "MiddleButton" ) then
iGuild:CountAchievements();
--@end-do-not-package@
end
end
iGuild.ldb.OnEnter = function(anchor)
if( iGuild:IsTooltip("Main") or not _G.IsInGuild() ) then
return; -- When not in a guild, fires no tooltip. I dislike addons which show a tooltip with the info "You are not in a guild!".
end
iGuild:HideAllTooltips();
local tip = iGuild:GetTooltip("Main", "UpdateTooltip");
tip:SmartAnchorTo(anchor);
tip:SetAutoHideDelay(0.25, anchor);
tip:Show();
end
iGuild.ldb.OnLeave = function() end -- some display addons refuse to display brokers when this is not defined
-- the DisplayedColumns table defines which columns gonna be displayed in the tooltip. It sorts out columns we cannot use (CanUse option).
iGuild.DisplayedColumns = {};
function iGuild:GetDisplayedColumns()
_G.wipe(self.DisplayedColumns);
local cols = {strsplit(",", self.db.Display)};
local canUse;
for i, v in ipairs(cols) do
v = strtrim(v);
canUse = self.Columns[v].canUse;
if( canUse ) then
if( canUse() ) then
table.insert(self.DisplayedColumns, v);
end
else
table.insert(self.DisplayedColumns, v);
end
end
end
----------------------
-- Boot
----------------------
function iGuild:Boot()
self.db = LibStub("AceDB-3.0"):New("iGuildDB", self:CreateDB(), "Default").profile;
-- dirty check if someone is using an old iGuild config, where this setting was a table
if( type(self.db.Display) == "table" ) then
self.db.Display = "grouped, level, class, name, zone, rank";
end
self:GetDisplayedColumns();
-- the following code snippet is used once after login
self.show_colored_columns();
self.show_colored_columns = nil;
self:RegisterEvent("PLAYER_GUILD_UPDATE", "EventHandler");
self:EventHandler();
self:UnregisterEvent("PLAYER_ENTERING_WORLD");
end
iGuild:RegisterEvent("PLAYER_ENTERING_WORLD", "Boot");
----------------------
-- RosterUpdate
----------------------
function iGuild:EventHandler()
if( _G.IsInGuild() ) then
if( not RosterTimer ) then
Bucket = self:RegisterBucketEvent({
"GUILD_MOTD",
"GUILD_ROSTER_UPDATE",
"GUILD_RANKS_UPDATE"
}, 5, "EventHandler");
self:RegisterEvent("GROUP_ROSTER_UPDATE", "GroupChanged");
self:RegisterEvent("GUILD_TRADESKILL_UPDATE", "TradeSkillUpdate");
-- this three functions are required to query much data from the WoW server.
_G.C_GuildInfo.GuildRoster();
_G.QueryGuildRecipes();
RosterTimer = self:ScheduleRepeatingTimer(_G.C_GuildInfo.GuildRoster, 45);
end
local total, _, totalOn = _G.GetNumGuildMembers();
local ldbText = ("%d/%d"):format(totalOn, total);
local guildName = _G.GetGuildInfo("player");
-- check if guildname is to be shown on the ldb
if( self.db.ShowGuildName and guildName ) then
ldbText = (COLOR_GOLD.." %s"):format(guildName, ldbText);
end
self.ldb.text = ldbText;
self:SetupGuildRoster();
self:CheckTooltips("Main", "Ranks");
else -- Not in Guild!
if( RosterTimer ) then
self:UnregisterBucket(Bucket);
self:UnregisterEvent("GROUP_ROSTER_UPDATE");
self:UnregisterEvent("GUILD_TRADESKILL_UPDATE");
self:UnregisterEvent("GUILD_RANKS_UPDATE");
self:CancelTimer(RosterTimer);
RosterTimer = nil;
end
_G.wipe(self.Roster);
_G.wipe(self.TradeSkills);
self.ldb.text = L["No guild"];
if( self:IsTooltip("Main") ) then
self:HideAllTooltips();
end
end
end
--------------------------------
-- Little event callbacks
--------------------------------
function iGuild:GroupChanged()
self:GetDisplayedColumns();
self:CheckTooltips("Main");
end
--------------------------
-- SetupGuildRoster
--------------------------
do
-- This metatable achieves that we don't need to declare table key pairs.
-- The Guild Roster is saved in an array, but we may access the values via keys, tho! So clever and not too memory intensive. :)
local mt = {
__index = function(t, k)
if ( k == "name" ) then return t[1]
elseif( k == "NAME" ) then return t[2]
elseif( k == "level" ) then return t[3]
elseif( k == "class" ) then return t[4]
elseif( k == "CLASS" ) then return ClassTranslate[t[4]]
elseif( k == "zone" ) then return t[5]
elseif( k == "status") then return t[6]
elseif( k == "mobile") then return t[7]
elseif( k == "apoints")then return t[8]
elseif( k == "arank" ) then return t[9]
elseif( k == "grank" ) then return t[10]
elseif( k == "note" ) then return t[11]
elseif( k == "onote" ) then return t[12]
elseif( k == "trade" ) then return t[13]
elseif( k == "ts1" ) then return t[13]
elseif( k == "ts1prog" and t[13] ) then return iGuild.TradeSkills[t[2]][2]
elseif( k == "ts1id" and t[13] ) then return TradeSkillDB[t[13]][2]
elseif( k == "ts1tex" and t[13] ) then return TradeSkillDB[t[13]][3]
elseif( k == "ts2" ) then return t[14]
elseif( k == "ts2prog" and t[14] ) then return iGuild.TradeSkills[t[2]][4]
elseif( k == "ts2id" and t[14] ) then return TradeSkillDB[t[14]][2]
elseif( k == "ts2tex" and t[14] ) then return TradeSkillDB[t[14]][3]
else return nil end
end,
};
local iter = 1;
function iGuild:SetupGuildRoster()
local total = _G.GetNumGuildMembers();
iter = 1;
_G.wipe(self.Roster);
-- preventing Lua from declaring local values 10000x times per loop - saving memory!
local _, charName, guildRank, guildRankN, charLevel, charClass, charZone, guildNote,
officerNote, isOnline, charStatus, _, acmPoints, acmRank, charMobile, canSoR, repStanding;
for i = 1, total do
charName, guildRank, guildRankN, charLevel, charClass, charZone, guildNote,
officerNote, isOnline, charStatus, _, acmPoints, acmRank, charMobile, canSoR, repStanding = _G.GetGuildRosterInfo(i);
if charMobile then
charZone = L["Remote Chat"];
end
if( isOnline or charMobile ) then
self.Roster[iter] = {
[1] = Ambiguate(charName, "all"),
[2] = charName,
[3] = charLevel,
[4] = charClass,
[5] = charZone or _G.UNKNOWN, -- actually may happen o_O
[6] = charStatus,
[7] = charMobile,
[8] = acmPoints,
[9] = acmRank,
[10] = guildRankN + 1, -- we do not store the guild rank name anymore to save some memory. the rank number must be + 1 to work with API rank indexes
[11] = guildNote or "",
[12] = officerNote or "",
};
if( self.db.Column.tradeskills.Enable and self.TradeSkills[charName] ) then
if( #self.TradeSkills[charName] >= 2 ) then
self.Roster[iter][13] = self.TradeSkills[charName][1];
end
if( #self.TradeSkills[charName] >= 4 ) then
self.Roster[iter][14] = self.TradeSkills[charName][3];
end
end
setmetatable(self.Roster[iter], mt);
iter = iter + 1;
end
end
--@do-not-package@
--[[
self.Roster[iter] = {
[1] = Ambiguate("Dummy01-Onyxia", "all"),
[2] = "Dummy01-Onyxia",
[3] = 100,
[4] = "Priester",
[5] = "Eisenschmiede",
[6] = 0,
[7] = false,
[8] = 10000,
[9] = 3,
[10] = 5,
[11] = "test note",
[12] = "test officer note",
};
setmetatable(self.Roster[iter], mt);
iter = iter + 1;
self.Roster[iter] = {
[1] = Ambiguate("Dummy02-Frostwolf", "all"),
[2] = "Dummy02-Frostwolf",
[3] = 110,
[4] = "Krieger",
[5] = "Orgrimmar",
[6] = 1,
[7] = true,
[8] = 15000,
[9] = 2,
[10] = 4,
[11] = "test note",
[12] = "test officer note",
};
setmetatable(self.Roster[iter], mt);
iter = iter + 1;
self.Roster[iter] = {
[1] = Ambiguate("Dummy03-Onyxia", "all"),
[2] = "Dummy03-Onyxia",
[3] = 80,
[4] = "Hexenmeister",
[5] = "Orgrimmar",
[6] = 1,
[7] = false,
[8] = 7000,
[9] = 4,
[10] = 4,
[11] = "test note",
[12] = "test officer note",
};
setmetatable(self.Roster[iter], mt);
iter = iter + 1;
--]]
--@end-do-not-package@
table.sort(self.Roster, self.Sort[self.db.Sort]);
end
end
--------------------------
-- TradeskillUpdate
--------------------------
-- This is a really tricky one. Due to memory and CPU load, we just want to update the tradeskills once: when the UI is (re)loaded.
-- TradeSkillsFetched determines if the tradeskills are previously updated and quits the function.
-- TradeSkillsUpdating is set to 1 when the below function is working. This is strongly recommended to prevent recursion!
local TradeSkillsFetched, TradeSkillsUpdating;
function iGuild:TradeSkillUpdate()
if( not self.db.Column.tradeskills.Enable ) then
return;
end
-- no tradeskill db, so we generate it from scratch.
if( not TradeSkillDB ) then
TradeSkillDB = {};
_G.TSDB=TradeSkillDB;
for i = 1, _G.GetNumGuildTradeSkill() do
local skillID, isCollapsed, iconTexture, headerName, _, _, _, _, _, _, _, _, _, _ = _G.GetGuildTradeSkillInfo(i);
-- when headerName is set, this is a tradeskill for our DB
if( headerName ) then
TradeSkillDB[headerName] = {
[1] = headerName,
[2] = skillID,
[3] = iconTexture,
[4] = isCollapsed
};
end
end
end
-- prevent recursion
if( TradeSkillsFetched or TradeSkillsUpdating ) then
return;
end
TradeSkillsUpdating = 1;
-- We need to expand the tradeskills in the guild-tradeskill tab in order to fetch the members.
for _, skill in pairs(TradeSkillDB) do
_G.ExpandGuildTradeSkillHeader(skill[2]);
end
-- loop through all tradeskills and users
_G.wipe(iGuild.TradeSkills);
local showOffline = _G.GetGuildRosterShowOffline(); -- store showOffline info set by the user
_G.SetGuildRosterShowOffline(true);
local currentTradeSkill;
for i = 1, _G.GetNumGuildTradeSkill() do
-- name, FULLname (with realm)
local _, _, _, headerName, _, _, _, _, playerName, _, _, _, skillLevel, _, _ = _G.GetGuildTradeSkillInfo(i);
if( headerName ) then
currentTradeSkill = headerName;
elseif( playerName ) then
if( not self.TradeSkills[playerName] ) then
self.TradeSkills[playerName] = {};
end
table.insert(self.TradeSkills[playerName], currentTradeSkill);
table.insert(self.TradeSkills[playerName], skillLevel);
end
end
_G.SetGuildRosterShowOffline(showOffline); -- reset showOffline to not change users configuration
-- We collapse the headers again if they were collapsed by the user before.
for _, skill in pairs(TradeSkillDB) do
if( skill[4] ) then
_G.CollapseGuildTradeSkillHeader(skill[2]);
end
end
TradeSkillsFetched = 1;
TradeSkillsUpdating = nil;
self:EventHandler();
end
-----------------------
-- UpdateTooltip
-----------------------
local function LineClick(_, name, button)
if( button == "LeftButton" ) then
if( _G.IsModifierKeyDown() ) then
if( _G.IsAltKeyDown() ) then
_G.InviteUnit(name);
end
else
_G.SetItemRef(("player:%s"):format(name), ("|Hplayer:%s|h[%s]|h"):format(name, name), "LeftButton");
end
end
end
local function ChangeMOTDClick(_, var, button)
if( button == "LeftButton" ) then
-- check if guild UI is loaded
if( not _G.GuildFrame or not _G.GuildFrame:IsVisible() ) then
_G.ToggleGuildFrame();
end
_G.GuildTextEditFrame_Show("motd");
end
end
function iGuild:UpdateTooltip(tip)
tip:Clear();
tip:SetColumnLayout(#self.DisplayedColumns);
-- check for addon updates
if( LibStub("iLib"):IsUpdate(AddonName) ) then
line = tip:AddHeader(" ");
tip:SetCell(line, 1, "|cffff0000"..L["Addon update available!"].."|r", nil, "CENTER", 0);
end
--------------------------
local name, info, line, member;
-- if MOTD is to be shown, place it first!
if( self.db.ShowGuildMOTD and _G.GetGuildRosterMOTD() ~= "" ) then
local edit = _G.CanEditMOTD();
-- at first, we add MOTD title and eventually a change button
line = tip:AddLine(" ");
tip:SetCell(line, 1, (COLOR_GOLD..(edit and " ["..L["change"].."]" or "")):format(_G.GUILD_MOTD), nil, "LEFT", 0);
if( edit ) then
tip:SetCellScript(line, 1, "OnMouseDown", ChangeMOTDClick);
end
-- now we add the MOTD text
line = tip:AddLine(" ");
tip:SetCell(line, 1, _G.GetGuildRosterMOTD(), nil, "LEFT", 0);
tip:AddLine(" "); -- space between MOTD and Roster
end
-- Looping thru Roster and displaying columns and lines
for y = (self.db.ShowLabels and 0 or 1), #self.Roster do
for x = 1, #self.DisplayedColumns do
name = self.DisplayedColumns[x];
info = self.Columns[name];
-- check if we add a line or a header
if( x == 1 ) then
if( y == 0 ) then
line = tip:AddHeader(" "); -- we have line 0, it's the header line.
else
line = tip:AddLine(" "); -- all others are member lines.
end
end
-- fill lines with content
if( y == 0 ) then
if( self.db.Column[name].ShowLabel ) then
-- in the header line (y = 0), we check if column labels are to be shown.
tip:SetCell(line, x, info.label, nil, self.db.Column[name].Align);
end
else
member = self.Roster[y]; -- fetch member from Roster and brush infos to the cells
tip:SetCell(line, x, info.brush(member), nil, self.db.Column[name].Align);
if( info.script and self.db.Column[name].EnableScript and info.scriptUse(member) ) then
tip:SetCellScript(line, x, "OnMouseDown", info.script, member);
end
if( self.db.Column[name].EnableScript and info.scriptUse(member) ) then
if( info.script ) then
tip:SetCellScript(line, x, "OnMouseDown", info.script, member);
end
if( info.scriptOnEnter and info.scriptOnLeave ) then
tip:SetCellScript(line, x, "OnEnter", info.scriptOnEnter, member);
tip:SetCellScript(line, x, "OnLeave", info.scriptOnLeave, member);
end
end
end
end -- end for x
if( member ) then
tip:SetLineScript(line, "OnMouseDown", LineClick, member.name);
member = nil;
end
end -- end for y
end