Module:DoD: Difference between revisions

From Empire of Dragons
Jump to navigation Jump to search
No edit summary
No edit summary
Line 68: Line 68:


   -- Generate the HTML table
   -- Generate the HTML table
   result = "<table><tr><td class='skills'>Skill</td><td class='spacer'></td><td>Level</td><td class='spacer'></td><td class='cost'>Cost</td></tr>"
   result = "<table><tr><td class='skills'>Färdighet</td><td class='spacer'></td><td>FV</td><td class='spacer'></td><td class='cost'>Kostnad</td></tr>"
   for _, skill in ipairs(skills) do
   for _, skill in ipairs(skills) do
     result = result .. string.format("<tr><td class='skills'>%s</td><td class='spacer'></td><td class='rd'>%s</td><td class='spacer'></td><td class='rd'>%s</td></tr>",
     result = result .. string.format("<tr><td class='skills'>%s</td><td class='spacer'></td><td class='rd'>%s</td><td class='spacer'></td><td class='rd'>%s</td></tr>",

Revision as of 12:44, 8 February 2024

FärdighetFVKostnad
Rörlighet144
Upptäcka153

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(frame)
    -- Extract the dice code from the function argument
    local diceCode = frame.args[1]

    -- Check if diceCode is nil or empty
    if not diceCode then
        return "Error: No valid input provided"
    end

    -- Extract the number of dice, number of sides per dice, and modifier
    local numDice, diceType, modifier = string.match(diceCode, "(%d+)d(%d+)([%+%-]?%d*)")

    -- If no modifier is provided, set it to 0
    modifier = tonumber(modifier) 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 of rolling the dice
    local median = 0
    local totalSides = tonumber(diceType)

    -- Calculate the median value of rolling the dice
    if totalSides then
        -- Calculate median for each die roll
        local medianPerRoll = (totalSides + 1) / 2
        -- Calculate the total median without modifier
        median = numDice * medianPerRoll
        -- Add modifier to the total median
        median = math.ceil (median + modifier)
    else
        return "Error: Invalid number of sides for dice"
    end

    return median
end

function p.skills(frame)
  local result
  local frames = mw.getCurrentFrame()
  local num_args = 0

  -- Count the number of arguments
  for k, v in pairs(frames.args) do
    num_args = num_args + 1
  end

  local num_skills = math.floor(num_args / 3)
  local skills = {} -- Table to store skills

  -- Populate the skills table
  for i = 1, num_skills do
    local name = frame.args[(i - 1) * 3 + 1]
    local level = frame.args[(i - 1) * 3 + 2]
    local cost = frame.args[(i - 1) * 3 + 3]

    table.insert(skills, {name = name, level = level, cost = cost})
  end

  -- Sort the skills table based on the 3rd field (cost)
  
  table.sort(skills, function(a, b) return a.name < b.name end)

  -- Generate the HTML table
  result = "<table><tr><td class='skills'>Färdighet</td><td class='spacer'></td><td>FV</td><td class='spacer'></td><td class='cost'>Kostnad</td></tr>"
  for _, skill in ipairs(skills) do
    result = result .. string.format("<tr><td class='skills'>%s</td><td class='spacer'></td><td class='rd'>%s</td><td class='spacer'></td><td class='rd'>%s</td></tr>",
      skill.name, skill.level, skill.cost)
  end
  result = result .. "</table>"

  return result
end


return p