From 128d7adf89a4a4ce3dfa7d4f7729ea2588c48602 Mon Sep 17 00:00:00 2001 From: Vincent Vandemeulebrouck Date: Mon, 2 Jan 2023 22:49:45 +0100 Subject: [PATCH] Migration des services en commerces --- module/migrations.js | 66 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 54 insertions(+), 12 deletions(-) diff --git a/module/migrations.js b/module/migrations.js index f2765567..9a553f80 100644 --- a/module/migrations.js +++ b/module/migrations.js @@ -1,6 +1,9 @@ +import { RdDBaseActor } from "./actor/base-actor.js"; import { LOG_HEAD, SYSTEM_RDD } from "./constants.js"; import { Environnement } from "./environnement.js"; import { Grammar } from "./grammar.js"; +import { Monnaie } from "./item-monnaie.js"; +import { RdDItem } from "./item.js"; class Migration { get code() { return "sample"; } @@ -41,9 +44,9 @@ class _1_5_34_migrationPngWebp { } function prepareDocumentsImgUpdate(documents) { return documents.filter(it => it.img && it.img.match(regexOldPngJpg)) - .map(it => { - return { _id: it.id, img: convertImgToWebp(it.img) } - }); + .map(it => { + return { _id: it.id, img: convertImgToWebp(it.img) } + }); } 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 { static getMigrations() { @@ -338,7 +381,8 @@ export class Migrations { new _10_2_10_DesirLancinant_IdeeFixe(), new _10_3_0_Inventaire(), 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() { const currentVersion = game.settings.get(SYSTEM_RDD, "systemMigrationVersion"); 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)); if (migrations.length > 0) { - migrations.sort((a, b) => - isNewerVersion(a.version, b.version) - ? 1 - : isNewerVersion(b.version, a.version) - ? -1 - : 0 - ); + migrations.sort((a, b) => this.compareVersions(a, b)); migrations.forEach(async (m) => { ui.notifications.info( `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`); } } + + compareVersions(a, b) { + return isNewerVersion(a.version, b.version) ? 1 : isNewerVersion(b.version, a.version) ? -1 : 0; + } }