Module:Dice/Format: Difference between revisions

From Caves of Qud Wiki
Jump to navigation Jump to search
(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.diceStats(frame)
function DiceFormat.dice_stats(frame)
     local succeeded, result = pcall(Dice.fromRangeString, frame.args.roll)
     local succeeded, dice = pcall(Dice.from_range_string, frame.args.roll)


     if not succeeded then
     if not succeeded then
         result = Dice.fromDiceString(frame.args.roll)
         dice = Dice.from_dice_string(frame.args.roll)
     end
     end


     local dice = result
     local template_title = 'Dice string'
 
    local templatetitle = 'Dice string'


     if (frame.args.template ~= nil and frame.args.template ~= '') then
     if (frame.args.template ~= nil and frame.args.template ~= '') then
         templatetitle = frame.args.template
         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 mean = dice:mean()
     local range = dice:range()
     local maximum = dice:maximum()
     local variance = dice:variance()


     return frame:expandTemplate{
     return frame:expandTemplate{
         title = templatetitle,
         title = template_title,
         args = {
         args = {
            average = average,
            average_truncated = average_truncated,
            maximum = maximum,
             minimum = minimum,
             minimum = minimum,
             mean = mean,
             range = range,
             mean_truncated = ('%d'):format(mean),
             variance = variance,
             maximum = maximum,
 
            -- for backwards compatibility
            mean = average,
             mean_truncated = average_truncated,
         },
         },
     }
     }

Revision as of 05:05, 19 July 2020


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