continued on regex and die roll maths

This commit is contained in:
2020-07-27 18:07:00 +03:00
parent 42f0463e26
commit 48167cfd10
2 changed files with 17 additions and 3 deletions

View File

@@ -1,6 +1,6 @@
from typing import Dict from typing import Dict
import re import re
import random import numpy as np
commands: Dict = {} commands: Dict = {}
@@ -15,7 +15,8 @@ def command(*commandlist):
def handle_command(kwargs): def handle_command(kwargs):
c = kwargs['message'].split()[0] c = kwargs['message'].split()[0]
params = kwargs['message'].replace(c, '').strip() params = kwargs['message'].replace(c, '')
params = re.sub(r'\s+', '', params)
if c in commands: if c in commands:
commands[c](kwargs, params) commands[c](kwargs, params)
@@ -25,8 +26,20 @@ def handle_command(kwargs):
@command('/roll') @command('/roll')
def custom_roll(kwargs, params): def custom_roll(kwargs, params):
pattern = re.compile('(?P<dice_num>(?:\+|\-)?\d+)(?i:d)(?P<dice_val>\d+)(?P<maths>(?:(?:\+|\-)\d+)*(?!(?i:d)))') 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) 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'/roll received {kwargs}. Roll parameters: {params}. Rolls recognized: {rolls}')

View File

@@ -3,3 +3,4 @@ flask==1.1.2
flask-cors==3.0.8 flask-cors==3.0.8
flask-restful==0.3.8 flask-restful==0.3.8
flask-socketio==4.3.1 flask-socketio==4.3.1
numpy==1.19.1