How to use the d20.Roller function in d20

To help you get started, we’ve selected a few d20 examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github avrae / avrae / cogs5e / funcs / scripting / functions.py View on Github external
def _vroll(dice, multiply=1, add=0, roller=None):
    if roller is None:
        roller = d20.Roller()

    dice_ast = roller.parse(dice)

    if multiply != 1 or add != 0:
        def mapper(node):
            if isinstance(node, d20.ast.Dice):
                node.num = (node.num * multiply) + add
            return node

        dice_ast = d20.utils.tree_map(mapper, dice_ast)

    try:
        rolled = roller.roll(dice_ast)
    except d20.RollError:
        return None
    return SimpleRollResult(rolled)
github avrae / avrae / cogs5e / funcs / scripting / functions.py View on Github external
def _roll(dice, roller=None):
    if roller is None:
        roller = d20.Roller()

    try:
        result = roller.roll(dice)
    except d20.RollError:
        return 0
    return result.total
github avrae / avrae / cogs5e / dice.py View on Github external
async def _roll_many(self, ctx, iterations, roll_str, dc=None, adv=None):
        if iterations < 1 or iterations > 100:
            return await ctx.send("Too many or too few iterations.")
        if adv is None:
            adv = d20.AdvType.NONE
        results = []
        successes = 0
        ast = d20.parse(roll_str, allow_comments=True)
        roller = d20.Roller(context=PersistentRollContext())

        for _ in range(iterations):
            res = roller.roll(ast, advantage=adv)
            if dc is not None and res.total >= dc:
                successes += 1
            results.append(res)

        if dc is None:
            header = f"Rolling {iterations} iterations..."
            footer = f"{sum(o.total for o in results)} total."
        else:
            header = f"Rolling {iterations} iterations, DC {dc}..."
            footer = f"{successes} successes, {sum(o.total for o in results)} total."

        if ast.comment:
            header = f"{ast.comment}: {header}"
github avrae / avrae / cogs5e / funcs / scripting / evaluators.py View on Github external
)

        # char-agnostic globals
        self.builtins.update(
            set=self.set, exists=self.exists, combat=self.combat,
            get_gvar=self.get_gvar,
            set_uvar=self.set_uvar, delete_uvar=self.delete_uvar, set_uvar_nx=self.set_uvar_nx,
            uvar_exists=self.uvar_exists,
            chanid=self.chanid, servid=self.servid,
            get=self.get,
            load_json=self.load_json, dump_json=self.dump_json,
            argparse=argparse
        )

        # roll limiting
        self._roller = d20.Roller(context=PersistentRollContext(max_rolls=1_000, max_total_rolls=10_000))
        self.builtins.update(
            vroll=self._limited_vroll,
            roll=self._limited_roll
        )

        self._cache = {
            "gvars": {},
            "uvars": {}
        }

        self.ctx = ctx
        self.character_changed = False
        self.combat_changed = False
        self.uvars_changed = set()