PageRenderTime 15ms CodeModel.GetById 1ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/Tukui/modules/datatext/gold.lua

http://github.com/Asphyxia/Tukui
Lua | 130 lines | 110 code | 15 blank | 5 comment | 18 complexity | 5ae8aaecdcbbe777597d8320937945a2 MD5 | raw file
  1local T, C, L = unpack(select(2, ...)) -- Import: T - functions, constants, variables; C - config; L - locales
  2--------------------------------------------------------------------
  3-- GOLD
  4--------------------------------------------------------------------
  5
  6if C["datatext"].gold and C["datatext"].gold > 0 then
  7	local Stat = CreateFrame("Frame")
  8	Stat:EnableMouse(true)
  9	Stat:SetFrameStrata("BACKGROUND")
 10	Stat:SetFrameLevel(3)
 11
 12	local Text  = TukuiInfoLeft:CreateFontString(nil, "OVERLAY")
 13	Text:SetFont(C.media.pixelfont, C["datatext"].fontsize, "MONOCHROMEOUTLINE")
 14	T.PP(C["datatext"].gold, Text)
 15
 16	local COPPER_ICON = "|TInterface\\MONEYFRAME\\UI-CopperIcon:0:0:0:-1|t"
 17	local SILVER_ICON = "|TInterface\\MONEYFRAME\\UI-SilverIcon:0:0:0:-1|t"
 18	local GOLD_ICON = "|TInterface\\MONEYFRAME\\UI-GoldIcon:0:0:0:-1|t"
 19	local Profit	= 0
 20	local Spent		= 0
 21	local OldMoney	= 0
 22	local myPlayerRealm = GetCVar("realmName");
 23
 24	local function formatMoney(money)
 25		local gold = floor(math.abs(money) / 10000)
 26		local silver = mod(floor(math.abs(money) / 100), 100)
 27		local copper = mod(floor(math.abs(money)), 100)
 28		if gold ~= 0 then
 29			return format("%s"..GOLD_ICON.." %s"..SILVER_ICON.." %s"..COPPER_ICON, gold, silver, copper)
 30		elseif silver ~= 0 then
 31			return format("%s"..SILVER_ICON.." %s"..COPPER_ICON, silver, copper)
 32		else
 33			return format("%s"..COPPER_ICON, copper)
 34		end
 35	end
 36
 37	local function FormatTooltipMoney(money)
 38		local gold, silver, copper = abs(money / 10000), abs(mod(money / 100, 100)), abs(mod(money, 100))
 39		local cash = ""
 40		cash = format("%.2d"..GOLD_ICON.." %.2d"..SILVER_ICON.." %.2d"..COPPER_ICON, gold, silver, copper)		
 41		return cash
 42	end	
 43
 44	local function OnEvent(self, event)
 45		if event == "PLAYER_ENTERING_WORLD" then
 46			OldMoney = GetMoney()
 47		end
 48		
 49		local NewMoney	= GetMoney()
 50		local Change = NewMoney-OldMoney -- Positive if we gain money
 51		
 52		if OldMoney>NewMoney then		-- Lost Money
 53			Spent = Spent - Change
 54		else							-- Gained Moeny
 55			Profit = Profit + Change
 56		end
 57		
 58		Text:SetText(formatMoney(NewMoney))
 59		-- Setup Money Tooltip
 60		self:SetAllPoints(Text)
 61
 62		local myPlayerName  = UnitName("player");				
 63		if (TukuiData == nil) then TukuiData = {}; end
 64		if (TukuiData.gold == nil) then TukuiData.gold = {}; end
 65		if (TukuiData.gold[myPlayerRealm]==nil) then TukuiData.gold[myPlayerRealm]={}; end
 66		TukuiData.gold[myPlayerRealm][myPlayerName] = GetMoney();
 67				
 68		OldMoney = NewMoney
 69	end
 70
 71	Stat:RegisterEvent("PLAYER_MONEY")
 72	Stat:RegisterEvent("SEND_MAIL_MONEY_CHANGED")
 73	Stat:RegisterEvent("SEND_MAIL_COD_CHANGED")
 74	Stat:RegisterEvent("PLAYER_TRADE_MONEY")
 75	Stat:RegisterEvent("TRADE_MONEY_CHANGED")
 76	Stat:RegisterEvent("PLAYER_ENTERING_WORLD")
 77	Stat:SetScript("OnMouseDown", function() ToggleAllBags() end)
 78	Stat:SetScript("OnEvent", OnEvent)
 79	Stat:SetScript("OnEnter", function(self)
 80		if not InCombatLockdown() then
 81			local anchor, panel, xoff, yoff = T.DataTextTooltipAnchor(Text)
 82			GameTooltip:SetOwner(panel, anchor, xoff, yoff)
 83			GameTooltip:ClearLines()
 84			GameTooltip:AddLine(L.datatext_session)
 85			GameTooltip:AddDoubleLine(L.datatext_earned, formatMoney(Profit), 1, 1, 1, 1, 1, 1)
 86			GameTooltip:AddDoubleLine(L.datatext_spent, formatMoney(Spent), 1, 1, 1, 1, 1, 1)
 87			if Profit < Spent then
 88				GameTooltip:AddDoubleLine(L.datatext_deficit, formatMoney(Profit-Spent), 1, 0, 0, 1, 1, 1)
 89			elseif (Profit-Spent)>0 then
 90				GameTooltip:AddDoubleLine(L.datatext_profit, formatMoney(Profit-Spent), 0, 1, 0, 1, 1, 1)
 91			end				
 92			GameTooltip:AddLine' '								
 93		
 94			local totalGold = 0				
 95			GameTooltip:AddLine(L.datatext_character)			
 96			local thisRealmList = TukuiData.gold[myPlayerRealm];
 97			for k,v in pairs(thisRealmList) do
 98				GameTooltip:AddDoubleLine(k, FormatTooltipMoney(v), 1, 1, 1, 1, 1, 1)
 99				totalGold=totalGold+v;
100			end 
101			GameTooltip:AddLine' '
102			GameTooltip:AddLine(L.datatext_server)
103			GameTooltip:AddDoubleLine(L.datatext_totalgold, FormatTooltipMoney(totalGold), 1, 1, 1, 1, 1, 1)
104
105			for i = 1, GetNumWatchedTokens() do
106				local name, count, extraCurrencyType, icon, itemID = GetBackpackCurrencyInfo(i)
107				if name and i == 1 then
108					GameTooltip:AddLine(" ")
109					GameTooltip:AddLine(CURRENCY)
110				end
111				local r, g, b = 1,1,1
112				if itemID then r, g, b = GetItemQualityColor(select(3, GetItemInfo(itemID))) end
113				if name and count then GameTooltip:AddDoubleLine(name, count, r, g, b, 1, 1, 1) end
114			end
115			GameTooltip:Show()
116		end
117	end)
118	Stat:SetScript("OnLeave", function() GameTooltip:Hide() end)	
119	-- reset gold data
120	local function RESETGOLD()
121		local myPlayerRealm = GetCVar("realmName");
122		local myPlayerName  = UnitName("player");
123		
124		TukuiData.gold = {}
125		TukuiData.gold[myPlayerRealm]={}
126		TukuiData.gold[myPlayerRealm][myPlayerName] = GetMoney();
127	end
128	SLASH_RESETGOLD1 = "/resetgold"
129	SlashCmdList["RESETGOLD"] = RESETGOLD
130end