Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V10 world migration #1089

Merged
merged 10 commits into from
Sep 24, 2022
2 changes: 1 addition & 1 deletion modules/actors/actor-sheet-ffg.js
Original file line number Diff line number Diff line change
Expand Up @@ -1170,7 +1170,7 @@ export class ActorSheetFFG extends ActorSheet {
}
});
}
data.system.talentList = mergeObject(data.system.talentList ? data.system.talentList : [], globalTalentList);
data.data.talentList = mergeObject(data.data.talentList ? data.data.talentList : [], globalTalentList);
}
}

Expand Down
2 changes: 1 addition & 1 deletion modules/dice/roll-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export default class RollBuilderFFG extends FormApplication {
if (game.user.isGM) {
game.users.contents.forEach((user) => {
if (user.visible && user.id !== game.user.id) {
users.push({ name: user.data.name, id: user.id });
users.push({ name: user.name, id: user.id });
}
});
}
Expand Down
4 changes: 2 additions & 2 deletions modules/dice/roll.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,8 @@ export class RollFFG extends Roll {
if (this?.data) {
if (this.data.flags?.starwarsffg?.ffgUuid) {
const item = await fromUuid(this.data.flags.starwarsffg.ffgUuid);
if (item?.data) {
this.data = item.data;
if (item) {
this.data = item;
}
}
this.data.additionalFlavorText = this.flavorText;
Expand Down
2 changes: 1 addition & 1 deletion modules/helpers/actor-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import ModifierHelpers from "./modifiers.js";
export default class ActorHelpers {
static updateActor(event, formData) {
formData = expandObject(formData);
const ownedItems = this.actor.items.map((item) => item.system);
const ownedItems = this.actor.items;

if (this.object.type !== "homestead") {
if (this.object.type !== "vehicle") {
Expand Down
10 changes: 5 additions & 5 deletions modules/helpers/flag-migration-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export default class FlagMigrationHelpers {
// and copy old flags into new scope
static rescopeItemFlags() {
for (let doc of game.items) {
for (let flag in doc.data.flags) {
for (let flag in doc.system.flags) {
this.rescopeFlag(doc, flag);
}
}
Expand All @@ -30,7 +30,7 @@ export default class FlagMigrationHelpers {
// Actors
static rescopeActorFlags() {
for (let doc of game.actors) {
for (let flag in doc.data.flags) {
for (let flag in doc.system.flags) {
this.rescopeFlag(doc, flag);
}
}
Expand Down Expand Up @@ -67,7 +67,7 @@ export default class FlagMigrationHelpers {
switch (documentName) {
case "Actor":
case "Item":
for (let flag in doc.data.flags) {
for (let flag in doc.system.flags) {
this.rescopeFlag(doc, flag);
}
}
Expand All @@ -94,9 +94,9 @@ export default class FlagMigrationHelpers {
static rescopeFlag(doc, flag) {
if (this.oldFlagScopes.includes(flag)) {
try {
doc.setFlag("starwarsffg", flag, doc.data.flags[flag]);
doc.setFlag("starwarsffg", flag, doc.system.flags[flag]);
console.debug(
`Copied flag into starwarsffg scope: ${doc.name}.data.flags.${flag}`
`Copied flag into starwarsffg scope: ${doc.name}.system.flags.${flag}`
);
} catch (err) {
console.log(`Flag migration error at document: ${doc.name}`);
Expand Down
2 changes: 1 addition & 1 deletion modules/items/itembase-ffg.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default class ItemBaseFFG extends Item {

// we're working on an embedded item
await this.sheet.render(true);
const appId = this.data?.flags?.starwarsffg?.ffgParentApp;
const appId = this.system?.flags?.starwarsffg?.ffgParentApp;
if (appId) {
const newData = ui.windows[appId].object;
newData[this.flags.starwarsffg.ffgTempItemType][this.flags.starwarsffg.ffgTempItemIndex] = mergeObject(newData[this.flags.starwarsffg.ffgTempItemType][this.flags.starwarsffg.ffgTempItemIndex], this);
Expand Down
2 changes: 1 addition & 1 deletion modules/settings/settings-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ export default class SettingsHelpers {
const playlists = {};
playlists["None"] = "";
game.playlists.contents.forEach((playlist, index) => {
playlists[playlist.id] = `${index}-${playlist.data.name}`;
playlists[playlist.id] = `${index}-${playlist.name}`;
});

// Playlist users can user for audio
Expand Down
102 changes: 90 additions & 12 deletions modules/swffg-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ import RollBuilderFFG from "./dice/roll-builder.js";
/* Foundry VTT Initialization */
/* -------------------------------------------- */

async function parseSkillList() {
try {
return JSON.parse(await game.settings.get("starwarsffg", "arraySkillList"));
} catch (e) {
CONFIG.logger.log("Could not parse custom skill list, returning raw setting");
return await game.settings.get("starwarsffg", "arraySkillList");
}
}

Hooks.once("init", async function () {
console.log(`Initializing SWFFG System`);
// Place our classes in their own namespace for later reference.
Expand Down Expand Up @@ -99,7 +108,7 @@ Hooks.once("init", async function () {

const pct = Math.clamped(val, 0, data.max) / data.max;
let h = Math.max(canvas.dimensions.size / 12, 8);
if (this.data.height >= 2) h *= 1.6; // Enlarge the bar for large tokens
if (this.height >= 2) h *= 1.6; // Enlarge the bar for large tokens
// Draw the bar
let color = number === 0 ? [1 - pct / 2, pct, 0] : [0.5 * pct, 0.7 * pct, 0.5 + pct / 2];
bar
Expand Down Expand Up @@ -202,7 +211,7 @@ Hooks.once("init", async function () {
type: Object,
});

let skillList = game.settings.get("starwarsffg", "arraySkillList");
let skillList = await parseSkillList();
try {
CONFIG.FFG.alternateskilllists = skillList;

Expand Down Expand Up @@ -415,7 +424,7 @@ Hooks.on("renderSidebarTab", (app, html, data) => {
const dicePool = new DicePoolFFG();

let user = {
data: game.user.data,
data: game.user.system,
};

await DiceHelpers.displayRollDialog(user, dicePool, game.i18n.localize("SWFFG.RollingDefaultTitle"), "");
Expand Down Expand Up @@ -491,6 +500,15 @@ Hooks.on("renderChatMessage", (app, html, messageData) => {
});
});

// Hook journal rendering to convert special text into images
Hooks.on("renderJournalPageSheet", (...args) => {
if (args[1].length > 1) {
// replace FFG characters
args[1][2].outerHTML = PopoutEditor.renderDiceImages(args[1][2].outerHTML, {});
}
return args;
});

// Handle migration duties
Hooks.once("ready", async () => {
SettingsHelpers.readyLevelSetting();
Expand All @@ -500,7 +518,7 @@ Hooks.once("ready", async () => {
const version = game.system.version;
const isAlpha = game.system.version.includes("alpha");

if ((isAlpha || currentVersion === "null" || parseFloat(currentVersion) < parseFloat(game.system.version)) && game.user.isGM) {
if ((isAlpha || currentVersion === "null" || currentVersion === '' || parseFloat(currentVersion) < parseFloat(game.system.version)) && game.user.isGM) {
CONFIG.logger.log(`Migrating to from ${currentVersion} to ${game.system.version}`);

// Calculating wound and strain .value from .real_value is no longer necessary due to the Token._drawBar() override in swffg-main.js
Expand Down Expand Up @@ -551,7 +569,7 @@ Hooks.once("ready", async () => {
}
});

if (currentVersion === "null" || parseFloat(currentVersion) < 1.1) {
if (isAlpha || currentVersion === "null" || currentVersion === '' || parseFloat(currentVersion) < 1.1) {
// Migrate alternate skill lists from file if found
try {
let skillList = [];
Expand All @@ -566,7 +584,7 @@ Hooks.once("ready", async () => {
skillList = fileData;
}
} else {
skillList = JSON.parse(game.settings.get("starwarsffg", "arraySkillList"));
skillList = await parseSkillList();
}

CONFIG.FFG.alternateskilllists = skillList;
Expand Down Expand Up @@ -600,21 +618,81 @@ Hooks.once("ready", async () => {
CONFIG.logger.error(err);
}
}
// migrate embedded items
if (isAlpha || currentVersion === "null" || currentVersion === '') {
ui.notifications.info(`Migrating Star Wars FFG System Deep Embedded Items`)
CONFIG.logger.debug('Migrating Star Wars FFG System Deep Embedded Items')

// start with items on actors
game.actors.forEach((actor) => {
actor.items.forEach((item) => {
if (["weapon", "armour"].includes(item.type)) {
// iterate over attachments and modifiers on the item
item.system.itemmodifier.forEach((modifier) => {
if (modifier?.hasOwnProperty('data')) {
modifier.system = modifier.data;
delete modifier.data;
}
});

item.system.itemattachment.forEach((attachment) => {
if (attachment.hasOwnProperty('data')) {
attachment.system = attachment.data;
delete attachment.data;
}
});
// copy the item, so we can delete the ID field (we can't update if we include an ID)
let item_migrated = JSON.parse(JSON.stringify(item));
delete item_migrated._id;
// persist the changes to the DB
item.update(item_migrated);
}
});
});
// move on to items in the world
game.items.forEach((item) => {
if (["weapon", "armour"].includes(item.type)) {
// iterate over attachments and modifiers on the item
item.system.itemmodifier.forEach((modifier) => {
if (modifier?.hasOwnProperty('data')) {
modifier.system = modifier.data;
delete modifier.data;
}
});

item.system.itemattachment.forEach((attachment) => {
if (attachment.hasOwnProperty('data')) {
attachment.system = attachment.data;
delete attachment.data;
}
});
// copy the item, so we can delete the ID field (we can't update if we include an ID)
let item_migrated = JSON.parse(JSON.stringify(item));
delete item_migrated._id;
// persist the changes to the DB
item.update(item_migrated);
}
});
CONFIG.logger.debug('Migration of Star Wars FFG System Deep Embedded Items completed!')
ui.notifications.info(`Migration of Star Wars FFG System Deep Embedded Items completed!`)
}

if (currentVersion === "null" || parseFloat(currentVersion) < 1.61) {
ui.notifications.info(`Migrating Starwars FFG System for version ${game.system.data.version}. Please be patient and do not close your game or shut down your server.`, { permanent: true });
// migrate compendiums and flags
if (isAlpha || currentVersion === "null" || currentVersion === '' || parseFloat(currentVersion) < 1.61) {
ui.notifications.info(`Migrating Starwars FFG System for version ${game.system.version}. Please be patient and do not close your game or shut down your server.`, { permanent: true });

try {

// Update old pack to latest data model
for (let pack of game.packs) {
await pack.migrate();
}
// TODO: uncomment
//for (let pack of game.packs) {
// await pack.migrate();
//}

// Copy old flags to new system scope
FlagMigrationHelpers.migrateFlags()

ui.notifications.info(`Starwars FFG System Migration to version ${game.system.data.version} completed!`, { permanent: true });
ui.notifications.info(`Starwars FFG System Migration to version ${game.system.version} completed!`, { permanent: true });
} catch (err) {
CONFIG.logger.error(`Error during system migration`, err);
}
Expand Down