Module:CoinDisplay: Difference between revisions

From IdleOn MMO Wiki
No edit summary
mNo edit summary
 
(7 intermediate revisions by 2 users not shown)
Line 1: Line 1:
require('strict')
require('strict')


-- local number_parser = require('Module:NumberParser')
local NumberParser = require('Module:NumberParser')


local p = {}
local p = {}


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


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


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


--- Real module entry point.
--- Real module entry point.
--- @param args table The arguments passed in from the frame.
--- @param args The provided arguments - args[1] contains the raw coin value.
--- @returns The formatted coin display
function p._main(args)
function p._main(args)
    -- The unformatted coin quantity.
-- The raw coin value to parse.
    local value = args[1] or '0' -- NumberParser.parse(args[1] or '0')
local value = NumberParser.parse(args[1] or '0')
    if type(value) == nil then
if value == nil then
    return 'BAD_VALUE'
return '<BAD_VALUE>'
end
end
 
    -- The index of the coin being processed.
-- Handle the edge case of 0 coins.
    local coin = 1
if value == '0' then
 
return getCoinImage(1) .. value
    local result = ''
end
 
    -- Handle processing for every coin except for the highest denomination.
-- The result string
    while #value > 0 and coin < #coins do
local result = ''
        -- Calculate the number of characters used to represent the current coin.
        local num_digits = math.min(2, value:len())
-- The coin that we're starting with.
       
local coin = 1
        -- Get the quantity for the current coin and strips leading zeroes.
        local quantity = tonumber(value:sub(-num_digits))
-- Handle processing for every coin except for the highest denomination.
       
while value:len() > 0 and coin < #coins do
        -- Prepend the coin quantity to the formatted display as long as it is greater than 0.
-- Calculate the number of digits which represent the coin.
        if quantity > 0 then
local num_digits = math.min(2, value:len())
        result = getCoinImage(coin) .. quantity .. result
-- Get the coin quantity. Number conversion strips leading zeroes.
local quantity = tonumber(value:sub(-num_digits))
-- Prepend the current coin value.
if quantity ~= 0 then
result = getCoinImage(coin) .. quantity .. result
end
end
 
        -- Strip the characters used to represent the current coin.
-- Strip the processed digits from the input value.
        value = value:sub(0, -num_digits - 1)
value = value:sub(0, -num_digits - 1)
       
        -- Move to the next denomination.
coin = coin + 1
        coin = coin + 1
end
    end
   
-- Any overflow should be displayed as the highest-tier coin available.
    -- Any overflow should be displayed as the highest-tier coin available.
if value:len() > 0 then
    if value:len() > 0 then
result = getCoinImage(coin) .. value .. result
        result = getCoinImage(coin) .. value .. result
end
    end
   
return result
    return result
end
end


return p
return p

Latest revision as of 15:25, 11 March 2024

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

require('strict')

local NumberParser = require('Module:NumberParser')

local p = {}

--- Contains the names of all coins available in the game ordered by value. Just
--- add 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 file for the specified coin index.
--- @param coin The index of the coin in `coins`.
local function getCoinImage(coin)
	return string.format(' [[File:%s Coin.png]]', coins[coin])
end

--- Module entry point to be called from 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 The provided arguments - args[1] contains the raw coin value.
--- @returns The formatted coin display
function p._main(args)
	-- The raw coin value to parse.
	local value = NumberParser.parse(args[1] or '0')
	if value == nil then
		return '<BAD_VALUE>'
	end
	
	-- Handle the edge case of 0 coins.
	if value == '0' then
		return getCoinImage(1) .. value
	end
	
	-- The result string
	local result = ''
	
	-- The coin that we're starting with.
	local coin = 1
	
	-- Handle processing for every coin except for the highest denomination.
	while value:len() > 0 and coin < #coins do
		-- Calculate the number of digits which represent the coin.
		local num_digits = math.min(2, value:len())
		
		-- Get the coin quantity. Number conversion strips leading zeroes.
		local quantity = tonumber(value:sub(-num_digits))
		
		-- Prepend the current coin value.
		if quantity ~= 0 then
			result = getCoinImage(coin) .. quantity .. result
		end
		
		-- Strip the processed digits from the input value.
		value = value:sub(0, -num_digits - 1)
		
		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