Improved handling of poorly migrated archive creatures

This commit is contained in:
Stefan Zermatten
2022-03-08 14:12:11 +02:00
parent 12fc9b1be3
commit fada07e048
3 changed files with 45 additions and 19 deletions

View File

@@ -0,0 +1,39 @@
import CreatureProperties from '/imports/api/creature/creatureProperties/CreatureProperties.js';
import { get, set } from 'lodash';
import applyFnToKey from '/imports/api/engine/computation/utility/applyFnToKey.js';
import { calculationUp } from '/imports/migrations/server/dbv1/dbv1.js';
export default function cleanAt1(archive){
archive.properties = archive.properties.map(prop => {
let cleanProp = prop;
try {
if (prop.type === 'attack') prop.type = 'action';
// Get the schema
const schema = CreatureProperties.simpleSchema(prop);
// Clean all the text fields with inline calcs
schema.inlineCalculationFields().forEach(key => {
applyFnToKey(prop, key, (prop, key) => {
let field = get(prop, key);
if (typeof field === 'string' || typeof field === 'number'){
field = calculationUp(field);
set(prop, key, {text: `${field}`});
}
});
});
schema.computedFields().forEach(key => {
applyFnToKey(prop, key, (prop, key) => {
let field = get(prop, key) || get(prop, key + 'Calculation');
if (typeof field === 'string' || typeof field === 'number'){
field = calculationUp(field);
set(prop, key, {calculation: `${field}`});
}
});
});
cleanProp = schema.clean(prop);
schema.validate(cleanProp);
} catch (e){
console.warn({propId: prop._id, error: e.message || e.reason || e.toString()});
}
return cleanProp;
});
}

View File

@@ -215,16 +215,17 @@ function getInlineComputationTransforms(key){
];
}
function calculationUp(val){
export function calculationUp(val){
if (typeof val !== 'string') return val;
if (!val.replace) console.log({val, replace: val.replace});
return val.replace(/#(\w+).(\w+)Result/g, '#$1.$2')
.replace('.value', '.total')
.replace('.currentValue', '.value');
.replace(/\.value/g, '.total')
.replace(/\.currentValue/g, '.value');
}
function calculationDown(val){
if (typeof val !== 'string') return val;
return val.replace('.value', '.currentValue').replace('.total', '.value');
return val.replace(/\.value/g, '.currentValue').replace(/\.total/g, '.value');
}
function nanToNull(val){

View File

@@ -1,4 +1,4 @@
import CreatureProperties from '/imports/api/creature/creatureProperties/CreatureProperties.js';
import cleanAt1 from '/imports/migrations/server/dbv1/cleanAt1.js';
/* eslint no-fallthrough: "off" -- Using switch fallthrough to run all
migration steps after the current version of the file. */
@@ -17,17 +17,3 @@ function migrateLegacyArchive(archive){
// TODO:
throw 'Not implemented';
}
function cleanAt1(archive){
archive.properties.map(prop => {
const schema = CreatureProperties.simpleSchema(prop);
const cleanProp = schema.clean(prop);
try {
schema.validate(cleanProp);
} catch (e){
console.warn('Prop did not pass schema validation');
console.warn(e);
}
return cleanProp;
});
}