From 7cb45afa13386f5e92645c6e50f2e9b9eea8d661 Mon Sep 17 00:00:00 2001 From: sladecraven Date: Fri, 8 Jan 2021 09:58:15 +0100 Subject: [PATCH] #123 : Verification equipement --- module/actor-sheet.js | 18 +++++++++++++++++- module/actor.js | 23 ++++++++++++++++++++++- module/rdd-utility.js | 18 ------------------ packs/equipement.db | 1 + 4 files changed, 40 insertions(+), 20 deletions(-) diff --git a/module/actor-sheet.js b/module/actor-sheet.js index 7cbcc9f0..0c360dd6 100644 --- a/module/actor-sheet.js +++ b/module/actor-sheet.js @@ -137,9 +137,25 @@ export class RdDActorSheet extends ActorSheet { return data; } + /* -------------------------------------------- */ + async processItemDropEvent( event) { + let dragData = JSON.parse(event.dataTransfer.getData("text/plain")); + let dropID = $(event.target).parents(".item").attr("data-item-id"); // Only relevant if container drop + if ( dropID ) { // Dropped over an item !!! + let objetId = dragData.id || dragData.data._id; + if ( this.objetVersConteneur[objetId] != dropID && objetId != dropID) { + if ( this.actor.validateConteneur(objetId, dropID) && this.actor.testConteneurCapacite(objetId, dropID) ) { + await this.actor.enleverDeConteneur(objetId, this.objetVersConteneur[objetId]); + await this.actor.ajouterAConteneur(objetId, dropID); + } + } + } + this.actor.computeEncombrementTotalEtMalusArmure(); + } + /* -------------------------------------------- */ async _onDrop(event) { - await RdDUtility.processItemDropEvent(this, event); + await this.processItemDropEvent(event); super._onDrop(event); } diff --git a/module/actor.js b/module/actor.js index 4f39d196..c48afb97 100644 --- a/module/actor.js +++ b/module/actor.js @@ -657,6 +657,25 @@ export class RdDActor extends Actor { await this.update( {"data.attributs": attributs } ); } + /* -------------------------------------------- */ + validateConteneur( itemId, conteneurId ) { + let conteneurDest = this.items.find( conteneur => conteneurId == conteneur._id); // recup conteneur + let conteneurSrc = this.items.find( conteneur => itemId == conteneur._id && conteneur.type == 'conteneur'); + if ( conteneurSrc ) { // Si c'est un conteneur, il faut vérifier qu'on ne le déplace pas vers un sous-conteneur lui appartenant + for (let id of conteneurSrc.data.data.contenu) { + let subObjet = this.items.find( subobj => id == subobj._id); + if ( subObjet && subObjet._id == conteneurDest._id ) { + ui.notifications.warn("Impossible de déplacer un conteneur parent dans son fils !"); + return false; // Loop detected ! + } + if ( subObjet && subObjet.type == 'conteneur') { + return this.validateConteneur( subObjet._id, conteneurId); + } + } + } + return true; + } + /* -------------------------------------------- */ /** Teste si le conteneur de destination a suffisament de capacité * pour recevoir le nouvel objet @@ -673,8 +692,10 @@ export class RdDActor extends Actor { } // Et gérer le nouvel objet let nouvelObjet = this.items.find( objet => (itemId == objet._id) ); - if ( currentEnc + nouvelObjet.data.data.encombrement > Number(conteneur.data.data.capacite) ) + if ( currentEnc + nouvelObjet.data.data.encombrement > Number(conteneur.data.data.capacite) ) { + ui.notifications.warn("Capacité d'encombrement insuffisante dans le conteneur !"); return false; + } } return true; } diff --git a/module/rdd-utility.js b/module/rdd-utility.js index 0d75eba6..9dd6a1f5 100644 --- a/module/rdd-utility.js +++ b/module/rdd-utility.js @@ -276,24 +276,6 @@ export class RdDUtility { data.data.objets = data.data.conteneurs.concat(data.data.materiel).concat(data.data.armes).concat(data.data.armures).concat(data.data.munitions).concat(data.data.livres).concat(data.data.potions).concat(data.data.herbes).concat(data.data.ingredients); } - /* -------------------------------------------- */ - static async processItemDropEvent( actorSheet, event) { - let dragData = JSON.parse(event.dataTransfer.getData("text/plain")); - let dropID = $(event.target).parents(".item").attr("data-item-id"); // Only relevant if container drop - if ( dropID ) { // Dropped over an item !!! - let objetId = dragData.id || dragData.data._id; - if ( actorSheet.objetVersConteneur[objetId] != dropID && objetId != dropID) { - if ( actorSheet.actor.testConteneurCapacite(objetId, dropID) ) { - await actorSheet.actor.enleverDeConteneur(objetId, actorSheet.objetVersConteneur[objetId]); - await actorSheet.actor.ajouterAConteneur(objetId, dropID); - } else { - ui.notifications.info("Capacité d'encombrement insuffisante dans le conteneur !"); - } - } - } - actorSheet.actor.computeEncombrementTotalEtMalusArmure(); - } - /* -------------------------------------------- */ static buildArbreDeConteneur( actorSheet, data ) { actorSheet.objetVersConteneur = {}; // Table de hash locale pour recupération rapide du conteneur parent (si existant) diff --git a/packs/equipement.db b/packs/equipement.db index bd649d1f..e14c6aaa 100644 --- a/packs/equipement.db +++ b/packs/equipement.db @@ -200,3 +200,4 @@ {"_id":"zQWlnUsd8bPySujd","name":"Aiguille à coudre","permission":{"default":0,"rYShh2P1DNavdoBD":3},"type":"objet","data":{"description":"","quantite":1,"encombrement":0.01,"equipe":false,"resistance":0,"qualite":0,"cout":0.1},"flags":{},"img":"systems/foundryvtt-reve-de-dragon/icons/objets/aiguille.webp","effects":[]} {"_id":"zYI8mDiysWtmsSyy","name":"Carquois","permission":{"default":0,"jOzRscDxoXZWpGS6":3},"type":"conteneur","data":{"description":"","capacite":2,"encombrement":0.1,"equipe":false,"qualite":0,"cout":0.5},"flags":{},"img":"systems/foundryvtt-reve-de-dragon/icons/objets/carquois.webp","effects":[]} {"_id":"zlDa1vwmls6Uf4pt","name":"Bourse de cuir","permission":{"default":0,"rYShh2P1DNavdoBD":3},"type":"conteneur","data":{"description":"","capacite":0.5,"encombrement":0.01,"equipe":false,"qualite":0,"contenu":[],"cout":0.1},"flags":{},"img":"systems/foundryvtt-reve-de-dragon/icons/objets/bourse.webp","effects":[]} +{"_id":"XlKp5BvZX8A03fUg","name":"Sac à dos de cuir","permission":{"default":0,"rYShh2P1DNavdoBD":3},"type":"conteneur","data":{"description":"","capacite":25,"encombrement":0.4,"equipe":false,"qualite":0,"contenu":[],"cout":1},"flags":{},"img":"systems/foundryvtt-reve-de-dragon/icons/objets/sac_a_dos.webp","effects":[]}