zz_itemsdb/module_bank.lua

212 lines
5.7 KiB
Lua
Raw Normal View History

2021-04-02 12:05:20 +00:00
local addonName, addon = ...
local childName = addon['childName']
local child = addon[childName]
local modulname = 'bank'
local events = {}
local modul = {
['frame'] = CreateFrame("FRAME"),
['name'] = modulname
}
local db
2021-04-04 13:22:38 +00:00
local desc = format('%sCount', modulname)
2021-04-04 13:56:03 +00:00
local desc2 = 'reagentCount'
2021-04-02 12:05:20 +00:00
local BankIsOpen = false
function modul.Guess(id)
local dbcount = modul.Get(id)
local count = GetItemCount(id,true)
local playerID = child.getPID()
local tt = false
if(playerID and dbcount > count) then
local dif = dbcount - count
modul['db'][playerID] = modul['db'][playerID] or {}
local tab = modul['db'][playerID]
2022-11-21 02:40:24 +00:00
for bag = Enum.BagIndex.BankBag_1 ,Enum.BagIndex.BankBag_1 + GetNumBankSlots() - 1 do
2021-04-02 12:05:20 +00:00
local btab = tab[bag]
if(btab and btab[id] and btab[id]['count'] > 0) then
while(dif > 0 and btab[id]['count'] > 0) do
tt = true
dif = dif - 1
btab[id]['count'] = btab[id]['count'] - 1
if(btab[id]['count'] == 0) then
btab[id] = nil
end
end
end
end
end
if(tt) then
child:updateTT()
end
end
local function guess(bag)
if(bag) then
for id, v in pairs(bag) do
local _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, isCraftingReagent = GetItemInfo(id)
if(isCraftingReagent) then
modul.Guess(id)
end
end
end
end
local function scanAllBankBags(delayed)
if(not child:funcSync(scanAllBankBags, delayed)) then return end
local playerID = child.getPID()
if(playerID) then
modul['db'][playerID] = modul['db'][playerID] or {}
local tab = modul['db'][playerID]
2022-11-21 02:40:24 +00:00
for bag = Enum.BagIndex.BankBag_1 ,Enum.BagIndex.BankBag_1 + GetNumBankSlots() - 1 do
2021-04-02 12:05:20 +00:00
tab[bag] = tab[bag] or {}
if(BankIsOpen) then
tab[bag] = child:scanBag(bag) -- loop all bought bankbags
else
guess(tab)
end
end
if(BankIsOpen) then
2022-11-21 02:40:24 +00:00
tab[Enum.BagIndex.Bank] = child:scanBag(Enum.BagIndex.Bank) -- Main bankbag
2021-04-02 12:05:20 +00:00
else
2022-11-21 02:40:24 +00:00
guess(tab[Enum.BagIndex.Bank])
2021-04-02 12:05:20 +00:00
end
2022-12-11 14:04:31 +00:00
if(BankIsOpen) then
tab[Enum.BagIndex.Reagentbank] = child:scanBag(Enum.BagIndex.Reagentbank) -- Main reagentbag
else
guess(tab[Enum.BagIndex.Reagentbank])
end
2021-04-02 12:05:20 +00:00
for k,v in pairs(modul['db'][playerID]) do
2022-11-21 02:40:24 +00:00
if(not(k == Enum.BagIndex.Bank) and not(k == Enum.BagIndex.Reagentbank) and not(k > Enum.BagIndex.ReagentBag and k <= GetNumBankSlots() + Enum.BagIndex.ReagentBag)) then
2021-04-02 12:05:20 +00:00
tab[k] = nil -- prevent bogus bankbags
end
end
child:updateTT()
end
end
local function bagLoop(id, res)
2021-04-04 13:22:38 +00:00
local dName = desc
2021-04-04 13:48:38 +00:00
local units = child:GetNamespace('units')
local playerID = child.getPID()
2021-04-02 12:05:20 +00:00
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
2022-11-21 02:40:24 +00:00
dName = (bag == Enum.BagIndex.Reagentbank) and desc2 or desc
2021-04-02 12:05:20 +00:00
for a, b in pairs(list) do
if(a == id) then
res[unitname] = res[unitname] or {
[dName] = 0
}
res[unitname][dName] = res[unitname][dName] or 0
res[unitname][dName] = res[unitname][dName] + b['count']
end
end
end
end
end
end
return res
end
function modul.Get(id)
2021-04-04 13:22:38 +00:00
local dName = desc
2021-04-02 12:05:20 +00:00
local res = 0
local playerID = child.getPID()
for k, v in pairs(modul['db']) do
if(playerID and k == playerID) then
for bag, list in pairs(v) do
for a, b in pairs(list) do
if(a == id) then
res = res + b['count']
end
end
end
end
end
return res
end
-- function events:BANKFRAME_OPENED()
-- BankIsOpen = true
-- scanAllBankBags()
-- end
-- function events:BANKFRAME_CLOSED()
-- BankIsOpen = false
-- end
2021-04-02 12:05:20 +00:00
function events:PLAYERBANKBAGSLOTS_CHANGED(...)
scanAllBankBags()
end
events.PLAYERBANKSLOTS_CHANGED = events.PLAYERBANKBAGSLOTS_CHANGED
events.PLAYERREAGENTBANKSLOTS_CHANGED = events.PLAYERBANKBAGSLOTS_CHANGED
events.ITEM_UNLOCKED = events.PLAYERBANKBAGSLOTS_CHANGED
events.BAG_UPDATE_DELAYED = events.PLAYERBANKBAGSLOTS_CHANGED
local function OnEvent(self, event, ...)
2022-12-11 14:04:31 +00:00
-- print(modulname, event, ...)
2021-04-02 12:05:20 +00:00
local playerID = child.getPID()
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
2021-04-04 13:56:03 +00:00
function modul.Search(id, res)
2021-04-02 12:05:20 +00:00
local playerID = child.getPID()
if(playerID) then
modul['db'][playerID] = modul['db'][playerID] or {}
end
2021-04-04 13:56:03 +00:00
return playerID and bagLoop(id, res) or res
2021-04-02 12:05:20 +00:00
end
2021-04-04 13:56:03 +00:00
function modul.Output(input, sum, b)
if(b[desc] and b[desc] > 0) then
input[#input+1] = format("Bank %s", b[desc])
sum = sum + b[desc]
2021-04-02 12:05:20 +00:00
end
2021-04-04 13:56:03 +00:00
if(b[desc2] and b[desc2] > 0) then
input[#input+1] = format("Reagents %s", b[desc2])
sum = sum + b[desc2]
2021-04-02 12:05:20 +00:00
end
return input,sum
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
local skip = {}
local function MFOpen()
BankIsOpen = true
scanAllBankBags()
2022-09-25 15:06:13 +00:00
end
local function MFClose()
BankIsOpen = false
2021-05-31 10:01:24 +00:00
end
2021-04-02 12:05:20 +00:00
function modul:Enable()
self['status'] = true
local list = {}
for event,func in pairs(events) do
2021-05-25 13:29:35 +00:00
if(not skip[WOW_PROJECT_ID] or not skip[WOW_PROJECT_ID][event]) then
2021-04-03 13:11:06 +00:00
list[#list+1] = event
end
2021-04-02 12:05:20 +00:00
end
for i = 1, #list do
2021-04-03 13:11:06 +00:00
self['frame']:RegisterEvent(list[i])
2021-04-02 12:05:20 +00:00
end
self['frame']:SetScript("OnEvent", OnEvent)
child:RegisterOpen(8, MFOpen)
child:RegisterClose(8, MFClose)
2021-04-02 12:05:20 +00:00
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')