forked from Anonomit/LootReserve
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClient.lua
352 lines (311 loc) · 13 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
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,
RollRequestAutoRollNotified = false,
RollRequestWinnerReaction = true,
RollRequestLoserReaction = true,
CollapsedExpansions = { },
CollapsedCategories = { },
SwapLDBButtons = false,
LibDBIcon = { },
AllowPreCache = false,
ShowReopenHint = true,
},
CharacterFavorites = { },
GlobalFavorites = { },
PendingItems = { },
PendingOpt = nil,
PendingOpen = false,
ServerSearchTimeoutTime = nil,
DurationUpdateRegistered = false,
SessionEventsRegistered = false,
CategoryFlashing = false,
PendingLootListUpdate = nil,
SelectedCategory = nil,
};
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
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
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"));
end,
}), self.Settings.LibDBIcon);
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 == LE_ITEM_BIND_ON_ACQUIRE and self.CharacterFavorites or self.GlobalFavorites;
favorites[itemID] = enabled and true or nil;
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;
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.SessionServer and self.PendingOpen then
self.Window:Show();
end
self.PendingOpen = false;
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, 50 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();
local tokenID = LootReserve.Data:GetToken(itemID);
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 ", 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;
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(LootReserve.Data:GetToken(itemID) or itemID) > 0;
end
function LootReserve.Client:IsItemReservedByMe(itemID, bypassMasquerade)
for _, player in ipairs(self:GetItemReservers(LootReserve.Data:GetToken(itemID) or 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[LootReserve.Data:GetToken(itemID) or 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;
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 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 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