39 lines
1002 B
Python
39 lines
1002 B
Python
"""掷骰子 —— 业务逻辑(Service 层,不依赖 nonebot,可单测)。"""
|
|||
|
|
|
||
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
import random
|
||
|
|
|
||
|
|
|
||
|
|
def build_dice_message(
|
||
|
|
num: int,
|
||
|
|
min_: int,
|
||
|
|
max_: int,
|
||
|
|
opr: int,
|
||
|
|
offset: int,
|
||
|
|
tip: str = "的掷骰结果是:",
|
||
|
|
) -> str:
|
||
|
|
if num == 0:
|
||
|
|
return "咦?我骰子呢?"
|
||
|
|
min_, max_ = min(min_, max_), max(min_, max_)
|
||
|
|
rolls = [random.randint(min_, max_) for _ in range(num)]
|
||
|
|
total = sum(rolls)
|
||
|
|
rolls_str = "+".join(map(str, rolls))
|
||
|
|
if len(rolls_str) > 100:
|
||
|
|
rolls_str = str(total)
|
||
|
|
res = total + opr * offset
|
||
|
|
sign = " -+"[opr]
|
||
|
|
return "".join(
|
||
|
|
[
|
||
|
|
f"{tip}\n",
|
||
|
|
str(num) if num > 1 else "",
|
||
|
|
"D",
|
||
|
|
f"{min_}~" if min_ != 1 else "",
|
||
|
|
str(max_),
|
||
|
|
f"{sign}{offset}" if offset else "",
|
||
|
|
f"={rolls_str}",
|
||
|
|
f"{sign}{offset}" if offset else "",
|
||
|
|
f"={res}" if offset or num > 1 else "",
|
||
|
|
]
|
||
|
|
)
|