Migration des services en commerces

This commit is contained in:
Vincent Vandemeulebrouck 2023-01-02 22:49:45 +01:00
parent ee42bdcf83
commit 128d7adf89

View File

@ -1,6 +1,9 @@
import { RdDBaseActor } from "./actor/base-actor.js";
import { LOG_HEAD, SYSTEM_RDD } from "./constants.js"; import { LOG_HEAD, SYSTEM_RDD } from "./constants.js";
import { Environnement } from "./environnement.js"; import { Environnement } from "./environnement.js";
import { Grammar } from "./grammar.js"; import { Grammar } from "./grammar.js";
import { Monnaie } from "./item-monnaie.js";
import { RdDItem } from "./item.js";
class Migration { class Migration {
get code() { return "sample"; } get code() { return "sample"; }
@ -41,9 +44,9 @@ class _1_5_34_migrationPngWebp {
} }
function prepareDocumentsImgUpdate(documents) { function prepareDocumentsImgUpdate(documents) {
return documents.filter(it => it.img && it.img.match(regexOldPngJpg)) return documents.filter(it => it.img && it.img.match(regexOldPngJpg))
.map(it => { .map(it => {
return { _id: it.id, img: convertImgToWebp(it.img) } return { _id: it.id, img: convertImgToWebp(it.img) }
}); });
} }
const itemsUpdates = prepareDocumentsImgUpdate(game.items); const itemsUpdates = prepareDocumentsImgUpdate(game.items);
@ -325,6 +328,46 @@ class _10_3_17_Monnaies extends Migration {
} }
} }
class _10_4_6_ServicesEnCommerces extends Migration {
get code() { return "migration-service-acteurs"; }
get version() { return "10.4.6"; }
async migrate() {
const servicesToMigrate = game.items.filter(it => it.type == 'service');
servicesToMigrate.forEach(async service => {
const commerce = await this.convertServiceToCommerce(service);
await RdDBaseActor.create(commerce, { renderSheet: false });
await service.delete();
});
}
async convertServiceToCommerce(service) {
return {
name: service.name, img: service.img, type: 'commerce',
system: {
description: service.system.description,
notesmj: service.system.descriptionmj,
illimite: service.system.illimite
},
items: await this.transformInventaireCommerce(service)
}
}
async transformInventaireCommerce(service) {
const serviceItems = (service.system.items ?? []);
const commerceItems = await Promise.all(serviceItems.map(async (it) => { return await this.transformToItemBoutique(it); }));
return commerceItems.concat(Monnaie.monnaiesStandard());
}
async transformToItemBoutique(serviceRefItem) {
const item = await RdDItem.getCorrespondingItem(serviceRefItem);
const itemToCreate = {
name: item.name, img: item.img, type: item.type,
system: mergeObject({ cout: serviceRefItem.system.cout, quantite: serviceRefItem.system.quantite }, item.system, { overwrite: false })
};
return itemToCreate;
}
}
export class Migrations { export class Migrations {
static getMigrations() { static getMigrations() {
@ -338,7 +381,8 @@ export class Migrations {
new _10_2_10_DesirLancinant_IdeeFixe(), new _10_2_10_DesirLancinant_IdeeFixe(),
new _10_3_0_Inventaire(), new _10_3_0_Inventaire(),
new _10_3_0_FrequenceEnvironnement(), new _10_3_0_FrequenceEnvironnement(),
new _10_3_17_Monnaies() new _10_3_17_Monnaies(),
new _10_4_6_ServicesEnCommerces(),
]; ];
} }
@ -355,16 +399,10 @@ export class Migrations {
migrate() { migrate() {
const currentVersion = game.settings.get(SYSTEM_RDD, "systemMigrationVersion"); const currentVersion = game.settings.get(SYSTEM_RDD, "systemMigrationVersion");
if (isNewerVersion(game.system.version, currentVersion)) { if (isNewerVersion(game.system.version, currentVersion)) {
//if (true) { /* comment previous and uncomment here to test before upgrade */ //if (true) { /* comment previous and uncomment here to test before upgrade */
const migrations = Migrations.getMigrations().filter(m => isNewerVersion(m.version, currentVersion)); const migrations = Migrations.getMigrations().filter(m => isNewerVersion(m.version, currentVersion));
if (migrations.length > 0) { if (migrations.length > 0) {
migrations.sort((a, b) => migrations.sort((a, b) => this.compareVersions(a, b));
isNewerVersion(a.version, b.version)
? 1
: isNewerVersion(b.version, a.version)
? -1
: 0
);
migrations.forEach(async (m) => { migrations.forEach(async (m) => {
ui.notifications.info( ui.notifications.info(
`Executing migration ${m.code}: version ${currentVersion} is lower than ${m.version}` `Executing migration ${m.code}: version ${currentVersion} is lower than ${m.version}`
@ -390,4 +428,8 @@ export class Migrations {
console.log(LOG_HEAD + `No system version changed`); console.log(LOG_HEAD + `No system version changed`);
} }
} }
compareVersions(a, b) {
return isNewerVersion(a.version, b.version) ? 1 : isNewerVersion(b.version, a.version) ? -1 : 0;
}
} }