Module:CoinDisplay: Difference between revisions
From IdleOn MMO Wiki
Kiokurashi (talk | contribs) mNo edit summary |
Kiokurashi (talk | contribs) mNo edit summary |
||
(16 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
require('strict') | require('strict') | ||
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 | ||
--- | --- add a new coin to the end of the list as they're added to the game. | ||
local coins = { | local coins = { | ||
'Copper', | |||
'Silver', | |||
'Gold', | |||
'Platinum', | |||
'Dementia', | |||
'Void', | |||
'Lustre', | |||
'Starfire', | |||
'Dreadlo', | |||
'Godshard', | |||
'Sunder', | |||
'Tydal', | |||
'Marbiglass', | |||
'Orberal', | |||
'Eclipse' | |||
}; | }; | ||
--- Gets the image for the coin | --- Gets the image file for the specified coin index. | ||
--- @param index | --- @param coin The index of the coin in `coins`. | ||
local function getCoinImage( | local function getCoinImage(coin) | ||
return string.format(' [[File:%s Coin.png]]', coins[coin]) | |||
end | end | ||
--- Module entry point to be | --- 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 | |||
return p._main(args) | |||
end | end | ||
--- Real module entry point. | --- Real module entry point. | ||
--- @param args | --- @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 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 | 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 | 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