171 lines
4.6 KiB
Lua
Executable File
171 lines
4.6 KiB
Lua
Executable File
local addonName, addon = ...
|
|
local childName = addon['childName']
|
|
local child = addon[childName]
|
|
local modulname = 'bags'
|
|
local events = {}
|
|
local modul = {
|
|
['frame'] = CreateFrame("FRAME"),
|
|
['name'] = modulname
|
|
}
|
|
local db
|
|
local desc = format('%sCount', modulname)
|
|
local GetContainerItemInfo = GetContainerItemInfo
|
|
if(C_Container and C_Container.GetContainerItemInfo) then
|
|
GetContainerItemInfo2 = C_Container.GetContainerItemInfo
|
|
else
|
|
GetContainerItemInfo2 = function(bag, slot)
|
|
local icon, itemCount, locked, quality, readable, lootable, itemLink, isFiltered, noValue, itemID, isBound = GetContainerItemInfo(bag, slot)
|
|
if(icon) then
|
|
return {
|
|
['iconFileID'] = icon,
|
|
['stackCount'] = itemCount,
|
|
['isLocked'] = locked,
|
|
['quality'] = quality,
|
|
['isReadable'] = readable,
|
|
['hasLoot'] = lootable,
|
|
['hyperlink'] = itemLink,
|
|
['isFiltered'] = isFiltered,
|
|
['hasNoValue'] = noValue,
|
|
['itemID'] = itemID,
|
|
['isBound'] = isBound
|
|
}
|
|
end
|
|
end
|
|
end
|
|
|
|
local function GetContainerNumSlots2(bag, slot)
|
|
if(C_Container and C_Container.GetContainerNumSlots) then
|
|
return C_Container.GetContainerNumSlots(bag, slot)
|
|
end
|
|
return GetContainerNumSlots(bag, slot)
|
|
end
|
|
function child:scanBag(bag)
|
|
local slot = 1
|
|
local itemlist = {}
|
|
local empty = true
|
|
while(slot<=GetContainerNumSlots2(bag)) do
|
|
local item = Item:CreateFromBagAndSlot(bag, slot)
|
|
if(item) then
|
|
local itemInfo = GetContainerItemInfo2(bag, slot)
|
|
local id = item:GetItemID()
|
|
if(itemInfo and id) then
|
|
empty = false
|
|
itemlist[id] = itemlist[id] or {}
|
|
itemlist[id]['count'] = itemlist[id]['count'] or 0
|
|
itemlist[id]['count'] = itemlist[id]['count'] + itemInfo['stackCount']
|
|
child:getName(id)
|
|
end
|
|
end
|
|
slot = slot + 1
|
|
end
|
|
return empty and nil or itemlist
|
|
end
|
|
local startbag, endbag
|
|
if(Enum and Enum.BagIndex) then
|
|
startbag = Enum.BagIndex.Backpack
|
|
endbag = Enum.BagIndex.ReagentBag
|
|
else
|
|
startbag = BACKPACK_CONTAINER
|
|
endbag = NUM_BAG_SLOTS
|
|
end
|
|
local function scanAllBags(delayed)
|
|
if(not child:funcSync(scanAllBags, delayed)) then return end
|
|
local playerID = child.getPID()
|
|
if(playerID) then
|
|
local tab = modul['db'][playerID]
|
|
print()
|
|
for bag=startbag ,endbag do
|
|
tab[bag] = child:scanBag(bag) -- loop all bought bags
|
|
end
|
|
for k,v in pairs(modul['db'][playerID]) do
|
|
if(k < startbag or k > endbag) then
|
|
tab[k] = nil -- prevent bogus bags
|
|
end
|
|
end
|
|
end
|
|
end
|
|
local function bagLoop(id, res)
|
|
local units = child:GetNamespace('units')
|
|
local playerID = child.getPID()
|
|
for k, v in pairs(modul['db']) do
|
|
if(units[k]) then
|
|
if(playerID and units[k]['faction'] == units[playerID]['faction']) then
|
|
local unitname = units[k]['name']
|
|
for bag, list in pairs(v) do
|
|
for a, b in pairs(list) do
|
|
if(a == id) then
|
|
res[unitname] = res[unitname] or {
|
|
[desc] = 0
|
|
}
|
|
res[unitname][desc] = res[unitname][desc] or 0
|
|
res[unitname][desc] = res[unitname][desc] + b['count']
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
return res
|
|
end
|
|
function modul.Search(id, res)
|
|
local playerID = child.getPID()
|
|
if(playerID) then
|
|
modul['db'][playerID] = modul['db'][playerID] or {}
|
|
end
|
|
return playerID and bagLoop(id, res) or res
|
|
end
|
|
function modul.Output(input, sum, b)
|
|
if(b[desc] and b[desc] > 0) then
|
|
input[#input+1] = format("Bags %s",b[desc])
|
|
sum = sum + b[desc]
|
|
end
|
|
return input, sum
|
|
end
|
|
function events:ITEM_UNLOCKED(event,...)
|
|
scanAllBags(nil)
|
|
end
|
|
function events:BAG_UPDATE_DELAYED(event,...)
|
|
scanAllBags(nil)
|
|
end
|
|
local function OnEvent(self, event, ...)
|
|
local playerID = child.getPID()
|
|
-- print(modulname, event, ...)
|
|
if(playerID and type(events[event])=='function') then
|
|
modul['db'] = child:GetNamespace(modul.name)
|
|
modul['db'][playerID] = modul['db'][playerID] or {}
|
|
events[event](self, event, ...)
|
|
end
|
|
end
|
|
function modul:Delete(id, typ)
|
|
self['db'] = child:GetNamespace(modul.name)
|
|
if(typ == self['typ'] and id and id>0) then
|
|
self['db'][id] = nil
|
|
end
|
|
end
|
|
function modul:Enable()
|
|
self['status'] = true
|
|
local list = {}
|
|
for event,func in pairs(events) do
|
|
list[#list+1] = event
|
|
end
|
|
for i = 1, #list do
|
|
self['frame']:RegisterEvent(list[i])
|
|
end
|
|
self['frame']:SetScript("OnEvent", OnEvent)
|
|
end
|
|
function modul:Disable()
|
|
self['status'] = false
|
|
for event, func in pairs(events) do
|
|
self['frame']:UnregisterEvent(event)
|
|
end
|
|
end
|
|
local init = {
|
|
Setup = function(self)
|
|
db = child['db']
|
|
modul.db,modul.status = child:RegisterModul(modul, modulname)
|
|
if(modul.status) then
|
|
modul:Enable()
|
|
end
|
|
end
|
|
}
|
|
addon.RegisterCallback(init, format("Init%s", childName), 'Setup') |