started refactoring of dicerolling to use a context-free grammar.

This commit is contained in:
2020-07-29 20:31:10 +03:00
parent 48167cfd10
commit d2654fac24
4 changed files with 81 additions and 17 deletions

View File

@@ -1,6 +1,7 @@
from typing import Dict
import re
import numpy as np
from dice.dice import Dice
commands: Dict = {}
@@ -16,7 +17,6 @@ def command(*commandlist):
def handle_command(kwargs):
c = kwargs['message'].split()[0]
params = kwargs['message'].replace(c, '')
params = re.sub(r'\s+', '', params)
if c in commands:
commands[c](kwargs, params)
@@ -26,21 +26,9 @@ def handle_command(kwargs):
@command('/roll')
def custom_roll(kwargs, params):
pattern = re.compile('(?P<sign>(?:\+|\-)?)(?P<dice_num>\d+)(?i:d)(?P<dice_val>\d+)(?P<maths>(?:(?:\+|\-)\d+)*(?!(?i:d)))')
rolls = re.findall(pattern, params)
rng = np.random.default_rng()
results = []
for roll in rolls:
result = []
if int(roll[2]) < 2:
result.append(int(roll[2]))
else:
result.append(rng.integers(1, int(roll[2]), size=int(roll[1])).tolist())
result.append(roll[3])
results.append(result)
print(results)
print(f'/roll received {kwargs}. Roll parameters: {params}. Rolls recognized: {rolls}')
print(f'Kwargs: {kwargs}. Params: {params}')
dice = Dice()
print(dice.roll(params))
def error_response(kwargs, params):