1,046
edits
(autodetect which kind of string we wanted) |
(update to be compatible with new version of Module:Dice) |
||
Line 3: | Line 3: | ||
local DiceFormat = {} | local DiceFormat = {} | ||
function DiceFormat. | function DiceFormat.dice_stats(frame) | ||
local succeeded, | local succeeded, dice = pcall(Dice.from_range_string, frame.args.roll) | ||
if not succeeded then | if not succeeded then | ||
dice = Dice.from_dice_string(frame.args.roll) | |||
end | end | ||
local | local template_title = 'Dice string' | ||
if (frame.args.template ~= nil and frame.args.template ~= '') then | if (frame.args.template ~= nil and frame.args.template ~= '') then | ||
template_title = frame.args.template | |||
end | end | ||
local average = dice:average() | |||
local average_truncated = ('%d'):format(average) | |||
local maximum = dice:maximum() | |||
local minimum = dice:minimum() | local minimum = dice:minimum() | ||
local | local range = dice:range() | ||
local | local variance = dice:variance() | ||
return frame:expandTemplate{ | return frame:expandTemplate{ | ||
title = | title = template_title, | ||
args = { | args = { | ||
average = average, | |||
average_truncated = average_truncated, | |||
maximum = maximum, | |||
minimum = minimum, | minimum = minimum, | ||
range = range, | |||
variance = variance, | |||
-- for backwards compatibility | |||
mean = average, | |||
mean_truncated = average_truncated, | |||
}, | }, | ||
} | } |