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. Defauts 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
 
    local a = tostring( a ) or '0'
    local color = 'green'
    local img = '1'
 
    -- strip commas from input
    -- @example {{GEPrice|Foo}} -> '1,000'
    a = a:gsub( ',', '' )
 
    -- cache tonumber( a ) to improve performance a bit
    local a2 = tonumber( a )
 
    -- dirty way of replicating #expr
    -- should be able to be replaced by backporting
    -- <https://git.wikimedia.org/commit/mediawiki%2Fextensions%2FParserFunctions/ea7bc2880a5e6e71d5b6c904e5299cdde30817bd>
    --
    -- 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 )
 
        if a2 == nil then
            a2 = 0
        end
    end
 
    -- round to 2 d.p.
    a = math.floor( a2 * 100 + 0.5 ) / 100
 
    -- set the color
    -- blue for positive, red for negative, green for zero
    -- default 'green' set above
    if a > 0 then
        -- blue
        color = '#014cc0'
    elseif a < 0 then
        -- red
        color = '#c02614'
    end
 
    -- set the coins image
    -- default '1' set above
    local num = math.abs( a )
 
    if num > 1000 then
        img = '1000'
    elseif num > 250 then
        img = '250'
    elseif num > 100 then
        img = '100'
    elseif num > 25 then
        img = '25'
    elseif num > 5 then
        img = '5'
    elseif num > 4 then
        img = '4'
    elseif num > 3 then
        img = '3'
    elseif num > 2 then
        img = '2'
    end
 
    if not rusty then
        img = '[[File:Coins ' .. img .. '.png|link=Coins]]'
    else
        img = '[[File:Rusty coins ' .. img .. '.png|link=Rusty coins]]'
    end
 
    -- format number with commas
    a = mw.language.getContentLanguage():formatNum( a )
 
    -- return '<span class="coins/rusty-coins' .. '-XXXX ' .. 'coins-pos/neg">' .. a .. '</span>'
 
    return '<span class="coins-templ" style="white-space:nowrap; color:' ..
        color .. '">' .. img .. '&nbsp;' .. 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.