zz_Money/broker.lua

165 lines
6.1 KiB
Lua

local addonName, addonTable = ...
local childName = "zzMoney2"
local db, dbr, dbp, L, lineIn, lineOut, session, faction, realm
local ldbicon = LibStub:GetLibrary("LibDBIcon-1.0")
function addonTable:SetupBroker()
L = addonTable['storage']['L']
db = addonTable['storage']['db']
dbr = addonTable['storage']['dbr']
dbp = addonTable['storage']['dbp']
session = addonTable['storage']['session']
faction = addonTable['storage']['faction']
realm = addonTable['storage']['realm']
local folder = addonName ~= childName and format("Interface\\AddOns\\%s\\%s", addonName, childName) or format("Interface\\AddOns\\%s", childName)
lineIn = format("|T%s:0|t %s", format("%s\\in.tga", folder), L['Einnahmen'])
lineOut = format("|T%s:0|t %s", format("%s\\out.tga", folder), L['Ausgaben'])
end
local function cdbCheck(n,realm)
db[realm] = db[realm] or {}
db[realm][n] = db[realm][n] or { currentGold = 0 }
end
local function getSumToday(d)
if(faction=='Neutral') then return 0,0 end
local sumIn,sumOut = 0,0
d = d or 0
local day = addonTable:getDateString() + d
for a,b in pairs(dbr['activity'][faction][tostring(day)] or {}) do
if(b < 0) then
sumOut = sumOut + b
else
sumIn = sumIn + b
end
end
return sumOut,sumIn
end
local function getSumWeek()
local sumIn, sumOut = 0,0
local sumWeekIn, sumWeekOut = 0,0
for a=0,6 do
sumOut, sumIn = getSumToday(a * -1)
sumWeekIn = sumWeekIn + sumIn
sumWeekOut = sumWeekOut + sumOut
end
return sumWeekOut,sumWeekIn
end
local function Helper_Tooltip3(realm)
if(not db['connectedRealms']) then return end
connectedRealms = connectedRealms or GetAutoCompleteRealms()
for k, v in pairs(connectedRealms) do
if(v == realm) then
return true
end
end
end
local function OnClick(self, button)
if(IsShiftKeyDown() and button == "LeftButton") then
local sb = addonTable['db']['global']['ldbicons']
sb[childName]['hide'] = not sb[childName]['hide']
if(ldbicon) then
if(sb[childName]['hide']) then
ldbicon:Hide(childName)
else
ldbicon:Show(childName)
end
end
else
if(not InCombatLockdown()) then
if(InterfaceOptionsFrame:IsVisible()) then
InterfaceOptionsFrame:Hide()
else
InterfaceOptionsFrame_OpenToCategory(childName)
InterfaceOptionsFrame_OpenToCategory(childName) -- Twice because of a bug in InterfaceOptionsFrame_OpenToCategory
end
end
end
end
local function OnTooltipShow(tip)
tip:AddLine(childName)
if(faction == 'Neutral') then
tip:AddLine(L['neutral'])
return
end
local sessdiff = dbp['currentGold'] - session['start']
local sdr,sdg,sdb = 1, 1, 1
if(sessdiff<0) then
sdr,sdg,sdb = 1, 0, 0
end
tip:AddDoubleLine(L['Sitzung'], addonTable:displayMoney(abs(sessdiff), db['shortMoneyTip']),1,1,0,sdr,sdg,sdb)
tip:AddDoubleLine(lineIn, addonTable:displayMoney(session['income'], db['shortMoneyTip']),1,1,1,1,1,1)
tip:AddDoubleLine(lineOut, addonTable:displayMoney(abs(session['expense']), db['shortMoneyTip']),1,1,1,1,1,1)
tip:AddLine(' ')
local sumOut,sumIn = getSumToday()
sessdiff = sumOut+sumIn
if(sessdiff<0) then
sdr,sdg,sdb = 1, 0, 0
else
sdr,sdg,sdb = 1, 1, 1
end
tip:AddDoubleLine(L['Heute'], addonTable:displayMoney(abs(sessdiff), db['shortMoneyTip']),1,1,0,sdr,sdg,sdb)
tip:AddDoubleLine(lineIn, addonTable:displayMoney(sumIn, db['shortMoneyTip']),1,1,1,1,1,1)
tip:AddDoubleLine(lineOut, addonTable:displayMoney(abs(sumOut), db['shortMoneyTip']),1,1,1,1,1,1)
tip:AddLine(' ')
local sumOut,sumIn = getSumToday(-1)
sessdiff = sumOut+sumIn
if(sessdiff<0) then
sdr,sdg,sdb = 1, 0, 0
else
sdr,sdg,sdb = 1, 1, 1
end
tip:AddDoubleLine(L['Gestern'], addonTable:displayMoney(abs(sessdiff), db['shortMoneyTip']),1,1,0,sdr,sdg,sdb)
tip:AddDoubleLine(lineIn, addonTable:displayMoney(sumIn, db['shortMoneyTip']),1,1,1,1,1,1)
tip:AddDoubleLine(lineOut, addonTable:displayMoney(abs(sumOut), db['shortMoneyTip']),1,1,1,1,1,1)
tip:AddLine(' ')
local sumOut,sumIn = getSumWeek()
sessdiff = sumOut+sumIn
if(sessdiff<0) then
sdr,sdg,sdb = 1, 0, 0
else
sdr,sdg,sdb = 1, 1, 1
end
tip:AddDoubleLine(L['7-Tage'], addonTable:displayMoney(abs(sessdiff), db['shortMoneyTip']),1,1,0,sdr,sdg,sdb)
tip:AddDoubleLine(lineIn, addonTable:displayMoney(sumIn, db['shortMoneyTip']),1,1,1,1,1,1)
tip:AddDoubleLine(lineOut, addonTable:displayMoney(abs(sumOut), db['shortMoneyTip']),1,1,1,1,1,1)
tip:AddLine(' ')
local sum = 0
local ttlist = {}
if(addonTable['db']['sv']['profileKeys']) then
for k,v in pairs(addonTable['db']['sv']['profileKeys']) do
local n,_,m = strsplit(' ',k,3)
if(n) then
local cr = Helper_Tooltip3(m)
if(m == realm or cr) then
cdbCheck(n,cr and m or realm)
if( faction == db[cr and m or realm][n]['faction']) then
sum = sum + db[cr and m or realm][n]['currentGold']
ttlist[k] = db[cr and m or realm][n]['currentGold']
end
end
end
end
end
for _,v in pairs(addonTable:sortArray(ttlist,db['sortDir'])) do
local n,_,m = strsplit(' ',v,3)
local cr = Helper_Tooltip3(m)
tip:AddDoubleLine(addonTable:classcolor(n,db[cr and m or realm][n]['class']),addonTable:displayMoney(db[cr and m or realm][n]['currentGold']+(db[cr and m or realm][n]['incoming'] or 0), db['shortMoneyTip']),0,1,0,1,1,1)
end
tip:AddLine(' ')
tip:AddDoubleLine(L['Gesamt'],addonTable:displayMoney(sum, db['shortMoneyTip']),0,1,0,1,1,1)
end
function addonTable:GetBroker(objname)
return LibStub:GetLibrary("zzLDB"):GetLDB(objname)
end
function addonTable:OnText(objname, message)
local ldbobj = addonTable:GetBroker(objname)
if(ldbobj['text'] ~= message) then
ldbobj['text'] = message
end
end
addonTable['ldb'] = { -- https://github.com/tekkub/libdatabroker-1-1/wiki/Data-Specifications
['type'] = 'data source', -- required: 'data source' or 'launcher'
['text'] = childName, -- required/optional for launcher
['icon'] = 'Interface\\Icons\\INV_Misc_Bag_10_Blue', -- optional/required for launcher
['OnClick'] = OnClick, -- optional/required for launcher
['OnTooltipShow'] = OnTooltipShow, -- optional
}