-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathLootReserve.lua
1405 lines (1248 loc) · 45.3 KB
/
LootReserve.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
local addon, ns = ...;
LootReserve = LibStub("AceAddon-3.0"):NewAddon("LootReserve", "AceComm-3.0");
LootReserve.Version = GetAddOnMetadata(addon, "Version");
LootReserve.MinAllowedVersion = GetAddOnMetadata(addon, "X-Min-Allowed-Version");
LootReserve.LatestKnownVersion = LootReserve.Version;
LootReserve.Enabled = true;
LootReserve.EventFrame = CreateFrame("Frame", nil, UIParent);
LootReserve.EventFrame:SetPoint("TOPLEFT", UIParent, "TOPLEFT", 0, 0);
LootReserve.EventFrame:SetSize(0, 0);
LootReserve.EventFrame:Show();
LootReserve.LibRangeCheck = LibStub("LibRangeCheck-2.0");
LootReserve.ItemCache = LibStub("ItemCache");
LootReserve.LibDD = LibStub("LibUIDropDownMenu-4.0");
LootReserveCharacterSave =
{
Client =
{
CharacterFavorites = nil,
},
Server =
{
CurrentSession = nil,
RequestedRoll = nil,
RollHistory = nil,
RecentLoot = nil,
},
};
LootReserveGlobalSave =
{
Client =
{
Settings = nil,
GlobalFavorites = nil,
},
Server =
{
NewSessionSettings = nil,
Settings = nil,
GlobalProfile = nil,
},
};
LootReserve.BagCache = nil;
LootReserve.Listeners = {
RESERVES = { },
};
StaticPopupDialogs["LOOTRESERVE_GENERIC_ERROR"] =
{
text = "%s",
button1 = CLOSE,
timeout = 0,
whileDead = 1,
hideOnEscape = 1,
};
LOOTRESERVE_BACKDROP_BLACK_4 =
{
bgFile = "Interface\\DialogFrame\\UI-DialogBox-Background",
edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
edgeSize = 16,
insets = { left = 4, right = 4, top = 4, bottom = 4 },
};
SLASH_LOOTRESERVE1 = "/lootreserve";
SLASH_LOOTRESERVE2 = "/reserve";
SLASH_LOOTRESERVE3 = "/res";
function SlashCmdList.LOOTRESERVE(command)
command = command:lower();
if command == "" then
LootReserve.Client.Window:SetShown(not LootReserve.Client.Window:IsShown());
elseif command == "server" or command == "host" then
LootReserve:ToggleServerWindow(not LootReserve.Server.Window:IsShown());
elseif command == "roll" or command == "rolls" then
LootReserve:ToggleServerWindow(not LootReserve.Server.Window:IsShown(), true);
end
end
local pendingToggleServerWindow = nil;
local pendingLockdownHooked = nil;
function LootReserve:ToggleServerWindow(state, rolls)
if InCombatLockdown() and LootReserve.Server.Window:IsProtected() then
pendingToggleServerWindow = { state, rolls };
if not pendingLockdownHooked then
pendingLockdownHooked = true;
self:RegisterEvent("PLAYER_REGEN_ENABLED", function()
if pendingToggleServerWindow then
local params = pendingToggleServerWindow;
pendingToggleServerWindow = nil;
self:ToggleServerWindow(unpack(params));
end
end);
end
self:PrintMessage("Host window will %s once you're out of combat", state and "open" or "close");
return;
end
if rolls then
self.Server.Window:Show();
self.Server:OnWindowTabClick(self.Server.Window.TabRolls);
else
self.Server.Window:SetShown(state);
end
end
function LootReserve:OnInitialize()
end
function LootReserve:OnEnable()
LootReserve.Client:Load();
LootReserve.Server:Load();
if LootReserve.Client.Settings.AllowPreCache then
LootReserve.ItemSearch:Load();
end
LootReserve.Comm:StartListening();
local function Startup()
LootReserve.Server:Startup();
-- Query other group members about their addon versions and request server session info if any
LootReserve.Client:SearchForServer(true);
end
LootReserve:RegisterEvent("GROUP_JOINED", function()
-- Load client and server after WoW client restart
-- Server session should not normally exist when the player is outside of any raid groups, so restarting it upon regular group join shouldn't break anything
-- With a delay, due to possible name cache issues
C_Timer.After(1, function() Startup(); end); -- Wrap in anonymous function just in case of blizzard bug
end);
-- Load client and server after UI reload
-- This should be the only case when a player is already detected to be in a group at the time of addon loading
Startup();
end
function LootReserve:OnDisable()
end
-- Other addons may use this to be notified of things that happen in LootReserve.
-- Return value is boolean, whether registration succeeded.
function LootReserve:RegisterListener(category, id, callback)
local success, result = pcall(function()
if not category or not self.Listeners[category] then return false; end
self.Listeners[category][id] = callback;
return true;
end);
return result or false;
end
function LootReserve:UnregisterListener(category, id)
local success, result = pcall(function()
if not category or not self.Listeners[category] then return false; end
self.Listeners[category][id] = nil;
return true;
end);
return result or false;
end
-- Other addons may use this to request data manually.
-- Return value is boolean, whether the notification was successful.
function LootReserve:PromptListener(category, id)
if id then
return self:NotifyListeners(category, id);
end
return false;
end
function LootReserve:NotifyListeners(category, whiteID)
local GetPackage;
if category == "RESERVES" then
local pkg;
GetPackage = function()
if not pkg then
pkg = { };
local session = LootReserve.Server.CurrentSession;
if session then
for member, memberData in pairs(session.Members) do
pkg[member] = { };
for _, reserve in ipairs(memberData.ReservedItems) do
table.insert(pkg[member], reserve)
end
end
end
end
return pkg;
end
end
local success = false;
if GetPackage then
for id, callback in pairs(self.Listeners.RESERVES) do
if not whiteID or id == whiteID then
pcall(function() callback(GetPackage()); end);
success = true;
end
end
end
return success;
end
function LootReserve:ShowError(fmt, ...)
StaticPopup_Show("LOOTRESERVE_GENERIC_ERROR", "|cFFFFD200LootReserve|r|n|n" .. format(fmt, ...) .. "|n ");
end
function LootReserve:PrintError(fmt, ...)
DEFAULT_CHAT_FRAME:AddMessage("|cFFFFD200LootReserve: |r" .. format(fmt, ...), 1, 0, 0);
end
function LootReserve:PrintMessage(fmt, ...)
DEFAULT_CHAT_FRAME:AddMessage("|cFFFFD200LootReserve: |r" .. format(fmt, ...), 1, 1, 1);
end
function LootReserve:debug(...)
--@debug@
print("[DEBUG] ", ...);
--@end-debug@
end
function LootReserve:debugFunc(func)
--@debug@
func();
--@end-debug@
end
function LootReserve:RegisterUpdate(handler)
LootReserve.EventFrame:HookScript("OnUpdate", function(self, elapsed)
handler(elapsed);
end);
end
function LootReserve:RegisterEvent(...)
if not LootReserve.EventFrame.RegisteredEvents then
LootReserve.EventFrame.RegisteredEvents = { };
LootReserve.EventFrame:SetScript("OnEvent", function(self, event, ...)
local handlers = self.RegisteredEvents[event];
if handlers then
for _, handler in ipairs(handlers) do
handler(...);
end
end
end);
end
local params = select("#", ...);
local handler = select(params, ...);
if type(handler) ~= "function" then
error("LootReserve:RegisterEvent: The last passed parameter must be the handler function");
return;
end
for i = 1, params - 1 do
local event = select(i, ...);
if type(event) == "string" then
LootReserve.EventFrame:RegisterEvent(event);
LootReserve.EventFrame.RegisteredEvents[event] = LootReserve.EventFrame.RegisteredEvents[event] or { };
table.insert(LootReserve.EventFrame.RegisteredEvents[event], handler);
else
error("LootReserve:RegisterEvent: All but the last passed parameters must be event names");
end
end
end
function LootReserve:SetResizeBounds(frame, minWidth, minHeight, maxWidth, maxHeight)
if frame.SetResizeBounds then
frame:SetResizeBounds(minWidth, minHeight, maxWidth, maxHeight);
else
if minWidth or minHeight then
frame:SetMinResize(minWidth, minHeight);
end
if maxWidth or maxHeight then
frame:SetMaxResize(maxWidth, maxHeight);
end
end
end
function LootReserve:GetContainerItemInfo(bag, slot)
local containerInfo;
if C_Container then
containerInfo = C_Container.GetContainerItemInfo(bag, slot);
else
local icon, itemCount, locked, quality, readable, lootable, itemLink, isFiltered, noValue, itemID, isBound = GetContainerItemInfo(bag, slot);
if itemLink then
containerInfo = {
iconFileID = icon,
stackCount = itemCount,
isLocked = locked,
quality = quality,
isReadable = readable,
hasLoot = lootable,
hyperlink = itemLink,
isFiltered = isFiltered,
hasNoValue = noValue,
itemID = itemID,
isBound = isBound,
};
end
end
return containerInfo;
end
function LootReserve:GetContainerNumSlots(bag)
if C_Container and C_Container.PickupContainerItem then
return C_Container.GetContainerNumSlots(bag);
else
return GetContainerNumSlots(bag);
end
end
function LootReserve:PickupContainerItem(bag, slot)
if C_Container and C_Container.PickupContainerItem then
return C_Container.PickupContainerItem(bag, slot);
else
return PickupContainerItem(bag, slot);
end
end
function LootReserve:OpenMenu(menu, menuContainer, anchor)
if L_UIDROPDOWNMENU_OPEN_MENU == menuContainer then
CloseMenus();
return;
end
local function FixMenu(menu)
for _, item in ipairs(menu) do
if item.notCheckable == nil then
item.notCheckable = item.checked == nil;
end
if item.keepShownOnClick == nil and item.checked ~= nil then
item.keepShownOnClick = true;
end
if item.tooltipText and item.tooltipTitle == nil then
item.tooltipTitle = item.text;
end
if item.tooltipText and item.tooltipOnButton == nil then
item.tooltipOnButton = true;
end
if item.hasArrow == nil and item.menuList then
item.hasArrow = true;
end
if item.keepShownOnClick == nil and item.menuList then
item.keepShownOnClick = true;
end
if item.menuList then
FixMenu(item.menuList);
end
end
end
FixMenu(menu);
LootReserve.LibDD:EasyMenu(menu, menuContainer, anchor, 0, 0, "MENU");
end
function LootReserve:OpenSubMenu(...)
for submenu = 1, select("#", ...) do
local arg1 = select(submenu, ...);
local opened = false;
for i = 1, L_UIDROPDOWNMENU_MAXBUTTONS do
local button = _G["L_DropDownList"..submenu.."Button"..i];
if button and button.arg1 == arg1 then
local arrow = _G[button:GetName().."ExpandArrow"];
if arrow then
local Click = arrow:GetScript("OnMouseDown");
if Click then
Click(arrow, "LButton");
opened = true;
end
break;
end
end
end
if not opened then
return false;
end
end
return true;
end
function LootReserve:ReopenMenu(button, ...)
CloseMenus();
button:Click();
self:OpenSubMenu(...);
end
function LootReserve:Round(num, nearest)
nearest = nearest or 1;
local lower = math.floor(num / nearest) * nearest;
local upper = lower + nearest;
return (upper - num < num - lower) and upper or lower;
end
-- Used to prevent LootReserve:SendChatMessage from breaking a hyperlink into multiple segments if the message is too long
-- Use it if a text of undetermined length preceeds the hyperlink
-- GOOD: format("%s win %s", strjoin(", ", players), LootReserve:FixLink(link)) - players might contain so many names that the message overflows 255 chars limit
-- BAD: format("%s won by %s", LootReserve:FixLink(link), strjoin(", ", players)) - link is always early in the message and will never overflow the 255 chars limit
function LootReserve:FixLink(link)
return link:gsub(" ", "\1");
end
function LootReserve:FixText(text)
return text:gsub("\1", " ");
end
function LootReserve:SendChatMessage(text, channel, target)
if channel == "RAID_WARNING" and not (UnitIsGroupLeader("player") or UnitIsGroupAssistant("player")) then
channel = "RAID";
end
if channel == "RAID" and not IsInRaid() then
channel = "PARTY";
end
if channel == "PARTY" and not IsInGroup() then
channel, target = "WHISPER", LootReserve:Me();
end
if target and not LootReserve:IsPlayerOnline(target) then return; end
local function Send(text)
if #text > 0 then
text = self:FixText(text);
if self.Server.SentMessages[text] then
self.Server.SentMessages[text]:Cancel();
end
self.Server.SentMessages[text] = C_Timer.NewTicker(10, function() self.Server.SentMessages[text] = nil end, 1);
if ChatThrottleLib then
ChatThrottleLib:SendChatMessage("NORMAL", self.Comm.Prefix, text, channel, nil, target);
else
SendChatMessage(text, channel, nil, target);
end
end
end
if #text <= 250 then
Send(text);
else
text = text .. " ";
local accumulator = "";
for word in text:gmatch("[^ ]- ") do
if #accumulator + #word > 250 then
Send(self:StringTrim(accumulator));
accumulator = "";
end
accumulator = accumulator .. word;
end
Send(self:StringTrim(accumulator));
end
end
function LootReserve:GetCurrentExpansion()
local version = GetBuildInfo();
local expansion, major, minor = strsplit(".", version);
return tonumber(expansion) - 1;
end
function LootReserve:IsCrossRealm()
return self:GetCurrentExpansion() == 0 and not C_Seasons.HasActiveSeason();
-- This doesn't really work, because even in non-connected realms UnitFullName ends up returning your realm name,
-- and we can't use UnitName either, because that one NEVER returns a realm for "player". WTB good API, 5g.
--[[
if self.CachedIsCrossRealm == nil then
local name, realm = UnitFullName("player");
if name then
self.CachedIsCrossRealm = realm ~= nil;
end
end
return self.CachedIsCrossRealm;
]]
end
function LootReserve:GetNumClasses()
return 11;
end
function LootReserve:GetClassInfo(classID)
local info = C_CreatureInfo.GetClassInfo(classID);
if info then
return info.className, info.classFile, info.classID;
end
end
function LootReserve:Player(player)
if not self:IsCrossRealm() then
return Ambiguate(player, "short");
end
local name, realm = strsplit("-", player);
if not realm then
realm = GetNormalizedRealmName();
if not realm then
-- it really does happen
realm = GetRealmName();
if realm then
realm = realm:gsub("[%s%-]", "");
end
if not realm then
-- ¯\_(ツ)_/¯
return name;
end
end
end
return name .. "-" .. realm;
end
function LootReserve:Me()
return self:Player(UnitName("player"));
end
function LootReserve:IsMe(player)
return self:IsSamePlayer(player, self:Me());
end
function LootReserve:IsSamePlayer(a, b)
return self:Player(a) == self:Player(b);
end
function LootReserve:IsPlayerOnline(player)
return self:ForEachRaider(function(name, _, _, _, _, _, _, online)
if self:IsSamePlayer(name, player) then
return online or false;
end
end);
end
local function LootReserve_UnitInRaid(player)
if not LootReserve:IsCrossRealm() then
return UnitInRaid(player);
end
return IsInRaid() and LootReserve:ForEachRaider(function(name, _, _, _, className, classFilename, _, online)
if LootReserve:IsSamePlayer(name, player) then
return true;
end
end);
end
local function LootReserve_UnitInParty(player)
if not LootReserve:IsCrossRealm() then
return UnitInParty(player);
end
return IsInGroup() and not IsInRaid() and LootReserve:ForEachRaider(function(name, _, _, _, className, classFilename, _, online)
if LootReserve:IsSamePlayer(name, player) then
return true;
end
end);
end
function LootReserve:UnitInGroup(player)
if not self:IsCrossRealm() then
if IsInRaid() then
return LootReserve_UnitInRaid(player) and true;
elseif IsInGroup() then
return LootReserve_UnitInParty(player);
else
return LootReserve:IsMe(player);
end
end
return self:ForEachRaider(function(name)
if self:IsSamePlayer(name, player) then
return true;
end
end);
end
function LootReserve:UnitClass(player)
if not self:IsCrossRealm() then
local className, classFilename, classId = UnitClass(player);
if not className then
if LootReserve.Server.CurrentSession and LootReserve.Server.CurrentSession.Members[player] and LootReserve.Server.CurrentSession.Members[player].Class then
return LootReserve:GetClassInfo(LootReserve.Server.CurrentSession.Members[player].Class);
elseif LootReserve.Server.NewSessionSettings and LootReserve.Server.NewSessionSettings.ImportedMembers and LootReserve.Server.NewSessionSettings.ImportedMembers[player] and LootReserve.Server.NewSessionSettings.ImportedMembers[player].Class then
return LootReserve:GetClassInfo(LootReserve.Server.NewSessionSettings.ImportedMembers[player].Class);
end
end
return className, classFilename, classId;
end
return self:ForEachRaider(function(name, _, _, _, className, classFilename)
if self:IsSamePlayer(name, player) then
return className, classFilename, LootReserve.Constants.ClassFilenameToClassID[classFilename];
end
end);
end
function LootReserve:UnitRace(player)
if not self:IsCrossRealm() then
return UnitRace(player);
end
return self:ForEachRaider(function(name)
if self:IsSamePlayer(name, player) then
local unitID = self:GetGroupUnitID(player);
if unitID then
return UnitRace(unitID);
end
end
end);
end
function LootReserve:UnitSex(player)
if not self:IsCrossRealm() then
return UnitSex(player);
end
return self:ForEachRaider(function(name)
if self:IsSamePlayer(name, player) then
local unitID = self:GetGroupUnitID(player);
if unitID then
return UnitSex(unitID);
end
end
end);
end
local function GetPlayerClassColor(player, dim, class)
local className, classFilename, classId = LootReserve:UnitClass(player);
if class then
className, classFilename, classId = LootReserve:GetClassInfo(class);
end
if classFilename then
local colors = RAID_CLASS_COLORS[classFilename];
if colors then
if dim then
local r, g, b, a = colors:GetRGBA();
return format("FF%02X%02X%02X", r * 128, g * 128, b * 128);
else
return colors.colorStr;
end
end
end
return dim and "FF404040" or "FF808080";
end
local function GetRaidUnitID(player)
for i = 1, MAX_RAID_MEMBERS do
local unit = UnitName("raid" .. i);
if unit and LootReserve:IsSamePlayer(LootReserve:Player(unit), player) then
return "raid" .. i;
end
end
if LootReserve:IsMe(player) then
return "player";
end
end
local function GetPartyUnitID(player)
for i = 1, MAX_PARTY_MEMBERS do
local unit = UnitName("party" .. i);
if unit and LootReserve:IsSamePlayer(LootReserve:Player(unit), player) then
return "party" .. i;
end
end
if LootReserve:IsMe(player) then
return "player";
end
end
function LootReserve:GetGroupUnitID(player)
if IsInRaid() then
return GetRaidUnitID(player);
elseif IsInGroup() then
return GetPartyUnitID(player);
elseif self:IsMe(player) then
return "player"
end
end
function LootReserve:ColoredPlayer(player, class)
local name, realm = strsplit("-", player);
return realm and format("|c%s%s|r|c%s-%s|r", GetPlayerClassColor(player, false, class), name, GetPlayerClassColor(player, true, class), realm)
or format("|c%s%s|r", GetPlayerClassColor(player, false, class), player);
end
function LootReserve:ForEachRaider(func)
if not IsInGroup() then
local className, classFilename = UnitClass("player");
local raceName, raceFilename = UnitRace("player");
return func(self:Me(), 0, 1, UnitLevel("player"), className, classFilename, nil, true, UnitIsDead("player"), nil, nil, nil, "player", nil);
end
for i = 1, MAX_RAID_MEMBERS do
local name, rank, subgroup, level, class, fileName, zone, online, isDead, role, isML, combatRole = GetRaidRosterInfo(i);
if name then
local name = self:Player(name);
local unitID = self:IsMe(name) and "player" or UnitInRaid("player") and ("raid" .. i) or ("party" .. i);
local result, a, b = func(name, rank, subgroup, level, class, fileName, zone, online, isDead, role, isML, combatRole, unitID, i);
if result ~= nil then
return result, a, b;
end
else
break;
end
end
end
local charSimplifications =
{
["a" ] = "ÀÁÂÃÄÅàáâãäåĀāĂ㥹ǍǎǞǟǠǡǺǻȀȁȂȃɐɑɒ",
["ae" ] = "ÆæǢǣǼǽ",
["b" ] = "ƀƁƂƃɓʙ",
["c" ] = "ÇçĆćĈĉĊċČčƇƈɔɕʗ",
["d" ] = "ĎďĐđƉƊƋƌɖɗ",
["dz" ] = "DŽDždžDZDzdzʣʥ",
["e" ] = "ÈÉÊËèéêëĒēĔĕĖėĘęĚěƎƐǝȄȅȆȇɘəɚɛɜɝɞʚ",
["eth"] = "ð",
["f" ] = "Ƒƒɟ",
["g" ] = "ĜĝĞğĠġĢģƓǤǥǦǧǴǵɠɡɢʛ",
["h" ] = "ĤĥĦħɥɦɧʜ",
["i" ] = "ÌÍÎÏìíîïĨĩĪīĬĭĮįİıƗǏǐȈȉȊȋɨɩɪ",
["ij" ] = "IJij",
["j" ] = "Ĵĵǰʄʝ",
["k" ] = "ĶķĸƘƙǨǩʞ",
["l" ] = "ĹĺĻļĽľĿŀŁłƚɫɬɭʟ",
["lj" ] = "LJLjlj",
["m" ] = "Ɯɯɰɱ",
["n" ] = "ÑñŃńŅņŇňʼnŊŋƝƞɲɳɴ",
["nj" ] = "NJNjnj",
["o" ] = "ÒÓÔÕÖØòóôõöøŌōŎŏŐőƆƟƠơǑǒǪǫǬǭǾǿȌȍȎȏɵ",
["oe" ] = "Œœɶ",
["oi" ] = "Ƣƣ",
["p" ] = "ÞþƤƥ",
["q" ] = "ʠ",
["r" ] = "ŔŕŖŗŘřƦȐȑȒȓɹɺɻɼɽɾɿʀʁ",
["s" ] = "ŚśŜŝŞşŠšſʂ",
["ss" ] = "ß",
["t" ] = "ŢţŤťŦŧƫƬƭƮʇʈ",
["u" ] = "ÙÚÛÜùúûüŨũŪūŬŭŮůŰűŲųƯưǓǔǕǖǗǘǙǚǛǜȔȕȖȗʉʊ",
["v" ] = "Ʋʋʌ",
["w" ] = "Ŵŵʍ",
["y" ] = "ÝýÿŶŷŸƳƴʎʏ",
["z" ] = "ŹźŻżŽžƵƶʐʑ",
};
local simplificationMapping = { }
for replacement, pattern in pairs(charSimplifications) do
local len = pattern:utf8len();
for i = 1, len do
local char = pattern:utf8sub(i, i);
simplificationMapping[char] = replacement;
end
end
function LootReserve:NormalizeName(name)
for i = 1, name:utf8len() do
if name:utf8sub(i, i) == "-" then
return name:utf8sub(1, 1):utf8upper() .. name:utf8sub(2, i - 1):utf8lower() .. name:utf8sub(i);
end
end
return name:utf8sub(1, 1):utf8upper() .. name:utf8sub(2):utf8lower();
end
local simplifiedNamesMemo = { };
local simplifiedNamesLowerMemo = { };
local function SimplifyName(self, name)
for i = 1, name:utf8len() do
if name:utf8sub(i, i) == "-" then
return self:NormalizeName(name:utf8sub(1, i - 1):utf8replace(simplificationMapping));
end
end
return self:NormalizeName(name:utf8replace(simplificationMapping));
end
function LootReserve:SimplifyName(name)
if not simplifiedNamesMemo[name] then
simplifiedNamesMemo[name] = SimplifyName(self, name);
end
return simplifiedNamesMemo[name];
end
function LootReserve:SimplifyNameLower(name)
if not simplifiedNamesLowerMemo[name] then
simplifiedNamesLowerMemo[name] = self:SimplifyName(name):lower();
end
return simplifiedNamesLowerMemo[name];
end
function LootReserve:GetNumGroupMembers(func)
local count = 0;
self:ForEachRaider(function() count = count + 1; end);
return count;
end
function LootReserve:IsTradeableItem(bag, slot)
-- can't use C_Item.IsBound because it sometimes bugs and gives a usage error despite correctly receiving an ItemLocation
return not LootReserve:IsItemSoulbound(bag, slot) or LootReserve:IsItemSoulboundTradeable(bag, slot);
end
local function CacheBagSlot(self, bag, slot, i)
local containerInfo = LootReserve:GetContainerItemInfo(bag, slot);
if containerInfo then
if i then
table.insert(self.BagCache, i, {bag = bag, slot = slot, item = self.ItemCache:Item(containerInfo.hyperlink), quantity = containerInfo.stackCount, locked = containerInfo.isLocked});
else
table.insert(self.BagCache, {bag = bag, slot = slot, item = self.ItemCache:Item(containerInfo.hyperlink), quantity = containerInfo.stackCount, locked = containerInfo.isLocked});
end
end
end
local bagCacheHooked = nil;
local function CheckBagCache(self)
if not bagCacheHooked then
bagCacheHooked = true;
LootReserve:RegisterEvent("BAG_UPDATE", function() -- Return to hooking BAG_UPDATED_DELAYED when blizzard fixes it
LootReserve:WipeBagCache();
C_Timer.After(0, function() LootReserve:WipeBagCache(); end);
end);
self:RegisterEvent("ITEM_LOCK_CHANGED", function(bag, slot)
if not slot then return; end
if self.BagCache then
for i, slotData in ipairs(self.BagCache) do
if slotData.slot == slot and slotData.bag == bag then
table.remove(self.BagCache, i);
CacheBagSlot(self, bag, slot, i);
end
end
end
end);
end
if not self.BagCache then
self.BagCache = { };
for bag = 0, NUM_BAG_SLOTS do
for slot = 1, LootReserve:GetContainerNumSlots(bag) do
CacheBagSlot(self, bag, slot);
end
end
end
end
local function match(item, itemOrID)
if type(itemOrID) == "number" then
return item:GetID() == itemOrID;
else
return item == itemOrID;
end
end
function LootReserve:WipeBagCache()
self.BagCache = nil;
end
function LootReserve:GetTradeableItemCount(itemOrID, includeLoot)
CheckBagCache(self);
local count, _ = 0;
if includeLoot then
_, count = self:IsLootingItem(itemOrID);
end
for _, slotData in ipairs(self.BagCache) do
if match(slotData.item, itemOrID) and self:IsTradeableItem(slotData.bag, slotData.slot) then
count = count + slotData.quantity;
end
end
for i = 0, 19 do
local link = GetInventoryItemLink("player", i);
if link and link:find("item:%d+") then
local item = self.ItemCache:Item(link);
if match(item, itemOrID) and not item:Binds() then
count = count + 1;
end
end
end
return count;
end
function LootReserve:GetBagSlot(itemOrID, permitLocked, skipCount)
CheckBagCache(self);
skipCount = skipCount or 0;
for _, slotData in ipairs(self.BagCache) do
if match(slotData.item, itemOrID) and (permitLocked or not slotData.locked) and self:IsTradeableItem(slotData.bag, slotData.slot) then
skipCount = skipCount - 1;
if skipCount < 0 then
return slotData.bag, slotData.slot;
end
end
end
return nil;
end
function LootReserve:IsItemSoulbound(bag, slot)
if not self.TooltipScanner then
self.TooltipScanner = CreateFrame("GameTooltip", "LootReserveTooltipScanner", nil, "GameTooltipTemplate");
self.TooltipScanner:Hide();
end
self.TooltipScanner:SetOwner(WorldFrame, "ANCHOR_NONE");
self.TooltipScanner:SetBagItem(bag, slot);
for i = LootReserve.TooltipScanner:NumLines(), 1, -1 do
local line = _G[self.TooltipScanner:GetName() .. "TextLeft" .. i];
if line and line:GetText() and line:GetText() == ITEM_SOULBOUND then
self.TooltipScanner:Hide();
return true;
end
end
self.TooltipScanner:Hide();
return false;
end
function LootReserve:IsItemSoulboundTradeable(bag, slot)
if not self.TooltipScanner then
self.TooltipScanner = CreateFrame("GameTooltip", "LootReserveTooltipScanner", nil, "GameTooltipTemplate");
self.TooltipScanner:Hide();
end
if not self.TooltipScanner.SoulboundTradeable then
self.TooltipScanner.SoulboundTradeable = BIND_TRADE_TIME_REMAINING:gsub("%.", "%%."):gsub("%%s", "(.+)");
end
self.TooltipScanner:SetOwner(WorldFrame, "ANCHOR_NONE");
self.TooltipScanner:SetBagItem(bag, slot);
for i = LootReserve.TooltipScanner:NumLines(), 1, -1 do
local line = _G[self.TooltipScanner:GetName() .. "TextLeft" .. i];
if line and line:GetText() and line:GetText():match(self.TooltipScanner.SoulboundTradeable) then
self.TooltipScanner:Hide();
return true;
end
end
self.TooltipScanner:Hide();
return false;
end
function LootReserve:IsItemBeingTraded(item)
for i = 1, 6 do
local link = GetTradePlayerItemLink(i);
if link then
local tradeItem = LootReserve.ItemCache:Item(link);
if tradeItem == item then
return true;
end
end
end
return false;
end
function LootReserve:PutItemInTrade(bag, slot)
for i = 1, 6 do
if not GetTradePlayerItemInfo(i) then
self:PickupContainerItem(bag, slot);
ClickTradeButton(i);
return true;
end
end
return false;
end
function LootReserve:GetItemDescription(itemID, noTokenRedirect)
local item = LootReserve.ItemCache:Item(itemID);
if not item:Cache():IsCached() then return; end
local name, _, _, _, _, itemType, itemSubType, _, equipLoc, _, _, _, _, bindType = item:GetInfo();
local skillRequired, skillLevelRequired = item:GetSkillRequired();
local itemText = "";
if not noTokenRedirect then
local tokenID = LootReserve.Data:GetToken(itemID);
if tokenID then
local token = LootReserve.ItemCache:Item(tokenID);
if not token:Cache():IsCached() then return; end
return "From: " .. token:GetName();
end
end
if item:IsUnique() then
itemText = ITEM_UNIQUE .. " " .. itemText
end
if skillRequired then
itemText = itemText .. skillLevelRequired .. " " .. skillRequired .. " "
end
if LootReserve.Constants.RedundantSubTypes[itemSubType] then
itemText = LootReserve.Constants.RedundantSubTypes[itemSubType];
elseif itemType == ARMOR then
if itemSubType == MISCELLANEOUS or equipLoc == "INVTYPE_CLOAK" then
itemText = itemText .. (_G[equipLoc] or "");
else
itemText = itemText .. itemSubType .. " " .. (_G[equipLoc] or "");
end
elseif itemType == WEAPON then
itemText = itemText .. (_G[equipLoc] and (_G[equipLoc] .. " ") or "") .. (LootReserve.Constants.WeaponTypeNames[itemSubType] or "");
elseif itemType == "Recipe" then
if itemSubType == "Book" then
itemText = itemText .. "Skill Book";
else
itemText = itemText .. name:match("^[^:]+");
end
elseif itemType == "Container" then
itemText = itemText .. (_G[equipLoc] or "");
elseif itemType == "Trade Goods" then
itemText = itemText .. "Trade Good";
elseif itemType == MISCELLANEOUS then
if item:StartsQuest() then
itemText = itemText .. "Quest";
else
if itemSubType == "Junk" or itemSubType == "Other" then
itemText = itemText .. itemType;
else
itemText = itemText .. itemSubType;
end