zz_AltMail/core.lua

367 lines
10 KiB
Lua

local name, addon = ...
local parentName = addon['parentName'] or "BrokerPack"
local childName = "zz_AltMail"
local defaults = {
}
local options = {
}
local db
local realm = GetRealmName()
local function GetUName()
local name = UnitName("player")
return name.."-"..realm
end
addon.GetUName = GetUName
local itemTypes = {}
local itemSubtypes = {}
local function populate()
--[[ for k, v in pairs(_G) do
if strsub(k, 1, 12) == 'NUM_LE_ITEM_' then
print(k, v)
end
end]]
local types = {}
for i = 1, NUM_LE_ITEM_CLASSS do
local n = 1
local subclassName = ''
local className = GetItemClassInfo(i - 1)
if(className) then
itemTypes[className] = i
itemSubtypes[className] = itemSubtypes[className] or {}
types[i] = {
['name'] = className,
['subtypes'] = {}
}
while(subclassName) do
subclassName = GetItemSubClassInfo(i - 1, n - 1)
if(subclassName and strlen(subclassName) > 0) then
itemSubtypes[className][subclassName] = n
types[i]['subtypes'][n] = subclassName
n = n + 1
end
if(subclassName and strlen(subclassName) < 1) then
subclassName = nil
end
end
end
end
return types
end
local function IsSoulbound(bag, slot)
local item = ItemLocation:CreateFromBagAndSlot(bag, slot)
if(item) then
return C_Item.IsBound(item)
end
end
local function GetDestination(bag,slot)
local item = ItemLocation:CreateFromBagAndSlot(bag, slot)
if(C_Item.IsBound(item)) then return end
local quality = C_Item.GetItemQuality(item)
local itemLink = C_Item.GetItemLink(item)
-- local texture, itemCount, locked, quality, readable, lootable, itemLink, isFiltered, noValue, itemID = GetContainerItemInfo(bag, slot)
if(itemLink) then
local typ = string.match(itemLink, "^(.-):")
if(typ~="battlepet" and quality<5 and quality>0) then
local itemName, itemLink, itemRarity, itemLevel, itemMinLevel, itemType, itemSubType, itemStackCount, itemEquipLoc, itemTexture, itemSellPrice, itemClassID, itemSubClassID, bindType, expacID, itemSetID, isCraftingReagent = GetItemInfo(itemLink)
if(itemName) then
-- itemType = LibBabbleDBR[itemType]
-- itemSubType = LibBabbleDBR[itemSubType]
-- print(itemType.."-"..itemSubType.."-"..itemLink)
if(db['itemdb'][realm] and db['itemdb'][realm][itemClassID] and db['itemdb'][realm][itemClassID][itemSubClassID]) then
local tmp = db['itemdb'][realm][itemClassID][itemSubClassID]
sort(tmp, function(a,b)
return (a['order'] or 0) < (b['order'] or 0)
end)
for dest,destInfo in pairs(tmp) do
if(type(destInfo) == 'table') then
local doSkip = false
-- print(dest,destInfo['skip'])
for _,skip in ipairs(destInfo['skip']) do
if(skip == GetUName()) then doSkip=true end
end
if(not doSkip) then
return dest
end
end
end
end
end
end
end
end
local function GetNumMailboxItems()
local count = 0
for i = 1, ATTACHMENTS_MAX_SEND do
local name, texture, count, quality = GetInboxItem(i)
if(name) then
count = count + 1
end
end
return count
end
local function sout(target)
SendMail(target, "Post",nil)
SELECTED_CHAT_FRAME:AddMessage("Sending "..target)
end
local sf_new = function()
if(not SendMailFrame:IsVisible()) then return end
local a,b = 0,0
local tab = {}
while(a<=NUM_BAG_SLOTS) do
b = 1
while(b<=GetContainerNumSlots(a)) do
local _, _, _, _, _, _, itemLink = GetContainerItemInfo(a, b)
if(itemLink) then
local itemName, itemLink, _, _, _, itemType, itemSubType, _, _, _, _, classID, subclassID, _, _, _, _ = GetItemInfo(itemLink)
if(classID) then
local it = db['itemtypes'][classID + 1]['name']
if(subclassID and it) then
itemType = it
local ist = db['itemtypes'][classID + 1]['subtypes'][subclassID + 1]
if(ist) then
itemSubType = ist
tab[classID] = tab[classID] or {}
tab[classID][subclassID] = tab[classID][subclassID] or {}
tab[classID][subclassID][#tab[classID][subclassID] + 1] = {
['bag'] = a,
['slot'] = b,
}
else
print("no itemsubtype for "..itemName)
end
else
print("no itemtype for "..itemName)
end
end
end
b = b + 1
end
a = a + 1
end
local mailout = {}
db['itemdb'][realm] = db['itemdb'][realm] or {}
local realmdb = db['itemdb'][realm]
for t, v in pairs(tab) do
if(t) then
realmdb[t] = realmdb[t] or {
['Default'] = {
[GetUName()] = {
['order'] = 1,
['skip'] = {},
}
}
}
local typedb = db['itemdb'][realm][t]
for s,d in pairs(v) do
if(s) then
typedb[s] = typedb[s] or CopyTable(typedb['Default'])
local subtypedb = typedb[s]
for g,h in pairs(d) do
local target = GetDestination(tonumber(h['bag']),tonumber(h['slot']))
if(target and subtypedb[target] and subtypedb[target]['active']) then
mailout[target] = mailout[target] or {}
mailout[target][#mailout[target] + 1] = {
['bag'] = h['bag'],
['slot'] = h['slot']
}
end
end
end
end
end
end
mailout[GetUName()] = nil
for k,v in pairs(mailout) do
local c = 0
for a,b in pairs(v) do
c = c + 1
UseContainerItem(tonumber(b['bag']),tonumber(b['slot']))
if(c >= 12) then
break
end
end
if(c > 0) then
sout(k)
break
end
end
end
SendMailFrame:HookScript("OnShow", function()
C_Timer.After(0.25, sf_new)
end)
local function OnEvent(self, event, arg1, ...)
if(event == "MAIL_SEND_SUCCESS") then
C_Timer.After(1,sf_new)
end
end
local function OnText(message)
addon:OnText(childName, message)
end
local function getPref(pref)
return db[pref[#pref]]
end
local function setPref(pref,value)
db[pref[#pref]] = value
end
local function getPrefI(pref)
--[[
print("3",pref[#pref]) -- pref name
print("2",pref[#pref-1]) -- dest name
print("1",pref[#pref-2]) -- subtype name
print("0",pref[#pref-3]) -- type name
]]
if(pref[#pref-2] ~= 'Default') then
-- print("D", db['itemdb'][realm][itemTypes[pref[#pref-3]] - 1][itemSubtypes[pref[#pref-3]][pref[#pref-2]] - 1])
return db['itemdb'][realm][itemTypes[pref[#pref-3]] - 1][itemSubtypes[pref[#pref-3]][pref[#pref-2]] - 1][pref[#pref-1]][pref[#pref]]
else
return db['itemdb'][realm][itemTypes[pref[#pref-3]] - 1]['Default'][pref[#pref-1]][pref[#pref]]
end
end
local function setPrefI(pref,value)
if(pref[#pref-2] ~= 'Default') then
db['itemdb'][realm][itemTypes[pref[#pref-3]] - 1][itemSubtypes[pref[#pref-3]][pref[#pref-2]] - 1][pref[#pref-1]][pref[#pref]] = value
else
db['itemdb'][realm][itemTypes[pref[#pref-3]] - 1]['Default'][pref[#pref-1]][pref[#pref]] = value
end
end
local function getPrefDest(pref)
--[[print("3",pref[#pref]) -- pref name
print("2",pref[#pref-1]) -- subtype name
print("1",pref[#pref-2]) -- type name]]
local t = itemTypes[pref[#pref-2]] - 1
local s = pref[#pref-1]=='Default' and 'Default' or itemSubtypes[pref[#pref-2]][pref[#pref-1]] - 1
if(t and s) then
for k, v in pairs(db['itemdb'][realm][t][s]) do
if(type(v) == 'table') then
return k
end
end
end
end
local function setPrefDest(pref, value)
local old = getPrefDest(pref)
--print(old, ">",value)
--[[print("3",pref[#pref]) -- pref name
print("2",pref[#pref-1]) -- subtype name
print("1",pref[#pref-2]) -- type name]]
local t = itemTypes[pref[#pref-2]] - 1
local s = pref[#pref-1]=='Default' and 'Default' or itemSubtypes[pref[#pref-2]][pref[#pref-1]] - 1
db['itemdb'][realm][t][s][value] = db['itemdb'][realm][t][s][old]
db['itemdb'][realm][t][s][old] = nil
addon:updatePrefs()
end
local function buildSelectAll()
local sel = {}
local n,m,_
if(addon['db']['sv']['profileKeys']) then
for k,v in pairs(addon['db']['sv']['profileKeys']) do
n,_,m = strsplit(' ',k,3)
sel[format("%s-%s",n,m)] = format("%s-%s",n,m)
end
end
return sel
end
function addon:updatePrefs()
db['itemdb'] = db['itemdb'] or {}
db['itemdb'][realm] = db['itemdb'][realm] or {}
local itemdb = db['itemdb'][realm]
db['itemtypes'] = populate()
local a,b = 0,0
local curName = GetUName()
while(a<=NUM_BAG_SLOTS) do
b = 1
while(b<=GetContainerNumSlots(a)) do
local _, _, _, _, _, _, itemLink = GetContainerItemInfo(a, b)
if(itemLink) then
local _, _, _, _, _, itemType, itemSubType, _, _, _, _, classID, subclassID, _, _, _, _ = GetItemInfo(itemLink)
if(classID and subclassID) then
itemdb[classID] = itemdb[classID] or {
['name'] = itemType,
['Default'] = {
['name'] = 'Default',
[curName] = {
['order'] = 1,
['skip'] = {},
}
}
}
local classdb = itemdb[classID]
classdb['name'] = itemType
classdb[subclassID] = classdb[subclassID] or CopyTable(classdb['Default'])
classdb[subclassID]['name'] = itemSubType
end
end
b = b + 1
end
a = a + 1
end
local p = addon['options']['args']['Types']['args']
for classID, v in pairs(db['itemdb'][realm]) do
if(v['name']) then
p[v['name']] = {
['type'] = 'group',
['childGroups'] = 'tree',
['name'] = v['name'],
['args'] = {},
}
for subclassID, b in pairs(v) do
if(type(b) == 'table' and b['name']) then
p[v['name']]['args'][b['name']]={
['type'] = 'group',
['name'] = b['name'],
['get'] = getPrefI,
['set'] = setPrefI,
['args'] = {},
}
local c = p[v['name']]['args'][b['name']]['args']
c['dest'] = {
['name'] = "Destination",
['type'] = "select",
['values'] = buildSelectAll,
['get'] = getPrefDest,
['set'] = setPrefDest
}
for d,e in pairs(b) do
if(d ~= 'name') then
c[d] = {
['name'] = d,
['type'] = "group",
['args'] = {},
}
local f = c[d]['args']
f['active'] = {
['name'] = "active",
['type'] = "toggle",
}
end
end
end
end
end
end
end
local function init()
realm = GetRealmName()
options = addon:InitConfig(childName, true, {
['type'] = "launcher",
}, getPref, setPref)
db = addon['db']['profile'][childName]
db['itemdb'] = db['itemdb'] or {}
db['itemdb'][realm] = db['itemdb'][realm] or {}
addon:AddConfigMenu({
['name'] = "Types",
['order'] = 2,
['childGroups'] = 'tree',
['menuGet'] = getPref,
['menuSet'] = setPref,
}, addon['options'])
addon:updatePrefs()
end
addon:startup(name, childName, init, true, defaults)
addon:RegisterFunc({'MAIL_SEND_SUCCESS'},"OnEvent", OnEvent)