-
Notifications
You must be signed in to change notification settings - Fork 3
/
Core.lua
1333 lines (1227 loc) · 49.5 KB
/
Core.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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
local addonName, _ = ...
local SavedClassic = LibStub("AceAddon-3.0"):GetAddon(addonName)
local L = LibStub("AceLocale-3.0"):GetLocale(addonName, true)
local LibTable = LibStub:GetLibrary("LibTable")
local MSG_PREFIX = "|cff00ff00■ |cffffaa00Saved!|r "
local MSG_SUFFIX = " |cff00ff00■|r"
local player , _ = UnitName("player")
local _, class, _ = UnitClass("player")
local p = function(str) print(MSG_PREFIX..str..MSG_SUFFIX) end
local dbDefault = {
realm = {
[player] = {
frameX = 100, frameY = 25,
showInfoPer = "realm",
showTotalGold = true,
hideLevelUnder = 1,
sortOrder = "level",
sortOption = 0,
currentFirst = true,
default = true,
minimapIcon = { hide = false },
expCurrent = -1, expMax = -1, expPercent = -1, ExpRest = -1,
honorPoint = -1, arenaPoint = -1,
dqComplete = -1, dqMax = -1, dqReset = -1,
lastUpdate = -1,
gearAvgLevel = -1,
gearEquippedLevel = -1,
exclude = "",
}
}
}
local _TranslationTable = {
["color" ] = function(_, _, color) return ((color and color ~= "") and "|cff"..color or "|r"), false end, -- Do not wrap with color for [color] keyword
["item" ] = function(db, option)
local _, itemLink = C_Item.GetItemInfo(option)
if itemLink then
local id = SavedClassic:StripLink(itemLink)
local result = "|T"..C_Item.GetItemIconByID(id)..":14:14|t"..(db.itemCount[id] or "-")
return result
else
return ""
end
end,
["currency" ] = function(db, option)
local currency_type
option:gsub("([^-]*)-(.*)", function(a, b) -- Dash-Separated option
option = a
currency_type = b
end)
local id = tonumber(option)
local currency = id and SavedClassic.currencies[id] or SavedClassic.currencies[option]
if currency then
local result = ""
id = currency.id
local saved_currency = db.currencyCount[id] or { }
saved_currency.quantity = saved_currency.quantity or saved_currency.total -- For compatibility
saved_currency.total = nil
if not currency_type or currency_type == "" then -- weekly max goes Type-2 else Type-0
if saved_currency.totalEarned then
currency_type = "2"
else
currency_type = "0"
end
end
if currency_type == "0" then -- Type-0: [Icon][Quantity]
result = currency.icon..(saved_currency.quantity or "")
elseif currency_type == "1" then -- Type-1: [Icon][Quantity]([Earnable])
local earnable = (saved_currency.totalEarned or 0) - (saved_currency.maxQuantity or 0) -- Earnable is MINUS value
result = currency.icon..(saved_currency.quantity or "").."("..earnable..")"
elseif currency_type == "2" then -- Type-2: [Icon][Quantity]([Earnable])
local earnable = (saved_currency.maxQuantity or 0) - (saved_currency.totalEarned or 0) -- Earnable is RED font
result = currency.icon..(saved_currency.quantity or "").."(|c".."FFFF7575"..earnable.."|r)"
elseif currency_type == "3" then -- Type-3: [Icon][Quantity]([Earned]/[MaxQuantity])
result = currency.icon..(saved_currency.quantity or "").."("..(saved_currency.totalEarned or 0)..(saved_currency.maxQuantity and "/"..saved_currency.maxQuantity or "")..")"
end
return result
else
return ""
end
end,
["name" ] = "coloredName",
["name2" ] = "name",
["zone" ] = "zone",
["subzone" ] = "subzone",
["cooldown" ] = "tsstr",
["elapsed" ] = "elapsedTime",
["level" ] = "level",
["expCur" ] = "expCurrent",
["expMax" ] = "expMax",
["exp%" ] = "expPercent",
["expRest" ] = "restXP",
["expRest%" ] = "restPercent",
["dqCom" ] = "dqComplete",
["dqMax" ] = "dqMax",
["dqReset" ] = "dqReset",
["ilvl_equip"] = "gearEquippedLevel",
["ilvl_avg" ] = "gearAvgLevel",
["ilvl" ] = function(db) return db.gearEquippedLevel.."/"..db.gearAvgLevel end,
["instName" ] = "name",
["instID" ] = "id",
["difficulty"]= "difficultyName",
["progress" ] = "progress",
["bosses" ] = "numBoss",
["time" ] = function(instance) return SecondsToTime(instance.reset - time()) end,
byLocale = {
[L["color" ] ] = "color",
[L["item" ] ] = "item",
[L["currency" ] ] = "currency",
[L["name" ] ] = "name",
[L["name2" ] ] = "name2",
[L["zone" ] ] = "zone",
[L["subzone" ] ] = "subzone",
[L["cooldown" ] ] = "cooldown",
[L["elapsed" ] ] = "elapsed",
[L["level" ] ] = "level",
[L["expCur" ] ] = "expCur",
[L["expMax" ] ] = "expMax",
[L["exp%" ] ] = "exp%",
[L["expRest" ] ] = "expRest",
[L["expRest%" ] ] = "expRest%",
[L["dqCom" ] ] = "dqCom",
[L["dqMax" ] ] = "dqMax",
[L["dqReset" ] ] = "dqReset",
[L["ilvl" ] ] = "ilvl",
[L["ilvl_equip"] ] = "ilvl_equip",
[L["ilvl_avg" ] ] = "ilvl_avg",
[L["instName" ] ] = "instName",
[L["instID" ] ] = "instID",
[L["difficulty"] ] = "difficulty",
[L["progress" ] ] = "progress",
[L["bosses" ] ] = "bosses",
[L["time" ] ] = "time",
}
}
setmetatable(_TranslationTable, { __index = function(t,k) return t.byLocale[k] and t[t.byLocale[k] ] or k end })
local function SavedClassic_GetCurrencyInfo(id)
id = tonumber(id) or 0
if id > 3 then
return C_CurrencyInfo.GetCurrencyInfo(id)
else
return nil
end
end
function SavedClassic:OnInitialize()
self.db = LibStub("AceDB-3.0"):New("SavedClassicDB", dbDefault)
-- Reset old db
if not self.db.global.version then
self:ResetWholeDB()
elseif self.db.global.version < "4.4.0.7" then
p(L["Reset due to update"](self.db.global.version, self.version))
self:ResetWholeDB()
end
self.db.global.version = self.version
if self.db.realm[player].default then self:InitPlayerDB() end
self:SetOrder()
self:InitUI()
self:InitDBIcon()
self:InitRaidTable()
self:BuildCurrencyInfo()
self:BuildOptions() -- Build some tables and self.optionsTable
LibStub("AceConfig-3.0"):RegisterOptionsTable(self.name, self.optionsTable)
LibStub("AceConfigDialog-3.0"):AddToBlizOptions(self.name, self.name, nil)
self:InitUsageTable()
--[[ hooksecurefunc("SetItemRef", function(link, text)
if link == "addon:"..addonName then
local editbox = GetCurrentKeyBoardFocus()
if editbox then
local _, _, keyword = text:find("|h(.+)|h")
editbox:Insert(keyword or "")
end
end
end)
]]
self:RegisterEvent("PLAYER_MONEY", "CurrencyUpdate")
self:RegisterEvent("PLAYER_XP_UPDATE")
self:RegisterEvent("PLAYER_EQUIPMENT_CHANGED")
self:RegisterEvent("PLAYER_LEAVING_WORLD", "SaveZone")
self:RegisterEvent("PLAYER_ENTERING_WORLD", "SaveInfo")
self:RegisterEvent("ZONE_CHANGED", "SaveInfo")
self:RegisterEvent("ZONE_CHANGED_INDOORS", "SaveInfo")
self:RegisterEvent("PLAYER_REGEN_ENABLED", "RequestRaidInfo")
self:RegisterEvent("PLAYER_REGEN_ENABLED", "CurrencyUpdate")
self:RegisterEvent("UPDATE_INSTANCE_INFO", "SaveInfo") -- API RequestRaidInfo() triggers UPDATE_INSTANCE_INFO
self:RegisterEvent("TRADE_SKILL_UPDATE", "SaveTSCooldowns")
self:RegisterEvent("BAG_UPDATE_DELAYED")
self:RegisterEvent("QUEST_TURNED_IN")
self:RegisterEvent("CHAT_MSG_COMBAT_HONOR_GAIN", "CurrencyUpdate")
self.totalMoney = 0 -- Total money except current character
for character, saved in pairs(self.db.realm) do
if character and (character ~= player) and saved.currencyCount and saved.currencyCount[0] then
self.totalMoney = self.totalMoney + (saved.currencyCount[0].total or 0)
end
end
self:ClearItemCount()
self:QUEST_TURNED_IN()
self:BAG_UPDATE_DELAYED()
end
function SavedClassic:OnEnable()
p(L["Enabled"])
end
function SavedClassic:OnDisable()
p(L["Disabled"])
end
function SavedClassic:SetOrder()
local db = self.db.realm[player]
local exclude = { }
self.order = { }
for ch in string.gmatch(db.exclude, "[^%s,;]*") do
exclude[ch] = true
end
for ch, chdb in pairs(self.db.realm) do
if not exclude[ch] then
table.insert(self.order, chdb)
end
end
table.sort(self.order,
function(a, b)
if db.currentFirst then
if a.name == player then return true end
if b.name == player then return false end
end
local aa = a[db.sortOrder] or 0
local bb = b[db.sortOrder] or 0
if db.sortOrder == "gold" then
aa = a.currencyCount[0] and a.currencyCount[0].total or 0
bb = b.currencyCount[0] and b.currencyCount[0].total or 0
end
if aa == bb then
return a.name < b.name
else
if db.sortOption == 1 then
return aa < bb
else
return aa > bb
end
end
end
)
end
function SavedClassic:InitPlayerDB()
local playerdb = self.db.realm[player]
local classColor = RAID_CLASS_COLORS[class]
playerdb.default = false
playerdb.name = player
playerdb.coloredName = classColor:WrapTextInColorCode(player)
playerdb.info1 = true
playerdb.info1_1 = ""
playerdb.info1_2 = format("\n[%s:%s/ffee99]", L["currency"], L["gold"])
playerdb.info2 = true
playerdb.info2_1 = ""
if UnitLevel("player") < GetMaxPlayerLevel() then
playerdb.info1_1 = format("\n[%s/00ff00]■[%s] [[%s/ffffff]:[%s]] [%s] [%s/ffffff]([%s]: [%s])[%s]",
L["color"], L["color"], L["level"], L["name"], L["ilvl"], L["color"], L["zone"], L["subzone"], L["color"])
playerdb.info2_1 = format(" [%s/cc66ff][%s]/[%s] ([%s]%%)[%s] [%s/66ccff]+[%s] ([%s]%%)[%s]",
L["color"], L["expCur"], L["expMax"], L["exp%"], L["color"], L["color"], L["expRest"], L["expRest%"], L["color"])
playerdb.info2_2 = format("[%s/ffffff][%s:%s][%s]",
L["color"], L["currency"], L["JP"], L["color"] )
else
playerdb.info1_1 = format("\n[%s/00ff00]■[%s] [[%s]] [%s] [%s/ffffff]([%s]: [%s])[%s]",
L["color"], L["color"], L["name"], L["ilvl"], L["color"], L["zone"], L["subzone"], L["color"])
playerdb.info2_1 = format(" [%s/ffffff][%s:%s] [%s:%s] [%s:%s] [%s:%s][%s]",
L["color"], L["currency"], L["VP"], L["currency"], L["JP"], L["currency"], L["conquest"], L["currency"], L["honor"], L["color"])
playerdb.info2_2 = format("[%s/ffffff][%s]/[%s][%s]", L["color"], L["dqCom"], L["dqMax"], L["color"])
end
playerdb.info3 = true
playerdb.info3_1 = format(" [%s] ([%s]) [%s]/[%s]", L["instName"], L["difficulty"], L["progress"], L["bosses"])
playerdb.info3_2 = format("[%s]", L["time"])
playerdb.info4 = true
playerdb.info4_1 = format(" [%s/ffff99][%s] ([%s]) [%s]/[%s][%s]", L["color"], L["instName"], L["difficulty"], L["progress"], L["bosses"], L["color"])
playerdb.info4_2 = format("[%s/ffff99]", L["time"])
playerdb.raids = { }
playerdb.heroics = { }
playerdb.tradeSkills = { }
playerdb.itemCount = { }
playerdb.currencyCount = { }
playerdb.zone = ""
playerdb.subzone = ""
playerdb.lastUpdate = time()
playerdb.frameShow = false
self:PLAYER_MONEY()
self:PLAYER_XP_UPDATE()
end
function SavedClassic:ResetPlayerDB()
for k,_ in pairs(self.db.realm[player]) do
self.db.realm[player][k] = nil
end
for k,v in pairs(dbDefault.realm[player]) do
self.db.realm[player][k] = v
end
self:InitPlayerDB()
self:SaveInfo()
end
function SavedClassic:ResetWholeDB()
self.db:ResetDB()
self.db.global.version = self.version
self:InitPlayerDB()
self:SetOrder()
self.totalMoney = 0
self:SaveInfo()
end
function SavedClassic:RequestRaidInfo()
RequestRaidInfo() -- RequestRaidInfo() triggers UPDATE_INSTANCE_INFO
end
function SavedClassic:SaveInfo()
local db = self.db.realm[player]
local classColor = RAID_CLASS_COLORS[class]
db.coloredName = classColor:WrapTextInColorCode(player)
local raids, heroics = { }, { }
local currentTime = time()
-- instanceName, instanceID, instanceReset, instanceDifficulty, locked, extended, instanceIDMostSig, isRaid, maxPlayers, difficultyName
-- = GetSavedInstanceInfo(index)
for i = 1, GetNumSavedInstances() do
local instance = { }
local isLocked, extended, remain, isRaid
instance.name, instance.id, remain, _, isLocked, extended, _, isRaid, _, instance.difficultyName, instance.numBoss, instance.progress = GetSavedInstanceInfo(i)
if isLocked or extended then
instance.reset = remain + currentTime
if extended then
instance.extended = L["extended"]
else
instance.extended = ""
end
if isRaid then
table.insert(raids, instance)
else
table.insert(heroics, instance)
end
end
end
table.sort(raids, function(a,b)
local aa, bb = self.abbr.raid[a.name], self.abbr.raid[b.name]
if aa and aa.order and bb and bb.order then
return ( aa.order < bb.order ) or ( aa.order == bb.order and a.difficultyName < b.difficultyName )
else
return ( a.name < b.name ) or ( a.name == b.name and a.difficultyName < b.difficultyName )
end
end)
table.sort(heroics, function(a,b) return ( a.name < b.name ) or ( a.name == b.name and a.difficultyName > b.difficultyName ) end)
db.raids = raids
db.heroics = heroics
self:PLAYER_XP_UPDATE()
self:PLAYER_EQUIPMENT_CHANGED()
self:CurrencyUpdate()
self:SaveZone()
self:SaveTSCooldowns()
end
function SavedClassic:QUEST_TURNED_IN()
local db = self.db.realm[player]
local completed = GetDailyQuestsCompleted() or 0
if (db.dqComplete or 0) == completed then
C_Timer.After(2, function()
db.dqComplete = GetDailyQuestsCompleted() or 0
end)
else
db.dqComplete = completed
end
db.dqMax = GetMaxDailyQuests() or 0
db.dqResetReal = time() + (GetQuestResetTime() or 0) -- resolve game time to real time
end
function SavedClassic:PLAYER_XP_UPDATE()
local db = self.db.realm[player]
db.level = UnitLevel("player")
db.expCurrent = UnitXP("player")
local expMax = UnitXPMax("player")
if expMax == 0 then expMax = db.expMax or 1 end
db.expMax = expMax
db.expPercent = floor(db.expCurrent / db.expMax * 100)
db.expRest = GetXPExhaustion() or 0
self:SetOrder()
end
function SavedClassic:PLAYER_EQUIPMENT_CHANGED()
local db = self.db.realm[player]
db.gearAvgLevel, db.gearEquippedLevel = GetAverageItemLevel()
db.gearAvgLevel = floor(db.gearAvgLevel)
db.gearEquippedLevel = floor(db.gearEquippedLevel)
end
function SavedClassic:SaveZone()
local db = self.db.realm[player]
local zone = GetZoneText()
if zone and zone ~= "" then
db.zone = zone
db.subzone = GetSubZoneText()
end
db.lastUpdate = time()
end
function SavedClassic:SaveTSCooldowns()
local db = self.db.realm[player]
local currentTime = time()
db.tradeSkills = db.tradeSkills or {}
for id, ts in pairs(self.ts) do
local start, duration = GetSpellCooldown(id)
if duration > 0 then
local remain = start + duration - GetTime()
if remain > 0 and remain < duration+100 then
local ends = currentTime + remain -- Resolve game time to real time
id = ts.share or id
db.tradeSkills[id] = db.tradeSkills[id] or {}
db.tradeSkills[id].ends = math.max(db.tradeSkills[id].ends or 0, ends)
end
else
db.tradeSkills[id] = nil
end
end
end
function SavedClassic:ClearItemCount()
self.db.realm[player].itemCount = { }
end
function SavedClassic:BAG_UPDATE_DELAYED()
local db = self.db.realm[player]
local infoStr = db.info1_1..db.info1_2..db.info2_1..db.info2_2
local itemList = string.gmatch(infoStr, "%["..L["item"]..":([^]]+)%]")
for item in itemList do -- item link or ID or name
local _, itemLink = C_Item.GetItemInfo(item)
if itemLink then
local itemID = self:StripLink(itemLink)
db.itemCount[itemID] = C_Item.GetItemCount(itemLink, true) or 0
end
end
for id, _ in pairs(self.items) do
db.itemCount[id] = C_Item.GetItemCount(id, true) or 0
end
end
function SavedClassic:StripLink(link)
return tonumber(string.match(link, "(%d+):") or string.match(link, "(%d+)")) or ""
end
function SavedClassic:PLAYER_MONEY()
local money = abs(GetMoney())
local db = self.db.realm[player]
db.currencyCount[0] = { total = money }
db.currencyCount[1] = { total = floor(money / 10000) }
db.currencyCount[2] = { total = floor(money % 10000 / 100) }
db.currencyCount[3] = { total = floor(money % 100) }
end
function SavedClassic:CurrencyUpdate()
local db = self.db.realm[player]
for _, currencyID in pairs(self.currencies.order) do
local info = SavedClassic_GetCurrencyInfo(currencyID)
if info then
db.currencyCount[currencyID] = { quantity = info.quantity }
if info.useTotalEarnedForMaxQty then
db.currencyCount[currencyID]["totalEarned"] = info.totalEarned
db.currencyCount[currencyID]["maxQuantity"] = info.maxQuantity
end
end
end
self:PLAYER_MONEY()
end
function SavedClassic:ShowInfoTooltip(tooltip)
local db = self.db.realm[player]
local realm = ""
if db.showInfoPer == "realm" then realm = " - " .. GetRealmName() end
if not self.ui.noticed then
p(L["Raid Table Notice"])
self.ui.noticed = true
end
local totalGold = ""
if db.showTotalGold then
totalGold = floor((self.totalMoney + db.currencyCount[0].total) / 10000).. self.currencies[1].icon
end
tooltip:AddDoubleLine(MSG_PREFIX .. realm .. MSG_SUFFIX, totalGold)
self:SaveZone()
self:QUEST_TURNED_IN()
self:BAG_UPDATE_DELAYED()
self:CurrencyUpdate()
if db.showInfoPer == "realm" then
for _, v in ipairs(self.order) do
if v.level < db.hideLevelUnder then
else
self:ShowInstanceInfo(tooltip, v.name)
end
end
else
self:ShowInstanceInfo(tooltip, player)
end
end
function SavedClassic:ShowInstanceInfo(tooltip, character)
local db = self.db.realm[character]
local currentTime = time()
local tsstr = ""
if db.tradeSkills then
for id, cooldown in pairs(db.tradeSkills) do
local ts = self.ts[id]
if ts and cooldown and cooldown.ends then
if ts.share then ts = self.ts[ts.share] end
local remain = cooldown.ends - currentTime
if remain > 0 then
local hh, mm = floor(remain / 3600), floor(remain % 3600 / 60)
local cooldown_str
if hh > 72 then
cooldown_str = format("%dd", floor(hh / 24))
else
cooldown_str = format("%02d:%02d", hh, mm)
end
tsstr = tsstr..("|T"..(ts.icon or ts.altName or "")..":14:14|t")..cooldown_str
else
db.tradeSkills[id] = nil
end
else
db.tradeSkills[id] = nil
end
end
end
db.tsstr = tsstr
if db.dqResetReal and currentTime > db.dqResetReal then
db.dqComplete = 0
end
db.dqReset = SecondsToTime(GetQuestResetTime() or 0)
db.elapsedTime = SecondsToTime(currentTime - db.lastUpdate)
db.restXP = floor(min(db.expRest + (currentTime - db.lastUpdate) / 28800 * 0.05 * db.expMax, db.expMax * 1.5))
db.restPercent = floor(db.restXP / db.expMax * 100)
if db.info1 then
local line1_1 = self:TranslateCharacter(db.info1_1, db)
local line1_2 = self:TranslateCharacter(db.info1_2, db)
tooltip:AddDoubleLine(line1_1, line1_2)
end
if db.info2 then
local line2_1 = self:TranslateCharacter(db.info2_1, db)
local line2_2 = self:TranslateCharacter(db.info2_2, db)
tooltip:AddDoubleLine(line2_1, line2_2)
end
db.raids = db.raids or {}
if db.info3 then
if db.info3oneline then
local lastName = ""
local oneline
for i = 1, #db.raids do
local instance = db.raids[i]
local remain = SecondsToTime(instance.reset - time())
local name = self.abbr.raid[instance.name] and self.abbr.raid[instance.name].name or instance.name
if remain and ( remain ~= "" ) then
if name ~= lastName then
if oneline then
oneline = oneline..") "
else
oneline = ""
end
oneline = oneline..name.."("..instance.difficultyName:gsub("[^0-9]*","")
else
oneline = oneline.."/"..instance.difficultyName:gsub("[^0-9]*","")
end
lastName = name
end
end
if oneline and oneline ~= "" then
oneline = oneline .. ")"
oneline = oneline:gsub("^ ","") -- trim leading space
tooltip:AddLine(" "..oneline)
end
else
for i = 1, #db.raids do
local instance = db.raids[i]
local remain = SecondsToTime(instance.reset - time())
if remain and ( remain ~= "" ) then
local line3_1 = self:TranslateInstance(db.info3_1, instance)
local line3_2 = self:TranslateInstance(db.info3_2, instance)
tooltip:AddDoubleLine(line3_1, line3_2)
end
end
end
end
db.heroics = db.heroics or {}
if db.info4 then
if db.info4oneline then
local oneline = ""
for i = 1, #db.heroics do
local instance = db.heroics[i]
local remain = SecondsToTime(instance.reset - time())
if remain and ( remain ~= "" ) then
oneline = oneline.." "..(self.abbr.heroic[instance.name] or instance.name)
end
end
if oneline ~= "" then
oneline = oneline:gsub("^ ","") -- trim leading space
tooltip:AddLine("|cffffff99 "..oneline.."|r")
end
else
for i = 1, #db.heroics do
local instance = db.heroics[i]
local remain = SecondsToTime(instance.reset - time())
if remain and ( remain ~= "" ) then
local line4_1 = self:TranslateInstance(db.info4_1, instance)
local line4_2 = self:TranslateInstance(db.info4_2, instance)
tooltip:AddDoubleLine(line4_1, line4_2)
end
end
end
end
end
function SavedClassic:TranslateCharacter(line, db)
-- [keyword] [keyword:option] [keyword/color] [keyword:option/color]
return line:gsub("%]?|h%[?", "|h"):gsub("(%[([^]^[^/^:]*):?([^]^[^/]*)/?([^]^[]*)%])", function(...) return self:TranslateCharacterWord(db, ...) end )
end
function SavedClassic:TranslateCharacterWord(db, strBefore, keyword, option, color)
local tKeyword = _TranslationTable[keyword]
local result = strBefore
local wrapColor = true
if tKeyword then
if type(tKeyword) == "function" then -- [color], [item], [currency] need option
result, wrapColor = tKeyword(db, option, color) -- arg color is only for [color] keyword
else
result = db[tKeyword] or strBefore
end
if wrapColor and color and color ~= "" then
result = "|cff"..color..result.."|r"
end
end
return result
end
function SavedClassic:TranslateInstance(line, instance)
-- [keyword] [keyword/color]
return line:gsub("(%[([^]^[^/]*)/?([^]^[]*)%])", function(...) return self:TranslateInstanceWord(instance, ...) end )
end
function SavedClassic:TranslateInstanceWord(instance, strBefore, keyword, color)
local tKeyword = _TranslationTable[keyword]
local result = strBefore
if tKeyword then
if type(tKeyword) == "function" then
result = tKeyword(instance)
else
result = instance[tKeyword] or strBefore
end
if color and color ~= "" then
result = "|cff"..color..result.."|r"
end
end
return result
end
function SavedClassic:InitUI()
local db = self.db.realm[player]
local ui = CreateFrame("Button", self.name.."FloatingUI", UIParent, "BackdropTemplate")
self.ui = ui
ui:EnableMouse(true)
ui:SetWidth(db.frameX)
ui:SetHeight(db.frameY)
ui:SetMovable(true)
ui:SetBackdrop({
bgFile = "Interface/TutorialFrame/TutorialFrameBackground",
edgeFile = "Interface/Tooltips/UI-Tooltip-Border",
tile = true, tileSize = 16, edgeSize = 16,
insets = { left = 2, right = 2, top = 2, bottom = 2 },
})
ui:SetBackdropBorderColor(1,0.82,0,1)
ui:SetPoint("CENTER")
local text = ui:CreateFontString()
text:SetAllPoints()
text:SetFontObject("GameFontNormal")
text:SetText("|cff00ff00Saved|r ")
ui:RegisterForDrag("LeftButton")
ui:RegisterForClicks("LeftButtonDown", "RightButtonDown")
--ui:SetUserPlaced(true)
ui:SetScript("OnEnter", function(s)
local divider = GetScreenHeight() / 2
local cursorY = select(2, GetCursorPosition())
GameTooltip:Hide()
GameTooltip:SetOwner(s, "ANCHOR_NONE", 0, 0)
if cursorY > divider then
GameTooltip:SetPoint("TOP", s , "BOTTOM")
else
GameTooltip:SetPoint("BOTTOM", s , "TOP")
end
self:ShowInfoTooltip(GameTooltip)
GameTooltip:Show()
end)
ui:SetScript("OnLeave", function() GameTooltip:Hide() end)
ui:SetScript("OnClick", function(s, btn)
if btn == "LeftButton" then
if not IsShiftKeyDown() then
self:ToggleRaidTable()
end
else
LibStub("AceConfigDialog-3.0"):Open(self.name)
end
end)
ui:SetScript("OnDragStart", function(s)
if IsShiftKeyDown() then
GameTooltip:Hide()
s:StartMoving()
end
end)
ui:SetScript("OnDragStop", function(s)
s:StopMovingOrSizing()
GameTooltip:Show()
end)
if self.db.realm[player].frameShow then
self.ui:Show()
else
self.ui:Hide()
end
end
function SavedClassic:InitDBIcon()
self.icon = LibStub("LibDBIcon-1.0")
self.iconLDB = LibStub("LibDataBroker-1.1"):NewDataObject(self.name .."Icon", {
type = "data source",
text = "Saved!",
icon = "135757",
OnClick = function(_, btn)
if btn == "LeftButton" then
self:ToggleRaidTable()
else
self:ToggleConfig()
end
end,
OnTooltipShow = function(tooltip) self:ShowInfoTooltip(tooltip) end,
})
self.icon:Register(self.name, self.iconLDB, self.db.realm[player].minimapIcon)
end
function SavedClassic:ToggleConfig()
if self.configOpened then
LibStub("AceConfigDialog-3.0"):Close(self.name)
self.configOpened = nil
else
LibStub("AceConfigDialog-3.0"):Open(self.name)
self.configOpened = true
end
end
function SavedClassic:InitRaidTable()
self.raidTable = self.raidTable or LibTable:CreateTable(self.name.."RaidTable", UIParent, nil,
{ SetMovable = true, SetUserPlaced = true, SetPoint = "CENTER", SetClampedToScreen = true, ESCClosable = true, PlaceCloseButton = true })
end
function SavedClassic:ToggleRaidTable()
local raidTable = self.raidTable
if raidTable:IsShown() then
raidTable:Hide()
return
end
local rdb = self.db.realm
local maxLv = GetMaxPlayerLevel()
local ilt = {} -- Instance Lock Table(Ordered)
for _, v in ipairs(self.order) do
local name, level = v.name, v.level
local db = rdb[name]
if level < maxLv then
else
local locked = {}
for i = 1, #db.raids do
local instance = db.raids[i]
local remain = SecondsToTime(instance.reset - time())
if remain and ( remain ~= "" ) then
local size = instance.difficultyName:gsub("[^0-9]*","")
locked[instance.name] = locked[instance.name] and (locked[instance.name].."/"..size) or size
end
end
table.insert(ilt, { name = name, locked = locked })
end
end
local lit = {} -- Locked raids only(Exclude not-locked)
for _, info in ipairs(ilt) do
local locked = info and info.locked
if locked then
for instancename, size in pairs(locked) do
if instancename then
lit[instancename] = 1
end
end
end
end
local olit = {} -- Ordered lit
for k, _ in pairs(lit) do
table.insert(olit, k)
end
table.sort(olit, function(a,b)
return (self.abbr.raid[a].order or -400) < (self.abbr.raid[b].order or -400)
end)
local data = {}
-- 1st Row(Title, Headers)
local row1 = { MSG_PREFIX }
for _, v in ipairs(olit) do
local color = self.abbr.raid[v].color
if color then
table.insert(row1, WrapTextInColorCode(v, color))
else
table.insert(row1, v)
end
end
table.insert(data, row1)
-- Rows
for _, info in ipairs(ilt) do
local name, locked = info.name, info.locked
local coloredName = (rdb[name] and rdb[name].coloredName) or name
local row = { coloredName }
for _, raidname in ipairs(olit) do table.insert(row, locked[raidname] or "-") end
table.insert(data, row)
end
--[[ Table [data] should be like
■ Saved Raid1 Raid2 Raids3 ...
Character1 10/25 - 10/25 ...
Character2 - 10 - ...
Character3 25 - - ...
...
]]
raidTable:Resize({ rows = #data, cols = #data[1], widths = 80, heights = 24, })
raidTable:SetRangeJustify(true, true, "CENTER")
raidTable:SetTable(data)
raidTable:SetRangeOption(true, true, { SetTextColor={ 1, 1, 1 } })
self.raidTable:Show()
end
function SavedClassic:InitUsageTable()
local uc = LibTable:CreateTable(self.name.."UsageCharacterTable", UIParent, nil,
{ SetMovable = true, SetPoint = "TOPLEFT", SetClampedToScreen = true, ESCClosable = true, PlaceCloseButton = true }
)
local ui = LibTable:CreateTable(self.name.."UsageInstanceTable", uc, nil,
{ SetPoint = { "TOPLEFT", uc, "BOTTOMLEFT" }, SetClampedToScreen = true }
)
ui:SetCallback({
OnMouseDown = uc.StartMoving,
OnMouseUp = uc.StopMovingOrSizing,
OnShow = function()
if not self.usage_character.loaded then
self:BuildUsageTable()
self.usage_character.loaded = true
end
end
})
self.usage_character = uc
self.usage_instance = ui
self:BuildUsageTable()
end
function SavedClassic:BuildUsageTable()
local uc = self.usage_character
local uctbl = L["Usage_Character"]
local ucdtbl = { }
local function CreateHyperlinkAndColor(_, cell)
local text = cell:GetText() or ""
text = text:gsub("|c[Ff][Ff]......", ""):gsub("|r", "") -- strip color
local _, _, keyword = text:find("^(%[[^]]+%])")
if cell.data or keyword then
local link = format("|Haddon:%s:|h%s|h", addonName, cell.data or keyword)
cell:SetTextColor(0.8, 0.67, 0) -- #ccaa00 - gold color
cell:SetText(link)
cell.data = nil
end
end
uc:Resize({ rows = #uctbl, cols = 4, widths = 100 })
uc:SetRangeOption(true, true, { SetTextColor={ 0.9, 0.9, 0.9 } })
--
for i, _ in ipairs(uctbl) do
ucdtbl[i] = { }
end
-- Here goes Currencies
-- Gold, Silver, Copper at 1st line
table.insert(uctbl, {
format("%s %s", self.currencies[1].icon, self.currencies[1].altName),
format("%s %s", self.currencies[2].icon, self.currencies[2].altName),
format("%s %s", self.currencies[3].icon, self.currencies[3].altName),
})
table.insert(ucdtbl, {
format("[%s:%s]", L["currency"], self.currencies[1].altName),
format("[%s:%s]", L["currency"], self.currencies[2].altName),
format("[%s:%s]", L["currency"], self.currencies[3].altName),
})
-- Next, 4 currencies at one line
table.insert(uctbl, {})
table.insert(ucdtbl, {})
for _, id in pairs(self.currencies.order) do
if id > 3 then
local currency = self.currencies[id]
if currency then
if #uctbl[#uctbl] >= 4 then
table.insert(uctbl, {})
table.insert(ucdtbl, {})
end
table.insert(uctbl[#uctbl], format("%s %s", currency.icon, currency.altName))
table.insert(ucdtbl[#ucdtbl], format("[%s:%s]", L["currency"], currency.altName))
end
end
end
uc:SetTable(uctbl)
uc:SetTableData(ucdtbl)
uc:RangeFunction(true, true, CreateHyperlinkAndColor)
local ui = self.usage_instance
local uitbl = L["Usage_Instance"]
ui:Resize({ rows = #uitbl, cols = 4, widths = 100 })
ui:SetRangeOption(true, true, { SetTextColor={ 0.9, 0.9, 0.9 } })
ui:SetTable(uitbl)
ui:RangeFunction(true, true, CreateHyperlinkAndColor)
end
function SavedClassic:BuildCurrencyInfo()
for _, id in pairs(self.currencies.order) do
local currency = self.currencies[id]
if currency then
if not currency.icon then
local info = SavedClassic_GetCurrencyInfo(id)
if info then
currency.name = info.name
currency.icon = "|T"..info.iconFileID..":14:14|t"
end
end
end
end
end
function SavedClassic:BuildOptions()
local rdb = self.db.realm
local ch = player
local copyTo = ""
local names = {}
local order = self.order
for i = 1, #order do
names[order[i].name] = rdb[order[i].name].coloredName