diff --git a/src/artigen/rollWorker.ts b/src/artigen/managers/artigenWorker.ts similarity index 83% rename from src/artigen/rollWorker.ts rename to src/artigen/managers/artigenWorker.ts index 51c068b..80966d5 100644 --- a/src/artigen/rollWorker.ts +++ b/src/artigen/managers/artigenWorker.ts @@ -1,11 +1,9 @@ import { closeLog, initLog } from '@Log4Deno'; -import { DEBUG } from '~flags'; - import { parseRoll } from 'artigen/parser.ts'; -import { loggingEnabled } from 'artigen/rollUtils.ts'; +import { loggingEnabled } from 'artigen/utils/logFlag.ts'; -loggingEnabled && initLog('logs/worker', DEBUG); +loggingEnabled && initLog('logs/worker', loggingEnabled); // Alert rollQueue that this worker is ready self.postMessage('ready'); diff --git a/src/artigen/managers/handler/workerComplete.ts b/src/artigen/managers/handler/workerComplete.ts index 669a3e3..cf39647 100644 --- a/src/artigen/managers/handler/workerComplete.ts +++ b/src/artigen/managers/handler/workerComplete.ts @@ -4,13 +4,14 @@ import { log, LogTypes as LT } from '@Log4Deno'; import config from '~config'; import { DEVMODE } from '~flags'; -import { loggingEnabled } from 'artigen/rollUtils.ts'; import { SolvedRoll } from 'artigen/solver.d.ts'; import { removeWorker } from 'artigen/managers/countManager.ts'; import { QueuedRoll } from 'artigen/managers/manager.d.ts'; +import { loggingEnabled } from 'artigen/utils/logFlag.ts'; import dbClient from 'db/client.ts'; + import { queries } from 'db/common.ts'; import stdResp from 'endpoints/stdResponses.ts'; diff --git a/src/artigen/managers/handler/workerReady.ts b/src/artigen/managers/handler/workerReady.ts index a103aa8..a30ef03 100644 --- a/src/artigen/managers/handler/workerReady.ts +++ b/src/artigen/managers/handler/workerReady.ts @@ -1,9 +1,9 @@ import { log, LogTypes as LT } from '@Log4Deno'; -import { loggingEnabled } from 'artigen/rollUtils.ts'; - import { QueuedRoll } from 'artigen/managers/manager.d.ts'; +import { loggingEnabled } from 'artigen/utils/logFlag.ts'; + export const onWorkerReady = (rollWorker: Worker, rollRequest: QueuedRoll) => { loggingEnabled && log(LT.LOG, `Sending roll to worker: ${rollRequest.rollCmd}, ${JSON.stringify(rollRequest.modifiers)}`); rollWorker.postMessage({ diff --git a/src/artigen/managers/workerManager.ts b/src/artigen/managers/workerManager.ts index 3b6ba55..b0c9df6 100644 --- a/src/artigen/managers/workerManager.ts +++ b/src/artigen/managers/workerManager.ts @@ -10,7 +10,7 @@ import { terminateWorker } from 'artigen/managers/handler/workerTerminate.ts'; export const handleRollRequest = (rollRequest: QueuedRoll) => { // Handle setting up and calling the rollWorker addWorker(); - const rollWorker = new Worker(new URL('../rollWorker.ts', import.meta.url).href, { type: 'module' }); + const rollWorker = new Worker(new URL('./artigenWorker.ts', import.meta.url).href, { type: 'module' }); const workerTimeout = setTimeout(() => terminateWorker(rollWorker, rollRequest), config.limits.workerTimeout); // Handle events from the worker diff --git a/src/artigen/parser.ts b/src/artigen/parser.ts index 63c00f1..723d0c7 100644 --- a/src/artigen/parser.ts +++ b/src/artigen/parser.ts @@ -3,10 +3,14 @@ import { log, LogTypes as LT } from '@Log4Deno'; import config from '~config'; import { formatRoll } from 'artigen/rollFormatter.ts'; -import { compareTotalRolls, compareTotalRollsReverse, escapeCharacters, legalMathOperators, loggingEnabled } from 'artigen/rollUtils.ts'; import { fullSolver } from 'artigen/solver.ts'; import { CountDetails, ReturnData, SolvedRoll, SolvedStep } from 'artigen/solver.d.ts'; +import { escapeCharacters } from 'artigen/utils/escape.ts'; +import { legalMathOperators } from 'artigen/utils/legalMath.ts'; +import { loggingEnabled } from 'artigen/utils/logFlag.ts'; +import { compareTotalRolls, compareTotalRollsReverse } from 'artigen/utils/sortFuncs.ts'; + import { RollModifiers } from 'src/mod.d.ts'; // parseRoll(fullCmd, modifiers) @@ -34,7 +38,7 @@ export const parseRoll = (fullCmd: string, modifiers: RollModifiers): SolvedRoll try { // Split the fullCmd by the command prefix to allow every roll/math op to be handled individually const sepRolls = fullCmd.split(config.prefix); - // TODO: HERE for the [[ ]] nesting stuff + // TODO(@burn-e99): HERE for the [[ ]] nesting stuff const tempReturnData: ReturnData[] = []; const tempCountDetails: CountDetails[] = [ diff --git a/src/artigen/rollFormatter.ts b/src/artigen/rollFormatter.ts index ca916a2..0008373 100644 --- a/src/artigen/rollFormatter.ts +++ b/src/artigen/rollFormatter.ts @@ -1,11 +1,12 @@ import { log, LogTypes as LT } from '@Log4Deno'; -import { rollCounter } from 'artigen/counter.ts'; +import { rollCounter } from './utils/counter.ts'; import { RollModifiers } from 'src/mod.d.ts'; import { roll } from 'artigen/roller.ts'; -import { loggingEnabled } from 'artigen/rollUtils.ts'; import { RollFormat } from 'artigen/solver.d.ts'; +import { loggingEnabled } from 'artigen/utils/logFlag.ts'; + // formatRoll(rollConf, modifiers) returns one SolvedStep // formatRoll handles creating and formatting the completed rolls into the SolvedStep format export const formatRoll = (rollConf: string, modifiers: RollModifiers): RollFormat => { diff --git a/src/artigen/rollUtils.ts b/src/artigen/rollUtils.ts deleted file mode 100644 index 986d848..0000000 --- a/src/artigen/rollUtils.ts +++ /dev/null @@ -1,96 +0,0 @@ -import { log, LogTypes as LT } from '@Log4Deno'; -import { RollModifiers } from 'src/mod.d.ts'; - -import { DPercentConf, ReturnData, RollSet } from 'artigen/solver.d.ts'; - -type MathFunction = (arg: number) => number; -export const loggingEnabled = false; -export const legalMath: MathFunction[] = []; -(Object.getOwnPropertyNames(Math) as (keyof Math)[]).forEach((propName) => { - const mathProp = Math[propName]; - if (typeof mathProp === 'function' && mathProp.length === 1) { - legalMath.push(mathProp as MathFunction); - } -}); -export const legalMathOperators = legalMath.map((oper) => oper.name); - -// genRoll(size) returns number -// genRoll rolls a die of size size and returns the result -export const genRoll = (size: number, modifiers: RollModifiers, dPercent: DPercentConf): number => { - let result; - if (modifiers.maxRoll) { - result = size; - } else if (modifiers.minRoll) { - result = 1; - } else { - // Math.random * size will return a decimal number between 0 and size (excluding size), so add 1 and floor the result to not get 0 as a result - result = modifiers.nominalRoll ? size / 2 + 0.5 : Math.floor(Math.random() * size + 1); - } - return dPercent.on ? (result - 1) * dPercent.sizeAdjustment : result; -}; - -// genFateRoll returns -1|0|1 -// genFateRoll turns a d6 into a fate die, with sides: -1, -1, 0, 0, 1, 1 -export const genFateRoll = (modifiers: RollModifiers): number => { - if (modifiers.nominalRoll) { - return 0; - } else { - const sides = [-1, -1, 0, 0, 1, 1]; - return sides[genRoll(6, modifiers, { on: false }) - 1]; - } -}; - -// compareRolls(a, b) returns -1|0|1 -// compareRolls is used to order an array of RollSets by RollSet.roll -export const compareRolls = (a: RollSet, b: RollSet): number => { - if (a.roll < b.roll) { - return -1; - } - if (a.roll > b.roll) { - return 1; - } - return 0; -}; - -const internalCompareTotalRolls = (a: ReturnData, b: ReturnData, dir: 1 | -1): number => { - if (a.rollTotal < b.rollTotal) { - return -1 * dir; - } - if (a.rollTotal > b.rollTotal) { - return 1 * dir; - } - return 0; -}; - -// compareTotalRolls(a, b) returns -1|0|1 -// compareTotalRolls is used to order an array of RollSets by RollSet.roll -export const compareTotalRolls = (a: ReturnData, b: ReturnData): number => internalCompareTotalRolls(a, b, 1); - -// compareTotalRollsReverse(a, b) returns 1|0|-1 -// compareTotalRollsReverse is used to order an array of RollSets by RollSet.roll reversed -export const compareTotalRollsReverse = (a: ReturnData, b: ReturnData): number => internalCompareTotalRolls(a, b, -1); - -// compareRolls(a, b) returns -1|0|1 -// compareRolls is used to order an array of RollSets by RollSet.origIdx -export const compareOrigIdx = (a: RollSet, b: RollSet): number => { - if (a.origIdx < b.origIdx) { - return -1; - } - if (a.origIdx > b.origIdx) { - return 1; - } - return 0; -}; - -// escapeCharacters(str, esc) returns str -// escapeCharacters escapes all characters listed in esc -export const escapeCharacters = (str: string, esc: string): string => { - // Loop thru each esc char one at a time - for (const e of esc) { - loggingEnabled && log(LT.LOG, `Escaping character ${e} | ${str}, ${esc}`); - // Create a new regex to look for that char that needs replaced and escape it - const tempRgx = new RegExp(`[${e}]`, 'g'); - str = str.replace(tempRgx, `\\${e}`); - } - return str; -}; diff --git a/src/artigen/roller.ts b/src/artigen/roller.ts index 86f8c8b..2716dc2 100644 --- a/src/artigen/roller.ts +++ b/src/artigen/roller.ts @@ -2,9 +2,12 @@ import { log, LogTypes as LT } from '@Log4Deno'; import config from '~config'; -import { compareOrigIdx, compareRolls, genFateRoll, genRoll, loggingEnabled } from 'artigen/rollUtils.ts'; import { RollConf, RollSet, RollType } from 'artigen/solver.d.ts'; +import { genFateRoll, genRoll } from 'artigen/utils/generateRoll.ts'; +import { loggingEnabled } from 'artigen/utils/logFlag.ts'; +import { compareOrigIdx, compareRolls } from 'artigen/utils/sortFuncs.ts'; + import { RollModifiers } from 'src/mod.d.ts'; // Call with loopCountCheck(++loopCount); diff --git a/src/artigen/solver.ts b/src/artigen/solver.ts index 1a12f16..ed4178f 100644 --- a/src/artigen/solver.ts +++ b/src/artigen/solver.ts @@ -6,7 +6,9 @@ import { log, LogTypes as LT } from '@Log4Deno'; import { SolvedStep } from 'artigen/solver.d.ts'; -import { legalMath, legalMathOperators, loggingEnabled } from 'artigen/rollUtils.ts'; + +import { legalMath, legalMathOperators } from 'artigen/utils/legalMath.ts'; +import { loggingEnabled } from 'artigen/utils/logFlag.ts'; // fullSolver(conf, wrapDetails) returns one condensed SolvedStep // fullSolver is a function that recursively solves the full roll and math diff --git a/src/artigen/counter.ts b/src/artigen/utils/counter.ts similarity index 100% rename from src/artigen/counter.ts rename to src/artigen/utils/counter.ts diff --git a/src/artigen/utils/escape.ts b/src/artigen/utils/escape.ts new file mode 100644 index 0000000..7fc43ca --- /dev/null +++ b/src/artigen/utils/escape.ts @@ -0,0 +1,16 @@ +import { log, LogTypes as LT } from '@Log4Deno'; + +import { loggingEnabled } from 'artigen/utils/logFlag.ts'; + +// escapeCharacters(str, esc) returns str +// escapeCharacters escapes all characters listed in esc +export const escapeCharacters = (str: string, esc: string): string => { + // Loop thru each esc char one at a time + for (const e of esc) { + loggingEnabled && log(LT.LOG, `Escaping character ${e} | ${str}, ${esc}`); + // Create a new regex to look for that char that needs replaced and escape it + const tempRgx = new RegExp(`[${e}]`, 'g'); + str = str.replace(tempRgx, `\\${e}`); + } + return str; +}; diff --git a/src/artigen/utils/generateRoll.ts b/src/artigen/utils/generateRoll.ts new file mode 100644 index 0000000..1843f57 --- /dev/null +++ b/src/artigen/utils/generateRoll.ts @@ -0,0 +1,29 @@ +import { RollModifiers } from 'src/mod.d.ts'; + +import { DPercentConf } from 'artigen/solver.d.ts'; + +// genRoll(size) returns number +// genRoll rolls a die of size size and returns the result +export const genRoll = (size: number, modifiers: RollModifiers, dPercent: DPercentConf): number => { + let result; + if (modifiers.maxRoll) { + result = size; + } else if (modifiers.minRoll) { + result = 1; + } else { + // Math.random * size will return a decimal number between 0 and size (excluding size), so add 1 and floor the result to not get 0 as a result + result = modifiers.nominalRoll ? size / 2 + 0.5 : Math.floor(Math.random() * size + 1); + } + return dPercent.on ? (result - 1) * dPercent.sizeAdjustment : result; +}; + +// genFateRoll returns -1|0|1 +// genFateRoll turns a d6 into a fate die, with sides: -1, -1, 0, 0, 1, 1 +export const genFateRoll = (modifiers: RollModifiers): number => { + if (modifiers.nominalRoll) { + return 0; + } else { + const sides = [-1, -1, 0, 0, 1, 1]; + return sides[genRoll(6, modifiers, { on: false }) - 1]; + } +}; diff --git a/src/artigen/utils/legalMath.ts b/src/artigen/utils/legalMath.ts new file mode 100644 index 0000000..c7dc358 --- /dev/null +++ b/src/artigen/utils/legalMath.ts @@ -0,0 +1,11 @@ +type MathFunction = (arg: number) => number; + +export const legalMath: MathFunction[] = []; +(Object.getOwnPropertyNames(Math) as (keyof Math)[]).forEach((propName) => { + const mathProp = Math[propName]; + if (typeof mathProp === 'function' && mathProp.length === 1) { + legalMath.push(mathProp as MathFunction); + } +}); + +export const legalMathOperators = legalMath.map((oper) => oper.name); diff --git a/src/artigen/utils/logFlag.ts b/src/artigen/utils/logFlag.ts new file mode 100644 index 0000000..177f748 --- /dev/null +++ b/src/artigen/utils/logFlag.ts @@ -0,0 +1 @@ +export const loggingEnabled = false; diff --git a/src/artigen/utils/sortFuncs.ts b/src/artigen/utils/sortFuncs.ts new file mode 100644 index 0000000..ec18f32 --- /dev/null +++ b/src/artigen/utils/sortFuncs.ts @@ -0,0 +1,43 @@ +import { ReturnData, RollSet } from 'src/artigen/solver.d.ts'; + +// compareRolls(a, b) returns -1|0|1 +// compareRolls is used to order an array of RollSets by RollSet.roll +export const compareRolls = (a: RollSet, b: RollSet): number => { + if (a.roll < b.roll) { + return -1; + } + if (a.roll > b.roll) { + return 1; + } + return 0; +}; + +const internalCompareTotalRolls = (a: ReturnData, b: ReturnData, dir: 1 | -1): number => { + if (a.rollTotal < b.rollTotal) { + return -1 * dir; + } + if (a.rollTotal > b.rollTotal) { + return 1 * dir; + } + return 0; +}; + +// compareTotalRolls(a, b) returns -1|0|1 +// compareTotalRolls is used to order an array of RollSets by RollSet.roll +export const compareTotalRolls = (a: ReturnData, b: ReturnData): number => internalCompareTotalRolls(a, b, 1); + +// compareTotalRollsReverse(a, b) returns 1|0|-1 +// compareTotalRollsReverse is used to order an array of RollSets by RollSet.roll reversed +export const compareTotalRollsReverse = (a: ReturnData, b: ReturnData): number => internalCompareTotalRolls(a, b, -1); + +// compareRolls(a, b) returns -1|0|1 +// compareRolls is used to order an array of RollSets by RollSet.origIdx +export const compareOrigIdx = (a: RollSet, b: RollSet): number => { + if (a.origIdx < b.origIdx) { + return -1; + } + if (a.origIdx > b.origIdx) { + return 1; + } + return 0; +}; diff --git a/src/commandUtils.ts b/src/commandUtils.ts index 942c437..183b002 100644 --- a/src/commandUtils.ts +++ b/src/commandUtils.ts @@ -2,9 +2,10 @@ import { log, LogTypes as LT } from '@Log4Deno'; import config from '~config'; -import { loggingEnabled } from 'artigen/rollUtils.ts'; import { CountDetails, SolvedRoll } from 'artigen/solver.d.ts'; +import { loggingEnabled } from 'artigen/utils/logFlag.ts'; + import { RollModifiers } from 'src/mod.d.ts'; export const failColor = 0xe71212;