Module:Dice/Format

From Caves of Qud Wiki
< Module:Dice
Revision as of 05:05, 19 July 2020 by Sol (talk | contribs) (update to be compatible with new version of Module:Dice)
Jump to navigation Jump to search

local Dice = require'Module:Dice'

local DiceFormat = {}

function DiceFormat.dice_stats(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 = 'Dice string'

    if (frame.args.template ~= nil and frame.args.template ~= '') then
        template_title = frame.args.template
    end

    local average = dice:average()
    local average_truncated = ('%d'):format(average)
    local maximum = dice:maximum()
    local minimum = dice:minimum()
    local range = dice:range()
    local variance = dice:variance()

    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