Module:CoinDisplay: Difference between revisions

From IdleOn MMO Wiki
mNo edit summary
mNo edit summary
Line 28: Line 28:
--- @param coin The index of the coin in `coins`.
--- @param coin The index of the coin in `coins`.
local function getCoinImage(coin)
local function getCoinImage(coin)
return string.format('[[File:%s Coin.png]]', coins[coin])
return string.format('[[File:%s Coin.png]] ', coins[coin])
end
end


Line 46: Line 46:
if value == nil then
if value == nil then
value = ''
value = ''
elseif value == "0" then
return getCoinImage(coin) .. "0"
end
end
-- The result string
-- The result string
Line 63: Line 66:
-- Prepend the current coin value.
-- Prepend the current coin value.
result = getCoinImage(coin) .. quantity .. result
if quantity > 0 then
result = getCoinImage(coin) .. quantity .. result
end
-- Strip the processed digits from the input value.
-- Strip the processed digits from the input value.

Revision as of 02:36, 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 = ''
	elseif value == "0" then
		return getCoinImage(coin) .. "0"
	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