Module:Dice/Format: Difference between revisions
Jump to navigation
Jump to search
imported>Ontoclasm m (Add a 'multiplier' field to Dice/Format) |
imported>Ontoclasm mNo edit summary |
||
Line 11: | Line 11: | ||
local template_title = (frame.args.template ~= nil and frame.args.template ~= '') and frame.args.template or 'Dice string' | local template_title = (frame.args.template ~= nil and frame.args.template ~= '') and frame.args.template or 'Dice string' | ||
local mult = (frame.args.multiplier ~= nil and frame.args.multiplier ~= '') and frame.args. | local mult = (frame.args.multiplier ~= nil and frame.args.multiplier ~= '') and frame.args.multiplier or 1 | ||
local average = dice:average() * mult | local average = dice:average() * mult |
Revision as of 01:40, 4 December 2020
local Dice = require'Module:Dice'
local DiceFormat = {}
function DiceFormat.diceStats(frame)
local succeeded, dice = pcall(Dice.from_range_string, frame.args.roll)
if not succeeded then
dice = Dice.from_dice_string(frame.args.roll)
end
local template_title = (frame.args.template ~= nil and frame.args.template ~= '') and frame.args.template or 'Dice string'
local mult = (frame.args.multiplier ~= nil and frame.args.multiplier ~= '') and frame.args.multiplier or 1
local average = dice:average() * mult
local average_truncated = ('%d'):format(average)
local maximum = dice:maximum() * mult
local minimum = dice:minimum() * mult
local range = dice:range() * mult
local variance = dice:variance() * mult
return frame:expandTemplate{
title = template_title,
args = {
average = average,
average_truncated = average_truncated,
maximum = maximum,
minimum = minimum,
range = range,
variance = variance,
-- for backwards compatibility
mean = average,
mean_truncated = average_truncated,
},
}
end
return DiceFormat