Add support for fate dice, fixed math with zeroes
This commit is contained in:
		
							parent
							
								
									876a97b9fd
								
							
						
					
					
						commit
						094974df80
					
				| 
						 | 
					@ -15,6 +15,7 @@ import { fullSolver } from './solver.ts';
 | 
				
			||||||
// parseRoll(fullCmd, modifiers)
 | 
					// parseRoll(fullCmd, modifiers)
 | 
				
			||||||
// parseRoll handles converting fullCmd into a computer readable format for processing, and finally executes the solving
 | 
					// parseRoll handles converting fullCmd into a computer readable format for processing, and finally executes the solving
 | 
				
			||||||
export const parseRoll = (fullCmd: string, modifiers: RollModifiers): SolvedRoll => {
 | 
					export const parseRoll = (fullCmd: string, modifiers: RollModifiers): SolvedRoll => {
 | 
				
			||||||
 | 
						const operators = ['^', '*', '/', '%', '+', '-'];
 | 
				
			||||||
	const returnmsg = <SolvedRoll> {
 | 
						const returnmsg = <SolvedRoll> {
 | 
				
			||||||
		error: false,
 | 
							error: false,
 | 
				
			||||||
		errorCode: '',
 | 
							errorCode: '',
 | 
				
			||||||
| 
						 | 
					@ -127,14 +128,14 @@ export const parseRoll = (fullCmd: string, modifiers: RollModifiers): SolvedRoll
 | 
				
			||||||
						containsCrit: false,
 | 
											containsCrit: false,
 | 
				
			||||||
						containsFail: false,
 | 
											containsFail: false,
 | 
				
			||||||
					}]);
 | 
										}]);
 | 
				
			||||||
				} else {
 | 
									} else if (!operators.includes(mathConf[i].toString())) {
 | 
				
			||||||
					// If nothing else has handled it by now, try it as a roll
 | 
										// If nothing else has handled it by now, try it as a roll
 | 
				
			||||||
					const formattedRoll = formatRoll(mathConf[i].toString(), modifiers.maxRoll, modifiers.nominalRoll);
 | 
										const formattedRoll = formatRoll(mathConf[i].toString(), modifiers.maxRoll, modifiers.nominalRoll);
 | 
				
			||||||
					mathConf[i] = formattedRoll.solvedStep;
 | 
										mathConf[i] = formattedRoll.solvedStep;
 | 
				
			||||||
					tempCountDetails.push(formattedRoll.countDetails);
 | 
										tempCountDetails.push(formattedRoll.countDetails);
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
				if (mathConf[i - 1] === '-' && (!mathConf[i - 2] || mathConf[i - 2] === '(')) {
 | 
									if (mathConf[i - 1] === '-' && ((!mathConf[i - 2] && mathConf[i - 2] !== 0) || mathConf[i - 2] === '(')) {
 | 
				
			||||||
					if (typeof mathConf[i] === 'number') {
 | 
										if (typeof mathConf[i] === 'number') {
 | 
				
			||||||
						mathConf[i] = <number> mathConf[i] * -1;
 | 
											mathConf[i] = <number> mathConf[i] * -1;
 | 
				
			||||||
					} else {
 | 
										} else {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -31,6 +31,7 @@ export const formatRoll = (rollConf: string, maximiseRoll: boolean, nominalRoll:
 | 
				
			||||||
			switch (e.type) {
 | 
								switch (e.type) {
 | 
				
			||||||
				case 'ova':
 | 
									case 'ova':
 | 
				
			||||||
				case 'roll20':
 | 
									case 'roll20':
 | 
				
			||||||
 | 
									case 'fate':
 | 
				
			||||||
					tempTotal += e.roll;
 | 
										tempTotal += e.roll;
 | 
				
			||||||
					break;
 | 
										break;
 | 
				
			||||||
				case 'cwod':
 | 
									case 'cwod':
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -19,6 +19,17 @@ export const genRoll = (size: number, maximiseRoll: boolean, nominalRoll: boolea
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// genFateRoll returns -1|0|1
 | 
				
			||||||
 | 
					// genFateRoll turns a d6 into a fate die, with sides: -1, -1, 0, 0, 1, 1
 | 
				
			||||||
 | 
					export const genFateRoll = (maximiseRoll: boolean, nominalRoll: boolean): number => {
 | 
				
			||||||
 | 
						if (nominalRoll) {
 | 
				
			||||||
 | 
							return 0;
 | 
				
			||||||
 | 
						} else {
 | 
				
			||||||
 | 
							const sides = [-1, -1, 0, 0, 1, 1];
 | 
				
			||||||
 | 
							return sides[genRoll(6, maximiseRoll, nominalRoll) - 1];
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// compareRolls(a, b) returns -1|0|1
 | 
					// compareRolls(a, b) returns -1|0|1
 | 
				
			||||||
// compareRolls is used to order an array of RollSets by RollSet.roll
 | 
					// compareRolls is used to order an array of RollSets by RollSet.roll
 | 
				
			||||||
export const compareRolls = (a: RollSet, b: RollSet): number => {
 | 
					export const compareRolls = (a: RollSet, b: RollSet): number => {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -6,7 +6,7 @@ import {
 | 
				
			||||||
} from '../../deps.ts';
 | 
					} from '../../deps.ts';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import { RollConf, RollSet, RollType } from './solver.d.ts';
 | 
					import { RollConf, RollSet, RollType } from './solver.d.ts';
 | 
				
			||||||
import { compareOrigidx, compareRolls, genRoll, loggingEnabled } from './rollUtils.ts';
 | 
					import { compareOrigidx, compareRolls, genFateRoll, genRoll, loggingEnabled } from './rollUtils.ts';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// roll(rollStr, maximiseRoll, nominalRoll) returns RollSet
 | 
					// roll(rollStr, maximiseRoll, nominalRoll) returns RollSet
 | 
				
			||||||
// roll parses and executes the rollStr, if needed it will also make the roll the maximum or average
 | 
					// roll parses and executes the rollStr, if needed it will also make the roll the maximum or average
 | 
				
			||||||
| 
						 | 
					@ -24,7 +24,7 @@ export const roll = (rollStr: string, maximiseRoll: boolean, nominalRoll: boolea
 | 
				
			||||||
	const dpts = rollStr.split('d');
 | 
						const dpts = rollStr.split('d');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Initialize the configuration to store the parsed data
 | 
						// Initialize the configuration to store the parsed data
 | 
				
			||||||
	let rollType: RollType = 'roll20';
 | 
						let rollType: RollType = '';
 | 
				
			||||||
	const rollConf: RollConf = {
 | 
						const rollConf: RollConf = {
 | 
				
			||||||
		dieCount: 0,
 | 
							dieCount: 0,
 | 
				
			||||||
		dieSize: 0,
 | 
							dieSize: 0,
 | 
				
			||||||
| 
						 | 
					@ -103,8 +103,18 @@ export const roll = (rollStr: string, maximiseRoll: boolean, nominalRoll: boolea
 | 
				
			||||||
		const ovaParts = rollStr.split('ovad');
 | 
							const ovaParts = rollStr.split('ovad');
 | 
				
			||||||
		rollConf.dieCount = parseInt(ovaParts[0] || '1');
 | 
							rollConf.dieCount = parseInt(ovaParts[0] || '1');
 | 
				
			||||||
		rollConf.dieSize = parseInt(ovaParts[1] || '6');
 | 
							rollConf.dieSize = parseInt(ovaParts[1] || '6');
 | 
				
			||||||
 | 
						} else if (remains.startsWith('f')) {
 | 
				
			||||||
 | 
							// fate dice setup
 | 
				
			||||||
 | 
							rollType = 'fate';
 | 
				
			||||||
 | 
							rollConf.dieCount = parseInt(tempDC);
 | 
				
			||||||
 | 
							// dieSize set to 1 as 1 is max face value, a six sided die is used internally
 | 
				
			||||||
 | 
							rollConf.dieSize = 1;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							// remove F from the remains
 | 
				
			||||||
 | 
							remains = remains.slice(1);
 | 
				
			||||||
	} else {
 | 
						} else {
 | 
				
			||||||
		// roll20 dice setup
 | 
							// roll20 dice setup
 | 
				
			||||||
 | 
							rollType = 'roll20';
 | 
				
			||||||
		rollConf.dieCount = parseInt(tempDC);
 | 
							rollConf.dieCount = parseInt(tempDC);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// Finds the end of the die size/beginnning of the additional options
 | 
							// Finds the end of the die size/beginnning of the additional options
 | 
				
			||||||
| 
						 | 
					@ -389,7 +399,7 @@ export const roll = (rollStr: string, maximiseRoll: boolean, nominalRoll: boolea
 | 
				
			||||||
		// Copy the template to fill out for this iteration
 | 
							// Copy the template to fill out for this iteration
 | 
				
			||||||
		const rolling = JSON.parse(JSON.stringify(templateRoll));
 | 
							const rolling = JSON.parse(JSON.stringify(templateRoll));
 | 
				
			||||||
		// If maximiseRoll is on, set the roll to the dieSize, else if nominalRoll is on, set the roll to the average roll of dieSize, else generate a new random roll
 | 
							// If maximiseRoll is on, set the roll to the dieSize, else if nominalRoll is on, set the roll to the average roll of dieSize, else generate a new random roll
 | 
				
			||||||
		rolling.roll = genRoll(rollConf.dieSize, maximiseRoll, nominalRoll);
 | 
							rolling.roll = rollType === 'fate' ? genFateRoll(maximiseRoll, nominalRoll) : genRoll(rollConf.dieSize, maximiseRoll, nominalRoll);
 | 
				
			||||||
		// Set origidx of roll
 | 
							// Set origidx of roll
 | 
				
			||||||
		rolling.origidx = i;
 | 
							rolling.origidx = i;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -403,7 +413,11 @@ export const roll = (rollStr: string, maximiseRoll: boolean, nominalRoll: boolea
 | 
				
			||||||
		if (rollConf.critFail.on && rollConf.critFail.range.indexOf(rolling.roll) >= 0) {
 | 
							if (rollConf.critFail.on && rollConf.critFail.range.indexOf(rolling.roll) >= 0) {
 | 
				
			||||||
			rolling.critFail = true;
 | 
								rolling.critFail = true;
 | 
				
			||||||
		} else if (!rollConf.critFail.on) {
 | 
							} else if (!rollConf.critFail.on) {
 | 
				
			||||||
			rolling.critFail = rolling.roll === 1;
 | 
								if (rollType === 'fate') {
 | 
				
			||||||
 | 
									rolling.critFail = rolling.roll === -1;
 | 
				
			||||||
 | 
								} else {
 | 
				
			||||||
 | 
									rolling.critFail = rolling.roll === 1;
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// Push the newly created roll and loop again
 | 
							// Push the newly created roll and loop again
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,6 +1,6 @@
 | 
				
			||||||
// solver.ts custom types
 | 
					// solver.ts custom types
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export type RollType = '' | 'roll20' | 'cwod' | 'ova';
 | 
					export type RollType = '' | 'roll20' | 'fate' | 'cwod' | 'ova';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// RollSet is used to preserve all information about a calculated roll
 | 
					// RollSet is used to preserve all information about a calculated roll
 | 
				
			||||||
export type RollSet = {
 | 
					export type RollSet = {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -111,7 +111,7 @@ export const fullSolver = (conf: (string | number | SolvedStep)[], wrapDetails:
 | 
				
			||||||
					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
 | 
				
			||||||
					if (operand1) {
 | 
										if (operand1 || operand1 == 0) {
 | 
				
			||||||
						oper1 = parseFloat(operand1.toString());
 | 
											oper1 = parseFloat(operand1.toString());
 | 
				
			||||||
						subStepSolve.details = `${oper1.toString()}\\${conf[i]}`;
 | 
											subStepSolve.details = `${oper1.toString()}\\${conf[i]}`;
 | 
				
			||||||
					}
 | 
										}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue