Typescript all the parser things

This commit is contained in:
Thaum Rystra
2024-02-20 23:21:12 +02:00
parent 3ea492ee78
commit ac15512bc5
86 changed files with 926 additions and 718 deletions

View File

@@ -1,8 +1,6 @@
import { EngineAction } from '/imports/api/engine/action/EngineActions';
type InputProvider = {
rollDice(
action: EngineAction, dice: { number: number, diceSize: number }[]
dice: { number: number, diceSize: number }[]
): Promise<number[][]>;
/**
* Choose from a provided selection
@@ -11,7 +9,6 @@ type InputProvider = {
* @param quantity Number of choices to make [min, max] inclusive, where -1 means no limit
*/
choose(
action: EngineAction,
choices: ({ _id: string } & Record<string, any>)[],
quantity?: [min: number, max: number],
): Promise<string[]>;

View File

@@ -6,7 +6,7 @@ const inputProviderForTests: InputProvider = {
* rollDice function returns the average roll for every dice rolled
* [5d10, 1d4] => [[6,6,6,6,6], [3]]
*/
async rollDice(action, dice) {
async rollDice(dice = []) {
const result: number[][] = [];
for (const diceRoll of dice) {
const averageRoll = Math.round(diceRoll.diceSize / 2);
@@ -21,7 +21,7 @@ const inputProviderForTests: InputProvider = {
/**
* For testing, always return the minimum number of choices, always choosing the first options
*/
async choose(action, choices, quantity = [1, 1]) {
async choose(choices, quantity = [1, 1]) {
const chosen: string[] = [];
const choiceQuantity = quantity[0] <= 0 ? 1 : quantity[0];
for (let i = 0; i < choiceQuantity && i < choices.length; i += 1) {

View File

@@ -1,4 +1,5 @@
import { Context, toPrimitiveOrString } from '/imports/parser/resolve';
import Context from '../../../../parser/types/Context';
import toPrimitiveOrString from '/imports/parser/toPrimitiveOrString';
import {
aggregateCalculationEffects,
aggregateCalculationProficiencies,
@@ -26,7 +27,7 @@ export default async function recalculateCalculation(
const {
result: unaffectedResult,
context
} = resolve(parseLevel, calcObj.parseNode, scope);
} = await resolve(parseLevel, calcObj.parseNode, scope);
calcObj.valueNode = unaffectedResult;
// store the unaffected value
@@ -47,7 +48,7 @@ export default async function recalculateCalculation(
// Resolve the modified valueNode, use the same context
const {
result: finalResult
} = resolve(parseLevel, calcObj.parseNode, scope, context);
} = await resolve(parseLevel, calcObj.parseNode, scope, context);
// Store the errors
calcObj.errors = context.errors;
@@ -55,12 +56,12 @@ export default async function recalculateCalculation(
// Store the value and its primitive
calcObj.value = toPrimitiveOrString(finalResult);
calcObj.valueNode = finalResult;
}
export async function rollAndReduceCalculation(
calcObj: CalculatedField, action: EngineAction, userInput: InputProvider
) {
if (!calcObj) throw new Error('calcObj is required');
const context = new Context();
const scope = await getEffectiveActionScope(action);
// Compile
@@ -68,10 +69,10 @@ export async function rollAndReduceCalculation(
const compiled = calcObj.valueNode;
// Roll
const { result: rolled } = resolve('roll', calcObj.valueNode, scope, context);
const { result: rolled } = await resolve('roll', calcObj.valueNode, scope, context, userInput);
// Reduce
const { result: reduced } = resolve('reduce', rolled, scope, context);
const { result: reduced } = await resolve('reduce', rolled, scope, context, userInput);
// Return
return { compiled, rolled, reduced, errors: context.errors };

View File

@@ -1,15 +1,13 @@
import embedInlineCalculations from '/imports/api/engine/computation/utility/embedInlineCalculations';
import recalculateCalculation from './recalculateCalculation'
export default async function recalculateInlineCalculations(inlineCalcObj, action) {
export default async function recalculateInlineCalculations(inlineCalcObj, action, parseLevel, userInput) {
// Skip if there are no calculations
if (!inlineCalcObj?.inlineCalculations?.length) return;
// Recalculate each calculation with the current scope
const promises = [];
for (const calc of inlineCalcObj.inlineCalculations) {
promises.push(recalculateCalculation(calc, action));
await recalculateCalculation(calc, action, undefined, userInput);
}
await Promise.all(promises);
// Embed the new calculated values
embedInlineCalculations(inlineCalcObj);
}

View File

@@ -1,6 +1,7 @@
import { getFromScope } from '/imports/api/creature/creatures/CreatureVariables';
import { EngineAction } from '/imports/api/engine/action/EngineActions';
import { getEffectiveActionScope } from '/imports/api/engine/action/functions/getEffectiveActionScope';
import recalculateCalculation from '/imports/api/engine/action/functions/recalculateCalculation';
import TaskResult from '/imports/api/engine/action/tasks/TaskResult';
import applyTask from '/imports/api/engine/action/tasks/applyTask';
import { getSingleProperty } from '/imports/api/engine/loadCreatures';
@@ -31,7 +32,7 @@ export default async function spendResources(
for (const att of prop.resources.attributesConsumed) {
const scope = await getEffectiveActionScope(action);
const statToDamage = await getFromScope(att.variableName, scope);
await recalculateCalculation(att.quantity, action, 'reduce');
await recalculateCalculation(att.quantity, action, 'reduce', userInput);
await applyTask(action, {
prop,
targetIds: [action.creatureId],
@@ -48,7 +49,7 @@ export default async function spendResources(
// Iterate through all the items consumed and consume them
if (prop.resources?.itemsConsumed?.length) {
for (const itemConsumed of prop.resources.itemsConsumed) {
await recalculateCalculation(itemConsumed.quantity, action, 'reduce');
await recalculateCalculation(itemConsumed.quantity, action, 'reduce', userInput);
if (!itemConsumed.itemId) {
throw 'No ammo was selected';
}