refactor: merge /spark command into /roll
Consolidate the `/spark` command into `/roll` to simplify the CLI. The `/roll` command now handles both dice notation and spark table slugs. - Update `parseInput` to treat `/roll` arguments as a generic string. - Update `dispatchCommand` to check for spark table matches before evaluating dice notation or variable expressions. - Update command completions to show both dice and spark table suggestions under `/roll`. - Update documentation and UI placeholders to reflect the change.
This commit is contained in:
@@ -126,7 +126,7 @@ export const RevealManager: Component = () => {
|
||||
rect: sparkTable.getBoundingClientRect(),
|
||||
action: () =>
|
||||
setActionPrefill({
|
||||
command: "/spark",
|
||||
command: "/roll",
|
||||
text: combinedSlug,
|
||||
}),
|
||||
title: "Roll spark table",
|
||||
|
||||
@@ -45,7 +45,6 @@ import journalGmRaw from "../doc-entries/journal-gm.md";
|
||||
import journalPlayerRaw from "../doc-entries/journal-player.md";
|
||||
import journalSetRaw from "../doc-entries/journal-set.md";
|
||||
import journalRollRaw from "../doc-entries/journal-roll.md";
|
||||
import journalSparkRaw from "../doc-entries/journal-spark.md";
|
||||
import journalLinkRaw from "../doc-entries/journal-link.md";
|
||||
|
||||
const rawDocuments: string[] = [
|
||||
@@ -53,7 +52,6 @@ const rawDocuments: string[] = [
|
||||
journalPlayerRaw,
|
||||
journalSetRaw,
|
||||
journalRollRaw,
|
||||
journalSparkRaw,
|
||||
journalLinkRaw,
|
||||
];
|
||||
|
||||
|
||||
@@ -265,7 +265,7 @@ export const JournalInput: Component = () => {
|
||||
onKeyDown={handleKeyDown}
|
||||
placeholder={
|
||||
isGm()
|
||||
? "输入消息,或使用 /roll、/spark、/link、/set 命令..."
|
||||
? "输入消息,或使用 /roll、/link、/set 命令..."
|
||||
: "输入消息,或使用 /set 命令..."
|
||||
}
|
||||
rows={2}
|
||||
|
||||
@@ -21,7 +21,6 @@ export function buildCompletions(
|
||||
|
||||
const commands = [
|
||||
{ label: "/roll", kind: "command" as const, insertText: "/roll " },
|
||||
{ label: "/spark", kind: "command" as const, insertText: "/spark " },
|
||||
{ label: "/link", kind: "command" as const, insertText: "/link " },
|
||||
{ label: "/set", kind: "command" as const, insertText: "/set " },
|
||||
];
|
||||
@@ -40,46 +39,52 @@ export function buildCompletions(
|
||||
: [{ label: "未知命令", kind: "no-results", insertText: "" }];
|
||||
}
|
||||
|
||||
// After /roll — show dice suggestions
|
||||
// After /roll — show dice AND spark table suggestions
|
||||
if (raw.startsWith("/roll ")) {
|
||||
const prefix = raw.slice("/roll ".length).toLowerCase();
|
||||
const matches = data.dice
|
||||
|
||||
const diceMatches = data.dice
|
||||
.filter(
|
||||
(d) =>
|
||||
d.notation.toLowerCase().includes(prefix) ||
|
||||
d.label.toLowerCase().includes(prefix),
|
||||
)
|
||||
.slice(0, 8);
|
||||
if (matches.length === 0) {
|
||||
return [{ label: "未找到骰子", kind: "no-results", insertText: "" }];
|
||||
}
|
||||
return matches.map((d) => ({
|
||||
label: d.notation,
|
||||
kind: "value" as const,
|
||||
insertText: "/roll " + d.notation,
|
||||
}));
|
||||
}
|
||||
.slice(0, 4);
|
||||
|
||||
// After /spark — show spark table suggestions
|
||||
if (raw.startsWith("/spark ")) {
|
||||
const prefix = raw.slice("/spark ".length).toLowerCase();
|
||||
const matches = data.sparkTables
|
||||
const sparkMatches = data.sparkTables
|
||||
.filter(
|
||||
(s) =>
|
||||
s.slug.toLowerCase().includes(prefix) ||
|
||||
s.label.toLowerCase().includes(prefix),
|
||||
)
|
||||
.slice(0, 8);
|
||||
if (matches.length === 0) {
|
||||
return [{ label: "未找到种子表", kind: "no-results", insertText: "" }];
|
||||
.slice(0, 4);
|
||||
|
||||
const items: CompletionItem[] = [];
|
||||
|
||||
for (const d of diceMatches) {
|
||||
items.push({
|
||||
label: `🎲 ${d.notation}`,
|
||||
kind: "value",
|
||||
insertText: "/roll " + d.notation,
|
||||
});
|
||||
}
|
||||
return matches.map((s) => ({
|
||||
label: `${s.slug} (${s.notation})`,
|
||||
kind: "value" as const,
|
||||
insertText: `/spark ${s.slug}`,
|
||||
}));
|
||||
|
||||
for (const s of sparkMatches) {
|
||||
items.push({
|
||||
label: `✨ ${s.slug} (${s.notation})`,
|
||||
kind: "value",
|
||||
insertText: `/roll ${s.slug}`,
|
||||
});
|
||||
}
|
||||
|
||||
if (items.length === 0) {
|
||||
return [{ label: "输入骰子表达式", kind: "no-results", insertText: "" }];
|
||||
}
|
||||
return items;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// After /link — show article and heading suggestions
|
||||
if (raw.startsWith("/link ")) {
|
||||
const prefix = raw.slice("/link ".length).toLowerCase();
|
||||
|
||||
@@ -87,30 +87,57 @@ export async function dispatchCommand(
|
||||
|
||||
// GM: all commands
|
||||
if (parsed.type === "roll") {
|
||||
const p = resolveRollPayload(
|
||||
parsed.payload as { notation: string; label?: string },
|
||||
);
|
||||
const arg = (parsed.payload as { arg: string }).arg;
|
||||
|
||||
// Try spark table first: if arg matches a known spark table slug, roll it
|
||||
const match = ctx.sparkTables.find((s) => s.slug === arg);
|
||||
if (match) {
|
||||
try {
|
||||
const csvPath = match.csvPath ?? "";
|
||||
const remix = match.remix ?? false;
|
||||
const p = await resolveSparkPayload({ key: arg, csvPath, remix });
|
||||
const result = sendMessage("spark", p);
|
||||
return finish(unwrap(result));
|
||||
} catch (e) {
|
||||
return finish({
|
||||
ok: false,
|
||||
error: e instanceof Error ? e.message : "种子表掷骰失败",
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Variable expression (e.g. "2d6 + $mod" or "$hp / 2")
|
||||
if (arg.includes("$")) {
|
||||
try {
|
||||
const ev = evaluateExpression(arg, {
|
||||
lookup: (name: string) => getCombined("$" + name, ctx.variables),
|
||||
});
|
||||
const payload = {
|
||||
notation: arg,
|
||||
label: arg,
|
||||
result: {
|
||||
total: ev.value,
|
||||
detail: "",
|
||||
plainDetail: "",
|
||||
pools: [] as { rolls: number[]; subtotal: number }[],
|
||||
},
|
||||
};
|
||||
const result = sendMessage("roll", payload);
|
||||
return finish(unwrap(result));
|
||||
} catch (e) {
|
||||
return finish({
|
||||
ok: false,
|
||||
error: e instanceof Error ? e.message : "表达式求值失败",
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Otherwise, treat as a dice expression
|
||||
const p = resolveRollPayload({ notation: arg, label: arg });
|
||||
const result = sendMessage("roll", p);
|
||||
return finish(unwrap(result));
|
||||
}
|
||||
|
||||
if (parsed.type === "spark") {
|
||||
try {
|
||||
const key = (parsed.payload as { key: string }).key;
|
||||
const match = ctx.sparkTables.find((s) => s.slug === key);
|
||||
const csvPath = match?.csvPath ?? "";
|
||||
const remix = match?.remix ?? false;
|
||||
const p = await resolveSparkPayload({ key, csvPath, remix });
|
||||
const result = sendMessage("spark", p);
|
||||
return finish(unwrap(result));
|
||||
} catch (e) {
|
||||
return finish({
|
||||
ok: false,
|
||||
error: e instanceof Error ? e.message : "种子表掷骰失败",
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (parsed.type === "set" || parsed.type === "rolltag") {
|
||||
return finish(dispatchSet(parsed.payload as Record<string, unknown>, ctx));
|
||||
}
|
||||
|
||||
@@ -12,23 +12,17 @@ export interface CompletionItem {
|
||||
}
|
||||
|
||||
export interface ParsedInput {
|
||||
type: "chat" | "roll" | "spark" | "link" | "set" | "rolltag";
|
||||
type: "chat" | "roll" | "link" | "set" | "rolltag";
|
||||
payload: Record<string, unknown>;
|
||||
error?: string;
|
||||
}
|
||||
|
||||
export function parseInput(raw: string): ParsedInput {
|
||||
if (raw.startsWith("/roll ")) {
|
||||
const notation = raw.slice("/roll ".length).trim();
|
||||
if (!notation)
|
||||
return { type: "roll", payload: {}, error: "需要骰子表达式" };
|
||||
return { type: "roll", payload: { notation, label: notation } };
|
||||
}
|
||||
|
||||
if (raw.startsWith("/spark ")) {
|
||||
const key = raw.slice("/spark ".length).trim();
|
||||
if (!key) return { type: "spark", payload: {}, error: "需要种子表键名" };
|
||||
return { type: "spark", payload: { key } };
|
||||
const arg = raw.slice("/roll ".length).trim();
|
||||
if (!arg)
|
||||
return { type: "roll", payload: {}, error: "需要骰子表达式或种子表键名" };
|
||||
return { type: "roll", payload: { arg } };
|
||||
}
|
||||
|
||||
if (raw.startsWith("/link ")) {
|
||||
@@ -69,7 +63,7 @@ export function parseInput(raw: string): ParsedInput {
|
||||
return { type: "set", payload: { key, expr } };
|
||||
}
|
||||
|
||||
if (raw === "/roll" || raw === "/spark" || raw === "/link" || raw === "/set") {
|
||||
if (raw === "/roll" || raw === "/link" || raw === "/set") {
|
||||
return { type: "chat", payload: {}, error: "请补全命令" };
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user