Module:Dice/Format

From Caves of Qud Wiki
< Module:Dice
Revision as of 01:36, 4 December 2020 by imported>Ontoclasm (Add a 'multiplier' field to Dice/Format)
Jump to navigation Jump to search

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.mult 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