Moved creature variables to their own collection
Another big change to the engine, expect bugs
This commit is contained in:
@@ -2,7 +2,7 @@ import { EJSON } from 'meteor/ejson';
|
||||
import createGraph from 'ngraph.graph';
|
||||
|
||||
export default class CreatureComputation {
|
||||
constructor(properties, creature){
|
||||
constructor(properties, creature, variables){
|
||||
// Set up fields
|
||||
this.originalPropsById = {};
|
||||
this.propsById = {};
|
||||
@@ -12,6 +12,7 @@ export default class CreatureComputation {
|
||||
this.dependencyGraph = createGraph();
|
||||
this.errors = [];
|
||||
this.creature = creature;
|
||||
this.variables = variables;
|
||||
|
||||
// Store properties for easy access later
|
||||
properties.forEach(prop => {
|
||||
|
||||
@@ -4,6 +4,7 @@ import CreatureProperties,
|
||||
from '/imports/api/creature/creatureProperties/CreatureProperties.js';
|
||||
import { loadedCreatures } from '../loadCreatures.js';
|
||||
import Creatures from '/imports/api/creature/creatures/Creatures.js';
|
||||
import CreatureVariables from '/imports/api/creature/creatures/CreatureVariables.js';
|
||||
import computedOnlySchemas from '/imports/api/properties/computedOnlyPropertySchemasIndex.js';
|
||||
import computedSchemas from '/imports/api/properties/computedPropertySchemasIndex.js';
|
||||
import linkInventory from './buildComputation/linkInventory.js';
|
||||
@@ -33,8 +34,9 @@ import removeSchemaFields from './buildComputation/removeSchemaFields.js';
|
||||
|
||||
export default function buildCreatureComputation(creatureId){
|
||||
const creature = getCreature(creatureId);
|
||||
const variables = getVariables(creatureId);
|
||||
const properties = getProperties(creatureId);
|
||||
const computation = buildComputationFromProps(properties, creature);
|
||||
const computation = buildComputationFromProps(properties, creature, variables);
|
||||
return computation;
|
||||
}
|
||||
|
||||
@@ -45,7 +47,7 @@ function getProperties(creatureId) {
|
||||
const cloneProps = EJSON.clone(props);
|
||||
return cloneProps
|
||||
}
|
||||
console.time(`Cache miss fetching from db: ${creatureId}`)
|
||||
// console.time(`Cache miss on creature properties: ${creatureId}`)
|
||||
const props = CreatureProperties.find({
|
||||
'ancestors.id': creatureId,
|
||||
'removed': {$ne: true},
|
||||
@@ -53,29 +55,41 @@ function getProperties(creatureId) {
|
||||
sort: { order: 1 },
|
||||
fields: { icon: 0 },
|
||||
}).fetch();
|
||||
console.timeEnd(`Cache miss fetching from db: ${creatureId}`);
|
||||
// console.timeEnd(`Cache miss on creature properties: ${creatureId}`);
|
||||
return props;
|
||||
}
|
||||
|
||||
function getCreature(creatureId) {
|
||||
if (loadedCreatures.has(creatureId)) {
|
||||
const loadedCreature = loadedCreatures.get(creatureId);
|
||||
const creature = loadedCreature.creatures.get(creatureId);
|
||||
const creature = loadedCreature.creature;
|
||||
if (creature) return creature;
|
||||
}
|
||||
console.time(`Cache miss on Creature: ${creatureId}`);
|
||||
// console.time(`Cache miss on Creature: ${creatureId}`);
|
||||
const creature = Creatures.findOne(creatureId, {
|
||||
denormalizedStats: 1,
|
||||
variables: 1,
|
||||
dirty: 1,
|
||||
});
|
||||
console.timeEnd(`Cache miss on Creature: ${creatureId}`);
|
||||
// console.timeEnd(`Cache miss on Creature: ${creatureId}`);
|
||||
return creature;
|
||||
}
|
||||
|
||||
export function buildComputationFromProps(properties, creature){
|
||||
function getVariables(creatureId) {
|
||||
if (loadedCreatures.has(creatureId)) {
|
||||
const loadedCreature = loadedCreatures.get(creatureId);
|
||||
const variables = loadedCreature.variables;
|
||||
if (variables) return variables;
|
||||
}
|
||||
// console.time(`Cache miss on variables: ${creatureId}`);
|
||||
const variables = CreatureVariables.findOne({_creatureId: creatureId});
|
||||
// console.timeEnd(`Cache miss on variables: ${creatureId}`);
|
||||
return variables;
|
||||
}
|
||||
|
||||
const computation = new CreatureComputation(properties, creature);
|
||||
export function buildComputationFromProps(properties, creature, variables){
|
||||
|
||||
const computation = new CreatureComputation(properties, creature, variables);
|
||||
// Dependency graph where edge(a, b) means a depends on b
|
||||
// The graph includes all dependencies even of inactive properties
|
||||
// such that any properties changing without changing their dependencies
|
||||
|
||||
@@ -1,15 +1,14 @@
|
||||
import CreatureVariables from '/imports/api/creature/creatures/CreatureVariables.js';
|
||||
import Creatures from '/imports/api/creature/creatures/Creatures.js';
|
||||
import { EJSON } from 'meteor/ejson';
|
||||
import { omitBy } from 'lodash';
|
||||
|
||||
export default function writeScope(creatureId, computation) {
|
||||
const scope = computation.scope;
|
||||
const variables = computation.creature?.variables || {};
|
||||
let $set, $unset;
|
||||
const variables = computation.variables || {};
|
||||
delete variables._id;
|
||||
|
||||
if (computation.creature.dirty) {
|
||||
$unset = { dirty: 1 };
|
||||
}
|
||||
let $set, $unset;
|
||||
|
||||
for (const key in scope){
|
||||
// Remove large properties that aren't likely to be accessed
|
||||
@@ -26,12 +25,14 @@ export default function writeScope(creatureId, computation) {
|
||||
// Only update changed fields
|
||||
if (!EJSON.equals(variables[key], scope[key])) {
|
||||
if (!$set) $set = {};
|
||||
// Set the changed key in the creature variables
|
||||
/* Log detailed diffs
|
||||
const diff = omitBy(variables[key], (v, k) => EJSON.equals(scope[key][k], v));
|
||||
for (let subkey in diff) {
|
||||
console.log(`${key}.${subkey}: ${variables[key][subkey]} => ${scope[key][subkey]}`)
|
||||
}
|
||||
$set[`variables.${key}`] = scope[key];
|
||||
*/
|
||||
// Set the changed key in the creature variables
|
||||
$set[key] = scope[key];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,12 +40,17 @@ export default function writeScope(creatureId, computation) {
|
||||
for (const key in variables) {
|
||||
if (!scope[key]) {
|
||||
if (!$unset) $unset = {};
|
||||
$unset[`variables.${key}`] = 1;
|
||||
$unset[key] = 1;
|
||||
}
|
||||
}
|
||||
|
||||
if ($set || $unset) {
|
||||
const updates = Creatures.update(creatureId, { $set, $unset });
|
||||
console.log('wrote scope: ', updates);
|
||||
const update = {};
|
||||
if ($set) update.$set = $set;
|
||||
if ($unset) update.$unset = $unset;
|
||||
CreatureVariables.upsert({_creatureId: creatureId}, update);
|
||||
}
|
||||
if (computation.creature.dirty) {
|
||||
Creatures.update({_creatureId: creatureId}, {$unset: { dirty: 1 }});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user