-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAltinatorData.lua
More file actions
463 lines (428 loc) · 16.6 KB
/
AltinatorData.lua
File metadata and controls
463 lines (428 loc) · 16.6 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
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
local AddonName, AltinatorNS = ...
local C = AltinatorNS.C
local L = LibStub("AceLocale-3.0"):GetLocale(AddonName)
local AltinatorData = {}
AltinatorNS.AltinatorData = AltinatorData
local function AutoReturnMail(mailData)
if mailData.Returned then
return
end
local data = AltinatorNS.AltinatorDB.global.characters[mailData.Sender]
if data then
table.insert(data.Mail, {
Sender = AltinatorNS.AltinatorAddon.CurrentCharacter.FullName,
Subject = mailData.Subject,
Body = mailData.Body,
Time = time(),
ArrivalTime = time(),
ExpiryTime = time() + C["MailExpiry"] * 86400,
HasAttachments = mailData.HasAttachments,
Money = mailData.Money,
Returned = true
})
end
end
local function FindInBagSlot(bags, itemId)
for _, bag in ipairs(bags) do
for slot=1, C_Container.GetContainerNumSlots(bag) do
local itemID = C_Container.GetContainerItemID(bag, slot)
if itemID == itemId then
return true
end
end
end
return false
end
function AltinatorData:SavePlayerDataLogin()
local name, realm = UnitFullName("player")
local data = AltinatorNS:MergeObjects(AltinatorNS.AltinatorDB.global.characters[name .. "-" .. realm] or {}, AltinatorNS.AltinatorAddon.CurrentCharacter)
AltinatorNS.AltinatorDB.global.characters[name .. "-" .. realm] = data
AltinatorNS.AltinatorAddon.CurrentCharacter = data
data.Name = name
data.Realm = realm
data.FullName= name .. "-" .. realm
data.Sex = UnitSex("player")
data.Level = UnitLevel("player")
data.Faction = UnitFactionGroup("player")
local overall, equipped = GetAverageItemLevel()
data.ItemLevel = data.ItemLevel or {}
data.ItemLevel.Overall = overall
data.ItemLevel.Equipped = equipped
data.Rank = data.Rank or {}
data.Rank.Value = UnitPVPRank("player")
data.Rank.Name = GetPVPRankInfo(data.Rank.Value)
local hk, points = GetPVPThisWeekStats()
data.Honour = data.Honour or {}
data.Honour.HKs = hk
data.Honour.Points = points
data.Money = GetMoney()
data.LastLogin = time()
data.LastLogout = data.LastLogout or time()
local className, classFilename, classId = UnitClass("player")
data.Class= data.Class or {}
data.Class.Name=className
data.Class.File=classFilename
data.Class.Id=classId
local raceName, raceFile, raceID = UnitRace("player")
data.Race= data.Race or {}
data.Race.Name=raceName
data.Race.File=raceFile
data.Race.Id=raceID
local guildName, guildRankName, guildRankIndex, guildRealm = GetGuildInfo("player")
data.Guild= data.Guild or {}
data.Guild.Name=guildName
data.Guild.Rank=guildRankName
data.XP=data.XP or{}
data.XP.Current=UnitXP("player")
data.XP.Needed=UnitXPMax("player")
data.XP.Rested=GetXPExhaustion()
data.Professions = data.Professions or {}
data.ProfessionsSecondairy = data.ProfessionsSecondairy or {}
local profNames_rev = tInvert(L["ProfessionIDs"])
local skillsFound = 0
local knownProfs = {}
for i = 1, GetNumSkillLines() do
local name, _, _, skillRank, _, _, skillMaxRank = GetSkillLineInfo(i)
if profNames_rev[name] then
local profId=profNames_rev[name]
skillsFound = skillsFound + 1
if C["SecondairyProfession"][profId] then
data.ProfessionsSecondairy[profId] = data.ProfessionsSecondairy[profId] or {}
data.ProfessionsSecondairy[profId].Name=name
data.ProfessionsSecondairy[profId].File=C["ProfessionIcons"][profNames_rev[name]]
data.ProfessionsSecondairy[profId].Skill=skillRank
data.ProfessionsSecondairy[profId].SkillMax=skillMaxRank
data.ProfessionsSecondairy[profId].Spells= data.ProfessionsSecondairy[profId].Spells or {}
data.ProfessionsSecondairy[profId].Items= data.ProfessionsSecondairy[profId].Items or {}
else
table.insert(knownProfs, profId)
data.Professions[profId] = data.Professions[profId] or {}
data.Professions[profId].Name=name
data.Professions[profId].File=C["ProfessionIcons"][profNames_rev[name]]
data.Professions[profId].Skill=skillRank
data.Professions[profId].SkillMax=skillMaxRank
data.Professions[profId].Spells= data.Professions[profId].Spells or {}
data.Professions[profId].Items= data.Professions[profId].Items or {}
end
end
end
-- in case a profession was unlearned, remove it from the data
for profId, _ in pairs(data.Professions) do
if not tContains(knownProfs, profId) then
data.Professions[profId] = nil
end
end
data.Mail = data.Mail or {}
for i=#data.Mail,1,-1 do
if data.Mail[i].ExpiryTime < time() then
AutoReturnMail(data.Mail)
table.remove(data.Mail, i)
end
end
data.Containers = data.Containers or {}
data.Containers.Bags = data.Containers.Bags or {}
data.Containers.Bank = data.Containers.Bank or {}
data.Attunements = data.Attunements or {}
for i, attunement in ipairs(C["Attunements"]) do
data.Attunements[i] = data.Attunements[i] or {}
if not data.Attunements[i].Completed then
if attunement.type==1 then
local bagSlots = {
}
for bag=0, NUM_BAG_SLOTS do
table.insert(bagSlots, bag)
local containerSlots = C_Container.GetContainerNumSlots(bag)
if containerSlots then
data.Containers.Bags["bag_" .. bag] = data.Containers.Bags["bag_" .. bag] or {}
data.Containers.Bags["bag_" .. bag].Slots = containerSlots
end
end
if attunement.attunementItem then
local hasItem = FindInBagSlot(bagSlots, attunement.attunementItem)
if hasItem then
data.Attunements[i].Completed = true
end
end
elseif attunement.type==2 then
for j=1, #attunement.attunementQuests do
if C_QuestLog.IsQuestFlaggedCompleted(attunement.attunementQuests[j]) then
data.Attunements[i].Completed = true
break
end
end
elseif attunement.type==3 then
if attunement.attunementItem then
local bagSlots = {
KEYRING_CONTAINER
}
local hasItem = FindInBagSlot(bagSlots, attunement.attunementItem)
if hasItem then
data.Attunements[i].Completed = true
end
end
end
end
end
end
function AltinatorData:ScanBank()
local data = AltinatorNS.AltinatorAddon.CurrentCharacter
local bankSlots = {
BANK_CONTAINER,
}
for bag=NUM_BAG_SLOTS+1, NUM_BAG_SLOTS+NUM_BANKBAGSLOTS do
table.insert(bankSlots, bag)
local containerSlots = C_Container.GetContainerNumSlots(bag)
if containerSlots then
data.Containers.Bank["bank_" .. bag] = data.Containers.Bank["bank_" .. bag] or {}
data.Containers.Bank["bank_" .. bag].Slots = containerSlots
end
end
for i, attunement in ipairs(C["Attunements"]) do
data.Attunements[i] = data.Attunements[i] or {}
if not data.Attunements[i].Completed then
if attunement.type==1 then
if attunement.attunementItem then
local hasItem = FindInBagSlot(bankSlots, attunement.attunementItem)
if hasItem then
data.Attunements[i].Completed = true
end
end
end
end
end
end
function AltinatorData:ClearPlayerMailData()
local data = AltinatorNS.AltinatorAddon.CurrentCharacter
data.Mail = data.Mail or {}
for i=#data.Mail,1,-1 do
if data.Mail[i].ArrivalTime < time() then
table.remove(data.Mail, i)
end
end
data.Money = GetMoney() -- update money in case mail had money attached
end
function AltinatorData:SavePlayerDataLogout()
local data = AltinatorNS.AltinatorAddon.CurrentCharacter
data.LastLogout = time()
end
function AltinatorData:SavePlayerTimePlayed(total, level)
local data = AltinatorNS.AltinatorAddon.CurrentCharacter
data.TimePlayed = {}
data.TimePlayed.Total = total
data.TimePlayed.Level = level
end
function AltinatorData:SavePlayerMoney(total, level)
local data = AltinatorNS.AltinatorAddon.CurrentCharacter
data.Money = GetMoney()
end
function AltinatorData:SavePlayerXP()
local data = AltinatorNS.AltinatorAddon.CurrentCharacter
data.Level = UnitLevel("player")
data.XP=data.XP or{}
data.XP.Current=UnitXP("player")
data.XP.Needed=UnitXPMax("player")
data.XP.Rested=GetXPExhaustion()
end
function AltinatorData:SavePlayerResting()
local data = AltinatorNS.AltinatorAddon.CurrentCharacter
data.Resting = IsResting()
end
function AltinatorData:SavePlayerGuild()
local data = AltinatorNS.AltinatorAddon.CurrentCharacter
local guildName, guildRankName, guildRankIndex, guildRealm = GetGuildInfo("player")
data.Guild= data.Guild or {}
data.Guild.Name=guildName
data.Guild.Rank=guildRankName
end
function AltinatorData:GetCharacterProfession(char, profId)
return char.Professions[profId] or char.ProfessionsSecondairy[profId] or nil
end
function AltinatorData:CharacterProfessionRecipeKnownOrLearnable(profId, spellId, itemId)
local characters = AltinatorNS:GetRealmCharactersSorted()
local charactersThatKnow = {}
local charactersThatCouldLearn = {}
for i, name in ipairs(characters) do
local char = AltinatorNS.AltinatorDB.global.characters[name]
if char then
local prof = self:GetCharacterProfession(char, profId)
if prof then
local known = false
if spellId and prof.Spells and prof.Spells[spellId] then
known = true
table.insert(charactersThatKnow, char)
end
if itemId and prof.Items and prof.Items[itemId] then
known = true
table.insert(charactersThatKnow, char)
end
if not known then
table.insert(charactersThatCouldLearn, char)
end
end
end
end
return charactersThatKnow, charactersThatCouldLearn
end
function AltinatorData:ScanEnchantingRecipes()
local tradeskillName = GetCraftDisplaySkillLine()
local profNames_rev = tInvert(L["ProfessionIDs"])
local profId = 0
if profNames_rev[tradeskillName] then
profId=profNames_rev[tradeskillName]
--print("Tradeskill Name: " .. (tradeskillName or "nil") .. ", profId: " .. (profId or "nil"))
for i = 1, GetNumCrafts() do
local skillName, _, skillType = GetCraftInfo(i) -- Ex: Runed Copper Rod
local _, _, icon, _, _, _, spellID = GetSpellInfo(skillName) -- Gets : icon = 135225, spellID = 7421
--print(format("name: %s, skillType: %s, spellID: %d, icon: %d", name or "nil", skillType or "nil", spellID or 0, icon or 0))
local prof = AltinatorNS.AltinatorData:GetCharacterProfession(AltinatorNS.AltinatorAddon.CurrentCharacter, profId)
if prof then
prof.Spells = prof.Spells or {}
prof.Spells[spellID] = {
Name = skillName,
SkillType = skillType,
Icon = icon
}
end
end
end
end
function AltinatorData:ScanTradeSkills()
local tradeskillName = GetTradeSkillLine()
local profNames_rev = tInvert(L["ProfessionIDs"])
local profId = 0
if profNames_rev[tradeskillName] then
profId=profNames_rev[tradeskillName]
--print("Tradeskill Name: " .. (tradeskillName or "nil") .. ", profId: " .. (profId or "nil"))
local numTradeSkills = GetNumTradeSkills()
local skillName, skillType, _, _, altVerb = GetTradeSkillInfo(1) -- test the first line and abort if not valid
if not tradeskillName or not numTradeSkills
or tradeskillName == "UNKNOWN"
or numTradeSkills == 0
or (skillType ~= "header" and skillType ~= "subheader") then
return
end
for i = 1, numTradeSkills do
local link
local itemID
skillName, skillType, _, _, altVerb = GetTradeSkillInfo(i)
local cooldown = GetTradeSkillCooldown(i)
link = GetTradeSkillItemLink(i)
if link then
itemID = tonumber(link:match("item:(%d+)"))
end
if itemID and skillType ~= "header" then
local prof = AltinatorNS.AltinatorData:GetCharacterProfession(AltinatorNS.AltinatorAddon.CurrentCharacter, profId)
if prof then
prof.Items = prof.Items or {}
prof.Items[itemID] = {
Name = skillName,
SkillType = skillType,
Cooldown = cooldown or 0,
CooldownEndTime = (cooldown and cooldown>0) and (time() + cooldown) or 0
}
end
end
end
end
end
function AltinatorData:ScanCooldowns()
local tradeskillName = GetTradeSkillLine()
local profNames_rev = tInvert(L["ProfessionIDs"])
local profId = 0
if profNames_rev[tradeskillName] then
profId=profNames_rev[tradeskillName]
profession = AltinatorNS.AltinatorData:GetCharacterProfession(AltinatorNS.AltinatorAddon.CurrentCharacter, profId)
for i = 1, GetNumTradeSkills() do
local skillName, skillType = GetTradeSkillInfo(i)
if skillType ~= "header" then
local cooldown = GetTradeSkillCooldown(i)
if cooldown then
link = GetTradeSkillItemLink(i)
if link then
itemID = tonumber(link:match("item:(%d+)"))
if itemID then
if profession then
profession.Items = profession.Items or {}
profession.Items[itemID] = {
Name = skillName,
SkillType = skillType,
Cooldown = cooldown or 0,
CooldownEndTime = (cooldown and cooldown>0) and (time() + cooldown) or 0
}
end
end
end
end
end
end
end
end
local function HasAttachments()
for attachmentIndex = 1, ATTACHMENTS_MAX_SEND do -- mandatory, loop through all 12 slots, since attachments could be anywhere (ex: slot 4,5,8)
local item, itemID, icon, count = GetSendMailItem(attachmentIndex)
if item then
return true
end
end
return false
end
hooksecurefunc("SendMail", function(recipient, subject, body, ...)
local recipientName, recipientRealm = strsplit("-", recipient)
recipientRealm = recipientRealm or GetNormalizedRealmName()
local data = AltinatorNS.AltinatorDB.global.characters[recipientName .. "-" .. recipientRealm]
if data then
local attachments = HasAttachments()
local moneySent = GetSendMailMoney()
local arrivalTime = time()
if moneySent>0 or attachments then
arrivalTime = arrivalTime + (C["MailDelivery"] * 3600)
end
data.Mail = data.Mail or {}
table.insert(data.Mail, {
Sender = AltinatorNS.AltinatorAddon.CurrentCharacter.FullName,
Subject = subject,
Body = body or "",
Time = time(),
ArrivalTime = arrivalTime,
ExpiryTime = time() + C["MailExpiry"] * 86400,
HasAttachments = attachments,
Money = moneySent,
Returned = false
})
end
end)
hooksecurefunc("ReturnInboxItem", function(index, ...)
local _, stationaryIcon, mailSender, mailSubject, moneySent, _, _, numAttachments = GetInboxHeaderInfo(index)
local recipientName, recipientRealm = strsplit("-", mailSender)
recipientRealm = recipientRealm or GetNormalizedRealmName()
local data = AltinatorNS.AltinatorDB.global.characters[recipientName .. "-" .. recipientRealm]
if data then
data.Mail = data.Mail or {}
table.insert(data.Mail, {
Sender = AltinatorNS.AltinatorAddon.CurrentCharacter.FullName,
Subject = subject,
Body = body or "",
Time = time(),
ArrivalTime = time(),
ExpiryTime = time() + C["MailExpiry"] * 86400,
HasAttachments = numAttachments>0,
Money = moneySent,
Returned = true
})
end
end)
function AltinatorData:GetProfessionCooldowns(profession)
local cooldowns = {}
if profession.Items then
for itemId, itemData in pairs(profession.Items) do
if itemData.Cooldown and itemData.Cooldown>0 and itemData.CooldownEndTime and itemData.CooldownEndTime>time() then
table.insert(cooldowns, {
Name = itemData.Name,
Cooldown = itemData.Cooldown,
CooldownEndTime = itemData.CooldownEndTime
})
end
end
end
return cooldowns
end