Module:CoinDisplay

From IdleOn MMO Wiki
Revision as of 01:29, 11 March 2024 by Kiokurashi (talk | contribs)

Documentation for this module may be created at Module:CoinDisplay/doc

require('strict')

local p = {}

--- Contains the names of all coins available in the game ordered by value.
--- Append a new coin to the end of the list as they're added to the game.
local coins = {
    'Copper',
    'Silver',
    'Gold',
    'Platinum',
    'Dementia',
    'Void',
    'Lustre',
    'Starfire',
    'Dreadlo',
    'Godshard',
    'Sunder',
    'Tydal',
    'Marbiglass',
    'Orberal',
    'Eclipse'
};

--- Gets the image for the coin with the specified index.
--- @param index number The coin's index.
local function getCoinImage(index)
    return string.format('[[File:%s Coin.png|link=]]', coins[index])
end

--- Module entry point to be used by templates. Unpacks the frame arguments and
--- invokes the real entry point.
function p.main(frame)
    local args = frame:getParent().args
    return p._main(args)
end

--- Real module entry point.
--- @param args table The arguments passed in from the frame.
function p._main(args)
    -- The unformatted coin quantity.
    local value = args[1] or "0"

    -- The index of the coin being processed.
    local coin = 1

    local result = ''

    -- Handle processing for every coin except for the highest denomination.
    while #value > 0 and coin < #coins do
        -- Calculate the number of characters used to represent the current coin.
        local num_digits = math.min(2, value:len())
        
        -- Get the quantity for the current coin and strips leading zeroes.
        local quantity = tonumber(value:sub(-num_digits))
        
        -- Prepend the coin quantity to the formatted display as long as it is greater than 0.
        if quantity > 0 then
	        result = getCoinImage(coin) .. quantity .. result
		end

        -- Strip the characters used to represent the current coin.
        value = value:sub(0, -num_digits - 1)
        
        -- Move to the next denomination.
        coin = coin + 1
    end
    
    -- Any overflow should be displayed as the highest-tier coin available.
    if value:len() > 0 then
        result = getCoinImage(coin) .. value .. result
    end
    
    return result
end

return p