Replaced most properties in creature variables with links

instead of storing the entire property twice
This commit is contained in:
ThaumRystra
2023-11-09 23:05:05 +02:00
parent 84edd74ff3
commit 7eada9effe
8 changed files with 117 additions and 29 deletions

View File

@@ -43,7 +43,7 @@ export function getSingleProperty(creatureId, propertyId) {
const prop = CreatureProperties.findOne({
_id: propertyId,
'ancestors.id': creatureId,
'removed': {$ne: true},
'removed': { $ne: true },
}, {
sort: { order: 1 },
});
@@ -61,7 +61,7 @@ export function getProperties(creatureId) {
// console.time(`Cache miss on creature properties: ${creatureId}`)
const props = CreatureProperties.find({
'ancestors.id': creatureId,
'removed': {$ne: true},
'removed': { $ne: true },
}, {
sort: { order: 1 },
}).fetch();
@@ -73,7 +73,7 @@ export function getPropertiesOfType(creatureId, propType) {
if (loadedCreatures.has(creatureId)) {
const creature = loadedCreatures.get(creatureId);
const props = []
for (const prop of creature.properties.values()){
for (const prop of creature.properties.values()) {
if (prop.type === propType) {
props.push(prop);
}
@@ -97,7 +97,7 @@ export function getCreature(creatureId) {
if (loadedCreatures.has(creatureId)) {
const loadedCreature = loadedCreatures.get(creatureId);
const creature = loadedCreature.creature;
if (creature) {
if (creature) {
const cloneCreature = EJSON.clone(creature);
return cloneCreature;
}
@@ -113,16 +113,24 @@ export function getVariables(creatureId) {
const loadedCreature = loadedCreatures.get(creatureId);
const variables = loadedCreature.variables;
if (variables) {
const cloneVarables = EJSON.clone(variables);
return cloneVarables;
const cloneVariables = EJSON.clone(variables);
return cloneVariables;
}
}
// console.time(`Cache miss on variables: ${creatureId}`);
const variables = CreatureVariables.findOne({_creatureId: creatureId});
const variables = CreatureVariables.findOne({ _creatureId: creatureId });
// console.timeEnd(`Cache miss on variables: ${creatureId}`);
return variables;
}
export function replaceLinkedVariablesWithProps(variables) {
for (const key in variables) {
const propId = variables[key]?._propId;
if (!propId) continue;
variables[key] = getSingleProperty(variables._creatureId, propId);
}
}
export function getProperyAncestors(creatureId, propertyId) {
const prop = getSingleProperty(creatureId, propertyId);
if (!prop) return [];
@@ -148,7 +156,7 @@ export function getProperyAncestors(creatureId, propertyId) {
// Fetch from database
return CreatureProperties.find({
_id: { $in: ancestorIds },
removed: {$ne: true},
removed: { $ne: true },
}, {
sort: { order: 1 },
}).fetch();
@@ -164,7 +172,7 @@ export function getPropertyDecendants(creatureId, propertyId) {
if (loadedCreatures.has(creatureId)) {
const creature = loadedCreatures.get(creatureId);
const props = [];
for(const prop of creature.properties.values()){
for (const prop of creature.properties.values()) {
if (prop.ancestors[expectedAncestorPostition]?.id === propertyId) {
props.push(prop);
}
@@ -216,7 +224,7 @@ class LoadedCreature {
compute();
},
});
// Observe the creature itself
self.creatureObserver = Creatures.find({
_id: creatureId,
@@ -239,7 +247,7 @@ class LoadedCreature {
self.variablesObserver = CreatureVariables.find({
_creatureId: creatureId,
}, {
fields: { _creatureId: 0},
fields: { _creatureId: 0 },
}).observeChanges({
added(id, fields) {
fields._id = id;