Progress on dependency updates

This commit is contained in:
Stefan Zermatten
2022-05-09 16:32:15 +02:00
parent caf50d1578
commit 23fa6fe634
6 changed files with 159 additions and 21 deletions

View File

@@ -84,12 +84,12 @@ const DenormalisedOnlyCreaturePropertySchema = new SimpleSchema({
}, },
// Dependency tree, the ID of the lowest ordered doc connected to this doc // Dependency tree, the ID of the lowest ordered doc connected to this doc
// via dependencies // via dependencies
/*depGroupId: { depGroupId: {
type: String, type: String,
regEx: SimpleSchema.RegEx.Id, regEx: SimpleSchema.RegEx.Id,
index: 1, index: 1,
removeBeforeCompute: true, removeBeforeCompute: true,
}*/ },
}); });
CreaturePropertySchema.extend(DenormalisedOnlyCreaturePropertySchema); CreaturePropertySchema.extend(DenormalisedOnlyCreaturePropertySchema);

View File

@@ -4,7 +4,7 @@ import SimpleSchema from 'simpl-schema';
import CreatureProperties from '/imports/api/creature/creatureProperties/CreatureProperties.js'; import CreatureProperties from '/imports/api/creature/creatureProperties/CreatureProperties.js';
import getRootCreatureAncestor from '/imports/api/creature/creatureProperties/getRootCreatureAncestor.js'; import getRootCreatureAncestor from '/imports/api/creature/creatureProperties/getRootCreatureAncestor.js';
import { assertEditPermission } from '/imports/api/sharing/sharingPermissions.js'; import { assertEditPermission } from '/imports/api/sharing/sharingPermissions.js';
import { computeCreatureDependencyGroup } from '/imports/api/engine/computeCreature.js'; import computeCreature, { computeCreatureDependencyGroup } from '/imports/api/engine/computeCreature.js';
const damageProperty = new ValidatedMethod({ const damageProperty = new ValidatedMethod({
name: 'creatureProperties.damage', name: 'creatureProperties.damage',
@@ -37,9 +37,13 @@ const damageProperty = new ValidatedMethod({
`Property of type "${property.type}" can't be damaged` `Property of type "${property.type}" can't be damaged`
); );
} }
let result = damagePropertyWork({property, operation, value}); let result = damagePropertyWork({ property, operation, value });
// Dependencies can't be changed through damage, only recompute deps if (property.depGroupId) {
computeCreatureDependencyGroup(property); // Dependencies can't be changed through damage, only recompute deps
computeCreatureDependencyGroup([property.depGroupId], rootCreature._id);
} else {
computeCreature(rootCreature._id);
}
return result; return result;
}, },
}); });

View File

