-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCore.lua
More file actions
305 lines (248 loc) · 8.37 KB
/
Core.lua
File metadata and controls
305 lines (248 loc) · 8.37 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
---@type table
local EventFrame = CreateFrame("FRAME");
---@type boolean
local addonLoaded, AtlasLootLoaded = false, false;
---@type table
local CharWishList;
---@type number
local ItemID, ItemName, ItemBoss = 2, 4, 5
---@type string
local itemLinkPatern = "|?c?f?f?(%x*)|?H?([^:]*):?(%d+):?(%d*):?(%d*):?(%d*):?(%d*):?(%d*):?(%-?%d*):?(%-?%d*):?(%d*):?(%d*):?(%-?%d*)|?h?%[?([^%[%]]*)%]?|?h?|?r?";
---@type string
local versionPattern = "(%d+)\.(%d+)\.(%d+)"
---@type boolean
local playerTradeDone = false;
---@type table
local timerFrame = CreateFrame("Frame")
---@type number
local waitTimeAfterTrade = 0.5; --after trading, items in bag are not updated instant.
---@type number
local _, _, _, StriLiEnabled = GetAddOnInfo("StriLi")
---@return void
local function checkVersion()
local versionString = GetAddOnMetadata("StriLiAtlasLootAddIn", "Version");
local _, _, major, minor, patch = string.find(versionString, versionPattern);
major, minor, patch = tonumber(major), tonumber(minor), tonumber(patch);
if StriLiAtlasLootVersion == nil then
StriLiAtlasLootVersion = {major=major, minor=minor, patch=patch, vString=versionString};
end
local newVersion = false
if (StriLiAtlasLootVersion.patch > patch) then
if (StriLiAtlasLootVersion.major >= major) and (StriLiAtlasLootVersion.minor >= minor) then
newVersion = true;
end
elseif (StriLiAtlasLootVersion.minor > minor) then
if (StriLiAtlasLootVersion.major >= major) then
newVersion = true;
end
elseif (StriLiAtlasLootVersion.major > major) then
newVersion = true;
end
if newVersion then
print(CONSTS.msgColorStringStart..StriLiAtlasLootAdIn.Lang.YourVersion..versionString.."|r");
print(CONSTS.msgColorStringStart..StriLiAtlasLootAdIn.Lang.AvailableVersion..StriLiAtlasLootVersion.major.."."..StriLiAtlasLootVersion.minor.."."..StriLiAtlasLootVersion.patch.."|r");
print(CONSTS.msgColorStringStart..StriLiAtlasLootAdIn.Lang.GithubLinkVersion.."|r");
end
end
---@param vString string
---@return void
local function versionCompare(vString)
local _, _, major, minor, patch = string.find(vString, versionPattern);
major, minor, patch = tonumber(major), tonumber(minor), tonumber(patch);
if StriLiAtlasLootVersion == nil then
StriLiAtlasLootVersion = {major=major, minor=minor, patch=patch, vString=vString};
end
local newVersion = false
if (StriLiAtlasLootVersion.patch < patch) then
if (StriLiAtlasLootVersion.major <= major) and (StriLiAtlasLootVersion.minor <= minor) then
newVersion = true;
end
elseif (StriLiAtlasLootVersion.minor < minor) then
if (StriLiAtlasLootVersion.major <= major) then
newVersion = true;
end
elseif (StriLiAtlasLootVersion.major < major) then
newVersion = true;
end
if newVersion then
StriLiAtlasLootVersion = {major=major, minor=minor, patch=patch, vString=vString};
end
end
---@param aString string
---@return number
local function getItemIdFromString(aString)
local _, _, _, _, Id = string.find(aString, itemLinkPatern);
return tonumber(Id);
end
---@param aString string
---@return number
local function isItemInWishList(aString)
for i = 1, table.getn(CharWishList) do
for j = 1, table.getn(CharWishList[i]) do
if tonumber(CharWishList[i][j][ItemID]) == getItemIdFromString(aString) then
return tonumber(CharWishList[i][j][ItemID]);
end
end
end
return nil;
end
---@param itemID number
---@return void
local function removeFromAtlasWishlist(itemID)
---@type boolean
local found = true;
while found do
found = false;
for i = 1, table.getn(CharWishList) do
for j = 1, table.getn(CharWishList[i]) do
if CharWishList[i][j] then
if tonumber(CharWishList[i][j][ItemID]) == itemID then
found = true;
table.remove(CharWishList[i], j);
end
end
end
end
end
end
---@return void
local function ShoutVersion()
if GetNumRaidMembers() > 1 then
local _, instanceType = IsInInstance()
if instanceType == "pvp" then
SendAddonMessage("SLALAI_VC", tostring(StriLiAtlasLootVersion.vString), "BATTLEGROUND");
else
SendAddonMessage("SLALAI_VC", tostring(StriLiAtlasLootVersion.vString), "RAID");
end
elseif GetNumPartyMembers() > 0 then
SendAddonMessage("SLALAI_VC", tostring(StriLiAtlasLootVersion.vString), "PARTY");
end
if IsInGuild() then
SendAddonMessage("SLALAI_VC", tostring(StriLiAtlasLootVersion.vString), "GUILD");
end
end
---@param prefix string
---@param message string
---@param distribution_type string
---@param sender string
---@return void
local function onVersionShouted(prefix, message, distribution_type, sender)
if sender == UnitName("player") then return end
if prefix == "SLALAI_VC" then
versionCompare(message);
end
end
---@return void
local function checkForAtlasLoot()
local _, _, _, AtlasLootEnabled = GetAddOnInfo("AtlasLoot")
if not AtlasLootEnabled then
print("|cffff3333 "..StriLiAtlasLootAdIn.Lang.AtlasLootNotFound.." |r");
end
end
---@param bagID number
---@return void
local function checkForReceivedItem(bagID)
local numberOfSlots = GetContainerNumSlots(bagID);
for i = 1, numberOfSlots do
local itemId = GetContainerItemID(bagID, i);
if itemId ~= nil then
removeFromAtlasWishlist(itemId);
end
end
end
---@param textMessage string
---@return void
local function informPlayerOnDemand(textMessage)
local charName = UnitName("player");
CharWishList = AtlasLootWishList["Own"][charName];
if not select(3, string.find(textMessage, "|c(.+)|r")) then return end
local itemLink = "|c"..select(3, string.find(textMessage, "|c(.+)|r")).."|r"
if isItemInWishList(itemLink) then
ItemRollInformFrame_show(getItemIdFromString(itemLink));
end
end
---@return void
local function onRaidLeft()
if not StriLiEnabled then
StriLi.master:set("");
RaidMembersDB:reset();
StriLi.ItemHistory:reset();
end
end
---@return void
local function checkIfInRaid()
if not UnitInRaid("player") then
onRaidLeft();
end
end
---@param event string
---@param ... any
---@return void
local function localOnEvent(event, ...)
if event == "ADDON_LOADED" then
if arg1 == "StriLiAtlasLootAddIn" then
checkVersion();
addonLoaded = true;
if IsAddOnLoaded("AtlasLoot") then
AtlasLootLoaded = true;
end
if not StriLiEnabled then
StriLi_initAddon();
end
if StriLiAtlasLoot_CustomRoll == nil then
StriLiAtlasLoot_CustomRoll = "";
end
elseif arg1 == "AtlasLoot" then
AtlasLootLoaded = true;
end
if addonLoaded and AtlasLootLoaded and (CharWishList == nil) then
local charName = UnitName("player");
CharWishList = AtlasLootWishList["Own"][charName];
EventFrame:RegisterEvent("CHAT_MSG_RAID_WARNING");
EventFrame:RegisterEvent("CHAT_MSG_RAID");
EventFrame:RegisterEvent("CHAT_MSG_RAID_LEADER");
EventFrame:RegisterEvent("TRADE_ACCEPT_UPDATE");
EventFrame:RegisterEvent("BAG_UPDATE");
EventFrame:RegisterEvent("PLAYER_LOGOUT");
EventFrame:RegisterEvent("PARTY_MEMBERS_CHANGED");
EventFrame:RegisterEvent("CHAT_MSG_ADDON");
EventFrame:UnregisterEvent("ADDON_LOADED");
checkForAtlasLoot();
end
elseif event == "CHAT_MSG_RAID_WARNING" or event=="CHAT_MSG_RAID" or event=="CHAT_MSG_RAID_LEADER" then
informPlayerOnDemand(arg1);
elseif event == "PLAYER_LOGOUT" and not StriLiEnabled then
StriLi_finalizeAddon();
elseif event == "CHAT_MSG_ADDON" then
if not StriLiEnabled then
StriLi.CommunicationHandler:On_CHAT_MSG_ADDON(...);
end
onVersionShouted(...);
elseif event == "TRADE_ACCEPT_UPDATE"then
if (arg1 == 1) and (arg2 == 1) then
playerTradeDone = true;
end
elseif event == "BAG_UPDATE" and playerTradeDone then
if arg1 < 0 then return end -- smaller than zero is not an bagID where items are stored from a trade.
playerTradeDone = false; --only if trade is done the BAG_UPDATE was called from an item receive ore remove from this trade. Other can be ignored.
local timeToWait = waitTimeAfterTrade;
local bagID = arg1;
timerFrame:SetScript("OnUpdate", function(_, elapsed)
timeToWait = timeToWait - elapsed;
if timeToWait < 0.0 then
-- time exceeded
timerFrame:SetScript("OnUpdate", nil);
checkForReceivedItem(bagID);
end
end);
elseif event == "PARTY_MEMBERS_CHANGED" then
ShoutVersion();
checkIfInRaid();
end
end
SLASH_STRILIATLASLOOTADDIN1 = '/slatlas'
SlashCmdList["STRILIATLASLOOTADDIN"] = function(_, _)
ItemRollInformFrame_show(0, StriLiAtlasLootAdIn.Lang.ItemRollFrame.headerText_alt);
end
EventFrame:SetScript("OnEvent", function(_, event, ...) localOnEvent(event, ...); end);
EventFrame:RegisterEvent("ADDON_LOADED");