feat: dice roller improved

This commit is contained in:
2026-02-28 16:49:59 +08:00
parent 66aaaa96e1
commit cc10cea31d
12 changed files with 736 additions and 40 deletions
@@ -4,6 +4,7 @@ import {
CommanderEntry,
CompletionItem,
} from "../types";
import { useDiceRoller } from "./useDiceRoller";
// ==================== 默认命令 ====================
@@ -43,32 +44,23 @@ export const defaultCommands: Record<string, MdCommanderCommand> = {
},
roll: {
command: "roll",
description: "掷骰子",
description: "掷骰子 - 支持骰池、修饰符和组合",
options: {
formula: {
option: "formula",
description: "骰子公式,如 2d6+3",
description: "骰子公式,如 3d6+{d4,d8}kh2-5",
type: "string",
required: true,
},
},
handler: (args) => {
const formula = args.formula || "1d6";
const match = formula.match(/^(\d+)?d(\d+)([+-]\d+)?$/i);
if (!match) {
return { message: `无效的骰子公式:${formula}`, type: "error" };
}
const count = parseInt(match[1] || "1");
const sides = parseInt(match[2]);
const modifier = parseInt(match[3] || "0");
const rolls: number[] = [];
for (let i = 0; i < count; i++) {
rolls.push(Math.floor(Math.random() * sides) + 1);
}
const total = rolls.reduce((a, b) => a + b, 0) + modifier;
const { rollSimple } = useDiceRoller();
const result = rollSimple(formula);
return {
message: `${formula} = [${rolls.join(", ")}]${modifier !== 0 ? (modifier > 0 ? `+${modifier}` : modifier) : ""} = ${total}`,
type: "success",
message: result.text,
isHtml: result.isHtml,
type: result.text.startsWith("错误") ? "error" : "success",
};
},
},