def _determine_count(self, group, monster_groups): try: count = int(group['count']) except ValueError: if dice_expr.match(group['count']): if self.count_resolver: count = self.count_resolver(group['count'], group['monster']) else: count = roll_dice_expr(group['count']) else: group_count = group['count'] names = {x: 0 for x in re.findall(r"(\w+)", group['count']) if not x.isdigit()} for name in names: if name in monster_groups: names[name] = len(monster_groups[name]) elif name == 'players': names[name] = len([x for x in self.combat.characters.values() if x.ctype == 'player']) elif name == 'sidekicks': names[name] = len([x for x in self.combat.characters.values() if x.ctype == 'sidekick']) elif name == 'party': names[name] = len(self.combat.characters) group_count = group_count.replace(name, str(names[name])) if re.match(r"[^\d\s\(\)\+\-\*\/]", group_count): raise ValueError(f"Invalid monster count: {group['count']}") count = max(eval(group_count), 1) return count
def max_hp(self, value): try: self._max_hp = int(value) except ValueError: # we have an expression for max hp, so roll it self._max_hp = dice.roll_dice_expr(value) # setting max_hp for the first time? we should set cur_hp too if self.cur_hp is None: self.cur_hp = self._max_hp
def do_command(self, *args): results = [] for dice_expr in args: try: results.append(str(roll_dice_expr(dice_expr))) except ValueError: print(f"Invalid dice expression: {dice_expr}") return print(", ".join(results))
def convert_to_int_or_dice_expr(value): try: value = int(value) except ValueError: if "d" in value: try: value = roll_dice_expr(value) except ValueError: value = None else: value = None return value
def _determine_count(self, group, monster_groups): try: count = int(group['count']) except ValueError: if dice_expr.match(group['count']): if self.count_resolver: count = self.count_resolver(group['count'], group['monster']) else: count = roll_dice_expr(group['count']) elif group['count'] in monster_groups: count = len(monster_groups[group['count']]) elif '+' in group['count']: keys = [x.strip() for x in group['count'].split('+')] count = sum([ len(monster_groups[x]) for x in keys if x in monster_groups ]) else: raise ValueError(f"Invalid monster count: {group['count']}") return count