Módulo:Moedas

De RuneScape Wiki
Ir para: navegação, pesquisa
Documentação de predefinição
Esta documentação é transcrita de Módulo:Moedas/doc. [editar] [atualizar]

--[[
{{Helper module
|name = Currency
|fname1 = _amount(a, coinType)
|ftype1 = number/string, string
|fuse1 = Returns the number a with the correct image in front of it and formatted with commas.  Current supported cointypes are <code>moedas</code>, <code>enferrujadas</code> and <code>sinos</code>.
}}
-- ]]
-- <pre>
-- Implements various currency templates
--

local p = {}

local function amount (a, coinType)
    -- convert used globals to locals where possible to improve performance
    local math = math
    local string = string
    local table = table
    local mw = mw
    local expr = mw.ext.ParserFunctions.expr

    local ret = {'<span class="coins inventory-image ', true, true, true, true, '</span>'}
    -- add class for CSS to add the correct image with
    -- see [[MediaWiki:Common.less/moedas.less]] for more details
    local coinClasses = {
        moedas = 'coins-',
        enferrujadas = 'rusty-coins-',
        sinos = 'chimes-',
        zemomarcos = 'zemomark-',
        cronotas = 'chronotes-',
        nocoins = '',
        nocoinsc = ''
    }
    local noCoins = {
    	nocoins = '',
    	nocoinsc = ' coins'
    }
    local a2, num, amounts, i, j

    ret[1] = '<span class="coins inventory-image '
    ret[2] = coinClasses[coinType]

	if noCoins[coinType] then
		ret[1] = '<span class="coins '
	end
    -- strip commas from input
    -- @example {{GEPrice|Foo}} -> '1,000'
    a = string.gsub(a, '.', '').gsub(a, ',', '').gsub(a, ' ', '')

    -- cache tonumber result
    a2 = tonumber(a)

    -- only do this if required so as not to impact performance too much
    if a2 == nil then
        a = expr(a)
        a2 = tonumber(a) or 0
    end

    -- round to 2 d.p.
    a = math.floor(a2 * 100 + 0.5) / 100

    -- select which image class to use for css to hook off
    num = math.abs(a)
    amounts = {10000, 1000, 250, 100, 25, 5, 4, 3, 2, 1}
    amountsChimes = {1000, 100, 50, 20, 1}
    amountsZemomark = {10000, 1000, 250, 100, 25, 5, 4, 3, 2, 1}

    if coinType == 'sinos' then
        amts = amountsChimes
    elseif coinType == 'zemomarcos' then
    	amts = amountsZemomark
    else
        amts = amounts
    end

    for i = 1, #amts do
        j = amts[i]

        if num >= j then
            break
        end
    end

    ret[3] = tostring(j)
    if noCoins[coinType] then
    	ret[3] = ''
    end

    -- set a class to denote positive or negative (css sets the colour)
    if a > 0 then
        ret[4] = ' coins-pos">'
    elseif a < 0 then
        ret[4] = ' coins-neg">'
    else
        ret[4] = '">'
    end

    -- format number with commas
    ret[5] = mw.language.getContentLanguage():formatNum(a)
    if noCoins[coinType] then
    	ret[5] = ret[5] .. noCoins[coinType]
    end

    return table.concat( ret )
end

--
-- {{Moedas}}
--
function p.moedas(frame)
    local args = frame:getParent().args
    -- for {{Moedas|111}} or {{Moedas|amount=111}}
    -- @todo remove named arg
    local a = args[1] or args.Quantidade or args.quantidade or args.Amount or args.amount or '0'
    return amount(a, 'moedas')
end

function p.nocoins(frame)
    local args = frame:getParent().args
    local c = 'nocoins'
    if string.find(args[2] or '', '%S') then
    	c = 'nocoinsc'
    end
    local a = args[1] or args.Quantidade or args.quantidade or args.Amount or args.amount or '0'
    return amount(a, c)
end

--
-- {{Moedas enferrujadas}}
--
function p.enferrujadas(frame)
    local args = frame:getParent().args
    -- @todo remove named arg
    local a = args[1] or args.Quantidade or args.quantidade or args.Amount or args.amount or '0'
    return amount(a, 'enferrujadas')
end

--
-- {{Sinos}}
--
function p.sinos(frame)
    local args = frame:getParent().args
    -- @todo remove named arg
    local a = args[1] or args.Quantidade or args.quantidade or args.Amount or args.amount or '0'
    return amount(a, 'sinos')
end

--
-- {{Zemomarcos}}
--
function p.zemomarcos(frame)
    local args = frame:getParent().args
    -- @todo remove named arg
    local a = args[1] or args.Quantidade or args.quantidade or args.Amount or args.amount or '0'
    return amount(a, 'zemomarcos')
end
--
-- {{Cronotas}}
--
function p.cronotas(frame)
    local args = frame:getParent().args
    -- @todo remove named arg
    local a = args[1] or args.Quantidade or args.quantidade or args.Amount or args.amount or '0'
    return amount(a, 'cronotas')
end
--
-- Module access point
--
function p._amount(a, coinType)
    a = tostring(a) or '0'
    return amount(a, coinType)
end

return p