break rollUtils into multiple files
This commit is contained in:
parent
730c441645
commit
2b579eb4ac
|
@ -1,11 +1,9 @@
|
||||||
import { closeLog, initLog } from '@Log4Deno';
|
import { closeLog, initLog } from '@Log4Deno';
|
||||||
|
|
||||||
import { DEBUG } from '~flags';
|
|
||||||
|
|
||||||
import { parseRoll } from 'artigen/parser.ts';
|
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
|
// Alert rollQueue that this worker is ready
|
||||||
self.postMessage('ready');
|
self.postMessage('ready');
|
|
@ -4,13 +4,14 @@ import { log, LogTypes as LT } from '@Log4Deno';
|
||||||
import config from '~config';
|
import config from '~config';
|
||||||
import { DEVMODE } from '~flags';
|
import { DEVMODE } from '~flags';
|
||||||
|
|
||||||
import { loggingEnabled } from 'artigen/rollUtils.ts';
|
|
||||||
import { SolvedRoll } from 'artigen/solver.d.ts';
|
import { SolvedRoll } from 'artigen/solver.d.ts';
|
||||||
|
|
||||||
import { removeWorker } from 'artigen/managers/countManager.ts';
|
import { removeWorker } from 'artigen/managers/countManager.ts';
|
||||||
import { QueuedRoll } from 'artigen/managers/manager.d.ts';
|
import { QueuedRoll } from 'artigen/managers/manager.d.ts';
|
||||||
|
|
||||||
|
import { loggingEnabled } from 'artigen/utils/logFlag.ts';
|
||||||
import dbClient from 'db/client.ts';
|
import dbClient from 'db/client.ts';
|
||||||
|
|
||||||
import { queries } from 'db/common.ts';
|
import { queries } from 'db/common.ts';
|
||||||
|
|
||||||
import stdResp from 'endpoints/stdResponses.ts';
|
import stdResp from 'endpoints/stdResponses.ts';
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
import { log, LogTypes as LT } from '@Log4Deno';
|
import { log, LogTypes as LT } from '@Log4Deno';
|
||||||
|
|
||||||
import { loggingEnabled } from 'artigen/rollUtils.ts';
|
|
||||||
|
|
||||||
import { QueuedRoll } from 'artigen/managers/manager.d.ts';
|
import { QueuedRoll } from 'artigen/managers/manager.d.ts';
|
||||||
|
|
||||||
|
import { loggingEnabled } from 'artigen/utils/logFlag.ts';
|
||||||
|
|
||||||
export const onWorkerReady = (rollWorker: Worker, rollRequest: QueuedRoll) => {
|
export const onWorkerReady = (rollWorker: Worker, rollRequest: QueuedRoll) => {
|
||||||
loggingEnabled && log(LT.LOG, `Sending roll to worker: ${rollRequest.rollCmd}, ${JSON.stringify(rollRequest.modifiers)}`);
|
loggingEnabled && log(LT.LOG, `Sending roll to worker: ${rollRequest.rollCmd}, ${JSON.stringify(rollRequest.modifiers)}`);
|
||||||
rollWorker.postMessage({
|
rollWorker.postMessage({
|
||||||
|
|
|
@ -10,7 +10,7 @@ import { terminateWorker } from 'artigen/managers/handler/workerTerminate.ts';
|
||||||
export const handleRollRequest = (rollRequest: QueuedRoll) => {
|
export const handleRollRequest = (rollRequest: QueuedRoll) => {
|
||||||
// Handle setting up and calling the rollWorker
|
// Handle setting up and calling the rollWorker
|
||||||
addWorker();
|
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);
|
const workerTimeout = setTimeout(() => terminateWorker(rollWorker, rollRequest), config.limits.workerTimeout);
|
||||||
|
|
||||||
// Handle events from the worker
|
// Handle events from the worker
|
||||||
|
|
|
@ -3,10 +3,14 @@ import { log, LogTypes as LT } from '@Log4Deno';
|
||||||
import config from '~config';
|
import config from '~config';
|
||||||
|
|
||||||
import { formatRoll } from 'artigen/rollFormatter.ts';
|
import { formatRoll } from 'artigen/rollFormatter.ts';
|
||||||
import { compareTotalRolls, compareTotalRollsReverse, escapeCharacters, legalMathOperators, loggingEnabled } from 'artigen/rollUtils.ts';
|
|
||||||
import { fullSolver } from 'artigen/solver.ts';
|
import { fullSolver } from 'artigen/solver.ts';
|
||||||
import { CountDetails, ReturnData, SolvedRoll, SolvedStep } from 'artigen/solver.d.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';
|
import { RollModifiers } from 'src/mod.d.ts';
|
||||||
|
|
||||||
// parseRoll(fullCmd, modifiers)
|
// parseRoll(fullCmd, modifiers)
|
||||||
|
@ -34,7 +38,7 @@ export const parseRoll = (fullCmd: string, modifiers: RollModifiers): SolvedRoll
|
||||||
try {
|
try {
|
||||||
// Split the fullCmd by the command prefix to allow every roll/math op to be handled individually
|
// Split the fullCmd by the command prefix to allow every roll/math op to be handled individually
|
||||||
const sepRolls = fullCmd.split(config.prefix);
|
const sepRolls = fullCmd.split(config.prefix);
|
||||||
// TODO: HERE for the [[ ]] nesting stuff
|
// TODO(@burn-e99): HERE for the [[ ]] nesting stuff
|
||||||
|
|
||||||
const tempReturnData: ReturnData[] = [];
|
const tempReturnData: ReturnData[] = [];
|
||||||
const tempCountDetails: CountDetails[] = [
|
const tempCountDetails: CountDetails[] = [
|
||||||
|
|
|
@ -1,11 +1,12 @@
|
||||||
import { log, LogTypes as LT } from '@Log4Deno';
|
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 { RollModifiers } from 'src/mod.d.ts';
|
||||||
import { roll } from 'artigen/roller.ts';
|
import { roll } from 'artigen/roller.ts';
|
||||||
import { loggingEnabled } from 'artigen/rollUtils.ts';
|
|
||||||
import { RollFormat } from 'artigen/solver.d.ts';
|
import { RollFormat } from 'artigen/solver.d.ts';
|
||||||
|
|
||||||
|
import { loggingEnabled } from 'artigen/utils/logFlag.ts';
|
||||||
|
|
||||||
// formatRoll(rollConf, modifiers) returns one SolvedStep
|
// formatRoll(rollConf, modifiers) returns one SolvedStep
|
||||||
// formatRoll handles creating and formatting the completed rolls into the SolvedStep format
|
// formatRoll handles creating and formatting the completed rolls into the SolvedStep format
|
||||||
export const formatRoll = (rollConf: string, modifiers: RollModifiers): RollFormat => {
|
export const formatRoll = (rollConf: string, modifiers: RollModifiers): RollFormat => {
|
||||||
|
|
|
@ -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, <DPercentConf> { 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;
|
|
||||||
};
|
|
|
@ -2,9 +2,12 @@ import { log, LogTypes as LT } from '@Log4Deno';
|
||||||
|
|
||||||
import config from '~config';
|
import config from '~config';
|
||||||
|
|
||||||
import { compareOrigIdx, compareRolls, genFateRoll, genRoll, loggingEnabled } from 'artigen/rollUtils.ts';
|
|
||||||
import { RollConf, RollSet, RollType } from 'artigen/solver.d.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';
|
import { RollModifiers } from 'src/mod.d.ts';
|
||||||
|
|
||||||
// Call with loopCountCheck(++loopCount);
|
// Call with loopCountCheck(++loopCount);
|
||||||
|
|
|
@ -6,7 +6,9 @@
|
||||||
import { log, LogTypes as LT } from '@Log4Deno';
|
import { log, LogTypes as LT } from '@Log4Deno';
|
||||||
|
|
||||||
import { SolvedStep } from 'artigen/solver.d.ts';
|
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(conf, wrapDetails) returns one condensed SolvedStep
|
||||||
// fullSolver is a function that recursively solves the full roll and math
|
// fullSolver is a function that recursively solves the full roll and math
|
||||||
|
|
|
@ -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;
|
||||||
|
};
|
|
@ -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, <DPercentConf> { on: false }) - 1];
|
||||||
|
}
|
||||||
|
};
|
|
@ -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);
|
|
@ -0,0 +1 @@
|
||||||
|
export const loggingEnabled = false;
|
|
@ -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;
|
||||||
|
};
|
|
@ -2,9 +2,10 @@ import { log, LogTypes as LT } from '@Log4Deno';
|
||||||
|
|
||||||
import config from '~config';
|
import config from '~config';
|
||||||
|
|
||||||
import { loggingEnabled } from 'artigen/rollUtils.ts';
|
|
||||||
import { CountDetails, SolvedRoll } from 'artigen/solver.d.ts';
|
import { CountDetails, SolvedRoll } from 'artigen/solver.d.ts';
|
||||||
|
|
||||||
|
import { loggingEnabled } from 'artigen/utils/logFlag.ts';
|
||||||
|
|
||||||
import { RollModifiers } from 'src/mod.d.ts';
|
import { RollModifiers } from 'src/mod.d.ts';
|
||||||
|
|
||||||
export const failColor = 0xe71212;
|
export const failColor = 0xe71212;
|
||||||
|
|
Loading…
Reference in New Issue