2020-08-05 11:00:57 +00:00
/ * *
* Perform a system migration for the entire World , applying migrations for Actors , Items , and Compendium packs
* @ return { Promise } A Promise which resolves once the migration is completed
* /
export const migrateWorld = async function ( ) {
ui . notifications . info ( ` Applying BITD Actors migration for version ${ game . system . data . version } . Please be patient and do not close your game or shut down your server. ` , { permanent : true } ) ;
// Migrate World Actors
for ( let a of game . actors . entities ) {
if ( a . data . type === 'character' ) {
try {
2020-08-18 14:22:07 +00:00
const updateData = _migrateActor ( a . data ) ;
2020-08-05 11:00:57 +00:00
if ( ! isObjectEmpty ( updateData ) ) {
console . log ( ` Migrating Actor entity ${ a . name } ` ) ;
await a . update ( updateData , { enforceTypes : false } ) ;
}
} catch ( err ) {
console . error ( err ) ;
}
}
}
// Set the migration as complete
game . settings . set ( "bitd" , "systemMigrationVersion" , game . system . data . version ) ;
ui . notifications . info ( ` BITD System Migration to version ${ game . system . data . version } completed! ` , { permanent : true } ) ;
} ;
/* -------------------------------------------- */
/* -------------------------------------------- */
/* Entity Type Migration Helpers */
/* -------------------------------------------- */
/ * *
2020-08-18 14:22:07 +00:00
* Migrate the actor attributes
2020-08-05 11:00:57 +00:00
* @ param { Actor } actor The actor to Update
* @ return { Object } The updateData to apply
* /
2020-08-18 14:22:07 +00:00
function _migrateActor ( actor ) {
2020-08-05 11:00:57 +00:00
let updateData = { }
2020-08-18 14:22:07 +00:00
// Migrate Skills
const attributes = game . system . model . Actor . character . attributes ;
2020-08-05 11:00:57 +00:00
for ( let attribute _name of Object . keys ( actor . data . attributes || { } ) ) {
// Insert attribute label
if ( typeof actor . data . attributes [ attribute _name ] . label === 'undefined' ) {
updateData [ ` data.attributes. ${ attribute _name } .label ` ] = attributes [ attribute _name ] . label ;
}
for ( let skill _name of Object . keys ( actor . data . attributes [ attribute _name ] [ 'skills' ] ) ) {
// Insert skill label
// Copy Skill value
if ( typeof actor . data . attributes [ attribute _name ] . skills [ skill _name ] . label === 'undefined' ) {
// Create Label.
updateData [ ` data.attributes. ${ attribute _name } .skills. ${ skill _name } .label ` ] = attributes [ attribute _name ] . skills [ skill _name ] . label ;
// Migrate from skillname = [0]
let skill _tmp = actor . data . attributes [ attribute _name ] . skills [ skill _name ] ;
if ( Array . isArray ( skill _tmp ) ) {
updateData [ ` data.attributes. ${ attribute _name } .skills. ${ skill _name } .value ` ] = [ skill _tmp [ 0 ] ] ;
}
}
}
}
2020-08-18 14:22:07 +00:00
// Migrate Stress to Array
if ( typeof actor . data . stress [ 0 ] !== 'undefined' ) {
updateData [ ` data.stress.value ` ] = actor . data . stress ;
updateData [ ` data.stress.max ` ] = 9 ;
updateData [ ` data.stress.max_default ` ] = 9 ;
updateData [ ` data.stress.name_default ` ] = "BITD.Stress" ;
updateData [ ` data.stress.name ` ] = "BITD.Stress" ;
}
// Migrate Trauma to Array
if ( typeof actor . data . trauma === 'undefined' ) {
updateData [ ` data.trauma.list ` ] = actor . data . traumas ;
2020-08-21 15:03:37 +00:00
updateData [ ` data.trauma.value ` ] = [ actor . data . traumas . length ] ;
2020-08-18 14:22:07 +00:00
updateData [ ` data.trauma.max ` ] = 4 ;
updateData [ ` data.trauma.max_default ` ] = 4 ;
updateData [ ` data.trauma.name_default ` ] = "BITD.Trauma" ;
updateData [ ` data.trauma.name ` ] = "BITD.Trauma" ;
}
2020-08-05 11:00:57 +00:00
return updateData ;
// for ( let k of Object.keys(actor.data.attributes || {}) ) {
// if ( k in b ) updateData[`data.bonuses.${k}`] = b[k];
// else updateData[`data.bonuses.-=${k}`] = null;
// }
}
/* -------------------------------------------- */