-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBroker_Cash.lua
1287 lines (1123 loc) · 43 KB
/
Broker_Cash.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
--
-- GLOBALS: LibStub, Broker_CashDB
local _G = _G
local string, table, math, date, time = string, table, math, date, time
local pairs, ipairs = pairs, ipairs
local time, difftime = time, difftime
local wipe = wipe
-- API
local GetAddOnMetadata, InCombatLockdown = C_AddOns.GetAddOnMetadata, InCombatLockdown
local UnitName, UnitGUID, GetRealmName, GetRealmID = UnitName, UnitGUID, GetRealmName, GetRealmID
local GetMoney, BreakUpLargeNumbers = GetMoney, BreakUpLargeNumbers
local CreateColor, CopyTable, tDeleteItem, SecondsToTime = CreateColor, CopyTable, tDeleteItem, SecondsToTime
local UIParent, GameTooltipText, GameTooltipTextSmall = UIParent, GameTooltipText, GameTooltipTextSmall
local GameMenuFrame, InterfaceOptionsFrame = GameMenuFrame, InterfaceOptionsFrame
local SILVER_PER_GOLD, COPPER_PER_GOLD, COPPER_PER_SILVER = SILVER_PER_GOLD, COPPER_PER_GOLD, COPPER_PER_SILVER
-- Environment
local addonName, addonSpace = ...
local addon = LibStub('AceAddon-3.0'):NewAddon(addonSpace, addonName, 'AceConsole-3.0', 'AceEvent-3.0', 'AceTimer-3.0')
local L = LibStub('AceLocale-3.0'):GetLocale(addonName)
local VERSION = GetAddOnMetadata(addonName, 'Version')
-- Libraries
local libLDB = LibStub('LibDataBroker-1.1')
local libQTip = LibStub('LibQTip-1.0')
local LibDBIcon = LibStub("LibDBIcon-1.0")
-- Textures
local tth = select(2, GameTooltipText:GetFontObject():GetFont())
local GOLD_ICON_STRING = ('|TInterface\\MoneyFrame\\UI-GoldIcon:%d:%d:2:0|t'):format(tth, tth)
local SILVER_ICON_STRING = ('|TInterface\\MoneyFrame\\UI-SilverIcon:%d:%d:2:0|t'):format(tth, tth)
local COPPER_ICON_STRING = ('|TInterface\\MoneyFrame\\UI-CopperIcon:%d:%d:2:0|t'):format(tth, tth)
local PLUS_BUTTON_STRING = ('|TInterface\\Buttons\\UI-PlusButton-Up:%d:%d:2:0|t'):format(tth, tth)
local MINUS_BUTTON_STRING = ('|TInterface\\Buttons\\UI-MinusButton-Up:%d:%d:2:0|t'):format(tth, tth)
local HORDE_ICON_STRING = ('|TInterface\\FriendsFrame\\PlusManz-Horde:13:13|t'):format(tth, tth)
local ALLIANCE_ICON_STRING = ('|TInterface\\FriendsFrame\\PlusManz-Alliance:13:13|t'):format(tth, tth)
local NEUTRAL_ICON_STRING = ('|TInterface\\FriendsFrame\\StatusIcon-Offline:13:13|t'):format(tth, tth)
-- Colors
local COLOR_RED = CreateColor(0.8, 0.1, 0.1, 1)
local COLOR_GREEN = CreateColor(0.1, 0.8, 0.1, 1)
local COLOR_YELLOW = CreateColor(0.8, 0.8, 0.1, 1)
-- Statistics management
local FIRST_DAY_OF_WEEK = 2 -- Monday
local MIN_SESSION_THRESHOLD, MAX_SESSION_THRESHOLD, DEFAULT_SESSION_THRESHOLD = 0, 300, 60 -- In seconds
-- Characters data
local currentChar = UnitName('player')
local currentRealm = GetRealmName()
local currentCharKey = currentChar .. ' - ' .. currentRealm
local currentCharGUID = UnitGUID('player')
local serverID = select(2, strsplit('-', currentCharGUID))
local realmID = GetRealmID()
serverID = tonumber(serverID)
local sortedRealms, sortedChars, realmsWealths = {}, {}, {}
-- Saved data
local sv_defaults = {
global = {
general = {
sessionThreshold = DEFAULT_SESSION_THRESHOLD
},
ldb = {
showSilverAndCopper = true,
highlight = false, -- v2.1.6
displayedText = 'CASH' -- v2.2.5
},
menu = {
disableInCombat = true,
showSilverAndCopper = true,
showSubTooltips = true
},
minimap = {
hide = false
},
account = { -- v2.2.7
token = 0,
lastSaved = 0,
money = 0,
day = 0,
week = 0,
month = 0,
year = 0,
ever = 0
},
serverID = {}
},
char = {
since = 0,
lastSaved = 0,
money = 0,
session = 0, -- v2.0.0
lastLogout = 0, -- v2.1.0
day = 0,
week = 0,
month = 0,
year = 0,
ever = 0 -- v1.4.0
}
}
-- Options panel
local options_panel = {
name = ('%s v%s'):format(addonName, VERSION),
handler = addon,
type = 'group',
childGroups = 'tab',
args = {
options = {
name = L['Options'],
type = 'group',
order = 1,
get = 'OptionsPanel_GetOpt',
set = 'OptionsPanel_SetOpt',
args = {
general = {
name = L['OPTIONS_GENERAL'],
type = 'group',
inline = true,
order = 10,
args = {
sessionThreshold = {
name = L['OPTS_SESSION_THRESHOLD'],
order = 1,
width = 'full',
type = 'range',
min = MIN_SESSION_THRESHOLD,
max = MAX_SESSION_THRESHOLD,
step = 1,
bigStep = 10
},
sessionThresholdDesc = {
name = '\n' .. L['OPTS_SESSION_THRESHOLD_DESC'],
order = 2,
type = 'description',
fontSize = 'small'
}
}
},
ldb = {
name = L['OPTIONS_LDB'],
type = 'group',
inline = true,
order = 20,
args = {
highlight = {
name = L['OPTS_HIGHLIGHT_LDB'],
desc = L['OPTS_HIGHLIGHT_LDB_DESC'],
descStyle = 'inline',
type = 'toggle',
width = 'full',
order = 1
},
showSilverAndCopper = {
name = L['OPTS_SMALL_PARTS'],
desc = L['OPTS_SMALL_PARTS_DESC'],
descStyle = 'inline',
type = 'toggle',
width = 'full',
order = 2
},
hideMinimapButton = {
name = L['OPTS_HIDE_MINIMAP_BUTTON'],
descStyle = 'inline',
type = 'toggle',
width = 'full',
order = 3,
get = function()
return addon.db.global.minimap.hide
end,
set = function(info, value)
addon.db.global.minimap.hide = value
if value then
LibDBIcon:Hide(addonName)
else
LibDBIcon:Show(addonName)
end
end
},
displayedText = {
type = "select",
name = L["OPTS_TEXT_LDB"],
values = {
["CASH"] = L['Cash'],
["SESSION"] = L['Current Session']
},
order = 4,
set = function(info, value)
addon.db.global.ldb.displayedText = value
addon:SetLDBText()
end
}
}
},
menu = {
name = L['OPTIONS_MENU'],
type = 'group',
inline = true,
order = 30,
args = {
disableInCombat = {
name = L['OPTS_DISABLE_IN_COMBAT'],
desc = L['OPTS_DISABLE_IN_COMBAT_DESC'],
descStyle = 'inline',
type = 'toggle',
width = 'full',
order = 1
},
showSilverAndCopper = {
name = L['OPTS_SMALL_PARTS'],
desc = L['OPTS_SMALL_PARTS_DESC'],
descStyle = 'inline',
type = 'toggle',
width = 'full',
order = 2
},
showSubTooltips = {
name = L['Show Details'],
desc = L['OPTS_SHOW_DETAILS_DESC'],
descStyle = 'inline',
type = 'toggle',
width = 'full',
order = 3
}
}
}
}
},
database = {
name = L['Characters'],
type = 'group',
order = 2,
args = {
infos = {
type = 'group',
name = L['Notice'],
inline = true,
order = 1,
args = {
line1 = {
type = 'description',
name = L['OPTS_CHARACTERS_INFO_1'],
fontSize = 'medium',
order = 1
},
line2 = {
type = 'description',
name = string.format('> ' .. YELLOW_FONT_COLOR_CODE .. '%s' .. FONT_COLOR_CODE_CLOSE .. ' %s', L['Reset'], L['OPTS_CHARACTERS_INFO_2']),
fontSize = 'medium',
order = 2
},
line3 = {
type = 'description',
name = string.format('> ' .. YELLOW_FONT_COLOR_CODE .. '%s' .. FONT_COLOR_CODE_CLOSE .. ' %s', L['Delete'], L['OPTS_CHARACTERS_INFO_3']),
fontSize = 'medium',
order = 3
},
line4 = {
type = 'description',
name = string.format('\n' .. ORANGE_FONT_COLOR_CODE .. '%s' .. FONT_COLOR_CODE_CLOSE, L['OPTS_CHARACTERS_INFO_4']),
fontSize = 'medium',
order = 4
}
}
},
actions = {
type = 'group',
name = function()
return false
end, -- Allow a group to have no title or frame
inline = true,
order = 100,
args = {
count = {
type = 'description',
name = function()
return addon:OptionsPanel_GetNumSelected()
end,
width = 'fill', -- Magic number in AceConfigDialog-3.0
order = 1,
fontSize = 'medium'
},
reset = {
type = 'execute',
name = L['Reset'],
order = 2,
disabled = 'OptionsPanel_IsActionDisabled',
confirm = 'OptionsPanel_ConfirmAction',
func = 'OptionsPanel_DoResetCharacters'
},
delete = {
type = 'execute',
name = L['Delete'],
order = 3,
disabled = 'OptionsPanel_IsActionDisabled',
confirm = 'OptionsPanel_ConfirmAction',
func = 'OptionsPanel_DoDeleteCharacters'
}
}
}
}
}
}
}
local selectedToons, numSelectedToons = {}, 0
-- Tooltips
local mainTooltip, subTooltip
local unfoldedRealms = {}
local highlightTexture = UIParent:CreateTexture(nil, 'OVERLAY')
highlightTexture:SetTexture('Interface\\QuestFrame\\UI-QuestTitleHighlight')
highlightTexture:SetBlendMode('ADD')
-------------------------------------------------------------------------------
-- Helpers
-------------------------------------------------------------------------------
local function MakeCharKey(charName, charRealm)
return charName:trim() .. ' - ' .. charRealm:trim()
end
local function SplitCharKey(charKey)
local charName, charRealm = ('-'):split(charKey, 2)
return charName:trim(), charRealm:trim()
end
local function InvertCharKey(charKey)
return charKey:gsub('(%S+) %- (%S+)', '%2 - %1')
end
local function ClassColorise(class, targetstring)
if class then
local c = "|c" .. RAID_CLASS_COLORS[class].colorStr
return c .. targetstring .. FONT_COLOR_CODE_CLOSE
else
return targetstring
end
end
local function GetFactionIcon(faction)
if faction == "Horde" then
return HORDE_ICON_STRING
elseif faction == "Alliance" then
return ALLIANCE_ICON_STRING
else
return NEUTRAL_ICON_STRING
end
end
local function SkinFrame(frame)
if C_AddOns.IsAddOnLoaded("ElvUI") or C_AddOns.IsAddOnLoaded("Tukui") then
if frame.StripTextures then
frame:StripTextures()
end
if frame.CreateBackdrop then
frame:CreateBackdrop("Transparent")
end
end
end
-------------------------------------------------------------------------------
if GetLocale() == 'frFR' then
-- Fix a bug in French GlobalStrings.lua
BreakUpLargeNumbers = function(amount)
local left, num, right = string.match(amount, '^([^%d]*%d)(%d*)(.-)$')
return left .. (num:reverse():gsub('(%d%d%d)', '%1 '):reverse()) .. right
end
end
local function GetAbsoluteMoneyString(amount, showSilverAndCopper)
amount = amount or 0
local gold = math.floor(amount / COPPER_PER_GOLD)
local silver = math.floor(amount / SILVER_PER_GOLD) % SILVER_PER_GOLD
local copper = amount % COPPER_PER_SILVER
local tbl, fmt = {}, ''
if gold > 0 then
tbl[#tbl + 1] = BreakUpLargeNumbers(gold) .. GOLD_ICON_STRING
end
local noGold = gold == 0
if showSilverAndCopper or noGold then
if silver > 0 or not noGold then
fmt = noGold and '%d%s' or '%02d%s'
tbl[#tbl + 1] = fmt:format(silver, SILVER_ICON_STRING)
end
fmt = (gold + silver == 0) and '%d%s' or '%02d%s'
tbl[#tbl + 1] = fmt:format(copper, COPPER_ICON_STRING)
end
return table.concat(tbl, ' ')
end
local function GetRelativeMoneyString(amount, showSilverAndCopper)
if (amount or 0) == 0 then
return COLOR_YELLOW:WrapTextInColorCode('0' .. COPPER_ICON_STRING)
elseif amount < 0 then
return COLOR_RED:WrapTextInColorCode('-' .. GetAbsoluteMoneyString(-amount, showSilverAndCopper))
else
return COLOR_GREEN:WrapTextInColorCode('+' .. GetAbsoluteMoneyString(amount, showSilverAndCopper))
end
end
-------------------------------------------------------------------------------
-- Control panel management
-------------------------------------------------------------------------------
-- Remove selected characters from saved data
function addon:OptionsPanel_DoDeleteCharacters(info, value)
local sv = self.sv
for key in pairs(selectedToons) do
sv.char[key] = nil
sv.profileKeys[key] = nil
-- Also remove the character from the options table
local name, realm = SplitCharKey(key)
tDeleteItem(sortedChars[realm], name)
if #sortedChars[realm] == 0 then
sortedChars[realm] = nil
tDeleteItem(sortedRealms, realm)
options_panel.args.database.args[realm] = nil
else
options_panel.args.database.args[realm].values[name] = nil
end
end
-- Reset Warband bank
addon:DoResetWarbandBank()
-- Deselect all characters
numSelectedToons = #wipe(selectedToons)
-- Recalculating the wealth of realms
self:AuditRealms()
end
-- Reset the statistics of selected characters
function addon:OptionsPanel_DoResetCharacters(info, value)
-- Special processing for the current character
if selectedToons[currentCharKey] then
selectedToons[currentCharKey] = nil
self.db.char.money = GetMoney()
self.db.char.session = 0
self.db.char.day = 0
self.db.char.week = 0
self.db.char.month = 0
self.db.char.year = 0
self.db.char.ever = 0
end
-- All the others...
local chars = self.sv.char
for key in pairs(selectedToons) do
chars[key].money = nil
chars[key].session = nil
chars[key].day = nil
chars[key].week = nil
chars[key].month = nil
chars[key].year = nil
chars[key].ever = nil
end
-- Reset Warband bank
addon:DoResetWarbandBank()
-- Deselect all characters
numSelectedToons = #wipe(selectedToons)
-- Recalculating the wealth of realms
self:AuditRealms()
end
-- Reset Warband bank
function addon:DoResetWarbandBank()
local account = self.db.global.account
account.money = C_Bank.FetchDepositedMoney(Enum.BankType.Account)
account.day = 0
account.week = 0
account.month = 0
account.year = 0
account.ever = 0
end
-- Display a confirmation request before resetting/deleting
function addon:OptionsPanel_ConfirmAction(info)
-- str = RESET_TOON(S) or DELETE_TOON(S)
local str = info[#info]:upper() .. '_TOON' .. (numSelectedToons > 1 and 'S' or '')
str = L[str]:format(numSelectedToons)
-- Build the confirmation request
local tbl = {}
for k in pairs(selectedToons) do
tbl[#tbl + 1] = k
end
table.sort(tbl, function(t1, t2)
return InvertCharKey(t1) < InvertCharKey(t2)
end)
return str .. '\n\n' .. table.concat(tbl, '\n') .. '\n\n' .. L['Are you sure?']
end
-- Check whether the Delete and Reset buttons should be disabled
function addon:OptionsPanel_IsActionDisabled(info)
-- True if (no selection) OR (the button is ‘Delete’ AND the current character is one of the selected characters)
return numSelectedToons == 0 or (info[#info] == 'delete' and selectedToons[currentCharKey])
end
-- Select / deselect a character
function addon:OptionsPanel_IsToonSelected(info, opt)
return selectedToons[MakeCharKey(opt, info[#info])]
end
function addon:OptionsPanel_SetToonSelected(info, opt, value)
local key = MakeCharKey(opt, info[#info])
if value then
selectedToons[key] = true
numSelectedToons = numSelectedToons + 1
else
selectedToons[key] = nil
numSelectedToons = numSelectedToons - 1
end
end
-- Update the number of characters selected in the dialog
function addon:OptionsPanel_GetNumSelected(info)
local fmt = 'NUMSELECTED_' .. (numSelectedToons > 1 and 'X' or numSelectedToons)
return L[fmt]:format(numSelectedToons)
end
-- Checkbox management
function addon:OptionsPanel_GetOpt(info)
return self.opts[info[#info - 1]][info[#info]]
end
function addon:OptionsPanel_SetOpt(info, value)
-- info[#info-1] = 'menu' or 'ldb', info[#info] = option clicked
self.opts[info[#info - 1]][info[#info]] = value
-- Update the LDB text where necessary
if info[#info - 1] == 'ldb' then
self.dataObject.text = GetAbsoluteMoneyString(self.db.char.money, self.opts.ldb.showSilverAndCopper)
end
end
-- Show/hide the options dialog
-- BuildOptionsPanel() is called by ShowOptionsPanel() and the standard options window
-- ShowOptionsPanel() is called by ToggleOptionsPanel() and slash commands
-- ToggleOptionsPanel() is called when the LDB icon is clicked
function addon:BuildOptionsPanel()
-- Build dialog if it hasn't already been done
if not options_panel.args.database.args[currentRealm] then
-- Sort the realms, but this time in alphabetical order
local orderedRealms = CopyTable(sortedRealms)
table.sort(orderedRealms)
-- Insert realms and their characters in the options panel
for i, realm in ipairs(orderedRealms) do
options_panel.args.database.args[realm] = {
name = realm,
type = 'multiselect',
descStyle = 'inline', -- No tooltip please :)
get = 'OptionsPanel_IsToonSelected',
set = 'OptionsPanel_SetToonSelected',
values = {},
order = 10 + i
}
for _, name in ipairs(sortedChars[realm]) do
options_panel.args.database.args[realm].values[name] = name
end
end
end
end
function addon:ShowOptionsPanel(msg)
-- Unless in combat or if the menu or standard options are displayed
if InCombatLockdown() or GameMenuFrame:IsShown() or InterfaceOptionsFrame then
return
end
self:BuildOptionsPanel()
LibStub('AceConfigDialog-3.0'):Open(addonName)
end
function addon:ToggleOptionsPanel()
-- Hide the tooltip
self:HideMainTooltip()
-- Hide the dialog if it is displayed, displays it otherwise
local acd = LibStub('AceConfigDialog-3.0')
if acd.OpenFrames[addonName] then
acd:Close(addonName)
else
self:ShowOptionsPanel()
end
end
-------------------------------------------------------------------------------
-- Secondary tooltip management
-------------------------------------------------------------------------------
function addon:PrepareSubTooltip(mainTooltipLine)
if not self.opts.menu.showSubTooltips then
return
end
-- Display (or moves) the sub-tooltip
if not subTooltip then
subTooltip = libQTip:Acquire(addonName .. '_SubTooltip', 2, 'LEFT', 'RIGHT')
subTooltip:SetFrameLevel(mainTooltipLine:GetFrameLevel() + 1)
SkinFrame(subTooltip)
end
-- Define the position of the tooltip (to the left of the parent line if there isn't enough space on the right)
local x, y, w, h = mainTooltipLine:GetRect()
local sw, sh = UIParent:GetSize()
if (x + w + 200) < sw then
subTooltip:SetPoint('TOPLEFT', mainTooltipLine, 'TOPRIGHT', 0, 10)
else
subTooltip:SetPoint('TOPRIGHT', mainTooltipLine, 'TOPLEFT', 0, 10)
end
subTooltip:SetClampedToScreen(true)
-- Delete content
subTooltip:Clear()
subTooltip:SetFont(GameTooltipTextSmall)
subTooltip:SetCellMarginV(2)
return subTooltip
end
-------------------------------------------------------------------------------
function addon:ShowRealmTooltip(realmLineFrame, selectedRealm)
local stt = self:PrepareSubTooltip(realmLineFrame)
if not stt then
return
end
-- Calculate and display realm data
local realmDay, realmWeek, realmMonth, realmYear, realmEver = 0, 0, 0, 0, 0
for key, char in pairs(self.sv.char) do
local _, realm = SplitCharKey(key)
if realm == selectedRealm then
realmDay = realmDay + (char.day or 0)
realmWeek = realmWeek + (char.week or 0)
realmMonth = realmMonth + (char.month or 0)
realmYear = realmYear + (char.year or 0)
realmEver = realmEver + (char.ever or 0)
end
end
local showSilverAndCopper = self.opts.menu.showSilverAndCopper
stt:AddLine(L['Today'], GetRelativeMoneyString(realmDay, showSilverAndCopper))
stt:AddLine(L['This week'], GetRelativeMoneyString(realmWeek, showSilverAndCopper))
stt:AddLine(L['This month'], GetRelativeMoneyString(realmMonth, showSilverAndCopper))
stt:AddLine(L['This year'], GetRelativeMoneyString(realmYear, showSilverAndCopper))
stt:AddLine(L['Ever'], GetRelativeMoneyString(realmEver, showSilverAndCopper))
stt:Show()
end
-------------------------------------------------------------------------------
function addon:ShowCharTooltip(charLineFrame, selectedCharKey)
local stt = self:PrepareSubTooltip(charLineFrame)
if not stt then
return
end
-- Display character data
local char = self.sv.char[selectedCharKey]
stt:AddLine()
stt:SetCell(1, 1, GetFactionIcon(char.faction) .. ClassColorise(char.class, selectedCharKey), 2)
stt:AddLine()
stt:SetCell(2, 1, L['RECORDED_SINCE']:format(date(L['DATE_FORMAT'], char.since)), 2)
stt:AddLine()
stt:SetCell(3, 1, L['LAST_SAVED']:format(date(L['DATE_TIME_FORMAT'], char.lastSaved)), 2)
stt:AddLine('')
stt:AddSeparator()
stt:AddLine('')
local showSilverAndCopper = self.opts.menu.showSilverAndCopper
if selectedCharKey == currentCharKey then
stt:AddLine(L['Current Session'], GetRelativeMoneyString(self.db.char.session, showSilverAndCopper))
end
stt:AddLine(L['Today'], GetRelativeMoneyString(char.day or 0, showSilverAndCopper))
stt:AddLine(L['This week'], GetRelativeMoneyString(char.week or 0, showSilverAndCopper))
stt:AddLine(L['This month'], GetRelativeMoneyString(char.month or 0, showSilverAndCopper))
stt:AddLine(L['This year'], GetRelativeMoneyString(char.year or 0, showSilverAndCopper))
stt:AddLine(L['Ever'], GetRelativeMoneyString(char.ever or 0, showSilverAndCopper))
stt:Show()
end
-------------------------------------------------------------------------------
function addon:HideSubTooltip()
if subTooltip then
subTooltip:Release()
subTooltip = nil
end
end
-------------------------------------------------------------------------------
-- Main tooltip management
-------------------------------------------------------------------------------
-- Unfold/fold a realm in the tooltip
local function MainTooltip_OnClickRealm(realmLineFrame, realm, button)
unfoldedRealms[realm] = not unfoldedRealms[realm]
addon:UpdateMainTooltip()
end
-- Display the secondary tooltip for a realm
local function MainTooltip_OnEnterRealm(realmLineFrame, realm)
addon:ShowRealmTooltip(realmLineFrame, realm)
end
local function MainTooltip_OnLeaveRealm(realmLineFrame)
addon:HideSubTooltip()
end
-- Display the secondary tooltip for a character
local function MainTooltip_OnEnterChar(charLineFrame, charKey)
addon:ShowCharTooltip(charLineFrame, charKey)
end
local function MainTooltip_OnLeaveChar(charLineFrame)
addon:HideSubTooltip()
end
-- Fill the main tooltip
function addon:UpdateMainTooltip()
local mtt = mainTooltip
if not mtt then
return
end
-- Build the tooltip
local showSilverAndCopper = self.opts.menu.showSilverAndCopper
local ln, rln
mtt:Hide()
mtt:Clear()
mtt:SetCellMarginV(2)
---------------------------------------------------------------------------
-- 1/ Header
---------------------------------------------------------------------------
mtt:AddHeader(L['Name'], L['Cash'])
mtt:AddSeparator()
mtt:AddLine('')
---------------------------------------------------------------------------
-- 2/ Current character
---------------------------------------------------------------------------
mtt:AddLine(GetFactionIcon(self.db.char.faction) .. ClassColorise(self.db.char.class, currentCharKey), GetAbsoluteMoneyString(self.db.char.money, showSilverAndCopper))
ln = mtt:AddLine()
mtt:SetCell(ln, 1, L['Current Session'], GameTooltipTextSmall, 1, 20)
mtt:SetCell(ln, 2, GetRelativeMoneyString(self.db.char.session, showSilverAndCopper), GameTooltipTextSmall)
ln = mtt:AddLine()
mtt:SetCell(ln, 1, L['Today'], GameTooltipTextSmall, 1, 20)
mtt:SetCell(ln, 2, GetRelativeMoneyString(self.db.char.day, showSilverAndCopper), GameTooltipTextSmall)
ln = mtt:AddLine()
mtt:SetCell(ln, 1, L['This week'], GameTooltipTextSmall, 1, 20)
mtt:SetCell(ln, 2, GetRelativeMoneyString(self.db.char.week, showSilverAndCopper), GameTooltipTextSmall)
ln = mtt:AddLine()
mtt:SetCell(ln, 1, L['This month'], GameTooltipTextSmall, 1, 20)
mtt:SetCell(ln, 2, GetRelativeMoneyString(self.db.char.month, showSilverAndCopper), GameTooltipTextSmall)
ln = mtt:AddLine()
mtt:SetCell(ln, 1, L['This year'], GameTooltipTextSmall, 1, 20)
mtt:SetCell(ln, 2, GetRelativeMoneyString(self.db.char.year, showSilverAndCopper), GameTooltipTextSmall)
ln = mtt:AddLine()
mtt:SetCell(ln, 1, L['Ever'], GameTooltipTextSmall, 1, 20)
mtt:SetCell(ln, 2, GetRelativeMoneyString(self.db.char.ever, showSilverAndCopper), GameTooltipTextSmall)
mtt:AddLine('')
mtt:AddSeparator()
mtt:AddLine('')
---------------------------------------------------------------------------
-- 3/ All the characters, grouped by realm
---------------------------------------------------------------------------
-- Sort realms in descending order of wealth
-- (done here because the order can change at any time depending on the current character)
table.sort(sortedRealms, function(r1, r2)
if realmsWealths[r1] == realmsWealths[r2] then
return r1 < r2
else
return realmsWealths[r1] > realmsWealths[r2]
end
end)
-- Add all the characters, realm by realm
local chars = self.sv.char
local totalMoney = 0
for _, realm in ipairs(sortedRealms) do
local unfolded, realmMoney = unfoldedRealms[realm], realmsWealths[realm]
-- Display realm if wealthy
if realmMoney > 0 then
-- Calculates total wealth
totalMoney = totalMoney + realmMoney
local wealthyCharsCount = 0
for _, name in ipairs(sortedChars[realm]) do
local key = MakeCharKey(name, realm)
if (chars[key].money or 0) > 0 then
wealthyCharsCount = wealthyCharsCount + 1
end
end
-- 1/ Realm name + number of characters + wealth of realm
mtt:AddLine('')
rln = mtt:AddLine()
mtt:SetCell(rln, 1, ('%s %s (%d)'):format(unfolded and MINUS_BUTTON_STRING or PLUS_BUTTON_STRING, realm, wealthyCharsCount))
mtt:SetCell(rln, 2, GetAbsoluteMoneyString(realmMoney, showSilverAndCopper))
mtt:SetLineTextColor(rln, COLOR_YELLOW:GetRGBA())
mtt:SetLineScript(rln, 'OnEnter', MainTooltip_OnEnterRealm, realm)
mtt:SetLineScript(rln, 'OnLeave', MainTooltip_OnLeaveRealm)
mtt:SetLineScript(rln, 'OnMouseDown', MainTooltip_OnClickRealm, realm)
-- 2/ All the characters in this realm
if unfolded then
-- Sorts the realm's characters in descending order of wealth
-- (done here because the order can change at any time depending on the current character)
table.sort(sortedChars[realm], function(n1, n2)
n1, n2 = MakeCharKey(n1, realm), MakeCharKey(n2, realm)
return ((chars[n1].money) or 0) > ((chars[n2].money) or 0)
end)
for _, name in ipairs(sortedChars[realm]) do
local key = MakeCharKey(name, realm)
-- Add the character if wealthy
if (chars[key].money or 0) > 0 then
ln = mtt:AddLine()
mtt:SetCell(ln, 1, GetFactionIcon(chars[key].faction) .. ClassColorise(chars[key].class, name), 1, 20)
mtt:SetCell(ln, 2, GetAbsoluteMoneyString(chars[key].money or 0, showSilverAndCopper))
mtt:SetLineScript(ln, 'OnEnter', MainTooltip_OnEnterChar, key)
mtt:SetLineScript(ln, 'OnLeave', MainTooltip_OnLeaveChar)
end
end
end
end
end
-- Add Warband bank money
totalMoney = totalMoney + self.db.global.account.money
---------------------------------------------------------------------------
-- 4/ Add Warband Bank
---------------------------------------------------------------------------
if self.db.global.account.money > 0 then
mtt:AddLine('')
mtt:AddSeparator()
mtt:AddLine('')
mtt:AddLine(L['Warband Bank'], GetAbsoluteMoneyString(self.db.global.account.money, showSilverAndCopper))
end
---------------------------------------------------------------------------
-- 5/ Add the grand total
---------------------------------------------------------------------------
-- Calculating the total balance
local totalBalance = {
day = 0,
week = 0,
month = 0,
year = 0,
ever = 0
}
-- Each character
for _, char in pairs(self.sv.char) do
totalBalance.day = totalBalance.day + (char.day or 0)
totalBalance.week = totalBalance.week + (char.week or 0)
totalBalance.month = totalBalance.month + (char.month or 0)
totalBalance.year = totalBalance.year + (char.year or 0)
totalBalance.ever = totalBalance.ever + (char.ever or 0)
end
-- Warband
local account = self.db.global.account
totalBalance.day = totalBalance.day + (account.day or 0)
totalBalance.week = totalBalance.week + (account.week or 0)
totalBalance.month = totalBalance.month + (account.month or 0)
totalBalance.year = totalBalance.year + (account.year or 0)
totalBalance.ever = totalBalance.ever + (account.ever or 0)
-- Rendering the total
mtt:AddLine('')
mtt:AddSeparator()
mtt:AddLine('')
mtt:AddLine(L['Total'], GetAbsoluteMoneyString(totalMoney, showSilverAndCopper))
ln = mtt:AddLine()
mtt:SetCell(ln, 1, L['Today'], GameTooltipTextSmall, 1, 20)
mtt:SetCell(ln, 2, GetRelativeMoneyString(totalBalance.day, showSilverAndCopper), GameTooltipTextSmall)
ln = mtt:AddLine()
mtt:SetCell(ln, 1, L['This week'], GameTooltipTextSmall, 1, 20)
mtt:SetCell(ln, 2, GetRelativeMoneyString(totalBalance.week, showSilverAndCopper), GameTooltipTextSmall)
ln = mtt:AddLine()
mtt:SetCell(ln, 1, L['This month'], GameTooltipTextSmall, 1, 20)
mtt:SetCell(ln, 2, GetRelativeMoneyString(totalBalance.month, showSilverAndCopper), GameTooltipTextSmall)
ln = mtt:AddLine()
mtt:SetCell(ln, 1, L['This year'], GameTooltipTextSmall, 1, 20)
mtt:SetCell(ln, 2, GetRelativeMoneyString(totalBalance.year, showSilverAndCopper), GameTooltipTextSmall)
ln = mtt:AddLine()
mtt:SetCell(ln, 1, L['Ever'], GameTooltipTextSmall, 1, 20)
mtt:SetCell(ln, 2, GetRelativeMoneyString(totalBalance.ever, showSilverAndCopper), GameTooltipTextSmall)
mtt:AddLine('')
mtt:AddSeparator()
mtt:AddLine('')
mtt:AddLine(L['WoW Token'], GetAbsoluteMoneyString(self.db.global.account.token, false))
-- Done
mtt:Show()
end
-------------------------------------------------------------------------------
function addon:ShowMainTooltip(LDBFrame)
-- Don't display the tooltip in the middle of a fight
if self.opts.menu.disableInCombat and InCombatLockdown() then
return
end
if not mainTooltip then
mainTooltip = libQTip:Acquire(addonName .. '_MainTooltip', 2, 'LEFT', 'RIGHT')
mainTooltip:SmartAnchorTo(LDBFrame)
mainTooltip:SetAutoHideDelay(0.1, LDBFrame, function()
addon:HideMainTooltip()
end)
SkinFrame(mainTooltip)
-- Highlight the LDB icon
if self.opts.ldb.highlight then
highlightTexture:SetParent(LDBFrame)
highlightTexture:SetAllPoints(LDBFrame)
highlightTexture:Show()
end
end
self:UpdateMainTooltip()
end
-------------------------------------------------------------------------------
function addon:HideMainTooltip()
self:HideSubTooltip()
if mainTooltip then
mainTooltip:Release()
mainTooltip = nil
end
-- Hide highlighting
highlightTexture:SetParent(UIParent)
highlightTexture:Hide()
end
-------------------------------------------------------------------------------
-- Global statistics management
-------------------------------------------------------------------------------
function addon:AuditRealms()
---------------------------------------------------------------------------
-- Lists all the characters in all the realms and counts the overall wealth of each realm.
-- NB: tables are not sorted here, but when the tooltip is displayed.
---------------------------------------------------------------------------
table.wipe(sortedRealms)
table.wipe(sortedChars)
table.wipe(realmsWealths)
for key, char in pairs(self.sv.char) do
local name, realm = SplitCharKey(key)
if not sortedChars[realm] then
sortedChars[realm] = {}
realmsWealths[realm] = 0
table.insert(sortedRealms, realm)
end
table.insert(sortedChars[realm], name)
realmsWealths[realm] = realmsWealths[realm] + (char.money or 0)
end
end
-------------------------------------------------------------------------------
function addon:CheckStatsResets()
local now = date('*t')