Progress on action choices

This commit is contained in:
Thaum Rystra
2024-03-30 21:12:35 +02:00
parent 6c3d4b91eb
commit 6138be8083
33 changed files with 210 additions and 55 deletions

View File

@@ -0,0 +1,28 @@
import Alea from 'alea';
/**
* Return a function that can be be used as InputProvider.rollDice
* this function instance must be used for the entire action
*/
export default function getDeterministicDiceRoller(
actionId: string
): (dice: { number: number, diceSize: number }[]) => Promise<number[][]> {
// Create a random number generator seeded on the ID of the action
if (!actionId) throw new Meteor.Error('Id Required', 'action ID can not be ' + actionId)
const randFrac = Alea(actionId);
return (dice) => {
const results: number[][] = [];
for (const diceRoll of dice) {
const values: number[] = [];
if (diceRoll.number > 100) {
throw new Meteor.Error('Too many dice', 'can only roll up to 100 dice at once');
}
for (let i = 0; i < diceRoll.number; i++) {
const rolledValue = ~~(randFrac() * diceRoll.diceSize) + 1
values.push(rolledValue);
}
results.push(values);
}
return Promise.resolve(results);
}
}