Module:Dice/Format: Difference between revisions

Jump to navigation Jump to search
767 bytes added ,  06:18, 25 August 2023
oops, pcall no longer necessary
imported>Ontoclasm
mNo edit summary
(oops, pcall no longer necessary)
 
(3 intermediate revisions by 2 users not shown)
Line 1: Line 1:
require 'math'
local Dice = require'Module:Dice'
local Dice = require'Module:Dice'


Line 4: Line 5:


function DiceFormat.diceStats(frame)
function DiceFormat.diceStats(frame)
     local succeeded, dice = pcall(Dice.from_range_string, frame.args.roll)
     local dice = Dice.parse(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 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.multiplier or 1
local mult = (frame.args.multiplier ~= nil and frame.args.multiplier ~= '') and frame.args.multiplier or 1
local extra_afterroll = (frame.args.extraafterroll ~= nil and frame.args.extraafterroll ~= '') and frame.args.extraafterroll or 0


     local average = dice:average() * mult
     local average = dice:average() * mult + extra_afterroll
     local average_truncated = ('%d'):format(average)
     local average_truncated = ('%d'):format(average)
     local maximum = dice:maximum() * mult
     local maximum = dice:maximum() * mult + extra_afterroll
     local minimum = dice:minimum() * mult
     local minimum = dice:minimum() * mult + extra_afterroll
     local range = dice:range() * mult
     local range = dice:range() * mult
     local variance = dice:variance() * mult
     local variance = dice:variance() * mult
   
    -- For character stats which have a multiplier, the game rounds up to the
    -- nearest integer; this also affects the calculated average. These values
    -- are used by Template:Character/Attribute and /DetailedAttribute.
    local maximum_ceil = math.floor(maximum + 0.5)
    local minimum_ceil = math.floor(minimum + 0.5)
    local ceil_based_average = (maximum_ceil + minimum_ceil) / 2.0
    local ceil_based_average_truncated = ('%d'):format(ceil_based_average)


     return frame:expandTemplate{
     return frame:expandTemplate{
Line 29: Line 35:
             range = range,
             range = range,
             variance = variance,
             variance = variance,
            maximum_ceil = maximum_ceil,
            minimum_ceil = minimum_ceil,
            ceil_based_average = ceil_based_average,
            ceil_based_average_truncated = ceil_based_average_truncated,


             -- for backwards compatibility
             -- for backwards compatibility

Navigation menu