From 26085e8238befcf7d8317e7e73423fb63ba4b822 Mon Sep 17 00:00:00 2001 From: "Ean Milligan (Bastion)" Date: Sat, 28 May 2022 04:10:09 -0400 Subject: [PATCH] actually fixed negative numbers --- src/solver/parser.ts | 11 +++++++++++ src/solver/solver.ts | 20 ++++---------------- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/src/solver/parser.ts b/src/solver/parser.ts index ab9bf8f..5f0aeb3 100644 --- a/src/solver/parser.ts +++ b/src/solver/parser.ts @@ -133,6 +133,17 @@ export const parseRoll = (fullCmd: string, modifiers: RollModifiers): SolvedRoll containsFail: false, }]); } + + if (mathConf[i - 1] === '-' && (!mathConf[i - 2] || mathConf[i - 2] === '(')) { + if (typeof mathConf[i] === 'number') { + mathConf[i] = mathConf[i] * -1; + } else { + ( mathConf[i]).total = ( mathConf[i]).total * -1; + ( mathConf[i]).details = `-${( mathConf[i]).details}`; + } + mathConf.splice(i - 1, 1); + i--; + } } // Now that mathConf is parsed, send it into the solver diff --git a/src/solver/solver.ts b/src/solver/solver.ts index 6c7eaa3..04d0b14 100644 --- a/src/solver/solver.ts +++ b/src/solver/solver.ts @@ -101,8 +101,6 @@ export const fullSolver = (conf: (string | number | SolvedStep)[], wrapDetails: containsCrit: 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 (typeof operand1 === 'object') { @@ -115,10 +113,6 @@ export const fullSolver = (conf: (string | number | SolvedStep)[], wrapDetails: if (operand1) { oper1 = parseFloat(operand1.toString()); subStepSolve.details = `${oper1.toString()}\\${conf[i]}`; - } else if (conf[i] === '-') { - oper1 = 0; - subStepSolve.details = `\\${conf[i]}`; - shouldDecrement = false; } } @@ -168,16 +162,10 @@ export const fullSolver = (conf: (string | number | SolvedStep)[], wrapDetails: throw new Error('EMDASNotNumber'); } - // Determine if we actually did math or just smashed a - sign onto a number - if (shouldDecrement) { - // Replace the two operands and their operator with our subStepSolve - 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); - } + // Replace the two operands and their operator with our subStepSolve + 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--; } } });