diff options
| author | 2023-12-15 10:08:56 +0800 | |
|---|---|---|
| committer | 2023-12-15 10:08:56 +0800 | |
| commit | d697a1774ad8571e9314e3bd85aa141170587d19 (patch) | |
| tree | cd3c3d84e3288db1f8e574a0b4dfb85e2e40f764 /src/infini | |
| parent | 4943d5eff52a75caaccda6a1d84183032f06be26 (diff) | |
| parent | 4dafb0f0a81255193f2a44df5d203239325e2236 (diff) | |
| download | infini-d697a1774ad8571e9314e3bd85aa141170587d19.tar.gz infini-d697a1774ad8571e9314e3bd85aa141170587d19.zip | |
Merge branch 'master' into master
Diffstat (limited to 'src/infini')
| -rw-r--r-- | src/infini/__init__.py | 8 | ||||
| -rw-r--r-- | src/infini/cli.py | 45 | ||||
| -rw-r--r-- | src/infini/consts/templates.py | 2 | ||||
| -rw-r--r-- | src/infini/event.py | 2 | ||||
| -rw-r--r-- | src/infini/exceptions.py | 2 | ||||
| -rw-r--r-- | src/infini/logging.py | 2 | ||||
| -rw-r--r-- | src/infini/manager.py | 18 | ||||
| -rw-r--r-- | src/infini/rule.py | 75 |
8 files changed, 147 insertions, 7 deletions
diff --git a/src/infini/__init__.py b/src/infini/__init__.py index 5f6d1105..9c88b4f9 100644 --- a/src/infini/__init__.py +++ b/src/infini/__init__.py @@ -1,4 +1,6 @@ -from infini.handler import Handler, Result -from infini.event import MessageEvent +from infini.cli import Cli +from infini.rule import Rule, Result, Dice +from infini.typing import Config as ConfigTyping +from infini.event import Event -__all__ = ["Handler", "Result", "MessageEvent"] +__all__ = ["Rule", "Cli", "Result", "Dice", "Event", "ConfigTyping"] diff --git a/src/infini/cli.py b/src/infini/cli.py new file mode 100644 index 00000000..e372b4b4 --- /dev/null +++ b/src/infini/cli.py @@ -0,0 +1,45 @@ +from pathlib import Path +from .consts import templates +from .logging import logger + +import argparse +import os +import sys +import importlib + + +class Cli: + def parse_args(self, argv: list[str] | None = None): + parser = argparse.ArgumentParser(description="HydroRoll 命令行工具") + + parser.add_argument("--new", action="store_true", help="创建一个 HydroRoll 规则包模板") + parser.add_argument("--run", action="store_true", help="运行 HydroRoll 规则包") + parser.add_argument("--gui", action="store_true", help="显示弹窗") + parser.add_argument("--path", help="指定路径") + + args = parser.parse_args(argv if argv else sys.argv[1:]) + + if args.gui: + logger.critical("选项[--gui]尚未被支持!") + sys.exit(1) + + path = Path(args.path).resolve() if args.path else Path(os.getcwd()).resolve() + if args.new and args.run: + logger.error("无法确定的指令要求: 你同时指定了new与run指令。") + sys.exit(1) + + if args.new: + if path.exists(): + logger.error("指定的文件夹已经存在!") + sys.exit(1) + + path.mkdir(parents=True, exist_ok=True) + (path / "rule.py").write_text(templates.RULE, encoding="utf-8") + (path / "event.py").write_text(templates.EVENT, encoding="utf-8") + (path / "dice.py").write_text(templates.DICE, encoding="utf-8") + + logger.success("HydroRoll 规则包模板已创建!") + + if args.run: + sys.path.append(str(path)) + importlib.import_module("event") diff --git a/src/infini/consts/templates.py b/src/infini/consts/templates.py index a662814c..d25894ae 100644 --- a/src/infini/consts/templates.py +++ b/src/infini/consts/templates.py @@ -34,4 +34,4 @@ def test(): return [error] finally: return [] -""" +"""
\ No newline at end of file diff --git a/src/infini/event.py b/src/infini/event.py index a83c4042..0363ac96 100644 --- a/src/infini/event.py +++ b/src/infini/event.py @@ -56,4 +56,4 @@ class MatcherEvent: return f"<MatcherEvent [{self.name}]>" -events = Events() +events = Events()
\ No newline at end of file diff --git a/src/infini/exceptions.py b/src/infini/exceptions.py index 2c181b48..e7c87b36 100644 --- a/src/infini/exceptions.py +++ b/src/infini/exceptions.py @@ -15,4 +15,4 @@ class UnknownMatcherEvent(EventError): class UnknownMessageEvent(EventError): - """未知的给出实现""" + """未知的给出实现"""
\ No newline at end of file diff --git a/src/infini/logging.py b/src/infini/logging.py index 72736ef7..3c3d2b02 100644 --- a/src/infini/logging.py +++ b/src/infini/logging.py @@ -30,4 +30,4 @@ def error_or_exception(message: str, exception: Exception, verbose: bool = True) logger.exception(exception) logger.critical(message) else: - logger.critical(f"{message} {exception!r}") + logger.critical(f"{message} {exception!r}")
\ No newline at end of file diff --git a/src/infini/manager.py b/src/infini/manager.py new file mode 100644 index 00000000..42e79563 --- /dev/null +++ b/src/infini/manager.py @@ -0,0 +1,18 @@ +from .event import Events, events +from .logging import logger +from .typing import Dict + + +class Manager: + """事件处理单元""" + + events: Events + + def __init__(self, _events: Events = None) -> None: + self.events = _events if _events else events + + def roll(self): + ... + + +manager = Manager() diff --git a/src/infini/rule.py b/src/infini/rule.py new file mode 100644 index 00000000..a1f04151 --- /dev/null +++ b/src/infini/rule.py @@ -0,0 +1,75 @@ +from abc import ABCMeta, abstractmethod +from enum import Enum +from .exceptions import HydroError +from .typing import Dict + +__all__ = ["RuleLoadType", "Result", "Dice", "Rule"] + + +class RuleLoadType(Enum): + """The Type Of Rules To Be Loaded""" + + DIR = "dir" + NAME = "name" + FILE = "file" + 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): + """规则基类""" + + name: str + dices: Dict[str, str] = {} + priority: int = 0 + + @abstractmethod + def __init__(self) -> None: + raise NotImplementedError + + @abstractmethod + def check(self, dice: Dice) -> Result: + raise NotImplementedError |
