Files
splittermond-backend/chat/chat_commands.py

76 lines
2.9 KiB
Python
Raw Normal View History

2019-06-17 18:17:59 +02:00
import chat.dice as dice
import chat.response as response
from chat.command_exception import CommandException
import re
class CommandHandler:
def __init__(self, skill_dict):
2019-06-17 18:17:59 +02:00
self.commands = {
'/roll': self.custom_roll,
'/r': self.custom_roll,
'/whisper': self.whisper,
'/w': self.whisper
}
self.skill_dict = skill_dict
2019-06-17 18:17:59 +02:00
def handle(self, char, content):
message = content.strip('\n')
if message[0] == '.':
return self.skill_roll(char, message)
else:
message = message.split(' ', 1)
if (len(message) > 1) and (message[0] in self.commands):
func = self.commands[message[0]]
r = func(char, message)
return r
return response.build_public_message(char, content)
def custom_roll(self, char, message):
pattern = '(?P<dice_num>\d+)(?:d|w)(?P<dice_val>\d+)(?P<maths>(?:(?:\+|\-)\d+)*)'
command = re.sub('\s+', '', message[1])
match = re.match(pattern, command)
if match is None:
return response.build_system_message(char,
'Ungültige Formatierung des Befehls: {}'.format(' '.join(message)))
eyes, result = dice.custom_roll(int(match.group('dice_num')),
int(match.group('dice_val')),
match.group('maths'))
return response.build_dice_roll(char, ' '.join(message), eyes, result)
def skill_roll(self, char, message):
if char not in self.skill_dict:
2019-06-17 18:17:59 +02:00
return response.build_system_message(char, 'Ungültiger Charakter: {}'.format(char))
pattern = '(?P<skill>[a-zA-ZäöüÄÖÜß .&]+)(?P<maths>(?:\s*(?:\+|\-)\s*\d+)*)(?P<type>[nrs]?)'
match = re.match(pattern, message)
if match is None:
return response.build_system_message(char,
'Unültige Formatierung des Befehls: {}'.format(message))
skills = self.skill_dict[char]
skill = match.group('skill')[1:]
skill = skill.rstrip(' ')
if skill not in skills:
2019-06-17 18:17:59 +02:00
return response.build_system_message(char,
'{} hat die Fertigkeit {} nicht.'.format(char, skill))
2019-06-17 18:17:59 +02:00
value = skills[skill]
2019-06-17 18:17:59 +02:00
type_ = match.group('type')
if len(type_) == 0 or type_ == 'n':
type_ = dice.RollTypes.NORMAL
elif type_ == 'r':
type_ = dice.RollTypes.RISKY
else:
type_ = dice.RollTypes.SAFE
eyes, result = dice.skill_roll(value,
2019-06-17 18:17:59 +02:00
match.group('maths'),
type_)
return response.build_dice_roll(char, message, eyes, result)
def whisper(self, char, message):
recipient, message = message.split(' ', 1)
return response.build_private_message(char, message, recipient)