Merge pull request #170 from unrealkakeman89/Develop-VJ

Power point upgrades are finally done
This commit is contained in:
CK 2021-03-13 13:26:11 -05:00 committed by GitHub
commit c586ae8c80
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 543 additions and 475 deletions

View file

@ -6,9 +6,10 @@ export const migrateWorld = async function() {
ui.notifications.info(`Applying SW5e System Migration for version ${game.system.data.version}. Please be patient and do not close your game or shut down your server.`, {permanent: true}); ui.notifications.info(`Applying SW5e System 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 // Migrate World Actors
for ( let a of game.actors.entities ) { for await ( let a of game.actors.entities ) {
try { try {
const updateData = migrateActorData(a.data); console.log(`Checking Actor entity ${a.name} for migration needs`);
const updateData = await migrateActorData(a.data);
if ( !isObjectEmpty(updateData) ) { if ( !isObjectEmpty(updateData) ) {
console.log(`Migrating Actor entity ${a.name}`); console.log(`Migrating Actor entity ${a.name}`);
await a.update(updateData, {enforceTypes: false}); await a.update(updateData, {enforceTypes: false});
@ -36,7 +37,7 @@ export const migrateWorld = async function() {
// Migrate Actor Override Tokens // Migrate Actor Override Tokens
for ( let s of game.scenes.entities ) { for ( let s of game.scenes.entities ) {
try { try {
const updateData = migrateSceneData(s.data); const updateData = await migrateSceneData(s.data);
if ( !isObjectEmpty(updateData) ) { if ( !isObjectEmpty(updateData) ) {
console.log(`Migrating Scene entity ${s.name}`); console.log(`Migrating Scene entity ${s.name}`);
await s.update(updateData, {enforceTypes: false}); await s.update(updateData, {enforceTypes: false});
@ -79,18 +80,18 @@ export const migrateCompendium = async function(pack) {
const content = await pack.getContent(); const content = await pack.getContent();
// Iterate over compendium entries - applying fine-tuned migration functions // Iterate over compendium entries - applying fine-tuned migration functions
for ( let ent of content ) { for await ( let ent of content ) {
let updateData = {}; let updateData = {};
try { try {
switch (entity) { switch (entity) {
case "Actor": case "Actor":
updateData = migrateActorData(ent.data); updateData = await migrateActorData(ent.data);
break; break;
case "Item": case "Item":
updateData = migrateItemData(ent.data); updateData = migrateItemData(ent.data);
break; break;
case "Scene": case "Scene":
updateData = migrateSceneData(ent.data); updateData = await migrateSceneData(ent.data);
break; break;
} }
if ( isObjectEmpty(updateData) ) continue; if ( isObjectEmpty(updateData) ) continue;
@ -123,7 +124,7 @@ export const migrateCompendium = async function(pack) {
* @param {object} actor The actor data object to update * @param {object} actor The actor data object to update
* @return {Object} The updateData to apply * @return {Object} The updateData to apply
*/ */
export const migrateActorData = function(actor) { export const migrateActorData = async function(actor) {
const updateData = {}; const updateData = {};
// Actor Data Updates // Actor Data Updates
@ -133,10 +134,11 @@ export const migrateActorData = function(actor) {
// Migrate Owned Items // Migrate Owned Items
if ( !!actor.items ) { if ( !!actor.items ) {
let hasItemUpdates = false; let hasItemUpdates = false;
const items = actor.items.map(i => { const items = await actor.items.reduce(async (memo, i) => {
const results = await memo;
// Migrate the Owned Item // Migrate the Owned Item
let itemUpdate = migrateItemData(i); let itemUpdate = await migrateActorItemData(i, actor);
// Prepared, Equipped, and Proficient for NPC actors // Prepared, Equipped, and Proficient for NPC actors
if ( actor.type === "npc" ) { if ( actor.type === "npc" ) {
@ -148,9 +150,11 @@ export const migrateActorData = function(actor) {
// Update the Owned Item // Update the Owned Item
if ( !isObjectEmpty(itemUpdate) ) { if ( !isObjectEmpty(itemUpdate) ) {
hasItemUpdates = true; hasItemUpdates = true;
return mergeObject(i, itemUpdate, {enforceTypes: false, inplace: false}); console.log(`Migrating Actor ${actor.name}'s ${i.name}`);
} else return i; return [...results, mergeObject(i, itemUpdate, {enforceTypes: false, inplace: false})];
}); } else return [...results, i];
}, []);
if ( hasItemUpdates ) updateData.items = items; if ( hasItemUpdates ) updateData.items = items;
} }
@ -201,23 +205,38 @@ function cleanActorData(actorData) {
*/ */
export const migrateItemData = function(item) { export const migrateItemData = function(item) {
const updateData = {}; const updateData = {};
_migrateItemClassPowerCasting(item, updateData) _migrateItemClassPowerCasting(item, updateData);
_migrateItemAttunement(item, updateData); _migrateItemAttunement(item, updateData);
return updateData; return updateData;
}; };
/* -------------------------------------------- */ /* -------------------------------------------- */
/**
* Migrate a single owned actor Item entity to incorporate latest data model changes
* @param item
* @param actor
*/
export const migrateActorItemData = async function(item, actor) {
const updateData = {};
_migrateItemClassPowerCasting(item, updateData);
_migrateItemAttunement(item, updateData);
await _migrateItemPower(item, actor, updateData);
return updateData;
};
/* -------------------------------------------- */
/** /**
* Migrate a single Scene entity to incorporate changes to the data model of it's actor data overrides * Migrate a single Scene entity to incorporate changes to the data model of it's actor data overrides
* Return an Object of updateData to be applied * Return an Object of updateData to be applied
* @param {Object} scene The Scene data to Update * @param {Object} scene The Scene data to Update
* @return {Object} The updateData to apply * @return {Object} The updateData to apply
*/ */
export const migrateSceneData = function(scene) { export const migrateSceneData = async function(scene) {
const tokens = duplicate(scene.tokens); const tokens = duplicate(scene.tokens);
return { return {
tokens: tokens.map(t => { tokens: await Promise.all(tokens.map(async (t) => {
if (!t.actorId || t.actorLink || !t.actorData.data) { if (!t.actorId || t.actorLink || !t.actorData.data) {
t.actorData = {}; t.actorData = {};
return t; return t;
@ -227,11 +246,11 @@ export const migrateSceneData = function(scene) {
t.actorId = null; t.actorId = null;
t.actorData = {}; t.actorData = {};
} else if ( !t.actorLink ) { } else if ( !t.actorLink ) {
const updateData = migrateActorData(token.data.actorData); const updateData = await migrateActorData(token.data.actorData);
t.actorData = mergeObject(token.data.actorData, updateData); t.actorData = mergeObject(token.data.actorData, updateData);
} }
return t; return t;
}) }))
}; };
}; };
@ -250,10 +269,13 @@ function _updateNPCData(actor) {
let actorData = actor.data; let actorData = actor.data;
const updateData = {}; const updateData = {};
// check for flag.core // check for flag.core, if not there is no compendium monster so exit
const hasSource = actor?.flags?.core?.sourceId !== undefined; const hasSource = actor?.flags?.core?.sourceId !== undefined;
if (!hasSource) return actor; if (!hasSource) return actor;
// shortcut out if dataVersion flag is set to 1.2.4 // shortcut out if dataVersion flag is set to 1.2.4 or higher
const hasDataVersion = actor?.flags?.sw5e?.dataVersion !== undefined;
if (hasDataVersion && (actor.flags.sw5e.dataVersion === "1.2.4" || isNewerVersion("1.2.4", actor.flags.sw5e.dataVersion))) return actor;
// Check to see what the source of NPC is
const sourceId = actor.flags.core.sourceId; const sourceId = actor.flags.core.sourceId;
const coreSource = sourceId.substr(0,sourceId.length-17); const coreSource = sourceId.substr(0,sourceId.length-17);
const core_id = sourceId.substr(sourceId.length-16,16); const core_id = sourceId.substr(sourceId.length-16,16);
@ -284,11 +306,11 @@ function _updateNPCData(actor) {
} }
} }
// get actor to create new powers
const liveActor = game.actors.get(actor._id); const liveActor = game.actors.get(actor._id);
// create the powers on the actor
liveActor.createEmbeddedEntity("OwnedItem", newPowers); liveActor.createEmbeddedEntity("OwnedItem", newPowers);
// let updateActor = await actor.createOwnedItem(newPowers);
// set flag to check to see if migration has been done so we don't do it again. // set flag to check to see if migration has been done so we don't do it again.
liveActor.setFlag("sw5e", "dataVersion", "1.2.4"); liveActor.setFlag("sw5e", "dataVersion", "1.2.4");
}) })
@ -453,6 +475,52 @@ function _migrateItemClassPowerCasting(item, updateData) {
return updateData; return updateData;
} }
/* -------------------------------------------- */
/**
* Update an Power Item's data based on compendium
* @param {Object} item The data object for an item
* @param {Object} actor The data object for the actor owning the item
* @private
*/
async function _migrateItemPower(item, actor, updateData) {
// if item is not a power shortcut out
if (item.type !== "power") return updateData;
console.log(`Checking Actor ${actor.name}'s ${item.name} for migration needs`);
// check for flag.core, if not there is no compendium power so exit
const hasSource = item?.flags?.core?.sourceId !== undefined;
if (!hasSource) return updateData;
// shortcut out if dataVersion flag is set to 1.2.4 or higher
const hasDataVersion = item?.flags?.sw5e?.dataVersion !== undefined;
if (hasDataVersion && (item.flags.sw5e.dataVersion === "1.2.4" || isNewerVersion("1.2.4", item.flags.sw5e.dataVersion))) return updateData;
// Check to see what the source of Power is
const sourceId = item.flags.core.sourceId;
const coreSource = sourceId.substr(0, sourceId.length - 17);
const core_id = sourceId.substr(sourceId.length - 16, 16);
//if power type is not force or tech exit out
let powerType = "none";
if (coreSource === "Compendium.sw5e.forcepowers") powerType = "sw5e.forcepowers";
if (coreSource === "Compendium.sw5e.techpowers") powerType = "sw5e.techpowers";
if (powerType === "none") return updateData;
const corePower = duplicate(await game.packs.get(powerType).getEntity(core_id));
console.log(`Updating Actor ${actor.name}'s ${item.name} from compendium`);
const corePowerData = corePower.data;
// copy Core Power Data over original Power
updateData["data"] = corePowerData;
updateData["flags"] = {"sw5e": {"dataVersion": "1.2.4"}};
return updateData;
//game.packs.get(powerType).getEntity(core_id).then(corePower => {
//})
}
/* -------------------------------------------- */ /* -------------------------------------------- */
@ -467,7 +535,6 @@ function _migrateItemAttunement(item, updateData) {
return updateData; return updateData;
} }
/* -------------------------------------------- */ /* -------------------------------------------- */
/** /**

File diff suppressed because one or more lines are too long

16
sw5e.js
View file

@ -196,16 +196,18 @@ Hooks.once("ready", function() {
// Determine whether a system migration is required and feasible // Determine whether a system migration is required and feasible
if ( !game.user.isGM ) return; if ( !game.user.isGM ) return;
const currentVersion = game.settings.get("sw5e", "systemMigrationVersion"); const currentVersion = game.settings.get("sw5e", "systemMigrationVersion");
const NEEDS_MIGRATION_VERSION = "R1-A1"; const NEEDS_MIGRATION_VERSION = "1.2.4.R1-A5";
// Check for R1 SW5E versions
const SW5E_NEEDS_MIGRATION_VERSION = "R1-A5";
const COMPATIBLE_MIGRATION_VERSION = 0.80; const COMPATIBLE_MIGRATION_VERSION = 0.80;
const needsMigration = currentVersion && isNewerVersion(NEEDS_MIGRATION_VERSION, currentVersion); const needsMigration = currentVersion && (isNewerVersion(SW5E_NEEDS_MIGRATION_VERSION, currentVersion) || isNewerVersion(NEEDS_MIGRATION_VERSION, currentVersion));
//if ( !needsMigration ) return; if ( !needsMigration ) return;
// Perform the migration // Perform the migration
//if ( currentVersion && isNewerVersion(COMPATIBLE_MIGRATION_VERSION, currentVersion) ) { if ( currentVersion && isNewerVersion(COMPATIBLE_MIGRATION_VERSION, currentVersion) ) {
// const warning = `Your SW5e system data is from too old a Foundry version and cannot be reliably migrated to the latest version. The process will be attempted, but errors may occur.`; const warning = `Your SW5e system data is from too old a Foundry version and cannot be reliably migrated to the latest version. The process will be attempted, but errors may occur.`;
// ui.notifications.error(warning, {permanent: true}); ui.notifications.error(warning, {permanent: true});
//} }
migrations.migrateWorld(); migrations.migrateWorld();
}); });

View file

@ -2,7 +2,7 @@
"name": "sw5e", "name": "sw5e",
"title": "SW 5th Edition", "title": "SW 5th Edition",
"description": "A comprehensive game system for running games of SW 5th Edition in the Foundry VTT environment.", "description": "A comprehensive game system for running games of SW 5th Edition in the Foundry VTT environment.",
"version": "R1-A4", "version": "1.2.4.R1-A5",
"author": "Dev Team", "author": "Dev Team",
"scripts": [], "scripts": [],
"esmodules": ["sw5e.js"], "esmodules": ["sw5e.js"],