View Single Post
12-29-18, 02:51 PM   #13
jlam
A Fallenroot Satyr
AddOn Author - Click to view addons
Join Date: Oct 2010
Posts: 29
Originally Posted by cokedrivers View Post
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
  Reply With Quote