Module realm_api.rpc.roll.parse
Parse segments from roll command arguments
Global variables
var addl_roll_regex-
Regex pattern for additional modifier rolls/constants
var roll_regex-
Regex pattern for initial roll
Functions
def parse_segments(roll: str) ‑> ParsedRoll-
Expand source code
def parse_segments(roll: str) -> ParsedRoll: """Parse a roll formula string into a list of RollSegment objects.""" batches = ParsedRoll() # strip all whitespace roll = roll.replace(" ", "") # parse first roll first = roll_regex.match(roll) assert first first = first.groupdict() # parse variable number of mods mods = [r.groupdict() for r in addl_roll_regex.finditer(roll)] batch = int(first["batch"] or 1) batches.max = int(first["max"]) if first["max"] else None batches.min = int(first["min"]) if first["min"] else None for m in mods: if m["batch"]: batch = int(m["batch"]) if m["max"]: batches.max = int(m["max"]) if m["min"]: batches.min = int(m["min"]) for _ in range(batch): segments: list[RollSegment] = [ DiceRoll( raw=first["all"], dice=int(first["num"] or "1"), faces=int(first["die"]), extra=first["fun"], ) ] for mod in mods: if mod["die"]: segments.append( DiceRoll( raw=mod["all"], negative=mod["op"] == "-", dice=int(mod["num"] or "1"), faces=int(mod["die"]), extra=mod["fun"], ) ) else: segments.append( ConstantModifier( raw=mod["all"], negative=mod["op"] == "-", number=int(mod["num"]), ) ) batches.append(segments) return batchesParse a roll formula string into a list of RollSegment objects.
Classes
class ParsedRoll (*args, **kwargs)-
Expand source code
class ParsedRoll(list[list[RollSegment]]): max: int | None = None min: int | None = NoneBuilt-in mutable sequence.
If no argument is given, the constructor creates a new empty list. The argument must be an iterable if specified.
Ancestors
- builtins.list
Class variables
var max : int | Nonevar min : int | None