143 lines
3.7 KiB
Lua
Executable File
143 lines
3.7 KiB
Lua
Executable File
local addonName, addon = ...
|
|
local childName = addon['childName']
|
|
local child = addon[childName]
|
|
local modulname = 'mail'
|
|
local events = {}
|
|
local modul = {
|
|
['frame'] = CreateFrame("FRAME"),
|
|
['name'] = modulname
|
|
}
|
|
local desc = format('%sCount', modulname)
|
|
local mailOpen = false
|
|
local db
|
|
local function scan()
|
|
if(not mailOpen) then return end
|
|
local numItems, totalItems = GetInboxNumItems()
|
|
local playerID = child.getPID()
|
|
local db = modul['db'][playerID]
|
|
db[1] = {}
|
|
for index = 1, numItems do
|
|
local packageIcon, stationeryIcon, sender, subject, money, CODAmount, daysLeft, hasItem, wasRead, wasReturned,
|
|
textCreated, canReply, isGM = GetInboxHeaderInfo(index)
|
|
if(hasItem) then
|
|
for itemIndex = 1, ATTACHMENTS_MAX_RECEIVE do
|
|
local name, itemID, texture, count, quality, canUse = GetInboxItem(index, itemIndex)
|
|
if(name) then
|
|
db[1][itemID] = db[1][itemID] or {
|
|
['count'] = 0,
|
|
}
|
|
db[1][itemID]['count'] = db[1][itemID]['count'] + count
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
function events:MAIL_INBOX_UPDATE()
|
|
scan()
|
|
end
|
|
function events:MAIL_SHOW()
|
|
mailOpen = true
|
|
end
|
|
function events:MAIL_CLOSED()
|
|
mailOpen = false
|
|
end
|
|
function events:MAIL_SUCCESS(event, itemID) --fires along with MAIL_SEND_SUCCESS, itemID always nil, fires when deleting mail
|
|
scan()
|
|
end
|
|
local function OnEvent(self, event, ...)
|
|
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
|
|
local function bagLoop(id, res)
|
|
for k, v in pairs(modul['db']) do
|
|
local units = child:GetNamespace('units')
|
|
if(units[k]) then
|
|
local playerID = child.getPID()
|
|
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("Mail %s",b[desc])
|
|
sum = sum + b[desc]
|
|
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 function MFOpen()
|
|
mailOpen = true
|
|
scan()
|
|
end
|
|
local function MFClose()
|
|
mailOpen = false
|
|
end
|
|
local skip = {
|
|
[WOW_PROJECT_MAINLINE] = {
|
|
['MAIL_SHOW'] = true,
|
|
['MAIL_CLOSED'] = true
|
|
}
|
|
}
|
|
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
|
|
if(not skip[WOW_PROJECT_ID] or not skip[WOW_PROJECT_ID][event]) then
|
|
self['frame']:RegisterEvent(list[i])
|
|
end
|
|
end
|
|
self['frame']:SetScript("OnEvent", OnEvent)
|
|
if(WOW_PROJECT_ID == WOW_PROJECT_MAINLINE) then
|
|
child:RegisterOpen(17, MFOpen)
|
|
child:RegisterClose(17, MFClose)
|
|
end
|
|
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') |