diff --git a/module/systemRegistration/migration-handlers/2_5_2.mjs b/module/systemRegistration/migration-handlers/2_5_2.mjs new file mode 100644 index 00000000..c2f9261a --- /dev/null +++ b/module/systemRegistration/migration-handlers/2_5_2.mjs @@ -0,0 +1,35 @@ +export class Migration_2_5_2 { + version = '2.5.2'; + + async updateEffect(effectSource, item) { + let shouldUpdate = false; + const newChanges = []; + const srdItem = item?._stats.compendiumSource ? + await foundry.utils.fromUuid(item?._stats.compendiumSource) : + null; + for (let i = 0; i < effectSource.system.changes.length; i++) { + const change = effectSource.system.changes[i]; + const srdEffect = srdItem?.effects.find(x => x.name === effectSource.name); + if (change.type === 'custom') { + const srdChange = srdEffect ? srdEffect.system.changes[i] : null; + if ( + change.key === srdChange.key && + change.value === srdChange.value && + change.type !== srdChange.type + ) { + shouldUpdate = true; + newChanges.push(srdChange); + } + } else { + newChanges.push(change); + } + } + + if (shouldUpdate) { + return { + _id: effectSource._id, + system: { changes: newChanges } + } + } + } +} \ No newline at end of file diff --git a/module/systemRegistration/migrations.mjs b/module/systemRegistration/migrations.mjs index 7df769d0..dbf4f395 100644 --- a/module/systemRegistration/migrations.mjs +++ b/module/systemRegistration/migrations.mjs @@ -1,4 +1,5 @@ import { defaultRestOptions } from '../config/generalConfig.mjs'; +import { Migration_2_5_2 } from './migration-handlers/2_5_2.mjs'; export async function runMigrations() { let lastMigrationVersion = game.settings.get(CONFIG.DH.id, CONFIG.DH.SETTINGS.gameSettings.LastMigrationVersion); @@ -328,41 +329,19 @@ export async function runMigrations() { progress.advance(); } + // todo: introduce a runner class that can handle passing on to the different update functions + const handler = new Migration_2_5_2(); + const batch = []; for (const actor of game.actors) { for (const item of actor.items) { for (const effect of item.effects) { - let shouldUpdate = false; - const newChanges = []; - const srdItem = item._stats.compendiumSource ? - await foundry.utils.fromUuid(item._stats.compendiumSource) : - null; - for (let i = 0; i < effect.system.changes.length; i++) { - const change = effect.system.changes[i]; - const srdEffect = srdItem?.effects.find(x => x.name === effect.name); - if (change.type === 'custom') { - const srdChange = srdEffect ? srdEffect.system.changes[i] : null; - if ( - change.key === srdChange.key && - change.value === srdChange.value && - change.type !== srdChange.type - ) { - shouldUpdate = true; - newChanges.push(srdChange); - } - } else { - newChanges.push(change); - } - } - - if (shouldUpdate) { + const changes = handler.updateEffect(effect.toObject(), effect.parent); + if (changes) { batch.push({ action: 'update', documentName: 'ActiveEffect', - updates: [{ - _id: effect.id, - system: { changes: newChanges } - }], + updates: [changes], parent: item }); }