Exemplo n.º 1
0
def roll(dice_spec: str) -> Tuple[int, Sequence[int]]:
    tracer = execution_context.get_opencensus_tracer()
    try:
        with tracer.span('initial_parse'):
            tracer.add_attribute_to_current_span("dice_spec", dice_spec)
            tree = get_parser().parse(dice_spec)
    except LarkError as e:
        raise RecognitionError(
            "Sorry, I couldn't understand your request") from e
    logging.debug("Initial parse tree:\n%s", pprint(tree))
    try:
        tree = NumberTransformer().transform(tree)
        with tracer.span('dnd_knowledge'):
            tree = DnD5eKnowledge().transform(tree)
        tree = SimplifyTransformer().transform(tree)
        with tracer.span('crit_transform'):
            tree = CritTransformer().transform(tree)
        logging.debug("DnD transformed parse tree:\n%s", pprint(tree))
        with tracer.span('final_eval'):
            transformer = EvalDice()
            tree = transformer.transform(tree)
        tree = SimplifyTransformer().transform(tree)
    except VisitError as e:
        #  Get our nice exception out of lark's wrapper
        raise e.orig_exc
    return (tree.children[0], transformer.dice_results)
Exemplo n.º 2
0
 def test_spell_default(self):
     initial_tree = Tree("spell_default", [Token('SPELL_NAME', 'fireball')])
     final_tree = DnD5eKnowledge().transform(initial_tree)
     self.assertSimpleTreeEqual(final_tree, self.fireball_tree)
Exemplo n.º 3
0
 def test_unknown_spell(self):
     initial_tree = Tree("value", [Token('SPELL_NAME', 'sadfsdf')])
     with self.assertRaises(Exception):
         DnD5eKnowledge().transform(initial_tree)
Exemplo n.º 4
0
 def test_dice_weapon(self):
     initial_tree = Tree("value", [Token('WEAPON', 'Club')])
     final_tree = DnD5eKnowledge().transform(initial_tree)
     self.assertSimpleTreeEqual(final_tree, self.club_tree)
Exemplo n.º 5
0
 def test_unknown_weapon(self):
     initial_tree = Tree("value", [Token('WEAPON', 'sadfsdf')])
     with self.assertRaises(Exception):
         DnD5eKnowledge().transform(initial_tree)
Exemplo n.º 6
0
 def test_weapon_wrong_case(self):
     initial_tree = Tree("value", [Token('WEAPON', 'cLuB')])
     final_tree = DnD5eKnowledge().transform(initial_tree)
     self.assertSimpleTreeEqual(final_tree, self.club_tree)
Exemplo n.º 7
0
 def test_const_weapon(self):
     initial_tree = Tree("value", [Token('WEAPON', 'Blowgun')])
     final_tree = DnD5eKnowledge().transform(initial_tree)
     self.assertSimpleTreeEqual(final_tree, 1)
Exemplo n.º 8
0
 def test_spell_reverse(self):
     initial_tree = Tree("spell_reversed",
                         [5, Token('SPELL_NAME', 'fireball')])
     final_tree = DnD5eKnowledge().transform(initial_tree)
     self.assertSimpleTreeEqual(final_tree, self.fireball_level_five_tree)
Exemplo n.º 9
0
 def test_spell_lower_level(self):
     initial_tree = Tree("spell", [Token('SPELL_NAME', 'fireball'), 1])
     with self.assertRaises(Exception):
         DnD5eKnowledge().transform(initial_tree)