diff --git a/app/imports/migrations/server/dbv1/cleanAt1.js b/app/imports/migrations/server/dbv1/cleanAt1.js new file mode 100644 index 00000000..d58e3c74 --- /dev/null +++ b/app/imports/migrations/server/dbv1/cleanAt1.js @@ -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; + }); +} diff --git a/app/imports/migrations/server/dbv1/dbv1.js b/app/imports/migrations/server/dbv1/dbv1.js index d299fc58..b265cd9d 100644 --- a/app/imports/migrations/server/dbv1/dbv1.js +++ b/app/imports/migrations/server/dbv1/dbv1.js @@ -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){ diff --git a/app/imports/migrations/server/migrateArchive.js b/app/imports/migrations/server/migrateArchive.js index 52403790..0fb557ed 100644 --- a/app/imports/migrations/server/migrateArchive.js +++ b/app/imports/migrations/server/migrateArchive.js @@ -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; - }); -}