Added dice functions to parse engine

This commit is contained in:
Stefan Zermatten
2023-04-01 11:27:52 +02:00
parent 8e610c2cd8
commit b9588c83b1
3 changed files with 223 additions and 64 deletions

View File

@@ -4,7 +4,7 @@ const rollArray = {
create({ values, diceSize, diceNum }) {
return {
parseType: 'rollArray',
values,
values: values.map(v => ({ value: v })),
diceSize,
diceNum,
};
@@ -16,10 +16,13 @@ const rollArray = {
};
},
toString(node) {
return `${node.diceNum || ''}d${node.diceSize} [ ${node.values.join(', ')} ]`;
return `${node.diceNum || ''}d${node.diceSize} [${valuesToString(node.values)}]`;
},
reduce(node, scope, context) {
const total = node.values.reduce((a, b) => a + b, 0);
const total = node.values.reduce((a, b) => {
if (b.disabled) return a;
return a + b.value;
}, 0);
return {
result: constant.create({
value: total,
@@ -29,4 +32,15 @@ const rollArray = {
},
}
function valuesToString(values) {
return values.map(v => {
let text = `${v.value}`;
if (v.disabled) text = `~~${text}~~`;
if (v.italics) text = `*${text}*`;
if (v.bold) text = `**${text}**`;
if (v.underline) text = `__${text}__`;
return text;
}).join(', ');
}
export default rollArray;