FANDOM


--
-- Implements {{Coins}}
--
 
local p = {}
local commas = require( 'Module:Addcommas' )
local num = require( 'Module:Number' )
 
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'] or 0
    return p._amount( a, frame )
 
end
 
-- @todo remove reliance on frame
function p._amount ( a, frame )
 
    local a = tostring( a )
    local color = 'green'
    local img = '1'
 
    -- strip commas from input for things like {{coins|{{GEPrice|Iron bar}}}}
    -- which returns the price formatted with commas
    a = commas._strip( a )
 
    -- dirty way of replicating #expr for the short term
    -- should be able to be replaced by backporting
    -- <https://git.wikimedia.org/commit/mediawiki%2Fextensions%2FParserFunctions/ea7bc2880a5e6e71d5b6c904e5299cdde30817bd>
    --
    -- when testing this function from the debug console, remember to comment out this line
    -- as we can't fake the frame object very easily
    a = frame:preprocess( '{{#expr:' .. a .. '}}' )
 
    -- round to 2 d.p.
    a = num._round( tonumber( a ), 2 )
 
    -- set the color
    -- blue for positive, red for negative, green for zero
    if a > 0 then
        -- blue
        color = '#014cc0'
    elseif a < 0 then
        -- red
        color = '#c02614'
    end
 
    -- set the coins image
    local num = math.abs( a )
    local amounts = { 1000, 250, 100, 25, 5, 4, 3, 2 }
    for _, v in pairs( amounts ) do
        if num >= v then
            img = tostring( v )
            break
        end
    end
 
    img = '[[File:Coins ' .. img .. '.png|link=Coins]]'
    a = commas._add( a )
 
    -- create span tag
    local span = mw.html.create( 'span' )
    span
        :css( {
            ['white-space'] = 'nowrap',
            ['color'] = color
        } )
        :wikitext( img .. '&nbsp;<span>' .. a .. '</span>' )
 
    return tostring( 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.