Module:DoD: Difference between revisions
No edit summary |
No edit summary |
||
Line 4: | Line 4: | ||
function p.calculateMedian(diceCode) | function p.calculateMedian(diceCode) | ||
-- Check if diceCode is nil or empty | -- Check if diceCode is nil or empty | ||
if not diceCode | if not diceCode then | ||
return "Error: No valid input provided" | return "Error: No valid input provided" | ||
end | end | ||
local | -- Extract the dice expression and modifier | ||
local diceExpression, modifier = string.match(diceCode, "([%d%+%-]+)d(%d+)%+?(%d*)") | |||
-- | -- If no modifier is provided, set it to 0 | ||
modifier = modifier == "" and 0 or tonumber(modifier) | |||
-- Extract the type of dice | -- Extract the number of dice and type of dice | ||
local diceType = | local numDice, diceType = string.match(diceExpression, "(%d+)d(%d+)") | ||
-- Check if any of the extracted values are nil | -- Check if any of the extracted values are nil | ||
Line 24: | Line 22: | ||
end | end | ||
-- Calculate the median value | -- Calculate the median value of rolling the dice | ||
local median = numDice * | local median = (numDice * (tonumber(diceType) + 1) / 2) + modifier | ||
return median | return median |
Revision as of 10:20, 6 February 2024
Script error: The function "skills" does not exist.
0-22 : Cost of 3 : Script error: The function "calculate_A_cost" does not exist.
0-18 : Cost of 2 : Script error: The function "calculate_A_cost" does not exist.
5-10 : Cost of 1 : Script error: The function "calculate_A_cost" does not exist.
B_Skill at level 1 : Cost of 5 Script error: The function "calculate_B_cost" does not exist.
B_Skill at level 2 : Cost of 5 Script error: The function "calculate_B_cost" does not exist.
B_Skill at level 3 : Cost of 5 Script error: The function "calculate_B_cost" does not exist.
B_Skill at level 4 : Cost of 5 Script error: The function "calculate_B_cost" does not exist.
B_Skill at level 5 : Cost of 5 Script error: The function "calculate_B_cost" does not exist.
B_Skill at level 5 from 2 : Cost of 5 Script error: The function "calculate_B_cost" does not exist.
local p = {} -- Function to parse D&D dice codes and return median value function p.calculateMedian(diceCode) -- Check if diceCode is nil or empty if not diceCode then return "Error: No valid input provided" end -- Extract the dice expression and modifier local diceExpression, modifier = string.match(diceCode, "([%d%+%-]+)d(%d+)%+?(%d*)") -- If no modifier is provided, set it to 0 modifier = modifier == "" and 0 or tonumber(modifier) -- Extract the number of dice and type of dice local numDice, diceType = string.match(diceExpression, "(%d+)d(%d+)") -- Check if any of the extracted values are nil if not numDice or not diceType then return "Error: Invalid dice code" end -- Calculate the median value of rolling the dice local median = (numDice * (tonumber(diceType) + 1) / 2) + modifier return median end return p