1
1
mirror of https://github.com/Burn-E99/TheArtificer.git synced 2026-01-08 05:17:54 -05:00

Roll command now utilizes embeds, count decorator (-c) complete, added [[api h shorthand

This commit is contained in:
Ean Milligan (Bastion)
2022-05-27 20:59:09 -04:00
parent bbba797dc3
commit b887b93bb2
7 changed files with 254 additions and 155 deletions

23
src/solver/counter.ts Normal file
View File

@ -0,0 +1,23 @@
import { CountDetails, RollSet } from './solver.d.ts';
export const rollCounter = (rollSet: RollSet[]): CountDetails => {
const countDetails = {
total: 0,
successful: 0,
failed: 0,
rerolled: 0,
dropped: 0,
exploded: 0,
};
rollSet.forEach((roll) => {
countDetails.total++;
if (roll.critHit) countDetails.successful++;
if (roll.critFail) countDetails.failed++;
if (roll.rerolled) countDetails.rerolled++;
if (roll.dropped) countDetails.dropped++;
if (roll.exploding) countDetails.exploded++;
});
return countDetails;
};

View File

@ -7,7 +7,7 @@ import {
import config from '../../config.ts';
import { RollModifiers } from '../mod.d.ts';
import { ReturnData, SolvedRoll, SolvedStep } from './solver.d.ts';
import { CountDetails, ReturnData, SolvedRoll, SolvedStep } from './solver.d.ts';
import { compareTotalRolls, escapeCharacters } from './rollUtils.ts';
import { formatRoll } from './rollFormatter.ts';
import { fullSolver } from './solver.ts';
@ -15,13 +15,8 @@ import { fullSolver } from './solver.ts';
// parseRoll(fullCmd, modifiers)
// parseRoll handles converting fullCmd into a computer readable format for processing, and finally executes the solving
export const parseRoll = (fullCmd: string, modifiers: RollModifiers): SolvedRoll => {
const returnmsg = {
const returnmsg = <SolvedRoll> {
error: false,
errorMsg: '',
errorCode: '',
line1: '',
line2: '',
line3: '',
};
// Whole function lives in a try-catch to allow safe throwing of errors on purpose
@ -30,6 +25,7 @@ export const parseRoll = (fullCmd: string, modifiers: RollModifiers): SolvedRoll
const sepRolls = fullCmd.split(config.prefix);
const tempReturnData: ReturnData[] = [];
const tempCountDetails: CountDetails[] = [];
// Loop thru all roll/math ops
for (const sepRoll of sepRolls) {
@ -68,7 +64,9 @@ export const parseRoll = (fullCmd: string, modifiers: RollModifiers): SolvedRoll
mathConf[i] = parseFloat(mathConf[i].toString());
} else if (/([0123456789])/g.test(mathConf[i].toString())) {
// If there is a number somewhere in mathconf[i] but there are also other characters preventing it from parsing correctly as a number, it should be a dice roll, parse it as such (if it for some reason is not a dice roll, formatRoll/roll will handle it)
mathConf[i] = formatRoll(mathConf[i].toString(), modifiers.maxRoll, modifiers.nominalRoll);
const formattedRoll = formatRoll(mathConf[i].toString(), modifiers.maxRoll, modifiers.nominalRoll);
mathConf[i] = formattedRoll.solvedStep;
tempCountDetails.push(formattedRoll.countDetails);
} else if (mathConf[i].toString().toLowerCase() === 'e') {
// If the operand is the constant e, create a SolvedStep for it
mathConf[i] = {
@ -77,7 +75,7 @@ export const parseRoll = (fullCmd: string, modifiers: RollModifiers): SolvedRoll
containsCrit: false,
containsFail: false,
};
} else if (mathConf[i].toString().toLowerCase() === 'pi' || mathConf[i].toString().toLowerCase() == '𝜋') {
} else if (mathConf[i].toString().toLowerCase() === 'pi' || mathConf[i].toString().toLowerCase() === '𝜋') {
// If the operand is the constant pi, create a SolvedStep for it
mathConf[i] = {
total: Math.PI,
@ -190,6 +188,16 @@ export const parseRoll = (fullCmd: string, modifiers: RollModifiers): SolvedRoll
returnmsg.line1 = line1;
returnmsg.line2 = line2;
returnmsg.line3 = line3;
// Reduce counts to a single object
returnmsg.counts = tempCountDetails.reduce((acc, cnt) => ({
total: acc.total + cnt.total,
successful: acc.successful + cnt.successful,
failed: acc.failed + cnt.failed,
rerolled: acc.rerolled + cnt.rerolled,
dropped: acc.dropped + cnt.dropped,
exploded: acc.exploded + cnt.exploded,
}));
} catch (solverError) {
// Welp, the unthinkable happened, we hit an error

View File

@ -5,11 +5,12 @@ import {
} from '../../deps.ts';
import { roll } from './roller.ts';
import { SolvedStep } from './solver.d.ts';
import { rollCounter } from './counter.ts';
import { RollFormat } from './solver.d.ts';
// formatRoll(rollConf, maximiseRoll, nominalRoll) returns one SolvedStep
// formatRoll handles creating and formatting the completed rolls into the SolvedStep format
export const formatRoll = (rollConf: string, maximiseRoll: boolean, nominalRoll: boolean): SolvedStep => {
export const formatRoll = (rollConf: string, maximiseRoll: boolean, nominalRoll: boolean): RollFormat => {
let tempTotal = 0;
let tempDetails = '[';
let tempCrit = false;
@ -59,9 +60,12 @@ export const formatRoll = (rollConf: string, maximiseRoll: boolean, nominalRoll:
tempDetails += ']';
return {
total: tempTotal,
details: tempDetails,
containsCrit: tempCrit,
containsFail: tempFail,
solvedStep: {
total: tempTotal,
details: tempDetails,
containsCrit: tempCrit,
containsFail: tempFail,
},
countDetails: rollCounter(tempRollSet),
};
};

View File

@ -29,16 +29,6 @@ export type ReturnData = {
initConfig: string;
};
// SolvedRoll is the complete solved and formatted roll, or the error said roll created
export type SolvedRoll = {
error: boolean;
errorMsg: string;
errorCode: string;
line1: string;
line2: string;
line3: string;
};
// CountDetails is the object holding the count data for creating the Count Embed
export type CountDetails = {
total: number;
@ -48,3 +38,20 @@ export type CountDetails = {
dropped: number;
exploded: number;
};
// RollFormat is the return structure for the rollFormatter
export type RollFormat = {
solvedStep: SolvedStep;
countDetails: CountDetails;
};
// SolvedRoll is the complete solved and formatted roll, or the error said roll created
export type SolvedRoll = {
error: boolean;
errorMsg: string;
errorCode: string;
line1: string;
line2: string;
line3: string;
counts: CountDetails;
};