Module:CoinDisplay: Difference between revisions

From IdleOn MMO Wiki
No edit summary
mNo edit summary
Line 45: Line 45:
local value = NumberParser.parse(args[1] or '0')
local value = NumberParser.parse(args[1] or '0')
if value == nil then
if value == nil then
value = ''
end
end

Revision as of 02:10, 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
		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.
		result = getCoinImage(coin) .. quantity .. result
		
		-- 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