Compare commits
5 Commits
main
...
chatcomman
| Author | SHA1 | Date | |
|---|---|---|---|
| d2654fac24 | |||
| 48167cfd10 | |||
| 42f0463e26 | |||
| c5a482c9b1 | |||
| 4f5c790dc1 |
35
chatCommands.py
Normal file
35
chatCommands.py
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
from typing import Dict
|
||||||
|
import re
|
||||||
|
import numpy as np
|
||||||
|
from dice.dice import Dice
|
||||||
|
|
||||||
|
commands: Dict = {}
|
||||||
|
|
||||||
|
|
||||||
|
def command(*commandlist):
|
||||||
|
def add_command(function):
|
||||||
|
for command in commandlist:
|
||||||
|
commands[command] = function
|
||||||
|
return function
|
||||||
|
return add_command
|
||||||
|
|
||||||
|
|
||||||
|
def handle_command(kwargs):
|
||||||
|
c = kwargs['message'].split()[0]
|
||||||
|
params = kwargs['message'].replace(c, '')
|
||||||
|
|
||||||
|
if c in commands:
|
||||||
|
commands[c](kwargs, params)
|
||||||
|
else:
|
||||||
|
error_response(kwargs, params)
|
||||||
|
|
||||||
|
|
||||||
|
@command('/roll')
|
||||||
|
def custom_roll(kwargs, params):
|
||||||
|
print(f'Kwargs: {kwargs}. Params: {params}')
|
||||||
|
dice = Dice()
|
||||||
|
print(dice.roll(params))
|
||||||
|
|
||||||
|
|
||||||
|
def error_response(kwargs, params):
|
||||||
|
print(f'Error, received unknown command: {kwargs}')
|
||||||
0
dice/__init__.py
Normal file
0
dice/__init__.py
Normal file
75
dice/dice.py
Normal file
75
dice/dice.py
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
import pyparsing
|
||||||
|
import math
|
||||||
|
import operator
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
from pyparsing import (
|
||||||
|
Literal,
|
||||||
|
CaselessLiteral,
|
||||||
|
Word,
|
||||||
|
Optional,
|
||||||
|
Group,
|
||||||
|
Forward,
|
||||||
|
nums,
|
||||||
|
ParseException,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class Dice:
|
||||||
|
|
||||||
|
exprStack = []
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def roll(self, roll_params):
|
||||||
|
self.exprStack[:] = [] # Reset the stack
|
||||||
|
try:
|
||||||
|
results = self.BNF().parseString(roll_params, parseAll=True)
|
||||||
|
except ParseException as pe:
|
||||||
|
print("Parse exception ", str(pe))
|
||||||
|
return
|
||||||
|
except Exception as e:
|
||||||
|
print("Exception ", str(e), self.exprStack)
|
||||||
|
|
||||||
|
return results
|
||||||
|
|
||||||
|
def push_first(self, tokens):
|
||||||
|
self.exprStack.append(tokens[0])
|
||||||
|
|
||||||
|
def BNF(self):
|
||||||
|
|
||||||
|
integer = Word(nums)
|
||||||
|
plus, minus = map(Literal, "+-")
|
||||||
|
|
||||||
|
addop = plus | minus
|
||||||
|
rollop = CaselessLiteral('d')
|
||||||
|
# lowop = CaselessLiteral('l')
|
||||||
|
# highop = CaselessLiteral('h')
|
||||||
|
|
||||||
|
# Define roll as a group
|
||||||
|
roll = Group(Optional(integer, default=1) + rollop + integer)
|
||||||
|
|
||||||
|
expression = Forward()
|
||||||
|
|
||||||
|
operand = (
|
||||||
|
addop[...]
|
||||||
|
+ (
|
||||||
|
# Numbers as a fallback, add everything else as (roll | exception | ...)
|
||||||
|
(roll).setParseAction(self.push_first)
|
||||||
|
| integer
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
term = operand + (rollop + operand).setParseAction(self.push_first)[...]
|
||||||
|
expression <<= term + (addop + term).setParseAction(self.push_first)[...]
|
||||||
|
bnf = expression
|
||||||
|
|
||||||
|
return bnf
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
7
main.py
7
main.py
@@ -1,5 +1,6 @@
|
|||||||
import random
|
import random
|
||||||
import time
|
import time
|
||||||
|
import chatCommands
|
||||||
|
|
||||||
from flask import Flask, request
|
from flask import Flask, request
|
||||||
from flask_cors import CORS
|
from flask_cors import CORS
|
||||||
@@ -12,6 +13,7 @@ cors = CORS(app)
|
|||||||
sio = SocketIO(app, cors_allowed_origins='*')
|
sio = SocketIO(app, cors_allowed_origins='*')
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@app.route('/')
|
@app.route('/')
|
||||||
def home():
|
def home():
|
||||||
return {'url': '/', 'body': 'test body'}
|
return {'url': '/', 'body': 'test body'}
|
||||||
@@ -36,6 +38,11 @@ def test(user):
|
|||||||
|
|
||||||
@sio.on('public message')
|
@sio.on('public message')
|
||||||
def public_message(kwargs):
|
def public_message(kwargs):
|
||||||
|
kwargs['message'].strip()
|
||||||
|
if 'message' in kwargs:
|
||||||
|
if kwargs['message'][0] == '/':
|
||||||
|
chatCommands.handle_command(kwargs)
|
||||||
|
else:
|
||||||
print(kwargs)
|
print(kwargs)
|
||||||
sio.emit('public message', kwargs)
|
sio.emit('public message', kwargs)
|
||||||
|
|
||||||
|
|||||||
@@ -3,3 +3,5 @@ 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
|
||||||
|
pyparsing==2.4.7
|
||||||
Reference in New Issue
Block a user