Commit bcb1a59a authored by nanahira's avatar nanahira

support sticky st

parent 0908da0c
......@@ -99,31 +99,61 @@ export class DbModule extends BaseModule implements LifecycleEvents {
channelId: session.channelId || 'priv',
};
const exprChain = expr.split(/\s/);
const skillsSet = new Set<string>();
const datas: Partial<DiceSkill>[] = [];
const errorMessages: string[] = [];
const appendData = (skillName: string, value: number) => {
if (!skillName) {
errorMessages.push('技能名称不能为空');
return;
}
if (value == null || value < 0 || value > 100) {
errorMessages.push(`${skillName} 的数值必须在 0 到 100 之间。`);
return;
}
if (skillsSet.has(skillName)) {
errorMessages.push(`${skillName} 出现重复。`);
return;
}
skillsSet.add(skillName);
datas.push({
...sessionInfo,
skillName,
value,
});
};
for (let i = 0; i < exprChain.length; i++) {
let skillName: string, value: number;
const fullMatch = exprChain[i].match(/^(\D+)(\d{1,3})$/);
const fullMatch = exprChain[i].match(/^(\D+\d{1,3})+$/);
if (fullMatch) {
skillName = fullMatch[1];
value = parseInt(fullMatch[2], 10);
let skillString = '',
valueString = '';
const pattern = exprChain[i];
for (let j = 0; j < pattern.length; j++) {
const char = pattern[j];
const isNumber = char.match(/\d/);
if (!isNumber) {
skillString += char;
} else {
valueString += char;
if (j === pattern.length - 1 || !pattern[j + 1].match(/\d/)) {
appendData(skillString, parseInt(valueString));
skillString = '';
valueString = '';
}
}
}
} else {
const nextValue = exprChain[i + 1];
if (nextValue.match(/^\d{1,3}$/)) {
skillName = exprChain[i];
value = parseInt(nextValue, 10);
appendData(exprChain[i], parseInt(nextValue));
i++;
} else {
return '语法错误。';
}
}
if (value == null || value < 0 || value > 100) {
return '数值必须在 0 到 100 之间。';
}
datas.push({
...sessionInfo,
skillName,
value,
});
}
if (errorMessages.length > 0) {
return errorMessages.join('\n');
}
if (!datas.length) {
return;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment