From 48167cfd10a0d5d1f8362398d72e06925f90ee96 Mon Sep 17 00:00:00 2001 From: yes Date: Mon, 27 Jul 2020 18:07:00 +0300 Subject: [PATCH] continued on regex and die roll maths --- chatCommands.py | 19 ++++++++++++++++--- requirements.txt | 1 + 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/chatCommands.py b/chatCommands.py index 8038dc4..2864038 100644 --- a/chatCommands.py +++ b/chatCommands.py @@ -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(?:\+|\-)?\d+)(?i:d)(?P\d+)(?P(?:(?:\+|\-)\d+)*(?!(?i:d)))') + pattern = re.compile('(?P(?:\+|\-)?)(?P\d+)(?i:d)(?P\d+)(?P(?:(?:\+|\-)\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}') diff --git a/requirements.txt b/requirements.txt index 9cff871..a4382a1 100644 --- a/requirements.txt +++ b/requirements.txt @@ -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 \ No newline at end of file