-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathClient.lua
536 lines (472 loc) · 20.1 KB
/
Client.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
LootReserve = LootReserve or { };
LootReserve.Client =
{
-- Server Connection
SessionServer = nil,
Masquerade = nil,
-- Server Session Info
StartTime = 0,
AcceptingReserves = false,
RemainingReserves = 0,
MaxReserves = 0,
Locked = false,
OptedOut = false,
LootCategories = nil,
Duration = nil,
MaxDuration = nil,
ItemReserves = { }, -- { [ItemID] = { "Playername", "Playername", ... }, ... }
ItemConditions = { },
RollRequest = nil,
Equip = true,
Blind = false,
Multireserve = 1,
Settings =
{
RollRequestShow = true,
RollRequestShowUnusable = false,
RollRequestShowUnusableBoE = false,
RollRequestGlowOnlyReserved = true,
RollRequestAutoRollReserved = true,
RollRequestAutoCloseTiered = false,
RollRequestAutoRollNotified = false,
RollRequestWinnerReaction = true,
RollRequestLoserReaction = true,
CollapsedExpansions = { },
CollapsedCategories = { },
SwapLDBButtons = false,
LibDBIcon = { },
AllowPreCache = false,
ShowReopenHint = true,
},
CharacterFavorites = { },
GlobalFavorites = { },
ReservableIDs = { },
PendingItems = { },
PendingTimers = { },
PendingOpt = nil,
PendingOpen = false,
SkipOpen = false,
ServerSearchTimeoutTime = nil,
DurationUpdateRegistered = false,
SessionEventsRegistered = false,
CategoryFlashing = false,
PendingLootListUpdate = nil,
SelectedCategory = nil,
RollOnLoot = nil,
};
StaticPopupDialogs["LOOTRESERVE_CONFIRM_NEED_ROLL_RESERVED_ITEM"] =
{
text = "Are you sure you want to roll Need?|n|n%s has been reserved by:|n%s.",
button1 = YES,
button2 = NO,
timeout = 0,
whileDead = 1,
hideOnEscape = 1,
OnAccept = function(self, data)
LootReserve.Client.RollOnLoot(data.rollID, 1);
end,
};
StaticPopupDialogs["LOOTRESERVE_PROMPT_REMOVE_FAVORITE"] =
{
text = "|cff00ff00Congratulations!|r|n|nWould you like to remove %s from your favorites?",
button1 = YES,
button2 = NO,
timeout = 0,
whileDead = 1,
hideOnEscape = 1,
OnAccept = function(self, data)
LootReserve.Client:SetFavorite(data.item:GetID(), false);
end,
};
function LootReserve.Client:Init()
local function LootReserveRollOnLoot(rollID, rollType, ...)
if not self.SessionServer then return; end
if rollType ~= 1 then return; end
local link = GetLootRollItemLink(rollID);
if not link then return; end
local item = LootReserve.ItemCache:Item(link);
local token;
if not self:IsItemReserved(itemID) then
token = LootReserve.Data:GetToken(item:GetID());
if token then
token = LootReserve.ItemCache:Item(token);
end
end
local itemID = token and token:GetID() or item:GetID();
if self:IsItemReserved(itemID) and not self:IsItemReservedByMe(itemID, true) then
local reservesText = LootReserve:FormatReservesTextColored(LootReserve.Client:GetItemReservers(itemID));
StaticPopup_Show("LOOTRESERVE_CONFIRM_NEED_ROLL_RESERVED_ITEM", item:GetLink(), reservesText, {rollID = rollID});
return true;
end
end
self.RollOnLoot = _G.RollOnLoot
_G.RollOnLoot = function(...)
local success, result = pcall(function(...) LootReserveRollOnLoot(...) end, ...);
if not success or not result then
self.RollOnLoot(...)
end
end
end
LootReserve.Client:Init();
function LootReserve.Client:Load()
LootReserveCharacterSave.Client = LootReserveCharacterSave.Client or { };
LootReserveGlobalSave.Client = LootReserveGlobalSave.Client or { };
-- Copy data from saved variables into runtime tables
-- Don't outright replace tables, as new versions of the addon could've added more fields that would be missing in the saved data
local function loadInto(to, from, field)
if from and to and field then
if from[field] then
for k, v in pairs(from[field]) do
to[field] = to[field] or { };
to[field][k] = v;
empty = false;
end
end
from[field] = to[field];
end
end
local versionSave = LootReserveGlobalSave.Client.Version and LootReserveGlobalSave.Client.Version or "0"
loadInto(self, LootReserveGlobalSave.Client, "Settings");
loadInto(self, LootReserveCharacterSave.Client, "CharacterFavorites");
loadInto(self, LootReserveGlobalSave.Client, "GlobalFavorites");
LibStub("LibDBIcon-1.0").RegisterCallback("LootReserve", "LibDBIcon_IconCreated", function(event, button, name)
if name == "LootReserve" then
button.icon:SetTexture("Interface\\AddOns\\LootReserve\\Assets\\Textures\\Icon");
end
end);
LibStub("LibDBIcon-1.0"):Register("LootReserve", LibStub("LibDataBroker-1.1"):NewDataObject("LootReserve", {
type = "launcher",
text = "LootReserve",
icon = "Interface\\Buttons\\UI-GroupLoot-Dice-Up",
OnClick = function(ldb, button)
if button == "LeftButton" or button == "RightButton" then
local window = ((button == "LeftButton") == self.Settings.SwapLDBButtons) and LootReserve.Server.Window or LootReserve.Client.Window;
if InCombatLockdown() and window:IsProtected() and window == LootReserve.Server.Window then
LootReserve:ToggleServerWindow(not window:IsShown());
else
window:SetShown(not window:IsShown());
end
elseif button == "MiddleButton" then
LootReserve.Client.Settings.LibDBIcon.hide = true;
LibStub("LibDBIcon-1.0"):Hide("LootReserve");
CloseMenus(); -- close options menu if it's open
end
end,
OnTooltipShow = function(tooltip)
tooltip:SetText("LootReserve", HIGHLIGHT_FONT_COLOR.r, HIGHLIGHT_FONT_COLOR.g, HIGHLIGHT_FONT_COLOR.b, 1);
tooltip:AddLine(format("Left-Click: Open %s Window", self.Settings.SwapLDBButtons and "Host" or "Reserves"));
tooltip:AddLine(format("Right-Click: Open %s Window", self.Settings.SwapLDBButtons and "Reserves" or "Host"));
tooltip:AddLine("Middle-Click: Hide Minimap Button");
tooltip:AddLine("(Toggle icon visibility in Reserves window)");
end,
}), self.Settings.LibDBIcon);
-- 2023-06-20: Reset auto roll notification
if versionSave < "2023-06-20" then
self.Settings.RollRequestAutoRollNotified = false;
end
LootReserveGlobalSave.Client.Version = LootReserve.Version;
end
function LootReserve.Client:IsFavorite(itemID)
return self.CharacterFavorites[itemID] or self.GlobalFavorites[itemID];
end
function LootReserve.Client:SetFavorite(itemID, enabled)
if self:IsFavorite(itemID) == (enabled and true or false) then return; end
local item = LootReserve.ItemCache:Item(itemID);
if not item or not item:GetInfo() then return; end
local bindType = item:GetBindType();
local favorites = bindType == Enum.ItemBind.OnAcquire and self.CharacterFavorites or self.GlobalFavorites;
favorites[itemID] = enabled and true or nil;
LootReserve.Client:UpdateLootList();
self:FlashCategory("Favorites");
end
function LootReserve.Client:SearchForServer(startup)
if not startup and self.ServerSearchTimeoutTime and time() < self.ServerSearchTimeoutTime then return; end
self.ServerSearchTimeoutTime = time() + 10;
LootReserve.Comm:BroadcastHello();
end
function LootReserve.Client:SetMasquerade(player)
local oldMasquerade = self.Masquerade;
if self.SessionServer and LootReserve:IsMe(self.SessionServer) and LootReserve.Server and LootReserve.Server.CurrentSession then
if not player or LootReserve:IsMe(player) then
self.Masquerade = nil;
else
self.Masquerade = player;
end
if oldMasquerade ~= self.Masquerade then
LootReserve.Comm:SendSessionInfo(LootReserve:Me());
end
end
end
function LootReserve.Client:StartSession(server, starting, startTime, acceptingReserves, lootCategories, duration, maxDuration, equip, blind, multireserve)
self:ResetSession(true);
self.SessionServer = server;
self.StartTime = startTime;
self.AcceptingReserves = acceptingReserves;
self.LootCategories = lootCategories;
self.Duration = duration;
self.MaxDuration = maxDuration;
self.Equip = equip;
self.Blind = blind;
self.Multireserve = multireserve;
-- Cache the list of items players can reserve
table.wipe(self.ReservableIDs);
for itemID, conditions in pairs(self.ItemConditions) do
if itemID ~= 0 and conditions.Custom then
if LootReserve.ItemConditions:TestServer(itemID) then
self.ReservableIDs[itemID] = true;
end
end
end
local rewardIDs = { };
local hiddenIDs = { };
for id, category in LootReserve:OrderedMemo(LootReserve.Data.Categories, LootReserve.Data.CategorySorter) do
if category.Children and (not self.LootCategories or LootReserve:Contains(self.LootCategories, id)) and LootReserve.Data:IsCategoryVisible(category) then
for _, child in ipairs(category.Children) do
if child.Loot then
for _, item in ipairs(child.Loot) do
if item ~= 0 then
item = LootReserve.ItemCache(item);
local itemID = item:GetID();
if LootReserve.ItemConditions:TestServer(itemID) then
if LootReserve.Data:IsTokenReward(itemID) then
table.insert(rewardIDs, itemID);
else
self.ReservableIDs[itemID] = true;
end
else
hiddenIDs[itemID] = true;
end
end
end
end
end
end
end
for _, rewardID in ipairs(rewardIDs) do
local tokenID = LootReserve.Data:GetToken(rewardID);
if not self.ReservableIDs[tokenID] and not hiddenIDs[itemID] then
self.ReservableIDs[rewardID] = true;
end
end
if self.MaxDuration ~= 0 and not self.DurationUpdateRegistered then
self.DurationUpdateRegistered = true;
LootReserve:RegisterUpdate(function(elapsed)
if self.SessionServer and self.AcceptingReserves and self.Duration ~= 0 then
if self.Duration > elapsed then
self.Duration = self.Duration - elapsed;
else
self.Duration = 0;
self:StopSession();
end
end
end);
end
if not self.SessionEventsRegistered then
self.SessionEventsRegistered = true;
LootReserve:RegisterEvent("GROUP_LEFT", function()
if self.SessionServer and not LootReserve:IsMe(self.SessionServer) then
self:StopSession();
self:ResetSession();
self:UpdateCategories();
self:UpdateLootList();
self:UpdateReserveStatus();
end
end);
LootReserve:RegisterEvent("GROUP_ROSTER_UPDATE", function()
if self.SessionServer and not LootReserve:UnitInGroup(self.SessionServer) then
self:StopSession();
self:ResetSession();
self:UpdateCategories();
self:UpdateLootList();
self:UpdateReserveStatus();
end
end);
LootReserve:RegisterEvent("PLAYER_REGEN_ENABLED", function()
if self.PendingOpen and self.SessionServer and self.AcceptingReserves and not self.Locked and self.RemainingReserves > 0 and not self.OptedOut then
self.Window:Show();
end
self.PendingOpen = false;
end);
-- Auto need on reserved items during group loot (when multireserves disabled)
LootReserve:RegisterEvent("START_LOOT_ROLL", function(rollID)
if not self.SessionServer or self.Multireserve > 1 or not self.Settings.RollRequestAutoRollReserved then return; end
local link = GetLootRollItemLink(rollID);
if not link then return; end
local item = LootReserve.ItemCache:Item(link);
local token;
if not self:IsItemReserved(itemID) then
token = LootReserve.Data:GetToken(item:GetID());
if token then
token = LootReserve.ItemCache:Item(token);
end
end
local itemID = token and token:GetID() or item:GetID();
if self:IsItemReservedByMe(itemID, true) then
LootReserve:PrintMessage("Automatically rolling Need on reserved item: %s", item:GetLink());
if not self.Settings.RollRequestAutoRollNotified then
LootReserve:PrintError("Automatic rolling on reserved items can be disabled in Reserve window settings.");
self.Settings.RollRequestAutoRollNotified = true;
end
self.RollOnLoot(rollID, 1);
ConfirmLootRoll(rollID, 1);
StaticPopup_Hide("CONFIRM_LOOT_ROLL");
end
end);
local function OnTooltipSetHyperlink(tooltip)
if self.SessionServer and not LootReserve:IsMe(self.SessionServer) then
local name, link = tooltip:GetItem();
if not link then return; end
-- Check if it's already been added
local frame, text;
for i = 1, tooltip:NumLines() do
frame = _G[tooltip:GetName() .. "TextLeft" .. i];
if frame then
text = frame:GetText();
end
if text and string.find(text, " Reserved by ", 1, true) then return; end
end
local itemID = LootReserve.ItemCache:Item(link):GetID();
itemID = LootReserve.Data:GetIntendedItem(itemID);
local tokenID = LootReserve.Data:GetToken(itemID);
if LootReserve.Client.SessionServer and not LootReserve.Client.ReservableIDs[tokenID] then
tokenID = nil;
end
if #self:GetItemReservers(tokenID or itemID) > 0 then
local reservesText = LootReserve:FormatReservesTextColored(self:GetItemReservers(tokenID or itemID));
tooltip:AddLine("|TInterface\\BUTTONS\\UI-GroupLoot-Dice-Up:32:32:0:-4|t Reserved by " .. reservesText, 1, 1, 1);
end
end
end
GameTooltip : HookScript("OnTooltipSetItem", OnTooltipSetHyperlink);
ItemRefTooltip : HookScript("OnTooltipSetItem", OnTooltipSetHyperlink);
ItemRefShoppingTooltip1 : HookScript("OnTooltipSetItem", OnTooltipSetHyperlink);
ItemRefShoppingTooltip2 : HookScript("OnTooltipSetItem", OnTooltipSetHyperlink);
ShoppingTooltip1 : HookScript("OnTooltipSetItem", OnTooltipSetHyperlink);
ShoppingTooltip2 : HookScript("OnTooltipSetItem", OnTooltipSetHyperlink);
end
if starting then
self.Masquerade = nil;
local lootCategoriesText = LootReserve:GetCategoriesText(self.LootCategories);
LootReserve:PrintMessage("Session started%s%s.", lootCategoriesText ~= "" and " for " or "", lootCategoriesText);
if self.AcceptingReserves then
PlaySound(SOUNDKIT.GS_CHARACTER_SELECTION_ENTER_WORLD);
end
end
end
function LootReserve.Client:StopSession()
self.AcceptingReserves = false;
end
function LootReserve.Client:ResetSession(refresh)
self.SessionServer = nil;
self.RemainingReserves = 0;
self.MaxReserves = 0;
self.LootCategories = nil;
self.ItemReserves = { };
self.ItemConditions = { };
self.Equip = true;
self.Blind = false;
self.Multireserve = 1;
self.PendingItems = { };
self.PendingOpts = nil;
for _, timer in pairs(self.PendingTimers) do
timer:Cancel();
end
wipe(self.PendingTimers);
if not refresh then
self:StopCategoryFlashing();
end
end
function LootReserve.Client:GetRemainingReserves()
return self.SessionServer and self.AcceptingReserves and self.RemainingReserves or 0;
end
function LootReserve.Client:HasRemainingReserves()
return self:GetRemainingReserves() > 0;
end
function LootReserve.Client:GetMaxReserves()
return self.SessionServer and self.MaxReserves or 0;
end
function LootReserve.Client:IsItemReserved(itemID)
return #self:GetItemReservers(itemID) > 0;
end
function LootReserve.Client:IsItemReservedByMe(itemID, bypassMasquerade)
for _, player in ipairs(self:GetItemReservers(itemID)) do
if LootReserve:IsSamePlayer(not bypassMasquerade and LootReserve.Client.Masquerade or LootReserve:Me(), player) then
return true;
end
end
return false;
end
function LootReserve.Client:GetItemReservers(itemID)
if not self.SessionServer then return { }; end
return self.ItemReserves[itemID] or { };
end
function LootReserve.Client:IsItemPending(itemID)
return self.PendingItems[itemID];
end
function LootReserve.Client:SetItemPending(itemID, pending)
self.PendingItems[itemID] = pending or nil;
if self.PendingTimers[itemID] then
self.PendingTimers[itemID]:Cancel();
self.PendingTimers[itemID] = nil;
end
if pending then
self.PendingTimers[itemID] = C_Timer.NewTimer(30, function()
LootReserve.Client:SetItemPending(itemID, false);
LootReserve.Client:UpdateReserveStatus();
end);
end
end
function LootReserve.Client:Reserve(itemID)
if not self.SessionServer then return; end
if not self.AcceptingReserves then return; end
local tokenID = LootReserve.Data:GetToken(itemID);
if not self.ReservableIDs[tokenID] then
tokenID = nil;
end
if tokenID then
LootReserve.Client:SetItemPending(tokenID, true);
end
LootReserve.Client:SetItemPending(itemID, true);
LootReserve.Client:UpdateReserveStatus();
LootReserve.Comm:SendReserveItem(tokenID or itemID);
end
function LootReserve.Client:CancelReserve(itemID)
if not self.SessionServer then return; end
if not self.AcceptingReserves then return; end
local tokenID = LootReserve.Data:GetToken(itemID);
if not self.ReservableIDs[tokenID] then
tokenID = nil;
end
if tokenID then
LootReserve.Client:SetItemPending(tokenID, true);
end
LootReserve.Client:SetItemPending(itemID, true);
LootReserve.Client:UpdateReserveStatus();
LootReserve.Comm:SendCancelReserve(tokenID or itemID);
end
function LootReserve.Client:IsOptPending()
return self.PendingOpt;
end
function LootReserve.Client:SetOptPending(pending)
self.PendingOpt = pending or nil;
end
function LootReserve.Client:IsOptedOut()
return self.OptedOut or false;
end
function LootReserve.Client:IsOptedIn()
return not self:IsOptedOut();
end
function LootReserve.Client:OptOut()
if not self.SessionServer then return; end
if not self.AcceptingReserves then return; end
self:SetOptPending(true);
LootReserve.Client:UpdateReserveStatus();
LootReserve.Comm:SendOptOut();
end
function LootReserve.Client:OptIn()
if not self.SessionServer then return; end
if not self.AcceptingReserves then return; end
self:SetOptPending(true);
LootReserve.Client:UpdateReserveStatus();
LootReserve.Comm:SendOptIn();
end