diff options
| author | 2023-12-09 22:34:27 +0800 | |
|---|---|---|
| committer | 2023-12-09 22:37:38 +0800 | |
| commit | 974230ca750963bb0346589da98b05f2d47454ed (patch) | |
| tree | d3d9a481705cd859be4c654c16742ab2b9cde0ab /src/hydrorollcore | |
| parent | e20c3f29ae8297a8c76587e1e6b732a147798786 (diff) | |
| download | infini-974230ca750963bb0346589da98b05f2d47454ed.tar.gz infini-974230ca750963bb0346589da98b05f2d47454ed.zip | |
:sparkles: 新增事件及模板
Diffstat (limited to 'src/hydrorollcore')
| -rw-r--r-- | src/hydrorollcore/cli.py | 42 | ||||
| -rw-r--r-- | src/hydrorollcore/consts/__init__.py | 0 | ||||
| -rw-r--r-- | src/hydrorollcore/consts/templates.py | 63 | ||||
| -rw-r--r-- | src/hydrorollcore/event.py | 12 | ||||
| -rw-r--r-- | src/hydrorollcore/utils.py | 2 |
5 files changed, 108 insertions, 11 deletions
diff --git a/src/hydrorollcore/cli.py b/src/hydrorollcore/cli.py index 8ce0fd9e..8b03b60b 100644 --- a/src/hydrorollcore/cli.py +++ b/src/hydrorollcore/cli.py @@ -1,22 +1,44 @@ +from pathlib import Path +from .consts import templates + +# from tkinter import messagebox + import argparse -from tkinter import messagebox +import os +import sys class Cli: def parse_args(self): - # 创建解析器对象 parser = argparse.ArgumentParser(description="HydroRoll 命令行工具") - # 添加命令行参数 - parser.add_argument("--gui", action="store_true", help="显示弹窗") + 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() - # 处理命令行参数 - if args.gui: - messagebox.showinfo("提示", "这是一个弹窗!") + # if args.gui: + # messagebox.showinfo("提示", "这是一个弹窗!") + + if not args.path: + path = Path(os.getcwd()).resolve() + else: + path = Path(args.path).resolve() + + if args.new and args.run: + print("无法确定的指令要求: 你同时指定了new与run指令。") + sys.exit(1) + + if args.new: + if path.exists(): + print("指定的文件夹已经存在!") + sys.exit(1) + + path.mkdir(parents=True, exist_ok=True) + (path / "rule.py").write_text(templates.RULE) + (path / "event.py").write_text(templates.EVENT) + (path / "dice.py").write_text(templates.DICE) - if args.path: - print("输入的路径:", args.path) + print("HydroRoll 规则包模板已创建!") diff --git a/src/hydrorollcore/consts/__init__.py b/src/hydrorollcore/consts/__init__.py new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/src/hydrorollcore/consts/__init__.py diff --git a/src/hydrorollcore/consts/templates.py b/src/hydrorollcore/consts/templates.py new file mode 100644 index 00000000..4cf72989 --- /dev/null +++ b/src/hydrorollcore/consts/templates.py @@ -0,0 +1,63 @@ +RULE = """from HydroRollCore import Rule, Result, Dice + +class MyRule(Rule): + \"\"\"自设规则包\"\"\" + + name = "MyRule" + priority: int = 0 + + def __init__(self) -> None: + \"\"\"初始化你的规则包\"\"\" + + def check(self, dice: Dice) -> Result: + \"\"\"声明规则包检定方式\"\"\" + return Result("myevent.event1", True) +""" + +EVENT = """from HydroRollCore import Event + +__events__ = ["MyEvent"] + +class MyEvent(Event): + name = "event1" + output = "检定成功!" +""" + +DICE = """from HydroRollCore import Dice + +class BaseDice(Dice): + \"\"\"多面骰\"\"\" + + def __init__(self, roll_string: str = "") -> None: + self.roll_string = roll_string + self.parse() + + def parse(self) -> "Dice": + self.dices = [] + split = re.split(r"[dD]", self.roll_string) + + if split[0]: + self.a = int(split[0]) + else: + self.a = 1 + + if split[1]: + self.b = int(split[1]) + else: + self.b = 100 + + self.db = f"{self.a}D{self.b}" + self.dices += [f"D{self.b}"] * self.a + return self + + def roll(self) -> int: + self.results = [] + + for _ in range(self.a): + result = random.randint(1, self.b) + + self.results.append(result) + + self.outcome = sum(self.results) + return self.outcome +""" diff --git a/src/hydrorollcore/event.py b/src/hydrorollcore/event.py new file mode 100644 index 00000000..e8edeb98 --- /dev/null +++ b/src/hydrorollcore/event.py @@ -0,0 +1,12 @@ +from abc import ABCMeta + + +class Event(metaclass=ABCMeta): + """事件基类""" + + name: str + output: str + + def __init__(self, name: str, output: str) -> None: + self.name = name + self.output = output diff --git a/src/hydrorollcore/utils.py b/src/hydrorollcore/utils.py index 71187856..1333815a 100644 --- a/src/hydrorollcore/utils.py +++ b/src/hydrorollcore/utils.py @@ -189,4 +189,4 @@ def sync_func_wrapper( async def _wrapper(*args: _P.args, **kwargs: _P.kwargs): return func(*args, **kwargs) - return _wrapper
\ No newline at end of file + return _wrapper |
