-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathKeystoneUtil.lua
More file actions
366 lines (321 loc) · 13.2 KB
/
KeystoneUtil.lua
File metadata and controls
366 lines (321 loc) · 13.2 KB
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
--- @class MPT_NS
local MPT = select(2, ...);
--- @class KeystoneSharingUtil
local Util = {};
MPT.KeystoneSharingUtil = Util;
local BNSendGameData = C_BattleNet and C_BattleNet.SendGameData or BNSendGameData
local LEMIX_SEASON_ID = 2;
local CTL = ChatThrottleLib;
Util.BNET_WHISPER_CHANNEL = 'BNET_WHISPER';
Util.CHARACTER_LIMIT = 255;
Util.CHARACTER_LIMIT_BNET = 4000; -- the real limit is documented to be 4078, but we'll just round it down a little
Util.lockedAt = nil;
Util.lockedKeystoneMapID = nil;
Util.lockedKeystoneLevel = nil;
--- @type table<number, table<number, string>> [mapID] = { [keystoneLevel] = keystoneLink }
Util.keystoneLinkCache = {};
--- @return number? keystoneChallengeMapID
--- @return number? keystoneLevel
function Util:GetOwnedKeystone()
if self.lockedAt then
local timeSinceLocked = GetTime() - self.lockedAt;
if timeSinceLocked < 3 then -- it can take a few seconds for the C_MythicPlus API to update after a keystone is changed
return self.lockedKeystoneMapID, self.lockedKeystoneLevel;
else
self.lockedAt = nil;
self.lockedKeystoneMapID = nil;
self.lockedKeystoneLevel = nil;
end
end
local keystoneMapID = C_MythicPlus.GetOwnedKeystoneChallengeMapID();
local keystoneLevel = C_MythicPlus.GetOwnedKeystoneLevel();
if keystoneMapID and keystoneLevel then
return keystoneMapID, keystoneLevel;
end
end
function Util:GetKeystoneLink()
if PlayerGetTimerunningSeasonID() ~= LEMIX_SEASON_ID then
local mapID, level = self:GetOwnedKeystone();
if not mapID or not level then
return nil;
end
if self.keystoneLinkCache[mapID] and self.keystoneLinkCache[mapID][level] then
return self.keystoneLinkCache[mapID][level];
end
end
local keyLink;
for bag = BACKPACK_CONTAINER, NUM_BAG_SLOTS do
for slot = 1, C_Container.GetContainerNumSlots(bag) do
local info = C_Container.GetContainerItemInfo(bag, slot);
local itemID = info and info.itemID;
local hyperlink = info and info.hyperlink;
if itemID and hyperlink and C_Item.IsItemKeystoneByID(itemID) then
keyLink = hyperlink;
local keyMapID, keyLevel = self:GetMapIDAndLevelFromKeystoneLink(hyperlink);
if keyMapID and keyLevel then
self.keystoneLinkCache[keyMapID] = self.keystoneLinkCache[keyMapID] or {};
self.keystoneLinkCache[keyMapID][keyLevel] = hyperlink;
end
break;
end
end
end
return keyLink;
end
---@param link string either an item link or a keystone link
---@return number|nil mapID
---@return number|nil level
function Util:GetMapIDAndLevelFromKeystoneLink(link)
local linkType, data = LinkUtil.ExtractLink(link);
local mapID, level;
local parts = strsplittable(':', data);
if linkType == 'item' then
local numBonusIDs = tonumber(parts[13]) or 0;
local offset = 14 + numBonusIDs;
local numModifiers = tonumber(parts[offset]) or 0;
for i = (offset + 1), (offset + numModifiers * 2), 2 do
local modifierID = tonumber(parts[i]) or 0;
local modifierValue = tonumber(parts[i + 1]) or 0;
if modifierID == Enum.ItemModification.KeystonePowerLevel then
level = modifierValue;
elseif modifierID == Enum.ItemModification.KeystoneMapChallengeModeID then
mapID = modifierValue;
end
end
elseif linkType == 'keystone' then
mapID = tonumber(parts[2]) or 0;
level = tonumber(parts[3]) or 0;
end
return mapID, level;
end
-- mapID, level, affix1, affix2, affix3, affix4
local KEYSTONE_LINK_FORMAT = '|cnIQ4:|Hkeystone:180653:%d:%d:%d:%d:%d:%d:0|h[' .. CHALLENGE_MODE_KEYSTONE_HYPERLINK .. ']|h|r';
--- @param itemLink string keystone item link
--- @return string|nil keystoneLink
function Util:ConvertKeystoneItemLinkToKeystoneLink(itemLink)
local _, data = LinkUtil.ExtractLink(itemLink);
local parts = strsplittable(':', data);
local numBonusIDs = tonumber(parts[13]) or 0;
local offset = 14 + numBonusIDs;
local numModifiers = tonumber(parts[offset]) or 0;
local mapID, level;
local affix1, affix2, affix3, affix4 = 0, 0, 0, 0;
for i = (offset + 1), (offset + numModifiers * 2), 2 do
local modifierID = tonumber(parts[i]) or 0;
local modifierValue = tonumber(parts[i + 1]) or 0;
if modifierID == Enum.ItemModification.KeystonePowerLevel then
level = modifierValue;
elseif modifierID == Enum.ItemModification.KeystoneMapChallengeModeID then
mapID = modifierValue;
elseif modifierID == Enum.ItemModification.KeystoneAffix0 then
affix1 = modifierValue;
elseif modifierID == Enum.ItemModification.KeystoneAffix01 then
affix2 = modifierValue;
elseif modifierID == Enum.ItemModification.KeystoneAffix02 then
affix3 = modifierValue;
elseif modifierID == Enum.ItemModification.KeystoneAffix03 then
affix4 = modifierValue;
end
end
if level and mapID then
local mapName = C_ChallengeMode.GetMapUIInfo(mapID);
return KEYSTONE_LINK_FORMAT:format(mapID, level, affix1, affix2, affix3, affix4, mapName, level)
end
end
function Util:GetWeeklyBest()
local weeklyBest = 0;
local runHistory = C_MythicPlus.GetRunHistory(false, true);
for _, entry in ipairs(runHistory) do
if entry.thisWeek and entry.level > weeklyBest then
weeklyBest = entry.level;
end
end
return weeklyBest;
end
--- @return FriendInfo[]
function Util:GetFriendList()
local friendList = {};
local numFriends = C_FriendList.GetNumFriends();
for i = 1, numFriends do
local friendInfo = C_FriendList.GetFriendInfoByIndex(i);
if friendInfo.connected then
tinsert(friendList, friendInfo);
end
end
return friendList;
end
function Util:GetBNetFriendList()
local friendList = {};
local numFriends = BNGetNumFriends();
for i = 1, numFriends do
for gameIndex = 1, C_BattleNet.GetFriendNumGameAccounts(i) do
local gameAccountInfo = C_BattleNet.GetFriendGameAccountInfo(i, gameIndex);
if gameAccountInfo and gameAccountInfo.clientProgram == BNET_CLIENT_WOW and gameAccountInfo.wowProjectID == WOW_PROJECT_MAINLINE then
tinsert(friendList, {
gameAccountID = gameAccountInfo.gameAccountID,
});
end
end
end
return friendList
end
local bnetMessageQueue = {};
--- @param prefix string
--- @param message string
--- @param channel string
--- @param target nil|string|number
function Util:SendMessage(prefix, message, channel, target)
if
(channel == "PARTY" and not IsInGroup(LE_PARTY_CATEGORY_HOME))
or (channel == "RAID" and not IsInRaid(LE_PARTY_CATEGORY_HOME))
or (channel == "INSTANCE" and not IsInGroup(LE_PARTY_CATEGORY_INSTANCE) and not IsInRaid(LE_PARTY_CATEGORY_INSTANCE))
or (channel == "GUILD" and not IsInGuild())
then
return;
end
if channel == self.BNET_WHISPER_CHANNEL then
tinsert(bnetMessageQueue, {
prefix = prefix,
message = message,
target = target,
});
return;
end
CTL:SendAddonMessage('NORMAL', prefix, message, channel, target);
end
do
local messageFrame = CreateFrame('Frame');
messageFrame:SetScript('OnUpdate', function(self, elapsed)
if #bnetMessageQueue == 0 then return; end
local throttle = 1; -- 1 message per second
self.throttle = (self.throttle or 0) + elapsed;
if self.throttle > throttle then
self.throttle = 0;
local message = tremove(bnetMessageQueue, 1);
if message then
BNSendGameData(message.target, message.prefix, message.message);
end
end
end);
end
local addonMessageReceivedRegistry = {};
function Util:RegisterAddonMessageReceivedCallback(prefix, owner, callback)
addonMessageReceivedRegistry[prefix] = addonMessageReceivedRegistry[prefix] or {};
addonMessageReceivedRegistry[prefix][owner] = callback;
end
function Util:UnregisterAddonMessageReceivedCallback(prefix, owner)
if addonMessageReceivedRegistry[prefix] then
addonMessageReceivedRegistry[prefix][owner] = nil;
end
end
--- @param prefix string
function Util:RegisterAddonMessagePrefix(prefix)
C_ChatInfo.RegisterAddonMessagePrefix(prefix);
end
local keystoneUpdate = {registry = {}, registrarCount = 0};
local startListeningForKeystoneUpdates, stopListeningForKeystoneUpdates;
function Util:RegisterKeystoneUpdateCallback(owner, callback)
if 0 == keystoneUpdate.registrarCount then
startListeningForKeystoneUpdates();
end
if not keystoneUpdate.registry[owner] then
keystoneUpdate.registrarCount = keystoneUpdate.registrarCount + 1;
end
keystoneUpdate.registry[owner] = callback;
end
function Util:UnregisterKeystoneUpdateCallback(owner)
if keystoneUpdate.registry[owner] then
keystoneUpdate.registry[owner] = nil;
keystoneUpdate.registrarCount = keystoneUpdate.registrarCount - 1;
end
if 0 == keystoneUpdate.registrarCount then
stopListeningForKeystoneUpdates();
end
end
local initializeTime = {}
initializeTime[GetCurrentRegion()] = 1500390000 -- US Tuesday at reset (default)
initializeTime[3] = 1500447600 -- EU Wednesday at reset
initializeTime[4] = 1500505200 -- TW Thursday at reset
function Util:GetWeek()
return math.floor((GetServerTime() - initializeTime[GetCurrentRegion()]) / 604800);
end
function Util:GetWeekTimestamp()
return GetServerTime() - initializeTime[GetCurrentRegion()] - (604800 * Util:GetWeek());
end
do
--- @class KeystoneSharingUtilEventFrame: Frame
local frame = CreateFrame('Frame');
frame.lastUpdate = 0;
frame.lastKnownKeystoneMapID = 0;
frame.lastKnownKeystoneLevel = 0;
frame:SetScript('OnEvent', function(self, event, ...) self[event](self, ...); end);
frame:RegisterEvent('CHAT_MSG_ADDON');
frame:RegisterEvent('BN_CHAT_MSG_ADDON');
function frame:CHAT_MSG_ADDON(prefix, message, channel, sender)
if addonMessageReceivedRegistry[prefix] then
for owner, callback in pairs(addonMessageReceivedRegistry[prefix]) do
securecallfunction(callback, owner, prefix, message, channel, sender);
end
end
end
function frame:BN_CHAT_MSG_ADDON(prefix, message, _, sender)
if addonMessageReceivedRegistry[prefix] then
for owner, callback in pairs(addonMessageReceivedRegistry[prefix]) do
securecallfunction(callback, owner, prefix, message, Util.BNET_WHISPER_CHANNEL, sender);
end
end
end
function frame:BAG_UPDATE_DELAYED()
self:Show();
end
function frame:ITEM_CHANGED(fromItem, toItem)
local itemID = C_Item.GetItemIDForItemInfo(toItem);
if C_Item.IsItemKeystoneByID(itemID) then
local mapID, level = Util:GetMapIDAndLevelFromKeystoneLink(toItem);
if mapID and level and (mapID ~= self.lastKnownKeystoneMapID or level ~= self.lastKnownKeystoneLevel) then
self:Hide()
self.lastUpdate = GetTime();
self.lastKnownKeystoneMapID = mapID;
self.lastKnownKeystoneLevel = level;
Util.lockedKeystoneMapID = mapID;
Util.lockedKeystoneLevel = level;
Util.lockedAt = GetTime();
Util.keystoneLinkCache[mapID] = Util.keystoneLinkCache[mapID] or {};
Util.keystoneLinkCache[mapID][level] = Util.keystoneLinkCache[mapID][level] or Util:ConvertKeystoneItemLinkToKeystoneLink(toItem);
RunNextFrame(function()
for owner, callback in pairs(keystoneUpdate.registry) do
securecallfunction(callback, owner, mapID, level);
end
end);
end
end
end
function frame:OnUpdate()
if (GetTime() - self.lastUpdate) < 2 then return; end
self:CheckKeystones();
end
function frame:CheckKeystones()
self:Hide();
self.lastUpdate = GetTime();
local keystoneMapID, keystoneLevel = Util:GetOwnedKeystone();
if keystoneMapID ~= self.lastKnownKeystoneMapID or keystoneLevel ~= self.lastKnownKeystoneLevel then
self.lastKnownKeystoneMapID = keystoneMapID;
self.lastKnownKeystoneLevel = keystoneLevel;
RunNextFrame(function()
for owner, callback in pairs(keystoneUpdate.registry) do
securecallfunction(callback, owner, keystoneMapID, keystoneLevel);
end
end);
end
end
function startListeningForKeystoneUpdates()
frame:RegisterEvent('BAG_UPDATE_DELAYED');
frame:RegisterEvent('ITEM_CHANGED');
frame:SetScript('OnUpdate', frame.OnUpdate);
end
function stopListeningForKeystoneUpdates()
frame:UnregisterEvent('BAG_UPDATE_DELAYED');
frame:UnregisterEvent('ITEM_CHANGED');
frame:SetScript('OnUpdate', nil);
end
end