Got tests running on single property character

This commit is contained in:
Stefan Zermatten
2021-09-15 15:15:18 +02:00
parent 856fc41429
commit dfd7ad4af5
24 changed files with 277 additions and 119 deletions

View File

@@ -0,0 +1,36 @@
import { cloneDeep } from 'lodash';
import createGraph from 'ngraph.graph';
export default class CreatureComputation {
constructor(properties){
// Set up fields
this.originalPropsById = {};
this.propsById = {};
this.propsByType = {};
this.propsByVariableName = {};
this.props = properties;
this.dependencyGraph = createGraph();
// Store properties for easy access later
properties.forEach(prop => {
// Store a copy of the unmodified prop
this.originalPropsById[prop._id] = cloneDeep(prop);
// Store by id
this.propsById[prop._id] = prop;
// Store by type
this.propsByType[prop.type] ?
this.propsByType[prop.type].push(prop) :
this.propsByType[prop.type] = [prop];
// Store by variableName
this.propsByVariableName[prop.variableName] ?
this.propsByVariableName[prop.variableName].push(prop) :
this.propsByVariableName[prop.variableName]= [prop];
// Store the prop in the dependency graph
this.dependencyGraph.addNode(prop._id, prop);
});
}
}

View File

@@ -9,7 +9,7 @@ export default function linkCalculationDependencies(dependencyGraph, prop, {prop
// ancestors: {} //this gets added if there are resolved ancestors
};
// Traverse the parsed calculation looking for variable names
calcObj._parsedCalculation.travese(node => {
calcObj._parsedCalculation.traverse(node => {
// Skip nodes that aren't symbols or accessors
if (!(node instanceof SymbolNode || node instanceof AccessorNode)) return;
// Link ancestor references as direct property dependencies

View File

@@ -27,6 +27,8 @@ export default function linkInventory(forest, dependencyGraph){
}
function handleProp(prop, containerStack, dependencyGraph){
// Skip props that aren't part of the inventory
if (prop.type !== 'inventory' && prop.type !== 'container') return;
// Determine if this property is carried, items are carried by default
let carried = prop.type === 'container' ? prop.carried : true;

View File

@@ -13,7 +13,7 @@ const linkDependenciesByType = {
}
export default function linkTypeDependencies(dependencyGraph, prop){
linkDependenciesByType[prop.type]?.(prop);
linkDependenciesByType[prop.type]?.(dependencyGraph, prop);
}
function linkClassLevel(dependencyGraph, prop){

View File

@@ -1,20 +1,54 @@
import INLINE_CALCULATION_REGEX from '/imports/constants/INLINE_CALCULTION_REGEX.js';
import { prettifyParseError, parse } from '/imports/parser/parser.js';
import ErrorNode from '/imports/parser/parseTree/ErrorNode.js';
import applyFnToKey from '/imports/api/creature/computation/newEngine/utility/applyFnToKey.js';
import { get } from 'lodash';
export default function parseCalculationFields(prop, schemas){
parseInlineCalculationFields(prop, schemas);
parseDirectCalculationFields(prop, schemas);
}
function parseInlineCalculationFields(prop, schemas){
// For each key in the schema
schemas[prop.type]._schemaKeys.forEach( key => {
// That ends in .inlineCalculations
if (key.slice(-19) === '.inlineCalculations'){
const inlineCalcKey = key.slice(0, -19);
applyFnToKey(prop, inlineCalcKey, (prop, key) => {
const inlineCalcObj = get(prop, key);
if (!inlineCalcObj) return;
// Store a reference to all the inline calculations
prop._computationDetails.inlineCalculations.push(inlineCalcObj);
// Extract the calculations and store them on the property
let string = inlineCalcObj.text;
inlineCalcObj.inlineCalculations = [];
let matches = string.matchAll(INLINE_CALCULATION_REGEX);
for (let match of matches){
let calculation = match[1];
inlineCalcObj.inlineCalculations.push({
calculation,
});
}
});
}
});
}
function parseDirectCalculationFields(prop, schemas){
// For each key in the schema
schemas[prop.type]._schemaKeys.forEach( key => {
// that ends in '.calculation'
if (key.slice(-12) === '.calculation'){
const calcKey = key.sclice(0, -12);
const calcKey = key.slice(0, -12);
// Determine the level the calculation should compute down to
let parseLevel = schemas[prop.type].getDefinition(calcKey).parseLevel;
let parseLevel = schemas[prop.type].getDefinition(calcKey).parseLevel || 'reduce';
// For all fields matching they keys
// supports `keys.$.with.$.arrays`
applyFnToKey(prop, calcKey, calcObj => {
applyFnToKey(prop, calcKey, (prop, key) => {
const calcObj = get(prop, key);
if (!calcObj) return;
// Store a reference to all the calculations
prop._computationDetails.calculations.push(calcObj);
// Store the level to compute down to later
@@ -23,14 +57,7 @@ export default function parseCalculationFields(prop, schemas){
parseCalculation(calcObj);
});
// Or that ends in .inlineCalculations
} else if (key.slice(-19) === '.inlineCalculations'){
const inlineCalcKey = key.sclice(0, -19);
applyFnToKey(prop, inlineCalcKey, inlineCalcObj => {
// Store a reference to all the inline calculations
prop._computationDetails.inlineCalculations.push(inlineCalcObj);
});
}
});
}

View File

@@ -0,0 +1,13 @@
import applyFnToKey from '../utility/applyFnToKey.js';
import { unset } from 'lodash';
export default function removeSchemaFields(schemas, prop){
schemas.forEach(schema => {
schema._schemaKeys.forEach(key => {
// Skip object keys
if (schema.getQuickTypeForKey(key) === 'object') return;
// Unset other computed only keys
applyFnToKey(prop, key, unset)
});
});
}