fix: dice issue

This commit is contained in:
2026-02-28 17:10:54 +08:00
parent cc10cea31d
commit 91d46dea75
3 changed files with 93 additions and 91 deletions
@@ -11,6 +11,48 @@ export function tokenize(input: string): Token[] {
while (i < str.length) {
const char = str[i];
// 修饰符 kh/kl/dh/dl - 必须在检查 'd' 骰子之前检查
if (char === "k" || char === "d") {
const rest = str.slice(i).toLowerCase();
if (rest.startsWith("kh")) {
i += 2;
let countStr = "";
while (i < str.length && /\d/.test(str[i])) {
countStr += str[i];
i++;
}
tokens.push({ type: "modifier", modType: "kh", count: parseInt(countStr) || 1 });
continue;
} else if (rest.startsWith("kl")) {
i += 2;
let countStr = "";
while (i < str.length && /\d/.test(str[i])) {
countStr += str[i];
i++;
}
tokens.push({ type: "modifier", modType: "kl", count: parseInt(countStr) || 1 });
continue;
} else if (rest.startsWith("dh")) {
i += 2;
let countStr = "";
while (i < str.length && /\d/.test(str[i])) {
countStr += str[i];
i++;
}
tokens.push({ type: "modifier", modType: "dh", count: parseInt(countStr) || 1 });
continue;
} else if (rest.startsWith("dl")) {
i += 2;
let countStr = "";
while (i < str.length && /\d/.test(str[i])) {
countStr += str[i];
i++;
}
tokens.push({ type: "modifier", modType: "dl", count: parseInt(countStr) || 1 });
continue;
}
}
// 数字字面量
if (/\d/.test(char)) {
let numStr = "";
@@ -86,45 +128,6 @@ export function tokenize(input: string): Token[] {
continue;
}
// 修饰符 kh/kl/dh/dl
if (char === "k" || char === "d") {
const rest = str.slice(i).toLowerCase();
if (rest.startsWith("kh")) {
i += 2;
let countStr = "";
while (i < str.length && /\d/.test(str[i])) {
countStr += str[i];
i++;
}
tokens.push({ type: "modifier", modType: "kh", count: parseInt(countStr) || 1 });
} else if (rest.startsWith("kl")) {
i += 2;
let countStr = "";
while (i < str.length && /\d/.test(str[i])) {
countStr += str[i];
i++;
}
tokens.push({ type: "modifier", modType: "kl", count: parseInt(countStr) || 1 });
} else if (rest.startsWith("dh")) {
i += 2;
let countStr = "";
while (i < str.length && /\d/.test(str[i])) {
countStr += str[i];
i++;
}
tokens.push({ type: "modifier", modType: "dh", count: parseInt(countStr) || 1 });
} else if (rest.startsWith("dl")) {
i += 2;
let countStr = "";
while (i < str.length && /\d/.test(str[i])) {
countStr += str[i];
i++;
}
tokens.push({ type: "modifier", modType: "dl", count: parseInt(countStr) || 1 });
}
continue;
}
// 未知字符,跳过
i++;
}
@@ -194,20 +197,21 @@ function parsePool(
continue;
}
if (tokens[i].type === "dice") {
for (let j = 0; j < tokens[i].count; j++) {
const token = tokens[i];
if (token.type === "dice") {
for (let j = 0; j < token.count; j++) {
dice.push({
sides: tokens[i].sides,
sides: token.sides,
value: 0,
isNegative: sign < 0,
});
}
i++;
} else if (tokens[i].type === "number") {
} else if (token.type === "number") {
// 数字作为 0 面骰子(固定值)
dice.push({
sides: 0,
value: tokens[i].value,
value: token.value,
isNegative: sign < 0,
});
i++;