Originally Posted by cokedrivers
can it be condensed?
|
Here is one way to shorten it. You're doing the same thing per faction, so you can create a loop that does the same thing. I've renamed some variables to make it more obvious what values they hold. I also think that by adding the blank line as a header for each faction's gold, it fixes the "extra" blank lines in your tooltip. Please note that I dry-coded this, so there may be typos and other errors.
Code:
local factions = { 'Alliance', 'Horde', 'Neutral' }
local factionGold = {
Alliance = 0,
Horde = 0,
Neutral = 0,
}
local factionIcon = {
Alliance = iconAlliance,
Horde = iconHorde,
Neutral = iconNeutral,
}
local totalRealmGold = 0
local goldDB = (db and db.Gold) and db.Gold[myPlayerRealm] or nil
if goldDB then
for _, faction in pairs(factions) do
if goldDB[faction] then
GameTooltip:AddDoubleLine(" ", " ")
GameTooltip:AddLine(faction .. " Characters:")
for name, amount in pairs(goldDB[faction]) do
GameTooltip:AddDoubleLine(factionIcon[faction] .. name, formatMoney(amount), 1, 1, 1, 1, 1, 1)
factionGold[faction] = factionGold[faction] + amount
end
GameTooltip:AddDoubleLine("Total " .. faction .. " Gold", formatMoney(factionGold[faction]))
totalRealmGold = totalRealmGold + factionGold[faction]
end
end
GameTooltip:AddDoubleLine("Total Gold for " .. myPlayerRealm, formatMoney(totalRealmGold))
end