continued on regex and die roll maths
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
from typing import Dict
|
||||
import re
|
||||
import random
|
||||
import numpy as np
|
||||
|
||||
commands: Dict = {}
|
||||
|
||||
@@ -15,7 +15,8 @@ def command(*commandlist):
|
||||
|
||||
def handle_command(kwargs):
|
||||
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:
|
||||
commands[c](kwargs, params)
|
||||
@@ -25,8 +26,20 @@ def handle_command(kwargs):
|
||||
|
||||
@command('/roll')
|
||||
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)
|
||||
|
||||
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}')
|
||||
|
||||
|
||||
|
||||
@@ -3,3 +3,4 @@ flask==1.1.2
|
||||
flask-cors==3.0.8
|
||||
flask-restful==0.3.8
|
||||
flask-socketio==4.3.1
|
||||
numpy==1.19.1
|
||||
Reference in New Issue
Block a user