initial commit backend
This commit is contained in:
79
chat/chat_commands.py
Normal file
79
chat/chat_commands.py
Normal file
@@ -0,0 +1,79 @@
|
||||
import chat.dice as dice
|
||||
import chat.response as response
|
||||
from chat.command_exception import CommandException
|
||||
import re
|
||||
|
||||
|
||||
class CommandHandler:
|
||||
def __init__(self):
|
||||
self.commands = {
|
||||
'/roll': self.custom_roll,
|
||||
'/r': self.custom_roll,
|
||||
'/whisper': self.whisper,
|
||||
'/w': self.whisper
|
||||
}
|
||||
self.skill_map = {'Tera': {'Akrobatik': 25,
|
||||
'Arkane Kunde': 10}
|
||||
}
|
||||
|
||||
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):
|
||||
print(char)
|
||||
print(message)
|
||||
print(self.skill_map)
|
||||
if char not in self.skill_map:
|
||||
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_map[char]
|
||||
if match.group('skill')[1:] not in skills:
|
||||
return response.build_system_message(char,
|
||||
'{} hat die Fertigkeit {} nicht.'.format(char, match.group('skill')))
|
||||
|
||||
skill = skills[match.group('skill')[1:]]
|
||||
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(skill,
|
||||
match.group('maths'),
|
||||
type_)
|
||||
print(eyes, result)
|
||||
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)
|
||||
Reference in New Issue
Block a user