Module:1inCalculator
From The Walkscape Walkthrough
1 in Calculator It takes in a percentage and calculates a 1 in x rate. It can also optionally take a percentage, step number and even more optionally a number or range and return the WEAR rate.
Percent Only
{{#invoke:1inCalculator|main|5.3%}}
Output: ~1 in 19
WEAR Calculation
{{#invoke:1inCalculator|main|5.3%|50|1-2}}
Output: ~1 in 19
(~629 steps)
- Note this calls the Module:WEARCalculator for calculating the WEAR value.
local p = {}
-- Import WEARCalculator
local WEARCalculator = require("Module:WEARCalculator")
-- Main function that can accept either a frame or direct arguments
function p.main(frameOrPercentage, totalsteps, quantity)
-- Check if the first argument is a frame object
local isFrame = type(frameOrPercentage) == "table" and frameOrPercentage.args ~= nil
local percentage
local totalstepsValue
local quantityValue
if isFrame then
-- If it's a frame, get the arguments from the frame object
percentage = frameOrPercentage.args[1]
totalstepsValue = tonumber(frameOrPercentage.args[2])
quantityValue = frameOrPercentage.args[3] or "1"
else
-- Otherwise, use the arguments passed directly
percentage = frameOrPercentage
totalstepsValue = tonumber(totalsteps)
quantityValue = quantity
end
-- Process the percentage input
local percent = string.gsub(percentage, "%%", "")
local decimal = tonumber(percent) / 100
local rate = math.ceil(1 / decimal)
-- Call WEARCalculator if totalsteps is provided
if totalstepsValue then
-- Call WEARCalculator to get the calculated steps
local calculatedSteps = WEARCalculator.main(percentage, totalstepsValue, quantityValue)
rate = format_int(rate)
return string.format("~1 in %s<br>(%s)", rate, calculatedSteps)
else
rate = format_int(rate)
return string.format("~1 in %s", rate)
end
end
-- Function to format an integer with commas
function format_int(number)
local i, j, minus, int, fraction = tostring(number):find('([-]?)(%d+)([.]?%d*)')
-- reverse the int-string and append a comma to all blocks of 3 digits
int = int:reverse():gsub("(%d%d%d)", "%1,")
-- reverse the int-string back, remove an optional comma, and put the
-- optional minus and fractional part back
return minus .. int:reverse():gsub("^,", "") .. fraction
end
return p