Module:Currency

From the RuneScape Wiki, the wiki for all things RuneScape
Jump to navigation Jump to search
Module documentation
This documentation is transcluded from Module:Currency/doc. [edit] [history] [purge]
Module:Currency is invoked by .
Function list
L 7 — amount
L 100 — p.coins
L 108 — p.nocoins
L 121 — p.rusty
L 131 — p.chimes
L 141 — p.zemomark
L 151 — p.chronotes
L 161 — p._amount
L 166 — p._coins

Formats many types of currency. Modules should ideally use Module:Currency short over this.

This module is a helper module to be used by other modules; it may not designed to be invoked directly. See RuneScape:Lua/Helper modules for a full list and more information. For a full list of modules using this helper click here

FunctionTypeUseExample
_amount( amount, type )number/string, stringReturns a string with amount colour coded and formatted with commas and an image of type type in front of it.

Current supported coin types are chimes, coins, nocoins, nocoinsc, rusty, and zemomarks.

nocoins has no image in front and nocoinsc adds the text 'coins' after the number.
_coins( amount )number/stringReturns a string with amount colour coded and formatted with commas and an image coins in front of it.

-- <pre>
-- Implements various currency templates
--

local p = {}

local function amount (a, currencyType)
    -- 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/coins.less]] for more details
    local coinClasses = {
        coins = 'coins-',
        rusty = 'rusty-coins-',
        chimes = 'chimes-',
        zemomark = 'zemomark-',
        chronotes = 'chronotes-',
        nocoins = '',
        nocoinsc = ''
    }
    local noCoins = {
    	nocoins = '',
    	nocoinsc = ' coins'
    }
    local a2, num, amounts, i, j

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

	if noCoins[currencyType] then
		ret[1] = '<span class="coins '
	end
    -- strip commas from input
    -- @example {{GEPrice|Foo}} -> '1,000'
    a = string.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}

    if currencyType == 'chimes' then
        amts = amountsChimes
    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[currencyType] 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[currencyType] then
    	ret[5] = ret[5] .. noCoins[currencyType]
    end

    return table.concat( ret )
end

--
-- {{Coins}}
--
function p.coins(frame)
    local args = frame:getParent().args
    -- for {{coins|111}} or {{coins|amount=111}}
    -- @todo remove named arg
    local a = args[1] or args.Amount or args.amount or '0'
    return amount(a, 'coins')
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.Amount or args.amount or '0'
    return amount(a, c)
end

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

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

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

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

--
-- Module access point
--
function p._amount(a, currencyType)
    a = tostring(a) or '0'
    return amount(a, currencyType)
end
-- Quick/lazy access to coins
function p._coins(a)
    a = tostring(a) or '0'
    return amount(a, 'coins')
end

return p