Module:DiceUtils: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
Line 22: | Line 22: | ||
Slashing = {} | Slashing = {} | ||
} | } | ||
for damage_die in string.gmatch(args.dice, "([^,]+)") do | for damage_die in string.gmatch(args.dice, "([^,]+)") do | ||
idx = string.find(damage_die, ":") | idx = string.find(damage_die, ":") | ||
Line 32: | Line 32: | ||
for d_type, dice in pairs(damage) do | for d_type, dice in pairs(damage) do | ||
if not next(dice) == nil then | if not (next(dice) == nil) then | ||
min_damage = 0 | min_damage = 0 | ||
max_damage = 0 | max_damage = 0 | ||
Line 48: | Line 48: | ||
damage_str = string.format("%s %s %s", table.concat(dice, " + "), d_type, avg_str) | damage_str = string.format("%s %s %s", table.concat(dice, " + "), d_type, avg_str) | ||
table.insert( | table.insert(damage_str) | ||
end | end | ||
end | end |
Revision as of 07:47, 19 July 2023
Documentation for this module may be created at Module:DiceUtils/doc
local p = {}
function p.calculateDamage(frame)
args = frame.args
result = {}
damage = {
Acid = {},
Cold = {},
Fire = {},
Force = {},
Healing = {},
Lightning = {},
Necrotic = {},
Poison = {},
Radiant = {},
Thunder = {},
Psychic = {},
Physical = {},
Piercing = {},
Bludgeoning = {},
Slashing = {}
}
for damage_die in string.gmatch(args.dice, "([^,]+)") do
idx = string.find(damage_die, ":")
die = string.sub(damage_die, 1, idx - 1)
d_type = string.sub(damage_die, idx + 1)
table.insert(damage[d_type], die)
end
for d_type, dice in pairs(damage) do
if not (next(dice) == nil) then
min_damage = 0
max_damage = 0
avg_str = ""
for i, die in ipairs(dice) do
idx = string.find(die, "d")
count = tonumber(string.sub(die, 1, idx -1))
value = tonumber(string.sub(die, idx + 1))
min_damage = min_damage + count
max_damage = max_damage + value * count
avg_str = string.format("(%d - %d)", min_damage, max_damage)
end
damage_str = string.format("%s %s %s", table.concat(dice, " + "), d_type, avg_str)
table.insert(damage_str)
end
end
return table.concat(result, " ")
end
return p