@@ -15,7 +15,7 @@ import linkTypeDependencies from './buildComputation/linkTypeDependencies.js';
import computeSlotQuantityFilled from './buildComputation/computeSlotQuantityFilled.js'; import computeSlotQuantityFilled from './buildComputation/computeSlotQuantityFilled.js';
import CreatureComputation from './CreatureComputation.js'; import CreatureComputation from './CreatureComputation.js';
import removeSchemaFields from './buildComputation/removeSchemaFields.js'; import removeSchemaFields from './buildComputation/removeSchemaFields.js';
// import assignDependencyGroups from '/imports/api/engine/computation/utility/assignDependencyGroups.js'; import assignDependencyGroups from '/imports/api/engine/computation/utility/assignDependencyGroups.js';
/** /**
* Store index of properties * Store index of properties
@@ -56,6 +56,10 @@ function getProperties(creatureId) {
} }
function getGroupProperties(depGroupIds) { function getGroupProperties(depGroupIds) {
console.log({ depGroupIds });
if (!depGroupIds || depGroupIds.includes(undefined)) {
throw `Expected array full of ids, got ${depGroupIds}`
}
return CreatureProperties.find({ return CreatureProperties.find({
depGroupId: { $in: depGroupIds }, depGroupId: { $in: depGroupIds },
'removed': { $ne: true }, 'removed': { $ne: true },
@@ -135,7 +139,7 @@ export function buildComputationFromProps(properties, creature){
}); });
// Store the connected groups of the dependency graph // Store the connected groups of the dependency graph
// assignDependencyGroups(dependencyGraph); assignDependencyGroups(dependencyGraph);
return computation; return computation;
} }

View File

@@ -1,4 +1,10 @@
export default function assignDependencyGroups(graph) { import { union } from "lodash";
export function assignDependencyGroups(graph) {
console.log('assigning dep group ids');
graph.forEachLink(function (link) {
console.dir(link);
});
// Iterate through all the nodes // Iterate through all the nodes
graph.forEachNode(node => { graph.forEachNode(node => {
if (node._depGroupVisited) { if (node._depGroupVisited) {
@@ -13,22 +19,20 @@ export default function assignDependencyGroups(graph) {
while (stack.length) { while (stack.length) {
top = stack.pop(); top = stack.pop();
if (top._depGroupVisited) continue; if (top._depGroupVisited) continue;
if ( if (top.data?._id && (
(lowestOrderId === undefined && top.data?._id) || lowestOrderId === undefined ||
(top.data?._id && top.data?.order < lowestOrder) top.data?.order < lowestOrder
) { )) {
lowestOrderId = top.data?._id; lowestOrderId = top.data._id;
lowestOrder = top.data?.order; lowestOrder = top.data.order;
}
if (top.data?._id) {
group.push(top)
} }
group.push(top)
top._depGroupVisited = true; top._depGroupVisited = true;
graph.forEachLinkedNode(top.id, linkedNode => stack.push(linkedNode)); graph.forEachLinkedNode(top.id, linkedNode => stack.push(linkedNode));
} }
// Assign group id // Assign group id
group.forEach(node => { group.forEach(node => {
if (!lowestOrderId) return; if (!node.data?._id) return;
if (group.length > 1) { if (group.length > 1) {
node.data.depGroupId = lowestOrderId; node.data.depGroupId = lowestOrderId;
} else { } else {
@@ -36,4 +40,34 @@ export default function assignDependencyGroups(graph) {
} }
}); });
}); });
} }
export default function assignDependencyGroups2(graph) {
const groups = new Set();
graph.forEachLink(function (link) {
const from = graph.getNode(link.fromId);
const to = graph.getNode(link.toId);
let depGroup;
if (from._depGroup) {
depGroup = from._depGroup;
groups.delete(to._depGroup);
} else if (to._depGroup) {
depGroup = to._depGroup;
} else {
depGroup = {};
groups.add(depGroup);
}
depGroup.nodes = union(from._depGroup?.nodes, to._depGroup?.nodes, [from, to])
from._depGroup = depGroup;
to._depGroup = depGroup;
});
groups.forEach(g => {
if (!g.nodes.length) return;
const rootId = g.nodes[0].id;
g.nodes.forEach(n => {
if (!n.data?._id) return;
n.data.depGroupId = rootId;
});
});
}

View File

@@ -36,7 +36,7 @@ export default function writeAlteredProperties(computation){
function addChangedKeysToOp(op, keys, original, changed) { function addChangedKeysToOp(op, keys, original, changed) {
// Loop through all keys that can be changed by computation // Loop through all keys that can be changed by computation
// and compile an operation that sets all those keys // and compile an operation that sets all those keys
for (let key of keys){ for (let key of keys) {
if (!EJSON.equals(original[key], changed[key])){ if (!EJSON.equals(original[key], changed[key])){
if (!op) op = newOperation(original._id, changed.type); if (!op) op = newOperation(original._id, changed.type);
let value = changed[key]; let value = changed[key];

View File

@@ -0,0 +1,96 @@
import SimpleSchema from 'simpl-schema';
import STORAGE_LIMITS from '/imports/constants/STORAGE_LIMITS.js';
import VARIABLE_NAME_REGEX from '/imports/constants/VARIABLE_NAME_REGEX.js';
import createPropertySchema from '/imports/api/properties/subSchemas/createPropertySchema.js';
/*
* PointBuys are reason-value attached to skills and abilities
* that modify their final value or presentation in some way
*/
let PointBuySchema = createPropertySchema({
name: {
type: String,
optional: true,
max: STORAGE_LIMITS.name,
},
variableName: {
type: String,
optional: true,
regEx: VARIABLE_NAME_REGEX,
min: 2,
max: STORAGE_LIMITS.variableName,
},
ignored: {
type: Boolean,
optional: true,
},
'values': {
type: Array,
defaultValue: [],
},
'values.$': {
type: Object,
},
'values.$.name': {
type: String,
optional: true,
max: STORAGE_LIMITS.name,
},
'values.$.variableName': {
type: String,
optional: true,
regEx: VARIABLE_NAME_REGEX,
min: 2,
max: STORAGE_LIMITS.variableName,
},
'values.$.value': {
type: Number,
optional: true,
},
min: {
type: 'fieldToCompute',
optional: true,
},
max: {
type: 'fieldToCompute',
optional: true,
},
total: {
type: 'fieldToCompute',
optional: true,
},
cost: {
type: 'fieldToCompute',
optional: true,
},
});
const ComputedOnlyPointBuySchema = createPropertySchema({
min: {
type: 'computedOnlyField',
optional: true,
},
max: {
type: 'computedOnlyField',
optional: true,
},
total: {
type: 'computedOnlyField',
optional: true,
},
cost: {
type: 'computedOnlyField',
optional: true,
},
spent: {
type: Number,
optional: true,
removeBeforeCompute: true,
},
});
const ComputedPointBuySchema = new SimpleSchema()
.extend(ComputedOnlyPointBuySchema)
.extend(PointBuySchema);
export { PointBuySchema, ComputedPointBuySchema, ComputedOnlyPointBuySchema };