forked from alarofrunetotem/MailCommander
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MailCommander.lua
3339 lines (3299 loc) · 114 KB
/
MailCommander.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 __FILE__ = tostring(debugstack(1, 2, 0):match("(.*):1:")) -- Always check line number in regexp and file
local me, ns = ...
local pp = print
--[===[@debug@
--Postal_BlackBookButton
-- SendMailNameEditBox
LoadAddOn("Blizzard_DebugTools")
LoadAddOn("LibDebug")
if LibDebug then LibDebug() end
local print=_G.LibDebug and print or function(...) print("MCom",...) end
--@end-debug@]===]
-- @non-debug@
local print = function()
end
local DevTools_Dump = function()
end
-- @end-non-debug@
local addon -- #MailCommander
local LibInit, minor = LibStub("LibInit", true)
assert(LibInit, me .. ": Missing LibInit, please reinstall")
addon = LibStub("LibInit"):NewAddon(ns, me, {
noswitch = false,
profile = true,
enhancedProfile = true
}, "AceHook-3.0", "AceEvent-3.0", "AceTimer-3.0", "AceBucket-3.0")
--[===[@debug@
addon.debug=true
addon:Debug("Started with debug enabled")
--@end-debug@]===]
local C = addon:GetColorTable()
local L = addon:GetLocale()
local I = LibStub("LibItemUpgradeInfo-1.0")
local math = math
local tContains = tContains
local toc = select(4, GetBuildInfo())
local allFactions
local allRealms
local db
local dbcategory
local legacy
local maxLevel
local currentID
local bagCache = {}
local fullyEnabled
local pseudolink = "|cffffd200|Hitem:%s:0:0:0:0:0:0:0:80:0|h[%s]|h|r"
local QUESTIONMARK_ICON = "Interface\\ICONS\\inv_misc_questionmark"
local NONAME = 'NONE'
local KCAP = 999
local STARCAP = 9999
local CAP = 999999
local MERCHANT_STOCK = MERCHANT_STOCK:gsub('%%d', '%%s')
local MONEY = MONEY
local ITEM_BNETACCOUNTBOUND = ITEM_BNETACCOUNTBOUND
local toc = select(4, GetBuildInfo())
local ISCLASSIC = toc < 90000
local NUM_BAG_SLOTS = NUM_TOTAL_BAG_FRAMES
local function keep(toon, id)
if not toon then
return 0
end
return (legacy and db.keep[toon][id] or db.toons[toon].keep[id]) or 0
end
local function cap(toon, id)
if not toon then
return CAP
end
return (legacy and db.cap[toon][id] or db.toons[toon].cap[id]) or CAP
end
local function stock(toon, id)
if not toon then
return 0
end
return (legacy and db.stock[toon][id] or db.toons[toon].stock[id]) or 0
end
local dbDefaults = {
global = {
toons = {
['*'] = {} -- char name plus realm
},
requests = { -- what this toon need: requests[toon]{itemdata(table)}
['**'] = {}
},
disabled = {
['*'] = { -- itemId
['*'] = { -- sender
-- receiver toon
}
}
},
cap = { -- This receeiver will not be allowed to have more than "cap" items
['*'] = { -- char name plus realm
-- itemId = number
}
},
keep = { -- This toon will keep at least "keep" items
['*'] = { -- char name plus realm
-- itemId = number
['*'] = 0
}
},
stock = { -- Storage data
['*'] = { -- char name plus realm
-- itemId = number
['*'] = 0
}
},
categories = {},
updateStock = {},
ignored = {},
lastReceiver = NONAME
}
}
local toonMeta = {
__index = function(t, k)
if (k == "keep") then
t.keep = {}
return t.keep
end
if (k == "requests") then
t.requests = {}
return t.requests
end
if (k == "cap") then
t.cap = {}
return t.cap
end
if (k == "keep") then
t.keep = {}
return t.keep
end
end
}
-- locals
local mailRecipient
local slots = 16
local mcf
local INEED = 1
local ISEND = 2
local IFILTER = 3
local ICATEGORIES = 4
local currentRequester
local currentReceiver
local currentCategory
local lastReceiver
local thisFaction
local thisRealm
local thisToon = NONE
local realmkey
local currentTab = 0
local dirty = true
local shouldsend
local oldshouldsend
local DontSendNow = {}
local sendable = {} -- For each toon, it's true if the current one has at least one object to send
local toonTable = {} -- precaculated toon table for initDropDown to avoid bursting memory
local toonIndex = {}
local presets = {}
local i2s = {
__index = function(table, key)
return rawget(table, tostring(key))
end,
__newindex = function(table, key, value)
return rawset(table, tostring(key), value)
end
}
local tobesent = setmetatable({}, i2s)
local sending = setmetatable({}, {
__index = function(table, key)
if type(key) == "string" and type(presets[key]) == "table" then
if key == "gold" then
return tonumber(SendMailMoneyGold:GetText()) or 0
end
local list = presets[key].list
if type(list) == "table" then
local c = 0
for k, v in pairs(table) do
if tContains(list, k) then
c = c + v
end
end
return c
end
end
return 0
end
})
local bags = setmetatable({}, {
__index = function()
return 0
end
})
local function parseLink(itemLink)
if (type(itemLink) == 'number') then
return itemLink
elseif (type(itemLink) == "string") then
local id = GetItemInfoFromHyperlink(itemLink)
if id and id > 0 then
return id
end
return itemLink:match("|Hitem:(.-):")
end
end
local tostring = tostring
local bit = bit
local function Bags()
local bags = {}
local bag = 0
local slot = 0
return function()
if not bags[bag] then
bags[bag] = C_Container.GetContainerNumSlots(bag)
end
slot = slot + 1
if slot > bags[bag] then
slot = 1
bag = bag + 1
end
if bag <= NUM_BAG_SLOTS then
return bag, slot
end
end
end
local function GetContainerItemInfo(bagId, slotId)
local t = C_Container.GetContainerItemInfo(bagId, slotId)
return t.stackCount, t.isLocked
end
local function currentToon()
return currentTab == INEED and currentRequester or currentReceiver
end
-- Counter object
local Count = {
cache = {},
samefaction = setmetatable({}, {
__index = function(table, key)
if type(key) == "number" then
error("Non doveva essere un numero")
end
table[key] = toonTable[key] or false
return table[key]
end
}),
connectedRealm = setmetatable({}, {
__index = function(table, key)
table[key] = toonTable[key] and realmkey:find(toonTable[key].realm or NONE, 1, true) or false
return table[key]
end
})
} -- #Count
function Count:Sending(id, toon)
return sending[id]
end
function Count:CanSendMail(toon)
return toon and self.samefaction[toon] and thisRealm:find(db.toons[toon].realm or NONE, 1, true)
end
function Count:Total(id, toon, bank)
if not toon then
toon = currentToon()
end
if type(id) == "string" and presets[id] then
return presets[id]:count(id, toon, bank) or 0
else
return GetItemCount(id, bank) - bags[id] or 0
end
end
function Count:Reserved(id)
if id == "boe" then
return 0
else
return self:Keep(id, thisToon)
end
end
function Count:Keep(id, toon)
if not toon then
toon = currentToon()
end
if id == "boe" then
return 0
else
return keep(toon, id)
end
end
function Count:Cap(id, toon)
if not toon then
toon = currentToon()
end
return cap(toon, id)
end
function Count:Stock(id, toon)
if not toon then
toon = currentToon()
end
if type(presets[id].stock) == "function" then
return presets[id]:stock(id, toon)
else
return stock(toon, id)
end
end
function Count:Sendable(id, toon)
local testid = -1
if id == testid then
DevTools_Dump {"Sendable", db.items[id]}
end
if not toon then
toon = currentToon()
end
if not Count:CanSendMail(toon) then
local boa = (id == 'boatoken') or (db.items[id].boa)
if not boa then
return 0
end
end
local totalWB = Count:Total(id, toon, true)
local total = Count:Total(id, toon)
local reserved = Count:Reserved(id)
local sending = Count:Sending(id)
local cap = Count:Cap(id, toon)
if id == testid then
print('totalwithbank', totalWB)
addon:Debug('total', total)
addon:Debug('reserved', reserved)
addon:Debug('sending', sending)
addon:Debug('cap', cap)
end
return math.min(totalWB - reserved + sending, math.min(total, cap))
end
function Count:IsSendable(id, idInBag, toon, bagId, slotId)
if not toon then
toon = currentToon()
end
if presets[id] and type(presets[id].validate) == "function" then
return presets[id]:validate(idInBag, toon, bagId, slotId)
else
local boa = I:IsBoa(GetContainerItemLink(bagId, slotId))
if not Count:CanSendMail(toon) and not boa then
return false
end
return id == idInBag
end
end
local function SendGold()
local toon = currentToon()
if toon and toon ~= NONAME then
if legacy then
for _, d in ipairs(db.requests[toon]) do
if d.i == 'gold' then
local g = Count:Sendable('gold')
if g > 0 then
SendMailMoneyGold:SetText(g)
break
end
end
end
else
for id, _ in pairs(db.toons[toon].requests) do
if id == 'gold' then
local g = Count:Sendable('gold')
if g > 0 then
SendMailMoneyGold:SetText(g)
break
end
end
end
end
end
end
local function nop(rc)
return rc
end
local function CountGroup(name)
local group = name
if type(group) == "string" then
group = presets[group]
end
local list = group.list
if type(list) == "function" then
return list()
elseif type(list) == "table" then
local c = 0
for k, v in pairs(list) do
local id = type(v) == "number" and v or k
local t = Count:Total(id)
if t > 0 then
c = c + t
end
end
return c
elseif type(list) == "number" then
return list
end
return 0
end
local function getProperty(key, toon, itemId, default)
if legacy then
return db.toons[toon][key][itemId] or default
else
local rc, val = pcall(function(toon, itemId)
return db[key][toon][itemId]
end)
if rc then
return val or default
else
return default
end
end
end
local basepresets = { -- #basepresets
boatoken = {
t = "Interface/ICONS/INV_Guild_Standard_Alliance_C",
i = 'boatoken',
l = pseudolink:format('boatoken', ITEM_BNETACCOUNTBOUND),
count = function(self, dummy, toon, bank)
local c = 0
for _, id in ipairs(self.list) do
if presets.boatoken:validate(id, toon) then
c = c + Count:Total(id, thisToon, bank)
end
end
return c
end,
validate = function(self, bagItemId, toon, bagId, slotId)
if db.toons[toon] then
local toonClass = db.toons[toon].class
local itemMask = ns.classBoa[tostring(bagItemId)] or 0
local toonMask = ns.classes[toonClass] and ns.classes[toonClass].mask or 0
return bit.band(toonMask, itemMask) > 0
end
return false
end,
list = {},
nosplit = true
},
gold = {
t = "Interface/ICONS/INV_Misc_Coin_01",
i = 'gold',
l = pseudolink:format('gold', MONEY),
count = function()
return math.floor(GetMoney() / 10000)
end
},
trainingstones = {
t = "Interface\\ICONS\\Icon_UpgradeStone_legendary",
i = 116429,
l = pseudolink:format('trainingstones', L["Battle-Training Stone"]),
list = ns.trainingstones,
nosplit = true,
validate = function(self, bagItemId, toon)
return tContains(self.list, bagItemId)
end
-- ]]
},
battlestones = {
t = "INTERFACE\\ICONS\\Icon_UpgradeStone_Rare",
l = pseudolink:format('battlestones', L["Battle-Stone"]),
i = 98715,
list = ns.battlestones,
nosplit = true,
validate = function(self, bagItemId, toon)
return tContains(self.list, bagItemId)
end
},
boe = {
t = "INTERFACE\\ICONS\\INV_Sword_39",
l = pseudolink:format("boe", ITEM_BIND_ON_EQUIP),
count = function(self, key, toon)
local count = 0
for bag, slot in Bags() do
if self:validate(nil, toon, bag, slot) then
count = count + 1
end
end
return count
end,
validate = function(self, _, toon, bag, slot)
local itemlink = C_Container.GetContainerItemLink(bag, slot)
if not itemlink then
return false
end
local id = parseLink(itemlink)
if not id then
self:Print(C("Invalid item ", "Orange"), itemlink)
return false
end
if not IsEquippableItem(itemlink) then
return false
end
local rc, alreadybound = pcall(C_Item.IsBound, ItemLocation:CreateFromBagAndSlot(bag, slot))
if alreadybound then
return false
end
local itemType, itemSubType = select(6, GetItemInfoInstant(id))
if itemType ~= Enum.ItemClass.Weapon and itemType ~= Enum.ItemClass.Armor then
return false
end
local min = keep(toon, "boe")
local max = cap(toon, "boe")
local level = GetDetailedItemLevelInfo(itemlink)
-- DEFAULT_CHAT_FRAME:AddMessage("====================")
-- DEFAULT_CHAT_FRAME:AddMessage("Toon " .. toon)
-- DEFAULT_CHAT_FRAME:AddMessage("Item " .. itemlink)
-- DEFAULT_CHAT_FRAME:AddMessage("id " .. id)
-- DEFAULT_CHAT_FRAME:AddMessage("IsEquippableItem " .. tostring(IsEquippableItem(itemlink)))
-- DEFAULT_CHAT_FRAME:AddMessage("alreadybound " .. tostring(alreadybound))
-- DEFAULT_CHAT_FRAME:AddMessage("itemType " .. itemType)
-- DEFAULT_CHAT_FRAME:AddMessage("Enum.ItemClass.Weapon " .. Enum.ItemClass.Weapon .. " " .. tostring(itemType ~= Enum.ItemClass.Weapon))
-- DEFAULT_CHAT_FRAME:AddMessage("Enum.ItemClass.Armor " .. Enum.ItemClass.Armor .. " " .. tostring(itemType ~= Enum.ItemClass.Armor))
-- DEFAULT_CHAT_FRAME:AddMessage("min " .. min)
-- DEFAULT_CHAT_FRAME:AddMessage("level " .. level)
-- DEFAULT_CHAT_FRAME:AddMessage("max " .. max)
return level >= min and level <= max
end,
res = false,
cap = L['Maximum Level'],
keep = L['Minimum Level']
}
}
local fake = {
count = false,
f = false,
validate = false,
res = true,
cap = L["Maximum Storage"],
keep = L["Minimum Storage"]
}
setmetatable(basepresets, {
__index = function(t, k)
return fake
end
})
_G.MC = basepresets
for k, _ in pairs(ns.classBoa) do
if tonumber(k) then
tinsert(basepresets.boatoken.list, tonumber(k))
end
end
-- upvalues
local SetItemButtonTexture, UIDropDownMenu_AddButton = SetItemButtonTexture, UIDropDownMenu_AddButton
local GetProfessions, GetUnitName, UnitClass, GetProfessionInfo, UnitLevel = GetProfessions, GetUnitName, UnitClass, GetProfessionInfo, UnitLevel
local ClearCursor, CreateFrame, print, GetCursorInfo = ClearCursor, CreateFrame, print, GetCursorInfo
local ButtonFrameTemplate_HidePortrait, SendMailFrame, UIParent, InboxFrame = ButtonFrameTemplate_HidePortrait, SendMailFrame, UIParent, InboxFrame
local UIDropDownMenu_CreateInfo, UIDropDownMenu_SetWidth = UIDropDownMenu_CreateInfo, UIDropDownMenu_SetWidth
local UIDropDownMenu_Initialize, UIDropDownMenu_SetText = UIDropDownMenu_Initialize, UIDropDownMenu_SetText
local PanelTemplates_SetNumTabs, PanelTemplates_SetTab = PanelTemplates_SetNumTabs, PanelTemplates_SetTab
local SetItemButtonDesaturated, SetItemButtonCount, SetItemButtonStock = SetItemButtonDesaturated, SetItemButtonCount, SetItemButtonStock
local MailFrame = MailFrame
local wipe, tinsert, pairs, ipairs, strcmputf8i = wipe, tinsert, pairs, ipairs, strcmputf8i
local PanelTemplates_DisableTab, PanelTemplates_EnableTab = PanelTemplates_DisableTab, PanelTemplates_EnableTab
local minibag = "Interface\\PaperDollInfoFrame\\UI-GearManager-ItemIntoBag"
local undo = "Interface\\PaperDollInfoFrame\\UI-GearManager-Undo"
local ignore = "Interface\\PaperDollInfoFrame\\UI-GearManager-LeaveItem-Opaque"
local ignore2 = "Interface\\PaperDollInfoFrame\\UI-GearManager-LeaveItem-Transparent"
local KEY_BUTTON1 = "\124TInterface\\TutorialFrame\\UI-Tutorial-Frame:12:12:0:0:512:512:10:65:228:283\124t" -- left mouse button
local KEY_BUTTON2 = "\124TInterface\\TutorialFrame\\UI-Tutorial-Frame:12:12:0:0:512:512:10:65:330:385\124t" -- right mouse button
-- local HELP_ICON = "\124TInterface\AddOns\MailCommander\helpItems.tga:256:64\124t"
local HELP_ICON = "\124TInterface\\AddOns\\MailCommander\\helpItems.tga:64:256\124t"
local CTRL_KEY_TEXT, SHIFT_KEY_TEXT = CTRL_KEY_TEXT, SHIFT_KEY_TEXT
local FILTER, SEND, NEED, CATEGORIES, CATEGORY, MISSING = FILTER, SEND_LABEL, NEED, CATEGORIES, CATEGORY, ADDON_MISSING
local kpairs = addon:getKpairs()
local GameTooltip = CreateFrame("GameTooltip", "MailCommanderTooltip", UIParent, "GameTooltipTemplate")
local function checkBags()
wipe(bags)
for i = 1, NUM_BAG_SLOTS do
local item = GetInventoryItemID("PLAYER", CONTAINER_BAG_OFFSET + i)
if item then
bags[item] = bags[item] + 1
end
end
end
local ldb
local icon
function addon:InitLdb()
-- ldb extension
local LDB = LibStub:GetLibrary("LibDataBroker-1.1", true)
icon = LibStub("LibDBIcon-1.0", true)
local fakeLdb = {
type = "data source",
label = me,
text = L["Nothing to send"],
category = "Interface",
icon = "Interface\\MailFrame\\Mail-Icon",
iconR = 1,
iconG = 1,
iconB = 1
}
ldb = LDB:NewDataObject(me, fakeLdb) -- #ldb
ldb.Update = function(self)
if oldshouldsend ~= shouldsend then
ldb.text = shouldsend and C(L["You have items to send"], "GREEN") or C(L["Nothing to send"], "SILVER")
local button = icon:GetMinimapButton(me)
if not button then
return
end
if shouldsend then
button.icon:SetVertexColor(0, 1, 0)
else
button.icon:SetVertexColor(1, 1, 1)
end
end
end
ldb.OnClick = function(self, button)
if button == "RightButton" then
addon:Gui()
return
end
if mcf:IsVisible() then
HideUIPanel(mcf)
else
addon:InitData()
addon:OpenConfig()
end
end
ldb.OnTooltipShow = function(self, ...)
if not shouldsend then
self:AddLine(L["Nothing to send"], C:Silver())
else
self:AddLine(L["Items available for:"], C:Green())
for name, data in pairs(db.toons) do
if sendable[name] and name ~= thisToon and toonTable[name] then
self:AddLine(toonTable[name].text)
if legacy then
for _, d in pairs(db.requests[name]) do
local c = Count:Sendable(d.i, name)
if c and c > 0 then
self:AddDoubleLine(" " .. d.l, c, nil, nil, nil, C:Green())
end
end
else
for itemId, data in pairs(db.toons[name].requests) do
local c = Count:Sendable(itemId, name)
if c and c > 0 then
self:AddDoubleLine(" " .. db.items[itemId].l, c, nil, nil, nil, C:Green())
end
end
end
end
end
end
self:AddDoubleLine(KEY_BUTTON1, L['Open requester'], nil, nil, nil, C:Green())
self:AddDoubleLine(KEY_BUTTON2, L['Open configuration'], nil, nil, nil, C:Green())
if thisFaction == "Neutral" then
self:AddLine(L["ATTENTION: Neutral characters cant use mail"], C:Orange())
end
end
if icon then
icon:Register(me, ldb, self.db.profile.ldb)
end
end
local function SetItemCounts(frame, cap, keep, stock, total)
if not frame then
return
end
local button = frame.ItemButton
if not button then
return
end
if type(cap) == "boolean" then
frame.Cap:Hide()
frame.Keep:Hide()
button.Stock:Hide()
button.ModifiedStock:Hide()
return
end
if not cap or cap == 0 then
cap = CAP
end
if cap == CAP then
cap = UNLIMITED
elseif cap > KCAP then
cap = math.floor(cap / 1000) .. 'K'
end
if keep then
if keep > KCAP then
keep = math.floor(keep / 1000) .. 'K'
end
else
keep = 0
end
frame.Cap:SetFormattedText("Max:%s", cap)
frame.Cap:SetWidth(60)
frame.Cap:Show()
if not keep then
keep = 0
end
-- frame.Keep:SetFormattedText(MERCHANT_STOCK, keep)
frame.Keep:SetFormattedText("Min:%s", keep)
frame.Keep:SetWidth(60)
frame.Keep:Show()
if stock and stock > -1 then
total = total or stock
button.ModifiedStock:SetText(stock > STARCAP and '*' or stock)
button.Stock:SetFormattedText(MERCHANT_STOCK, total > STARCAP and '*' or total)
button.Stock:Show()
button.ModifiedStock:Show()
else
button.Stock:Hide()
button.ModifiedStock:Hide()
end
end
function addon:BAG_UPDATE_DELAYED(event, ...)
-- self:Debug(event)
self:InitData()
addon:RefreshSendable()
if mcf:IsVisible() then
self:UpdateMailCommanderFrame()
end
-- self:UpdateMailCommanderFrame()
end
function addon:LOOT_OPENED(event, ...)
print(event, ...)
end
function addon:CHAT_MSG_LOOT(event, ...)
print(event, ...)
end
function addon:CHAT_MSG_CURRENCY(event, ...)
print(event, ...)
end
function addon:LOOT_CLOSED(event, ...)
print(event, ...)
end
function addon:PLAYER_MONEY(event, ...)
print(event, ...)
if mcf:IsVisible() then
self:UpdateMailCommanderFrame()
end
end
function addon:SetDbDefaults(default)
pp("Db defaults set")
default.global = {
categories = {},
items = {
['*'] = {
l = pseudolink:format(MISSING, MISSING),
t = QUESTIONMARK_ICON,
boa = false,
boe = false,
bop = false
}
},
toons = {
['*'] = {
requests = {
['*'] = false
},
cap = {
['*'] = CAP
},
keep = {
['*'] = 0
},
stock = {
['*'] = 0
},
disabled = false
}
}
}
default.profile.ldb = {
hide = false
}
return true
end
function addon:IsDisabled(itemid, to, from)
if not itemid then
return false
end
if currentTab == ICATEGORIES then
return false
end
if not from then
from = thisToon
end
if not to then
to = currentTab == INEED and currentRequester or currentReceiver
end
-- self:Debug("IsDisabled",itemid,from,to,currentRequester,currentReceiver)
local disabled = db.toons[to].requests[itemid]
if type(disabled) ~= "table" then
db.toons[to].requests[itemid] = {}
return false, false, false
else
return disabled['ALL'] or disabled[from] or DontSendNow[itemid], disabled['ALL'], disabled[from]
end
return false
end
function addon:IsIgnored(toon, ignorelevel)
if not toon then
return false
end
if toon == thisToon then
return false
end
return db.toons[toon].ignored or (not ignorelevel and self:GetNumber("MINLEVEL") > toonTable[toon].level)
end
function addon:ToggleIgnored(toon)
if not toon then
return false
end
if toon == thisToon then
return false
end
db.toons[toon].ignored = not db.toons[toon].ignored
end
function addon:AddButton(i, data, section)
if i < 1 then
return 1
end
local hide = type(data) == 'boolean' and not data
if not mcf.Items[i] then
if hide then
return 1
end
mcf.Items[i] = CreateFrame("Frame", nil, mcf, "MailCommanderItemTemplate")
end
local frame = mcf.Items[i]
if hide then
frame:Hide()
return 1
end
if (i % 2) == 0 then -- even
frame:SetPoint("TOPLEFT", mcf.Items[i - 1], "TOPRIGHT", 10, 0)
elseif i > 1 then -- odd
frame:SetPoint("TOPLEFT", mcf.Items[i - 2], "BOTTOMLEFT", 0, 20)
-- else -- first one
-- frame:SetPoint("TOPLEFT",mcf,"TOPLEFT",10,-60)
end
frame.ItemButton:SetAttribute("section", section)
if section == "items" then
local id = data
data = db.items[id]
frame.ItemButton.MailCommanderDragTarget = true
if type(data) == 'table' then
frame.ItemButton:SetAttribute("itemlink", data.l)
SetItemButtonTexture(frame.ItemButton, data.t)
frame.Name:SetText(data.l:gsub('[%]%[]', ''))
if addon:IsDisabled(id) then
frame.ItemButton.Disabled:Show()
else
frame.ItemButton.Disabled:Hide()
end
addon:SetLimit(id)
local toon = currentToon()
local totalcount = Count:Total(id, toon, true)
local cap = Count:Cap(id, toon)
local keep = currentTab == ISEND and Count:Reserved(id) or Count:Keep(id, toon)
local sending = Count:Sending(id, toon)
local count = totalcount - keep - sending
if tobesent[id] then
count = tobesent[id] - sending
end
if cap and count > cap then
count = cap
elseif count < 0 then
count = 0
end
local count = math.min(count, Count:Total(id, toon)) -- count can never be more than the actual quantity in bags
SetItemCounts(frame, cap, keep, count, totalcount)
SetItemButtonDesaturated(frame.ItemButton, count and count < 1 and currentTab == ISEND)
else
frame.ItemButton:SetAttribute("itemlink", nil)
SetItemButtonTexture(frame.ItemButton, nil)
SetItemButtonDesaturated(frame.ItemButton, false)
frame.ItemButton.Disabled:Hide()
SetItemCounts(frame, false)
if type(data) == 'nil' then
frame.Name:SetText(L["Drag here to add an item"])
frame.ItemButton.MailCommanderDragTarget = true
else
frame:Hide()
return 1
end
end
elseif section == "toons" then
local name = data
data = toonTable[name]
frame.ItemButton.MailCommanderDragTarget = false
frame.ItemButton:SetAttribute('toon', name)
SetItemButtonDesaturated(frame.ItemButton, false)
if self:IsIgnored(name, true) then
frame.ItemButton.Disabled:Show()
else
frame.ItemButton.Disabled:Hide()
end
frame.Name:SetText(data.text)
SetItemButtonTexture(frame.ItemButton, "Interface\\ICONS\\ClassIcon_" .. data.class)
SetItemCounts(frame, false)
frame:Show()
return 1
end
frame:Show()
return 1
end
function addon:CloseTip()
if _G.GameTooltip then
_G.GameTooltip:Hide()
end
if GameTooltip then
GameTooltip:Hide()
end
end
function addon:loadSelf(level)
local p1, p2 = ISCLASSIC and nil, nil or GetProfessions()
thisFaction = UnitFactionGroup("player")
thisRealm = GetRealmName()
thisToon = GetUnitName("player") .. '-' .. thisRealm
ns.localizedClass, ns.class = UnitClass("player")
if p1 then
local name, icon, level = GetProfessionInfo(p1)
db.toons[thisToon].p1 = name .. "(" .. level .. ")\n"
end
if p2 then
local name, icon, level = GetProfessionInfo(p2)
db.toons[thisToon].p2 = name .. "(" .. level .. ")\n"
end
db.toons[thisToon].localizedClass = ns.localizedClass
db.toons[thisToon].level = level or UnitLevel("player")
db.toons[thisToon].class = ns.class
db.toons[thisToon].faction = thisFaction
db.toons[thisToon].realm = thisRealm
db.toons[thisToon].updated = date("%Y-%m-%d %H:%M:%S", time())
end
local function mkkey(realm, faction, name)
local prefix = 9
if not realm then
addon:Debug("realm is nuil for ", name)
end
if realm == thisRealm then
prefix = 0
elseif realmkey:find(realm or NONE, 1, true) then
prefix = 5
end
local r, k = pcall(strconcat, prefix, realm, faction == thisFaction and ' ' or faction, name)
return strlower(k)
end
local function toonSort(a, b)
local k1 = mkkey(toonTable[a].realm, toonTable[a].faction, a)
local k2 = mkkey(toonTable[b].realm, toonTable[b].faction, b)
return strcmputf8i(k1, k2) < 0
end
function addon:loadToonList()
self:Debug("LoadToonList")
wipe(toonTable)
wipe(toonIndex)
allRealms = self:GetBoolean('ALLREALMS')
allFactions = self:GetBoolean('ALLFACTIONS')
for name, data in pairs(db.toons) do
if name:find('-') then
if allRealms or realmkey:find(data.realm or NONE, 1, true) then
if allFactions or not data.faction or data.faction == thisFaction then
data.class = data.class or UNKNOWN
data.localizedClass = data.localizedClass or UNKNOWN
data.level = data.level or maxLevel
local classcolor = C_ClassColor.GetClassColor(data.class)
if classcolor then
classcolor = classcolor:GenerateHexColorMarkup()
else
classcolor = "|cff" .. C.Gray
end
local pattern = "%s%s (%s %d)|r"
toonTable[name] = {
-- text=data.class and format("|c%s%s (%s %d)|r",_G.RAID_CLASS_COLORS[data.class].colorStr,name,data.localizedClass,data.level) or name,
text = pattern:format(classcolor, name, data.localizedClass, data.level),
tooltip = (data.p1 and data.p1 .. "\n" or "") .. (data.p2 and data.p2 .. "\n" or ""),
realm = data.realm,
level = data.level,
class = data.class,
faction = data.faction
}
data.text = toonTable[name].text
tinsert(toonIndex, name)
end
end
else
addon:Debug("Found and ignore toon with no realm: " .. name)
db.toons[name] = nil
end
end
-- db.toonTable=nil
table.sort(toonIndex, toonSort)
db.toonIndex = toonIndex
end
function addon:InitData()