Module:Dice/Format: Difference between revisions

From Caves of Qud Wiki
Jump to navigation Jump to search
m (DiceStats -> DiceFormat)
(add truncated mean; minor refactoring)
Line 5: Line 5:
function DiceFormat.diceStats(frame)
function DiceFormat.diceStats(frame)
     local dice = Dice.fromString(frame.args.roll)
     local dice = Dice.fromString(frame.args.roll)
     local templatetitle
 
     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
         templatetitle = frame.args.template
    else
        templatetitle = 'Dice string'
     end
     end
    local minimum = dice:minimum()
    local mean = dice:mean()
    local maximum = dice:maximum()
     return frame:expandTemplate{
     return frame:expandTemplate{
          
         title = templatetitle,
            title = templatetitle,
         args = {
         args = {
             minimum = dice:minimum(),
             minimum = minimum,
             mean = dice:mean(),
             mean = mean,
             maximum = dice:maximum(),
            mean_truncated = ('%d'):format(mean),
             maximum = maximum,
         },
         },
     }
     }

Revision as of 17:50, 10 August 2019


local Dice = require'Module:Dice'

local DiceFormat = {}

function DiceFormat.diceStats(frame)
    local dice = Dice.fromString(frame.args.roll)

    local templatetitle = 'Dice string'

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

    local minimum = dice:minimum()
    local mean = dice:mean()
    local maximum = dice:maximum()

    return frame:expandTemplate{
        title = templatetitle,
        args = {
            minimum = minimum,
            mean = mean,
            mean_truncated = ('%d'):format(mean),
            maximum = maximum,
        },
    }
end

return DiceFormat