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): self.commands = { '/roll': self.custom_roll, '/r': self.custom_roll, '/whisper': self.whisper, '/w': self.whisper } self.skill_dict = skill_dict 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\d+)(?:d|w)(?P\d+)(?P(?:(?:\+|\-)\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: return response.build_system_message(char, 'Ungültiger Charakter: {}'.format(char)) pattern = '(?P[a-zA-ZäöüÄÖÜß .&]+)(?P(?:\s*(?:\+|\-)\s*\d+)*)(?P[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: return response.build_system_message(char, '{} hat die Fertigkeit {} nicht.'.format(char, skill)) value = skills[skill] 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, 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)