From e6b74981c6f713fb30d9c439cce2c1226d36acb5 Mon Sep 17 00:00:00 2001 From: 苏向夜 Date: Sat, 9 Dec 2023 21:20:40 +0800 Subject: :sparkles: 新增掷骰基类 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/hydrorollcore/rule.py | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) (limited to 'src/hydrorollcore/rule.py') diff --git a/src/hydrorollcore/rule.py b/src/hydrorollcore/rule.py index a1b07cb9..7dd24836 100644 --- a/src/hydrorollcore/rule.py +++ b/src/hydrorollcore/rule.py @@ -13,6 +13,34 @@ class RuleLoadType(Enum): CLASS = "class" +class Result(metaclass=ABCMeta): + """规则检定结果基类""" + + event: str + + +class Dice(metaclass=ABCMeta): + """掷骰基类""" + + roll_string: str + db: str + outcome: int + + def __repr__(self) -> str: + return f'' + + def __str__(self) -> str: + return self.db.upper() + + @abstractmethod + def parse(self) -> "Dice": + raise NotImplementedError + + @abstractmethod + def roll(self) -> int: + raise NotImplementedError + + class Rule(metaclass=ABCMeta): """规则基类""" @@ -20,9 +48,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) -> Result: raise NotImplementedError -- cgit v1.2.3-70-g09d2 From c6a15e6f8fc96ae9fe61bbadcaca8e14af593320 Mon Sep 17 00:00:00 2001 From: 苏向夜 Date: Sat, 9 Dec 2023 21:48:31 +0800 Subject: :sparkles: 完善Result基类 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/hydrorollcore/__init__.py | 4 ++-- src/hydrorollcore/rule.py | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) (limited to 'src/hydrorollcore/rule.py') diff --git a/src/hydrorollcore/__init__.py b/src/hydrorollcore/__init__.py index f0a096fd..7a2bba77 100644 --- a/src/hydrorollcore/__init__.py +++ b/src/hydrorollcore/__init__.py @@ -1,6 +1,6 @@ from HydroRollCore.cli import Cli from HydroRollCore.config import Config -from HydroRollCore.rule import Rule +from HydroRollCore.rule import Rule, Result, Dice from HydroRollCore.core import Core -__all__ = ["Core", "Rule", "Config", "Cli"] +__all__ = ["Core", "Rule", "Config", "Cli", "Result", "Dice"] diff --git a/src/hydrorollcore/rule.py b/src/hydrorollcore/rule.py index 7dd24836..d2ea4a19 100644 --- a/src/hydrorollcore/rule.py +++ b/src/hydrorollcore/rule.py @@ -1,5 +1,6 @@ from abc import ABCMeta, abstractmethod from enum import Enum +from .exceptions import HydroError __all__ = ["RuleLoadType", "Rule"] @@ -17,6 +18,17 @@ 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): @@ -32,12 +44,17 @@ class Dice(metaclass=ABCMeta): 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 -- cgit v1.2.3-70-g09d2 From 2838466c563df69d9de8c6e9b69f14b07b8cf2c0 Mon Sep 17 00:00:00 2001 From: 苏向夜 Date: Sat, 9 Dec 2023 22:21:11 +0800 Subject: :bug: 修复Rule参数不全的问题 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/hydrorollcore/rule.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/hydrorollcore/rule.py') diff --git a/src/hydrorollcore/rule.py b/src/hydrorollcore/rule.py index d2ea4a19..127ed805 100644 --- a/src/hydrorollcore/rule.py +++ b/src/hydrorollcore/rule.py @@ -2,7 +2,7 @@ from abc import ABCMeta, abstractmethod from enum import Enum from .exceptions import HydroError -__all__ = ["RuleLoadType", "Rule"] +__all__ = ["RuleLoadType", "Result", "Dice", "Rule"] class RuleLoadType(Enum): @@ -69,5 +69,5 @@ class Rule(metaclass=ABCMeta): raise NotImplementedError @abstractmethod - def check(self) -> Result: + def check(self, dice: Dice) -> Result: raise NotImplementedError -- cgit v1.2.3-70-g09d2