Module:CoinDisplay: Difference between revisions

From IdleOn MMO Wiki
(Created page with "local p = {} function p.Main(frame) local n = tonumber(frame.args.coins) local ret = {} local coins = {"Copper", "Silver", "Gold", "Platinum", "Dementia", "Void", "Lustre", "Starfire", "Dreadlo", "Godshard", "Sunder", "Tydal", "Marbiglass", "Orberal", "Eclipse"} local coininc = 1 local stringpiece = [=[link= %s]=] repeat -- Set denomVStr to remaining n if on last type, or get last 2 for current denom. local denomV = n if coininc < #co...")
 
mNo edit summary
 
(24 intermediate revisions by 3 users not shown)
Line 1: Line 1:
require('strict')
local NumberParser = require('Module:NumberParser')
local p = {}
local p = {}


function p.Main(frame)
--- Contains the names of all coins available in the game ordered by value. Just
local n = tonumber(frame.args.coins)
--- add a new coin to the end of the list as they're added to the game.
local ret = {}
local coins = {
local coins = {"Copper", "Silver", "Gold", "Platinum", "Dementia", "Void", "Lustre", "Starfire", "Dreadlo", "Godshard", "Sunder", "Tydal", "Marbiglass", "Orberal", "Eclipse"}  
'Copper',
local coininc = 1
'Silver',
local stringpiece = [=[[[File:%s Coin.png|link=]] %s]=]
'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
repeat
-- The result string
-- Set denomVStr to remaining n if on last type, or get last 2 for current denom.
local result = ''
local denomV = n
if coininc < #coins then
-- The coin that we're starting with.
denomV = denomV % 100
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
end
if denomV > 0 then
ret[#ret+1] = string.format(stringpiece, coins[coininc], tostring(denomV))
-- Strip the processed digits from the input value.
end
value = value:sub(0, -num_digits - 1)
if coininc < #coins then
n = math.floor(n / 100)
coin = coin + 1
else
end
n = 0
end
-- Any overflow should be displayed as the highest-tier coin available.
coininc = coininc + 1
if value:len() > 0 then
until(n == 0)
result = getCoinImage(coin) .. value .. result
end
return table.concat(ret, " ")
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