-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored to introduce operation result and allow ui improvement and… (
#17) * Refactored to introduce operation results and allow ui improvement and web output formatter. * linting * linting and extract function for removing prefix * fix import issue.
- Loading branch information
1 parent
10b4464
commit 67bed45
Showing
28 changed files
with
440 additions
and
220 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,33 @@ | ||
from . import basic, token, regex, shadow_run | ||
from .basic import BasicOperation | ||
from .regex import RegexOperation | ||
from .shadow_run import ShadowRunOperation | ||
from .token import TokenOperation | ||
|
||
operations = { | ||
"!r-t": token, | ||
"!r-r": regex, | ||
"!rb": basic, | ||
"!r-b": basic, | ||
"!roll": token, | ||
"!r": token, | ||
"!sr": shadow_run, | ||
"!r-t": TokenOperation(), | ||
"!r-r": RegexOperation(), | ||
"!rb": BasicOperation(), | ||
"!r-b": BasicOperation(), | ||
"!roll": TokenOperation(), | ||
"!r": TokenOperation(), | ||
"!sr": ShadowRunOperation(), | ||
} | ||
|
||
|
||
def clean_command(command_string): | ||
return command_string.lower().replace("! ", "!").lstrip() | ||
|
||
|
||
def remove_prefix(command_string, prefix): | ||
if command_string.startswith(prefix): | ||
return command_string[len(prefix):] | ||
return command_string.lstrip() | ||
|
||
|
||
def get_command(message_content: str): | ||
clean_command_string = clean_command(message_content) | ||
for prefix, op_getter in operations.items(): | ||
if clean_command_string.startswith(prefix): | ||
operation_input = clean_command_string[len(prefix):].lstrip() | ||
operation_input = remove_prefix(clean_command_string, prefix) | ||
return op_getter, operation_input | ||
return None, message_content |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,39 @@ | ||
from roll_witch.rolling.input import get_regex_parser | ||
from roll_witch.rolling.output import TargetedOutputWriter, StandardOutputWriter | ||
from roll_witch.rolling.roller import TargetedRoller, StandardRoller | ||
|
||
|
||
def execute(roll_string: str, user: str): | ||
roll_spec = get_spec(roll_string) | ||
if roll_spec.has_target(): | ||
return do_targeted_roll(roll_spec, user) | ||
else: | ||
return do_standard_roll(roll_spec, user) | ||
|
||
|
||
def get_spec(roll_string): | ||
parser = get_regex_parser() | ||
roll_spec = parser.parse(roll_string) | ||
return roll_spec | ||
|
||
|
||
def do_targeted_roll(spec, user): | ||
try: | ||
roller = TargetedRoller() | ||
output = TargetedOutputWriter() | ||
roll_result = roller.roll(spec) | ||
output = output.write_output(roll_result, user) | ||
if len(output) > 2000: | ||
raise ValueError() | ||
return output | ||
except ValueError: | ||
raise Exception("Your answer is just too big to give you") | ||
except Exception as e: | ||
raise Exception(f"{e}") | ||
|
||
|
||
def do_standard_roll(spec, user): | ||
try: | ||
roller = StandardRoller() | ||
output = StandardOutputWriter() | ||
roll_result = roller.roll(spec) | ||
output = output.write_output(roll_result, user) | ||
if len(output) > 2000: | ||
raise ValueError() | ||
return output | ||
except ValueError: | ||
raise Exception("Your answer is just too big to give you") | ||
except Exception as e: | ||
raise Exception(f"{e}") | ||
from roll_witch.rolling.protocols import Operation, OperationResult | ||
from roll_witch.rolling.roller import OperationRollResults | ||
|
||
|
||
class RegexOperation(Operation): | ||
def __init__(self): | ||
self.name = "Regular Expression Roll" | ||
self.targeted_roller = TargetedRoller() | ||
self.standard_roller = StandardRoller() | ||
self.targeted_output_writer = TargetedOutputWriter() | ||
self.standard_output_writer = StandardOutputWriter() | ||
self.parser = get_regex_parser() | ||
|
||
def execute(self, roll_string: str, user: str) -> OperationResult: | ||
try: | ||
roll_spec = self.parser.parse(roll_string) | ||
result = OperationRollResults(roll_spec) | ||
if roll_spec.has_target(): | ||
result.append_roll_result(self.targeted_roller.roll(roll_spec)) | ||
result.met_target = self.targeted_roller.met_target(roll_spec, result.total) | ||
else: | ||
result.append_roll_result(self.standard_roller.roll(roll_spec)) | ||
return result | ||
except Exception as e: | ||
raise Exception(f"{e}") | ||
|
||
def format_output(self, result: OperationResult, user): | ||
try: | ||
if result.had_target(): | ||
return self.targeted_output_writer.write_output(result, user) | ||
else: | ||
return self.standard_output_writer.write_output(result, user) | ||
except ValueError: | ||
raise Exception("Your answer is just too big to give you") | ||
except Exception as e: | ||
raise Exception(f"{e}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,9 @@ | ||
from .standard import StandardOutputWriter | ||
from .target import TargetedOutputWriter | ||
from .operation import OperationOutputWriter | ||
from .base import OutputParser | ||
|
||
__all__ = [ | ||
"StandardOutputWriter", | ||
"TargetedOutputWriter", | ||
"OperationOutputWriter", | ||
"OutputParser", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,17 @@ | ||
from roll_witch.rolling.roller import RollResult | ||
from roll_witch.rolling.protocols import Result | ||
from abc import ABC, abstractmethod | ||
from typing import Protocol | ||
|
||
|
||
class OutputParser(Protocol): | ||
def write_output(self, roll_result: Result, user: str): | ||
return "Unknown" | ||
from ..protocols.result import OperationResult | ||
|
||
|
||
class BaseOutputWriter(ABC): | ||
def write_output(self, roll_result: Result, user): | ||
total_string = self.build_total_string(roll_result) | ||
return self.build_result_string(roll_result, total_string, user) | ||
def write_output(self, result: OperationResult, user): | ||
total_string = self.build_total_string(result) | ||
return self.build_result_string(result, total_string, user) | ||
|
||
def build_total_string(self, roll_result: Result): | ||
if isinstance(roll_result, RollResult): | ||
modifier_string = roll_result.formatted_modifier() | ||
return f"{roll_result.rolls} = {roll_result.roll_total}{modifier_string}" | ||
else: | ||
raise Exception("Invalid Output parser for given data") | ||
def build_total_string(self, result: OperationResult): | ||
roll_result = result.rolls[0] | ||
modifier_string = roll_result.formatted_modifier() | ||
return f"{roll_result.rolls} = {roll_result.roll_total}{modifier_string}" | ||
|
||
@abstractmethod | ||
def build_result_string(self, roll_result: Result, total_string, user): | ||
def build_result_string(self, roll_result: OperationResult, total_string, user): | ||
return "Unsupported" |
Oops, something went wrong.