Fixes issues around negative numbers

This commit is contained in:
Ean Milligan (Bastion) 2022-05-05 03:39:22 -04:00
parent 6a305d1479
commit 120532b630
1 changed files with 22 additions and 7 deletions

View File

@ -560,7 +560,7 @@ const formatRoll = (rollConf: string, maximiseRoll: boolean, nominalRoll: boolea
tempDetails += `${preFormat}${e.roll}${postFormat} + `; tempDetails += `${preFormat}${e.roll}${postFormat} + `;
}); });
// After the looping is done, remove the extra " + " from the details and cap it with the closing ] // After the looping is done, remove the extra " + " from the details and cap it with the closing ]
tempDetails = tempDetails.substring(0, (tempDetails.length - 1)); tempDetails = tempDetails.substring(0, (tempDetails.length - 3));
tempDetails += "]"; tempDetails += "]";
return { return {
@ -660,6 +660,8 @@ const fullSolver = (conf: (string | number | SolvedStep)[], wrapDetails: boolean
containsCrit: false, containsCrit: false,
containsFail: false containsFail: false
}; };
// Flag to prevent infinte loop when dealing with negative numbers (such as [[-1+20]])
let shouldDecrement = true;
// If operand1 is a SolvedStep, populate our subStepSolve with its details and crit/fail flags // If operand1 is a SolvedStep, populate our subStepSolve with its details and crit/fail flags
if (typeof operand1 === "object") { if (typeof operand1 === "object") {
@ -669,8 +671,14 @@ const fullSolver = (conf: (string | number | SolvedStep)[], wrapDetails: boolean
subStepSolve.containsFail = operand1.containsFail; subStepSolve.containsFail = operand1.containsFail;
} else { } else {
// else parse it as a number and add it to the subStep details // else parse it as a number and add it to the subStep details
oper1 = parseFloat(operand1.toString()); if (operand1) {
subStepSolve.details = `${oper1.toString()}\\${conf[i]}`; oper1 = parseFloat(operand1.toString());
subStepSolve.details = `${oper1.toString()}\\${conf[i]}`;
} else if (conf[i] === '-') {
oper1 = 0;
subStepSolve.details = `\\${conf[i]}`;
shouldDecrement = false;
}
} }
// If operand2 is a SolvedStep, populate our subStepSolve with its details without overriding what operand1 filled in // If operand2 is a SolvedStep, populate our subStepSolve with its details without overriding what operand1 filled in
@ -719,16 +727,23 @@ const fullSolver = (conf: (string | number | SolvedStep)[], wrapDetails: boolean
throw new Error("EMDASNotNumber"); throw new Error("EMDASNotNumber");
} }
// Replace the two operands and their operator with our subStepSolve // Determine if we actually did math or just smashed a - sign onto a number
conf.splice((i - 1), 3, subStepSolve); if (shouldDecrement) {
// Because we are messing around with the array we are iterating thru, we need to back up one idx to make sure every operator gets processed // Replace the two operands and their operator with our subStepSolve
i--; conf.splice((i - 1), 3, subStepSolve);
// Because we are messing around with the array we are iterating thru, we need to back up one idx to make sure every operator gets processed
i--;
} else {
// Replace the one operand and its operator (-) with our subStepSolve
conf.splice(i, 2, subStepSolve);
}
} }
} }
}); });
// If we somehow have more than one item left in conf at this point, something broke, throw an error // If we somehow have more than one item left in conf at this point, something broke, throw an error
if (conf.length > 1) { if (conf.length > 1) {
log(LT.LOG, `ConfWHAT? ${JSON.stringify(conf)}`);
throw new Error("ConfWhat"); throw new Error("ConfWhat");
} else if (singleNum && (typeof (conf[0]) === "number")) { } else if (singleNum && (typeof (conf[0]) === "number")) {
// If we are only left with a number, populate the stepSolve with it // If we are only left with a number, populate the stepSolve with it