diff options
| author | 2023-12-09 23:16:50 +0800 | |
|---|---|---|
| committer | 2023-12-09 23:16:50 +0800 | |
| commit | 5781a072250a147d5e636b269fd0a8b7a0b045da (patch) | |
| tree | affaf98bdc946e01318e965dbff74baf2343f747 /src/hydrorollcore/rule.py | |
| parent | 0e0c734226b0e954ccf66dc3c20598e11f494555 (diff) | |
| parent | 672373d8d973d3c862fc7365aed39fee29613638 (diff) | |
| download | infini-5781a072250a147d5e636b269fd0a8b7a0b045da.tar.gz infini-5781a072250a147d5e636b269fd0a8b7a0b045da.zip | |
Merge pull request #35 from HydroRoll-Team/sourcery/pull-34
✨ 完善基类声明 (Sourcery refactored)
Diffstat (limited to 'src/hydrorollcore/rule.py')
| -rw-r--r-- | src/hydrorollcore/rule.py | 51 |
1 files changed, 48 insertions, 3 deletions
diff --git a/src/hydrorollcore/rule.py b/src/hydrorollcore/rule.py index a1b07cb9..127ed805 100644 --- a/src/hydrorollcore/rule.py +++ b/src/hydrorollcore/rule.py @@ -1,7 +1,8 @@ from abc import ABCMeta, abstractmethod from enum import Enum +from .exceptions import HydroError -__all__ = ["RuleLoadType", "Rule"] +__all__ = ["RuleLoadType", "Result", "Dice", "Rule"] class RuleLoadType(Enum): @@ -13,6 +14,50 @@ class RuleLoadType(Enum): CLASS = "class" +class Result(metaclass=ABCMeta): + """规则检定结果基类""" + + event: str + status: bool + exception: HydroError | None = None + + def __init__(self, event: str, status: bool, exception: HydroError | None) -> None: + self.event = event + self.status = status + self.exception = exception + + def ok(self): + """规则执行期间是否产生异常""" + return isinstance(self.exception, HydroError) + + +class Dice(metaclass=ABCMeta): + """掷骰基类""" + + roll_string: str + db: str + outcome: int + + def __repr__(self) -> str: + return f'<HydroDice "{self.db.upper()}">' + + def __str__(self) -> str: + return self.db.upper() + + def __int__(self) -> int: + return self.outcome + + @abstractmethod + def parse(self) -> "Dice": + """解析传入的掷骰字符串`roll_string`,返回一个`Dice`对象""" + raise NotImplementedError + + @abstractmethod + def roll(self) -> int: + """掷骰方法,返回掷骰结果""" + raise NotImplementedError + + class Rule(metaclass=ABCMeta): """规则基类""" @@ -20,9 +65,9 @@ class Rule(metaclass=ABCMeta): priority: int = 0 @abstractmethod - def __init__(self): + def __init__(self) -> None: raise NotImplementedError @abstractmethod - async def run(self): + def check(self, dice: Dice) -> Result: raise NotImplementedError |
