From 1624f9292cb875ba4d328839539facbb22fa51cd Mon Sep 17 00:00:00 2001 From: Vincent Vandemeulebrouck Date: Tue, 25 May 2021 23:41:53 +0200 Subject: [PATCH] =?UTF-8?q?Fix:=20d=C3=A9placements=20conteneurs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- module/actor.js | 66 ++++++++++++++++++++------------------ templates/actor-sheet.html | 7 ++-- 2 files changed, 37 insertions(+), 36 deletions(-) diff --git a/module/actor.js b/module/actor.js index 2381485c..583ce685 100644 --- a/module/actor.js +++ b/module/actor.js @@ -968,6 +968,7 @@ export class RdDActor extends Actor { contenu.splice(index, 1); index = contenu.indexOf(item.id); } + item.data.estContenu = false; await this.updateEmbeddedDocuments('Item', [data2use]); delete objetVersConteneur[item.id]; } @@ -980,6 +981,7 @@ export class RdDActor extends Actor { if (conteneur?.isConteneur()) { let data2use = duplicate(Misc.data(conteneur)); data2use.data.contenu.push(item.id); + item.data.estContenu = true; await this.updateEmbeddedDocuments('Item', [data2use]); objetVersConteneur[item.id] = conteneur.id; } @@ -988,15 +990,19 @@ export class RdDActor extends Actor { /* -------------------------------------------- */ /** Fonction de remise à plat de l'équipement (ie vide les champs 'contenu') */ async nettoyerConteneurs() { - let conteneurs = this.items.filter(it => it.type == 'conteneur'); - let conteneurFixedList = []; - for (let conteneur of conteneurs) { - if (Misc.templateData(conteneur).contenu.length > 0) { - conteneurFixedList.push({ _id: conteneur._id, 'data.contenu': [] }); + const corrections = []; + for (let item of this.items) { + let itemData = Misc.data(item); + if (itemData.estContenu) { + itemData.estContenu = undefined; + } + if (itemData.type == 'conteneur' && itemData.data.contenu.length > 0) { + corrections.push({ _id: itemData._id, 'data.contenu': [] }); } } - if (conteneurFixedList.length > 0) - await this.updateEmbeddedDocuments('Item', conteneurFixedList); + if (corrections.length > 0) { + await this.updateEmbeddedDocuments('Item', corrections); + } } async processDropItem(event, dragData, objetVersConteneur) { @@ -1010,39 +1016,35 @@ export class RdDActor extends Actor { let result = true; const destId = $(event.target).parents(".item").attr("data-item-id"); const item = this.getObjet(itemId); - if (item?.isEquipement()) { - if (dragData.actorId == this.id) { - // rangement - const srcId = objetVersConteneur[itemId]; - if (srcId != destId && itemId != destId) { // déplacement de l'objet - const dest = this.getObjet(destId); - const src = this.getObjet(srcId); - if (dest?.isConteneur()) { - if (this.conteneurPeutContenir(dest, item)) { - await this.enleverDeConteneur(item, src, objetVersConteneur); - await this.ajouterDansConteneur(item, dest, objetVersConteneur); - } - } - else { - if (src?.isConteneur() && !item?.isConteneur()) { - await this.enleverDeConteneur(item, src, objetVersConteneur); - // simuler la re-création - dragData.actorId = null; - await this.deleteEmbeddedDocuments('Item', [item.id]); - } - else if (dest?.isEquipementSimilaire(item)) { - await this.regrouperEquipementsSimilaires(item, dest); - result = false; - } - } + if (item?.isEquipement() && dragData.actorId == this.id) { + // rangement + const srcId = objetVersConteneur[itemId]; + if (srcId != destId && itemId != destId) { // déplacement de l'objet + const dest = this.getObjet(destId); + const src = this.getObjet(srcId); + // changer de conteneur + if (this.conteneurPeutContenir(dest, item)) { + await this.enleverDeConteneur(item, src, objetVersConteneur); + await this.ajouterDansConteneur(item, dest, objetVersConteneur); + } + else if (dest?.isEquipementSimilaire(item)) { + await this.regrouperEquipementsSimilaires(item, dest); + result = false; } } } await this.computeEncombrementTotalEtMalusArmure(); return result; } + /* -------------------------------------------- */ conteneurPeutContenir(dest, item) { + if (!dest) { + return true; + } + if (!dest.isConteneur()) { + return false; + } const destData = Misc.data(dest); if (this._isConteneurContenu(item, dest)) { ui.notifications.warn(`Impossible de déplacer un conteneur parent (${item.name}) dans un de ses contenus ${destData.name} !`); diff --git a/templates/actor-sheet.html b/templates/actor-sheet.html index 13ec61e4..a1cb96fe 100644 --- a/templates/actor-sheet.html +++ b/templates/actor-sheet.html @@ -610,14 +610,13 @@ {{#each objets as |item id|}} {{#unless item.estContenu}} - {{#if (ne item.type 'conteneur')}} + {{#if (eq item.type 'conteneur')}} + {{buildConteneur this}} + {{else}} {{> "systems/foundryvtt-reve-de-dragon/templates/actor-sheet-inventaire-conteneur.html" item=item }} {{/if}} {{/unless}} {{/each}} - {{#each conteneurs as |conteneur id|}} - {{buildConteneur this}} - {{/each}}

Montures