Alt outcome syntax

This commit is contained in:
dakedres 2023-05-20 17:07:04 -06:00
parent 3aa24443d4
commit 31e6ccdaab
2 changed files with 33 additions and 27 deletions

View File

@ -1,7 +1,7 @@
const constants = { const constants = {
rollRegex: /^(\d+)?([dhl])(\d+)(\s*([+\-*x\/])\s*(\d+))?/, rollRegex: /^(\d+)?([dhl])(\d+)(\s*([+\-*x\/])\s*(\d+))?/,
optionRollRegex: /^(\d+)?(([dhl])(\d+))?(\s*[+\-*x\/]\s*\d+)?/, optionRollRegex: /^(\d+)?(([dhl])(\d+))?(\s*[+\-*x\/]\s*\d+)?/,
descriptionRegex: /\s*(\d+(-(\d+)?)?)?([^;\n]+)/g, descriptionRegex: /\s*(\d*-\d*)|(\d+)/g,
macroNameRegex: /^[a-z0-9]+$/, macroNameRegex: /^[a-z0-9]+$/,
commands: { commands: {

View File

@ -69,40 +69,46 @@ const pullDescription = (expression, match) => {
} }
const parseDescription = description => { const parseDescription = description => {
let conditions = [], const regex = constants.descriptionRegex
match
while((match = constants.descriptionRegex.exec(description)) !== null) { let conditions = []
// Prevent infinite loops if there's somehow a zero-length match let range
if(match.index == constants.descriptionRegex.lastIndex) let descriptionStart
regex.lastIndex++ let match
let finishCondition = index =>
conditions.push({
range,
content: description.slice(descriptionStart, index).trim()
})
while((match = regex.exec(description)) !== null) {
if(range)
finishCondition(match.index)
let [ let [
range, rangeExp,
upperRangeExpression, valueExp
upperRange,
content
] = match.slice(1) ] = match.slice(1)
let lower = range[0], if(rangeExp) {
upper let split = rangeExp.split('-')
if(!upperRange) { range = {
// Allow "X-" ranges to represent anything higher than X lower: split[0] || -Infinity,
upper = upperRangeExpression ? Infinity : lower upper: split[1] || Infinity
} else { }
upper = upperRange[0] } else if(valueExp) {
range = {
upper: valueExp,
lower: valueExp
}
} }
conditions.push({ descriptionStart = regex.lastIndex
range: range && {
lower,
upper
},
content: content.trim()
})
} }
finishCondition()
return conditions return conditions
} }
@ -408,10 +414,10 @@ client.on('interactionCreate', async interaction => {
if(roll) { if(roll) {
let dice = parseRoll(roll) let dice = parseRoll(roll)
let optionsRoll = interaction.options.get('options').value let optionsRoll = interaction.options.get('options')
if(optionsRoll) { if(optionsRoll) {
let optionDice = parseOptionRoll(optionsRoll) let optionDice = parseOptionRoll(optionsRoll.value)
for(let [ key, value ] of Object.entries(optionDice)) { for(let [ key, value ] of Object.entries(optionDice)) {
if(value) if(value)