Module:DoD: Difference between revisions

From Empire of Dragons
Jump to navigation Jump to search
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 or diceCode == "" then
     if not diceCode or type(diceCode) ~= "table" or not diceCode.args[1] then
         return "Error: No input provided"
         return "Error: No valid input provided"
     end
     end


     -- Extract the number of dice, the type of dice, and the modifier from the dice code
    local diceString = diceCode.args[1]
     local numDice, diceType, modifier = string.match(diceCode, "(%d+)d(%d+)([%+%-]?%d*)")
 
   
     -- Extract the number of dice from the dice code
     -- Check if any match is found
    local numDice = tonumber(string.match(diceString, "(%d+)d"))
 
    -- Extract the type of dice from the dice code
     local diceType = tonumber(string.match(diceString, "d(%d+)"))
 
    -- Extract the modifier from the dice code
    local modifier = tonumber(string.match(diceString, "[%+%-]?%d+$")) or 0
 
     -- Check if any of the extracted values are nil
     if not numDice or not diceType then
     if not numDice or not diceType then
         return "Error: Invalid dice code"
         return "Error: Invalid dice code"
     end
     end
    -- If no modifier is provided, set it to 0
    modifier = modifier == "" and 0 or tonumber(modifier)


     -- Calculate the median value
     -- Calculate the median value
     local median = math.floor((tonumber(numDice) * ((tonumber(diceType) + 1) / 2)) + modifier)
     local median = math.floor((numDice * ((diceType + 1) / 2)) + modifier)


     return median
     return median

Revision as of 10:07, 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 or type(diceCode) ~= "table" or not diceCode.args[1] then
        return "Error: No valid input provided"
    end

    local diceString = diceCode.args[1]

    -- Extract the number of dice from the dice code
    local numDice = tonumber(string.match(diceString, "(%d+)d"))

    -- Extract the type of dice from the dice code
    local diceType = tonumber(string.match(diceString, "d(%d+)"))

    -- Extract the modifier from the dice code
    local modifier = tonumber(string.match(diceString, "[%+%-]?%d+$")) or 0

    -- 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
    local median = math.floor((numDice * ((diceType + 1) / 2)) + modifier)

    return median
end

return p