FANDOM


-- <pre>
--
-- Implements {{Coins}} and {{Rusty coins}}
--
 
local p = {}
 
--
-- {{Coins}} access point
--
function p.amount(frame)
	local args = frame:getParent().args
	-- for {{coins|111}} or {{coins|amount=111}}
	local a = args[1] or args.Amount or args.amount
	return p._amount(a)
end
 
--
-- {{Rusty coins}} access point
--
function p.rusty(frame)
	local args = frame:getParent().args
	local a = args[1] or args.Amount or args.amount
	return p._amount(a, true)
end
 
--
-- @param a {number} Number to output with coins image
-- @param rusty {boolean} If true, outputs rusty coins image. Defaults to false
-- @return {string}
--
function p._amount (a, rusty)
	-- convert used globals to locals where possible to improve performance
	local tostring = tostring
	local tonumber = tonumber
	local math = math
	local mw = mw
 
	-- set defaults
	local ret = '<span class="coins coins-'
	local a = tostring(a) or '0'
	local posneg = '">'
 
	if rusty then
		ret = '<span class="coins coins-rusty-'
	end
 
	-- strip commas from input
	-- @example {{GEPrice|Foo}} -> '1,000'
	a = a:gsub(',', '')
 
	-- cache tonumber result
	local a2 = tonumber(a)
 
	-- dirty way of replicating #expr
	-- only do this if required so as not to impact performance too much
	if a2 == nil then
		a = mw.getCurrentFrame():preprocess('{{#expr:' .. a .. '}}')
		a2 = tonumber(a) or 0
	end
 
	-- round to 2 d.p.
	a = math.floor(a2 * 100 + 0.5) / 100
 
	-- set a class to denote positive or negative (css sets the colour)
	if a > 0 then
		posneg = ' coins-pos">'
	elseif a < 0 then
		posneg = ' coins-neg">'
	end
 
	-- select which image class to use for css to hook off
	local num = math.abs(a)
	local amounts = {1000, 250, 100, 25, 5, 4, 3, 2, 1}
	local i, j
 
	for i = 1, 9 do
		j = amounts[i]
 
		if num >= j then
			break
		end
	end
 
	-- format number with commas
	a = mw.language.getContentLanguage():formatNum(a)
 
	return ret .. tostring(j) .. posneg .. a .. '</span>'
end
 
return p

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.