Added rollWorker so that multiple dice rolls can happen simultaneously

This commit is contained in:
Ean Milligan (Bastion) 2022-05-28 03:37:11 -04:00
parent 7391a0fd68
commit 1f4d1e3ef6
5 changed files with 140 additions and 45 deletions

View File

@ -1,7 +1,7 @@
{
"compilerOptions": {
"allowJs": true,
"lib": ["deno.window"],
"lib": ["deno.worker"],
"strict": true
},
"lint": {

View File

@ -12,6 +12,7 @@ import {
} from '../../deps.ts';
import solver from '../solver/_index.ts';
import { SolvedRoll } from '../solver/solver.d.ts';
import { RollModifiers } from '../mod.d.ts';
import { generateCountDetailsEmbed, generateDMFailed, generateRollEmbed, infoColor1, warnColor } from '../commandUtils.ts';
import rollFuncs from './roll/_index.ts';
@ -59,8 +60,36 @@ export const roll = async (message: DiscordenoMessage, args: string[], command:
// Rejoin all of the args and send it into the solver, if solver returns a falsy item, an error object will be substituded in
const rollCmd = `${command} ${args.join(' ')}`;
const returnmsg = solver.parseRoll(rollCmd, modifiers) || <SolvedRoll> { error: true, errorCode: 'EmptyMessage', errorMsg: 'Error: Empty message' };
// const returnmsg = solver.parseRoll(rollCmd, modifiers) || <SolvedRoll>{ error: true, errorCode: 'EmptyMessage', errorMsg: 'Error: Empty message' };
const rollWorker = new Worker(new URL('../solver/rollWorker.ts', import.meta.url).href, { type: 'module' });
const workerTimeout = setTimeout(async () => {
rollWorker.terminate();
m.edit({
embeds: [
(await generateRollEmbed(
message.authorId,
<SolvedRoll>{
error: true,
errorCode: 'TooComplex',
errorMsg: 'Error: Roll Too Complex, try breaking roll down into simpler parts',
},
<RollModifiers>{},
)).embed,
],
});
}, 60000);
rollWorker.postMessage({
rollCmd,
modifiers,
});
rollWorker.addEventListener('message', async (e) => {
try {
clearTimeout(workerTimeout);
const returnmsg = e.data;
const pubEmbedDetails = await generateRollEmbed(message.authorId, returnmsg, modifiers);
const gmEmbedDetails = await generateRollEmbed(message.authorId, returnmsg, gmModifiers);
const countEmbed = generateCountDetailsEmbed(returnmsg.counts);
@ -102,17 +131,21 @@ export const roll = async (message: DiscordenoMessage, args: string[], command:
});
} else {
// Not a gm roll, so just send normal embed to correct channel
await m.edit({
const n = await m.edit({
embeds: modifiers.count ? [pubEmbedDetails.embed, countEmbed] : [pubEmbedDetails.embed],
});
if (pubEmbedDetails.hasAttachment) {
// Attachment requires you to send a new message
message.send({
n.reply({
file: pubEmbedDetails.attachment,
});
}
}
}
} catch (e) {
log(LT.ERROR, `Unddandled Error: ${JSON.stringify(e)}`);
}
});
} catch (e) {
log(LT.ERROR, `Undandled Error: ${JSON.stringify(e)}`);
}

View File

@ -17,6 +17,19 @@ import { fullSolver } from './solver.ts';
export const parseRoll = (fullCmd: string, modifiers: RollModifiers): SolvedRoll => {
const returnmsg = <SolvedRoll> {
error: false,
errorCode: '',
errorMsg: '',
line1: '',
line2: '',
line3: '',
counts: {
total: 0,
successful: 0,
failed: 0,
rerolled: 0,
dropped: 0,
exploded: 0,
},
};
// Whole function lives in a try-catch to allow safe throwing of errors on purpose
@ -25,7 +38,14 @@ export const parseRoll = (fullCmd: string, modifiers: RollModifiers): SolvedRoll
const sepRolls = fullCmd.split(config.prefix);
const tempReturnData: ReturnData[] = [];
const tempCountDetails: CountDetails[] = [];
const tempCountDetails: CountDetails[] = [{
total: 0,
successful: 0,
failed: 0,
rerolled: 0,
dropped: 0,
exploded: 0,
}];
// Loop thru all roll/math ops
for (const sepRoll of sepRolls) {
@ -75,6 +95,21 @@ export const parseRoll = (fullCmd: string, modifiers: RollModifiers): SolvedRoll
containsCrit: false,
containsFail: false,
};
} else if (mathConf[i].toString().toLowerCase() === 'fart' || mathConf[i].toString().toLowerCase() === '💩') {
mathConf[i] = {
total: 7,
details: '💩',
containsCrit: false,
containsFail: false,
};
} else if (mathConf[i].toString().toLowerCase() === 'inf' || mathConf[i].toString().toLowerCase() === 'infinity' || mathConf[i].toString().toLowerCase() === '∞') {
// If the operand is the constant Infinity, create a SolvedStep for it
mathConf[i] = {
total: Infinity,
details: '∞',
containsCrit: false,
containsFail: false,
};
} 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] = {

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

@ -0,0 +1,23 @@
import { parseRoll } from './parser.ts';
self.onmessage = async (e: any) => {
const payload = e.data;
const returnmsg = parseRoll(payload.rollCmd, payload.modifiers) || {
error: true,
errorCode: 'EmptyMessage',
errorMsg: 'Error: Empty message',
line1: '',
line2: '',
line3: '',
counts: {
total: 0,
successful: 0,
failed: 0,
rerolled: 0,
dropped: 0,
exploded: 0,
},
};
self.postMessage(returnmsg);
self.close();
};

View File

@ -83,6 +83,10 @@ export const roll = (rollStr: string, maximiseRoll: boolean, nominalRoll: boolea
rollConf.dieSize = parseInt(remains.slice(0, afterDieIdx));
remains = remains.slice(afterDieIdx);
if (!rollConf.dieCount || !rollConf.dieSize) {
throw new Error('YouNeedAD');
}
log(LT.LOG, `Handling roll ${rollStr} | Parsed Die Count: ${rollConf.dieCount}`);
log(LT.LOG, `Handling roll ${rollStr} | Parsed Die Size: ${rollConf.dieSize}`);