-
Notifications
You must be signed in to change notification settings - Fork 0
/
GuildTracker.lua
2490 lines (2200 loc) · 83.3 KB
/
GuildTracker.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
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
GuildTracker = LibStub("AceAddon-3.0"):NewAddon("GuildTracker", "AceEvent-3.0", "AceConsole-3.0", "AceTimer-3.0")
local DB_VERSION = 4
local ICON_DELETE = "|TInterface\\Buttons\\UI-GroupLoot-Pass-Up:16:16:0:0:16:16:0:16:0:16|t"
local ICON_CHAT = "|TInterface\\ChatFrame\\UI-ChatWhisperIcon:16:16:0:0:16:16:0:16:0:16|t"
local ICON_TIMESTAMP = "|TInterface\\HelpFrame\\HelpIcon-ReportLag:16:16:0:0:16:16:0:16:0:16|t"
local ICON_TOGGLE = "|TInterface\\Buttons\\UI-%sButton-Up:18:18:1:0|t"
local CLR_YELLOW = "|cffffd200"
local CLR_DARKYELLOW = "|caaaa9000"
local CLR_GRAY = "|cff909090"
local ROSTER_REFRESH_THROTTLE = 10
local ROSTER_REFRESH_TIMER = 30
local CHANGE_LIMIT = 50
local LDB = LibStub("LibDataBroker-1.1")
local LibQTip = LibStub:GetLibrary("LibQTip-1.0")
local LDBIcon = LibStub("LibDBIcon-1.0")
--- Some local helper functions
local _G = _G
local tinsert, tremove, tsort, twipe = _G.table.insert, _G.table.remove, _G.table.sort, _G.table.wipe
local pairs, ipairs = _G.pairs, _G.ipairs
local wowversion = select(4, GetBuildInfo())
local IsRetail = (wowversion > 90000)
local IsClassic = (wowversion < 20000)
local function tcopy(t)
local u = { }
if type(t) == 'table' then
for k, v in pairs(t) do
u[k] = v
end
end
return setmetatable(u, getmetatable(t))
end
local function tfind(list, val)
for k,v in pairs(list) do
if v == val then
return k
end
end
return nil
end
local function tchelper(first, rest)
return first:upper()..rest:lower()
end
local function toproper(str)
return str:gsub("(%a)([%w']*)", tchelper):gsub("_"," ")
end
local function RGB2HEX(red, green, blue)
return ("|cff%02x%02x%02x"):format(red * 255, green * 255, blue * 255)
end
local RAID_CLASS_COLORS_hex = {}
for k, v in pairs(RAID_CLASS_COLORS) do
RAID_CLASS_COLORS_hex[k] = ("|cff%02x%02x%02x"):format(v.r * 255, v.g * 255, v.b * 255)
end
local function pluralize(amount, singular, plural)
if amount == 1 then
return singular or ""
else
return plural or "s"
end
end
--- DataBroker events -----
local function do_OnEnter(frame)
local tooltip = LibQTip:Acquire("GuildTrackerTip")
tooltip:SmartAnchorTo(frame)
tooltip:SetAutoHideDelay(0.1, frame)
tooltip:EnableMouse(true)
GuildTracker:UpdateTooltip()
tooltip:Show()
end
local function do_OnLeave()
end
local function do_OnClick(frame, button)
--print(button)
if button == "RightButton" then
SettingsInbound.OpenToCategory("Guild Tracker")
else
GuildTracker:Refresh()
end
end
--- Player table cache
local tablecache = {}
local function recycleplayertable(t)
while #t > 0 do
tinsert(tablecache, tremove(t))
end
return t
end
local function getplayertable(...)
local t
if #tablecache > 0 then
t = tremove(tablecache)
else
t = {}
end
for i = 1, select('#', ...) do
t[i] = select(i, ...)
end
return t
end
local function sanitizeName(name)
local hyphen = strfind(name, "-")
if hyphen ~= nil then
if strsub(name, hyphen + 1) == GetRealmName() then
name = strsub(name, 1, hyphen - 1)
end
end
return name
end
-- The available chat types
local ChatType = { "SAY", "YELL", "GUILD", "OFFICER", "PARTY", "RAID", "INSTANCE_CHAT" }
local ChatFormat = { [1] = "Short", [2] = "Long", [3] = "Full" }
local TimeFormat = { [1] = "Absolute", [2] = "Relative", [3] = "Intuitive", [4] = "Small" }
-- These are name lookup lists
local GuildRoster_reverse = {}
local SavedRoster_reverse = {}
-- The guild information fields we store
local Field = {
Name = 1,
Rank = 2,
Class = 3,
Level = 4,
Note = 5,
OfficerNote = 6,
LastOnline = 7,
Points = 8,
SoR = 9,
RepStanding = 10,
}
-- The types of tracked changes
local State = {
Unchanged = 0,
GuildLeave = 1,
GuildJoin = 2,
RankDown = 3,
RankUp = 4,
AccountDisabled = 5,
AccountEnabled = 6,
Inactive = 7,
Active = 8,
OfficerNoteChange = 9,
NoteChange = 10,
LevelChange = 11,
PointsChange = 12,
NameChange = 13,
RepChange = 14,
}
local ComboState = {
[State.RankUp] = State.RankDown,
[State.RankDown] = State.RankUp,
[State.AccountDisabled] = State.AccountEnabled,
[State.AccountEnabled] = State.AccountDisabled,
[State.GuildJoin] = State.GuildLeave,
[State.GuildLeave] = State.GuildJoin,
[State.Inactive] = State.Active,
[State.Active] = State.Inactive
}
local StateInfo = {
[State.Unchanged] = {
color = RGB2HEX(1,1,1),
category = "None",
shorttext = "Unchanged",
longtext = "is unchanged",
template = "is unchanged",
},
[State.GuildJoin] = {
color = RGB2HEX(0.6,1,0.6),
category = "Member",
shorttext = "Member joined",
longtext = "has joined the guild",
template = "has joined the guild (note: '%s')",
},
[State.GuildLeave] = {
color = RGB2HEX(0.95,0.5,0.5),
category = "Member",
shorttext = "Member left",
longtext = "has left the guild",
template = "has left the guild (note: '%s')",
},
[State.RankUp] = {
color = RGB2HEX(0.55,1,1),
category = "Rank",
shorttext = "Rank up",
longtext = "has gone up in rank",
template = "got promoted from '%s' to '%s'",
},
[State.RankDown] = {
color = RGB2HEX(0.15,0.65,0.65),
category = "Rank",
shorttext = "Rank down",
longtext = "has gone down in rank",
template = "got demoted from '%s' to '%s'",
},
[State.AccountEnabled] = {
color = RGB2HEX(1,0.65,1),
category = "Account",
shorttext = "Activated account",
longtext = "activated their account (or received a SoR)",
template = "activated their account (or received a SoR)",
},
[State.AccountDisabled] = {
color = RGB2HEX(0.7,0.3,0.7),
category = "Account",
shorttext = "Deactivated account",
longtext = "deactivated their account (or let their SoR expire)",
template = "deactivated their account (or let their SoR expire)",
},
[State.Active] = {
color = RGB2HEX(0.6,0.6,1),
category = "Activity",
shorttext = "Active",
longtext = "has returned from inactivity",
template = "has logged on after %s of inactivity",
},
[State.Inactive] = {
color = RGB2HEX(0.35,0.35,0.8),
category = "Activity",
shorttext = "Inactive",
longtext = "has been marked inactive",
template = "has not logged on for more than %d days",
},
[State.LevelChange] = {
color = RGB2HEX(1,0.95,0.45),
category = "Level",
shorttext = "Level up",
longtext = "has gained one or more levels",
template = "is now level %d (gained %d)",
},
[State.NoteChange] = {
color = RGB2HEX(0.79,0.58,0.58),
category = "Note",
shorttext = "Note change",
longtext = "got a new guild note",
template = "note changed from \"%s\" to \"%s\"",
},
[State.OfficerNoteChange] = {
color = RGB2HEX(0.65,0.45,0.45),
category = "Note",
shorttext = "Officer note change",
longtext = "got a new officer note",
template = "officer note changed from \"%s\" to \"%s\"",
},
[State.PointsChange] = {
color = RGB2HEX(1.0,0.72,0.22),
category = "Points",
shorttext = "Achievement points",
longtext = "gained one or more achievements",
template = "has now %d achievement points (gained %d)",
},
[State.NameChange] = {
color = RGB2HEX(0.15,0.65,0.15),
category = "Name",
shorttext = "Name change",
longtext = "had a name change",
template = "changed name to \"%s\"",
},
[State.RepChange] = {
color = RGB2HEX(0.35,0.82,0.48),
category = "Reputation",
shorttext = "Guild reputation",
longtext = "has gone up in guild reputation standing",
template = "has become %s with the guild",
},
}
local sorts = {
["Name"] = function(a, b)
local aInfo = (a.type ~= State.GuildLeave) and a.newinfo or a.oldinfo
local bInfo = (b.type ~= State.GuildLeave) and b.newinfo or b.oldinfo
if GuildTracker.db.profile.options.tooltip.sort_ascending then
return aInfo[Field.Name] < bInfo[Field.Name]
end
return aInfo[Field.Name] > bInfo[Field.Name]
end,
["Points"] = function(a, b)
local aInfo = (a.type ~= State.GuildLeave) and a.newinfo or a.oldinfo
local bInfo = (b.type ~= State.GuildLeave) and b.newinfo or b.oldinfo
if aInfo[Field.Points] and bInfo[Field.Points] and aInfo[Field.Points] ~= bInfo[Field.Points] then
if GuildTracker.db.profile.options.tooltip.sort_ascending then
return aInfo[Field.Points] < bInfo[Field.Points]
end
return aInfo[Field.Points] > bInfo[Field.Points]
else
if a.timestamp ~= b.timestamp then
if GuildTracker.db.profile.options.tooltip.sort_ascending then
return a.timestamp < b.timestamp
end
return a.timestamp > b.timestamp
else
if GuildTracker.db.profile.options.tooltip.sort_ascending then
return aInfo[Field.Name] < bInfo[Field.Name]
end
return aInfo[Field.Name] > bInfo[Field.Name]
end
end
end,
["Level"] = function(a, b)
local aInfo = (a.type ~= State.GuildLeave) and a.newinfo or a.oldinfo
local bInfo = (b.type ~= State.GuildLeave) and b.newinfo or b.oldinfo
if aInfo[Field.Level] ~= bInfo[Field.Level] then
if GuildTracker.db.profile.options.tooltip.sort_ascending then
return aInfo[Field.Level] < bInfo[Field.Level]
end
return aInfo[Field.Level] > bInfo[Field.Level]
else
if a.timestamp ~= b.timestamp then
if GuildTracker.db.profile.options.tooltip.sort_ascending then
return a.timestamp < b.timestamp
end
return a.timestamp > b.timestamp
else
if GuildTracker.db.profile.options.tooltip.sort_ascending then
return aInfo[Field.Name] < bInfo[Field.Name]
end
return aInfo[Field.Name] > bInfo[Field.Name]
end
end
end,
["Rank"] = function(a, b)
local aInfo = (a.type ~= State.GuildLeave) and a.newinfo or a.oldinfo
local bInfo = (b.type ~= State.GuildLeave) and b.newinfo or b.oldinfo
if aInfo[Field.Rank] ~= bInfo[Field.Rank] then
if GuildTracker.db.profile.options.tooltip.sort_ascending then
return aInfo[Field.Rank] < bInfo[Field.Rank]
end
return aInfo[Field.Rank] > bInfo[Field.Rank]
else
if a.timestamp ~= b.timestamp then
if GuildTracker.db.profile.options.tooltip.sort_ascending then
return a.timestamp < b.timestamp
end
return a.timestamp > b.timestamp
else
if GuildTracker.db.profile.options.tooltip.sort_ascending then
return aInfo[Field.Name] < bInfo[Field.Name]
end
return aInfo[Field.Name] > bInfo[Field.Name]
end
end
end,
["Type"] = function(a, b)
if a.type ~= b.type then
if GuildTracker.db.profile.options.tooltip.sort_ascending then
return a.type < b.type
end
return a.type > b.type
else
if a.timestamp ~= b.timestamp then
if GuildTracker.db.profile.options.tooltip.sort_ascending then
return a.timestamp < b.timestamp
end
return a.timestamp > b.timestamp
else
local aInfo = (a.type ~= State.GuildLeave) and a.newinfo or a.oldinfo
local bInfo = (b.type ~= State.GuildLeave) and b.newinfo or b.oldinfo
if GuildTracker.db.profile.options.tooltip.sort_ascending then
return aInfo[Field.Name] < bInfo[Field.Name]
end
return aInfo[Field.Name] > bInfo[Field.Name]
end
end
end,
["Offline"] = function(a, b)
local aInfo = (a.type ~= State.GuildLeave) and a.newinfo or a.oldinfo
local bInfo = (b.type ~= State.GuildLeave) and b.newinfo or b.oldinfo
if aInfo[Field.LastOnline] ~= bInfo[Field.LastOnline] then
if GuildTracker.db.profile.options.tooltip.sort_ascending then
return aInfo[Field.LastOnline] < bInfo[Field.LastOnline]
end
return aInfo[Field.LastOnline] > bInfo[Field.LastOnline]
else
if a.timestamp ~= b.timestamp then
if GuildTracker.db.profile.options.tooltip.sort_ascending then
return a.timestamp < b.timestamp
end
return a.timestamp > b.timestamp
else
if GuildTracker.db.profile.options.tooltip.sort_ascending then
return aInfo[Field.Name] < bInfo[Field.Name]
end
return aInfo[Field.Name] > bInfo[Field.Name]
end
end
end,
["Timestamp"] = function(a, b)
if a.timestamp ~= b.timestamp then
if GuildTracker.db.profile.options.tooltip.sort_ascending then
return a.timestamp < b.timestamp
end
return a.timestamp > b.timestamp
else
local aInfo = (a.type ~= State.GuildLeave) and a.newinfo or a.oldinfo
local bInfo = (b.type ~= State.GuildLeave) and b.newinfo or b.oldinfo
if GuildTracker.db.profile.options.tooltip.sort_ascending then
return aInfo[Field.Name] < bInfo[Field.Name]
end
return aInfo[Field.Name] > bInfo[Field.Name]
end
end,
}
StaticPopupDialogs["GUILDTRACKER_REMOVEALL"] = {
preferredIndex = STATICPOPUP_NUMDIALOGS, -- avoid some UI taint, see http://www.wowace.com/announcements/how-to-avoid-some-ui-taint/
text = "Are you sure you want to remove all changes?",
button1 = "Yes",
button2 = "No",
OnAccept = function()
GuildTracker:RemoveAllChanges()
end,
timeout = 0,
whileDead = true,
hideOnEscape = true,
}
--------------------------------------------------------------------------------
function GuildTracker:OnInitialize()
--------------------------------------------------------------------------------
self.db = LibStub("AceDB-3.0"):New("GuildTrackerDB", self:GetDefaults(), true)
self.options = self:GetOptions()
self.options.args.Profiles = LibStub("AceDBOptions-3.0"):GetOptionsTable(self.db)
self.options.args.Profiles.disabled = function() return not self.db.profile.enabled end
self:GenerateStateOptions()
self.dbo = LDB:NewDataObject("GuildTracker", {
type = "data source",
text = "...",
icon = [[Interface\Addons\GuildTracker\GuildTracker]],
OnEnter = do_OnEnter,
OnLeave = do_OnLeave,
OnClick = do_OnClick,
})
LDBIcon:Register("GuildTrackerIcon", self.dbo, self.db.profile.options.minimap)
LibStub("AceConfig-3.0"):RegisterOptionsTable("GuildTracker", self.options)
self.optionsFrame = LibStub("AceConfigDialog-3.0"):AddToBlizOptions("GuildTracker", "Guild Tracker")
self.GuildRoster = {}
self.ChangesPerState = {}
self.LastRosterUpdate = 0
self:RegisterAddonCompartment()
self:Print("Initialized")
end
--------------------------------------------------------------------------------
function GuildTracker:OnEnable()
--------------------------------------------------------------------------------
self:RegisterEvent("PLAYER_LOGOUT")
self:RegisterEvent("PLAYER_GUILD_UPDATE")
self:PLAYER_GUILD_UPDATE()
self:Debug("Enabled")
end
--------------------------------------------------------------------------------
function GuildTracker:OnDisable()
--------------------------------------------------------------------------------
self:Debug("Disabled")
self:UnregisterEvent("PLAYER_LOGOUT")
self:UnregisterEvent("PLAYER_GUILD_UPDATE")
end
--------------------------------------------------------------------------------
function GuildTracker:Toggle()
--------------------------------------------------------------------------------
if not self:IsEnabled() then
self:Enable()
else
self:Disable()
end
end
--------------------------------------------------------------------------------
function GuildTracker:Refresh()
--------------------------------------------------------------------------------
if time() - self.LastRosterUpdate > ROSTER_REFRESH_THROTTLE then
self:Debug("Refresh requested")
C_GuildInfo.GuildRoster()
else
self:Debug("Refresh requested, but throttled")
end
end
--------------------------------------------------------------------------------
function GuildTracker:StartUpdateTimer()
--------------------------------------------------------------------------------
if self.timerGuildUpdate == nil then
self:Debug("Starting update timer")
self.timerGuildUpdate = self:ScheduleRepeatingTimer("Refresh", ROSTER_REFRESH_TIMER)
end
end
--------------------------------------------------------------------------------
function GuildTracker:StopUpdateTimer()
--------------------------------------------------------------------------------
if self.timerGuildUpdate then
self:Debug("Cancelling update timer")
self:CancelTimer(self.timerGuildUpdate)
self.timerGuildUpdate = nil
end
end
--------------------------------------------------------------------------------
function GuildTracker:PLAYER_LOGOUT()
--------------------------------------------------------------------------------
-- Weird things happen during logout, profile options may have been already cleaned up
if self.db and self.db.profile and self.db.profile.options and self.db.profile.options.autoreset then
self:RemoveAllChanges()
end
end
--------------------------------------------------------------------------------
function GuildTracker:PLAYER_GUILD_UPDATE(event, unit)
--------------------------------------------------------------------------------
self:Debug("PLAYER_GUILD_UPDATE " .. (unit or "(nil)"))
if unit and unit ~= "player" then return end
if IsInGuild() then
self:RegisterEvent("GUILD_ROSTER_UPDATE")
if self.db.profile.options.autorefresh then
self:StartUpdateTimer()
else
self:StopUpdateTimer()
end
-- We can't load the database here yet, because the guildname is unknown at this point during login
-- so wait for the guild roster update event
self:Refresh()
else
-- This will unload the database
self:Debug("Not in guild, unloading database")
self.GuildDB = nil
self.GuildName = nil
self:UpdateLDB()
self:StopUpdateTimer()
self:UnregisterEvent("GUILD_ROSTER_UPDATE")
end
end
--------------------------------------------------------------------------------
function GuildTracker:GUILD_ROSTER_UPDATE()
--------------------------------------------------------------------------------
self:Debug("GUILD_ROSTER_UPDATE")
if self.LastRosterUpdate and time() - self.LastRosterUpdate <= ROSTER_REFRESH_THROTTLE then
self:Debug("Recently scanned, event ignored")
return
end
if InCombatLockdown() and not self.db.profile.options.scanincombat then
self:Debug("Not scanning in combat")
return
end
self.GuildName, _, _, self.GuildRealm = GetGuildInfo("player")
if self.GuildName == nil then
self:Debug("No guildname available yet, event ignored")
return
end
if self.GuildRealm == nil then
self.GuildRealm = GetRealmName()
end
-- Load current guild roster into self.GuildRoster
if not self:UpdateGuildRoster() then
self:Debug("Guild roster incomplete, event ignored")
return
end
-- At this point, the loaded roster is considered healthy
self.LastRosterUpdate = time()
-- Switch to our current guild database, and initialize if needed
self:InitGuildDatabase()
-- Find changes between the saved roster and the current guild roster
self:UpdateGuildChanges()
-- Save the current guild roster
self:SaveGuildRoster()
-- Alerts for any new changes
self:ReportNewChanges()
-- Merge any similar changes
self:MergeChanges()
-- Auto expire changes
self:AutoExpireChanges()
-- Update text and tooltips
self:UpdateLDB()
end
-- PRE: IsInGuild() == true and self.GuildName ~= nil
--------------------------------------------------------------------------------
function GuildTracker:InitGuildDatabase()
--------------------------------------------------------------------------------
local guildname = self.GuildName
local guildrealm = self.GuildRealm
-- If necessary, initialize guild database for first use
if self.db.global.guild == nil then
self.db.global.guild = {}
end
if self.db.global.guild[guildrealm] == nil then
self.db.global.guild[guildrealm] = {}
end
if self.db.global.guild[guildrealm][guildname] == nil then
-- See if we can migrate realm data
if (self.db.realm.guild ~= nil and self.db.realm.guild[guildname] ~= nil) then
self:Print(string.format("Migrating existing database for guild '%s' at realm '%s'", guildname, guildrealm))
self.db.global.guild[guildrealm][guildname] = tcopy(self.db.realm.guild[guildname])
self.db.realm.guild[guildname] = nil
else
self:Print(string.format("Creating new database for guild '%s' at realm '%s'", guildname, guildrealm))
self.db.global.guild[guildrealm][guildname] = {
updated = time(),
version = DB_VERSION,
roster = {},
changes = {}
}
end
else
self:Debug(string.format("Using existing database for guild '%s' at realm '%s'", guildname, guildrealm))
end
self.GuildDB = self.db.global.guild[guildrealm][guildname]
self:UpgradeGuildDatabase()
end
--------------------------------------------------------------------------------
function GuildTracker:UpgradeGuildDatabase()
--------------------------------------------------------------------------------
if self.GuildDB.version == nil then
self:Debug("Upgrading database to version 1")
self.GuildDB.version = 1
self.GuildDB.isOfficer = C_GuildInfo:CanViewOfficerNote()
end
if self.GuildDB.version == 1 then
self:Debug("Upgrading database to version 2")
self.GuildDB.version = 2
for i = 1, #self.GuildDB.roster do
local info = self.GuildDB.roster[i]
info[Field.SoR] = info[Field.Points]
info[Field.Points] = nil
end
for i = 1, #self.GuildDB.changes do
local change = self.GuildDB.changes[i]
if change.type == State.PointsChange then
change.type = State.NameChange -- 12 -> 13
elseif change.type == State.LevelChange then
change.type = State.OfficerNoteChange -- 11 -> 9
elseif change.type == State.OfficerNoteChange then
change.type = State.LevelChange -- 9 -> 11
end
if change.oldinfo then
change.oldinfo[Field.SoR] = change.oldinfo[Field.Points]
change.oldinfo[Field.Points] = nil
end
if change.newinfo then
change.newinfo[Field.SoR] = change.newinfo[Field.Points]
change.newinfo[Field.Points] = nil
end
end
end
if self.GuildDB.version == 2 then
self:Debug("Upgrading database to version 3")
self.GuildDB.version = 3
for i = 1, #self.GuildDB.roster do
local info = self.GuildDB.roster[i]
info[Field.Name] = sanitizeName(info[Field.Name])
end
end
if self.GuildDB.version == 3 then
self:Debug("Upgrading database to version 4")
self.GuildDB.version = 4
local homeRealm = GetRealmName()
local function FixName(name)
if strfind(name, "-") == nil then
return name .. "-" .. homeRealm
end
return name
end
for i = 1, #self.GuildDB.roster do
local info = self.GuildDB.roster[i]
info[Field.Name] = FixName(info[Field.Name])
end
for i = 1, #self.GuildDB.changes do
local change = self.GuildDB.changes[i]
if (change.oldinfo[Field.Name]) then
change.oldinfo[Field.Name] = FixName(change.oldinfo[Field.Name])
end
if (change.newinfo[Field.Name]) then
change.newinfo[Field.Name] = FixName(change.newinfo[Field.Name])
end
end
end
end
--------------------------------------------------------------------------------
function GuildTracker:UpdateGuildRoster()
--------------------------------------------------------------------------------
local numGuildMembers = GetNumGuildMembers()
local name, rank, rankIndex, level, class, zone, note, officernote, online, status, classconst, achievementPoints, achievementRank, isMobile, canSoR, repStanding
local hours, days, months, years, lastOnline
local players = recycleplayertable(self.GuildRoster)
twipe(GuildRoster_reverse)
self:Debug(string.format("Scanning %d guild members", numGuildMembers))
local isRosterHealthy = true
for i = 1, numGuildMembers, 1 do
name, rank, rankIndex, level, class, zone, note, officernote, online, status, classconst, achievementPoints, achievementRank, isMobile, canSoR, repStanding = GetGuildRosterInfo(i)
-- During initial load some character names may only load partially
if name == nil or strfind(name, "-") == nil then
self:Debug("Warning: Found incomplete name: " .. (name or "nil"))
isRosterHealthy = false
break
end
years, months, days, hours = GetGuildRosterLastOnline(i)
lastOnline = (online or not years) and 0 or (years * 365 + months * 30.417 + days + hours/24)
tinsert(players, getplayertable(name, rankIndex, classconst, level, note, officernote, lastOnline, achievementPoints, canSoR, repStanding))
-- Keep our reverse lookup table in sync
GuildRoster_reverse[name] = #players
end
if #self.GuildRoster ~= numGuildMembers then
self:Debug(string.format("Warning: Expected %d guild members but got %d", numGuildMembers, #self.GuildRoster))
isRosterHealthy = false
end
return isRosterHealthy
end
--------------------------------------------------------------------------------
function GuildTracker:UpdateGuildChanges()
--------------------------------------------------------------------------------
-- We don't know what guild we're updating yet
if self.GuildDB == nil then
return
end
-- Fail-safe when the roster returned 0 guild members; this can't be right so ignore
if #self.GuildRoster == 0 then
return
end
local oldRoster = self.GuildDB.roster
-- If there's no saved roster data available, we can't determine any changes yet
-- This happens when the first time we scan a guild
if oldRoster == nil or #oldRoster == 0 then
return
end
self:Debug("Detecting guild changes")
-- Sync our name lookup table
self:FixReverseTable(oldRoster, SavedRoster_reverse)
-- First update the existing entries of our SavedRoster
for i = 1, #oldRoster do
local info = oldRoster[i]
local newPlayerInfo = self:FindPlayerByName(self.GuildRoster, info[Field.Name], GuildRoster_reverse)
if newPlayerInfo == nil then
self:AddGuildChange(State.GuildLeave, info, nil)
else
-- Rank
if newPlayerInfo[Field.Rank] < info[Field.Rank] then
self:AddGuildChange(State.RankUp, info, newPlayerInfo)
elseif newPlayerInfo[Field.Rank] > info[Field.Rank] then
self:AddGuildChange(State.RankDown, info, newPlayerInfo)
end
-- Scroll of resurrection
if newPlayerInfo[Field.SoR] and not info[Field.SoR] then
self:AddGuildChange(State.AccountDisabled, info, newPlayerInfo)
elseif not newPlayerInfo[Field.SoR] and info[Field.SoR] then
self:AddGuildChange(State.AccountEnabled, info, newPlayerInfo)
end
-- Activity
if newPlayerInfo[Field.LastOnline] >= self.db.profile.options.inactive and info[Field.LastOnline] < self.db.profile.options.inactive then
self:AddGuildChange(State.Inactive, info, newPlayerInfo)
elseif newPlayerInfo[Field.LastOnline] < self.db.profile.options.inactive and info[Field.LastOnline] >= self.db.profile.options.inactive then
self:AddGuildChange(State.Active, info, newPlayerInfo)
end
-- Level
if newPlayerInfo[Field.Level] ~= info[Field.Level] and newPlayerInfo[Field.Level] >= self.db.profile.options.minlevel then
self:AddGuildChange(State.LevelChange, info, newPlayerInfo)
end
-- Achievement Points
if info[Field.Points] and info[Field.Points] > 0 and newPlayerInfo[Field.Points] <= 0 then
-- Points going down to zero indicates faulty roster info from a guild member who just logged on
-- Let's mark it in our snapshot, so we can ignore it when it recovers
self:Debug("Achievement points for member " .. sanitizeName(info[Field.Name]) .. " was 0 in last update, marking faulty roster entry")
newPlayerInfo[Field.Points] = nil
elseif info[Field.Points] and newPlayerInfo[Field.Points] and newPlayerInfo[Field.Points] > 0 and newPlayerInfo[Field.Points] ~= info[Field.Points] and newPlayerInfo[Field.Level] >= self.db.profile.options.minlevel then
self:AddGuildChange(State.PointsChange, info, newPlayerInfo)
end
-- Note
if newPlayerInfo[Field.Note] ~= info[Field.Note] then
self:AddGuildChange(State.NoteChange, info, newPlayerInfo)
end
-- Officer note
if newPlayerInfo[Field.OfficerNote] ~= info[Field.OfficerNote] and self.GuildDB.isOfficer == C_GuildInfo:CanViewOfficerNote() then
self:AddGuildChange(State.OfficerNoteChange, info, newPlayerInfo)
end
-- Reputation standing
if info[Field.RepStanding] and newPlayerInfo[Field.RepStanding] ~= info[Field.RepStanding] then
self:AddGuildChange(State.RepChange, info, newPlayerInfo)
end
end
end
-- Then add any new entries
for i = 1, #self.GuildRoster do
local info = self.GuildRoster[i]
local oldPlayerInfo = self:FindPlayerByName(oldRoster, info[Field.Name], SavedRoster_reverse)
if oldPlayerInfo == nil then
self:AddGuildChange(State.GuildJoin, nil, info)
end
end
self:SortAndGroupChanges()
self:DetectNameChanges()
end
--------------------------------------------------------------------------------
function GuildTracker:SortAndGroupChanges()
--------------------------------------------------------------------------------
-- Remove 'unchanged' entries
local lastUsedIdx = 1
for i = 1, #self.GuildDB.changes do
self.GuildDB.changes[lastUsedIdx] = self.GuildDB.changes[i]
if self.GuildDB.changes[i].type ~= State.Unchanged then
lastUsedIdx = lastUsedIdx + 1
end
end
for i = lastUsedIdx, #self.GuildDB.changes do
self.GuildDB.changes[i] = nil
end
-- Apply sorting
local sort_func = sorts[self.db.profile.options.tooltip.sorting]
if sort_func ~= nil then
tsort(self.GuildDB.changes, sort_func)
end
-- Apply grouping per state
local groupChanges = {}
for key, val in pairs(State) do
groupChanges[val] = {}
end
for idx,change in ipairs(self.GuildDB.changes) do
tinsert(groupChanges[change.type], idx)
end
self.ChangesPerState = groupChanges
end
--------------------------------------------------------------------------------
function GuildTracker:DetectNameChanges()
--------------------------------------------------------------------------------
local foundNameChanges = false
local joins = self.ChangesPerState[State.GuildJoin]
local quits = self.ChangesPerState[State.GuildLeave]
if joins and quits and #joins > 0 and #quits > 0 then
for _, joinIdx in ipairs(joins) do
local changeJoin = self.GuildDB.changes[joinIdx]
for _, quitIdx in ipairs(quits) do
local changeQuit = self.GuildDB.changes[quitIdx]
if self:IsNameChange(changeJoin, changeQuit) then
self:Debug(format("Merging '%s quit' and '%s joined' into name change", changeQuit.oldinfo[Field.Name], changeJoin.newinfo[Field.Name]))
changeJoin.type = State.NameChange
changeJoin.oldinfo = changeQuit.oldinfo
changeQuit.type = State.Unchanged
foundNameChanges = true
break
end
end
end
end
if foundNameChanges then
-- This will clean up our table, and re-group the name change entries
self:SortAndGroupChanges()
end
end
--------------------------------------------------------------------------------
function GuildTracker:IsNameChange(changeJoin, changeQuit)
--------------------------------------------------------------------------------
local infoJoin = changeJoin.newinfo
local infoQuit = changeQuit.oldinfo
local pointDiff = infoJoin[Field.Points] - infoQuit[Field.Points]
return changeJoin.timestamp == changeQuit.timestamp
and infoJoin[Field.Class] == infoQuit[Field.Class]
and infoJoin[Field.Rank] == infoQuit[Field.Rank]
and infoJoin[Field.Level] == infoQuit[Field.Level]
and infoJoin[Field.Note] == infoQuit[Field.Note]
and infoJoin[Field.RepStanding] == infoQuit[Field.RepStanding]
and pointDiff >= 0 and pointDiff < 100 -- arbitrary range that we'll consider the same character
end
--------------------------------------------------------------------------------
function GuildTracker:AddGuildChange(state, oldInfo, newInfo)
--------------------------------------------------------------------------------
if not self.db.profile.options.filter[state] then
self:Debug("Ignoring guild change of type " .. state)
return
end
self:Debug("Adding guild change of type " .. state)
local newchange = {}
newchange.timestamp = self.LastRosterUpdate
newchange.oldinfo = tcopy(oldInfo)
newchange.newinfo = tcopy(newInfo)
newchange.type = state
tinsert(self.GuildDB.changes, newchange)
end
--------------------------------------------------------------------------------
function GuildTracker:MergeChanges()
--------------------------------------------------------------------------------
if self.db.profile.options.tooltip.merging then
self:MergeStateChanges(State.LevelChange)
self:MergeStateChanges(State.PointsChange)
self:MergeStateChanges(State.NoteChange)
self:MergeStateChanges(State.OfficerNoteChange)
self:MergeStateChanges(State.Active)
self:MergeStateChanges(State.AccountEnabled)
--self:MergeStateChanges(State.GuildJoin)
self:MergeStateChanges(State.RankUp)
self:MergeStateChanges(State.NameChange)
self:SortAndGroupChanges()
end
end
--------------------------------------------------------------------------------
function GuildTracker:MergeStateChanges(state)
--------------------------------------------------------------------------------