From 1d6768aff09a1d05934661179cc779f4d433be4e Mon Sep 17 00:00:00 2001 From: sladecraven Date: Thu, 12 Nov 2020 14:43:08 +0100 Subject: [PATCH] #11 Gestion des conteneurs --- module/actor-sheet.js | 67 +++++++++++++++++------ module/actor.js | 30 +++++++++- module/rdd-main.js | 3 + module/rdd-utility.js | 20 ++++++- system.json | 2 +- template.json | 1 + templates/actor-inventaire-conteneur.html | 8 +++ templates/actor-sheet.html | 64 ++++------------------ 8 files changed, 120 insertions(+), 75 deletions(-) create mode 100644 templates/actor-inventaire-conteneur.html diff --git a/module/actor-sheet.js b/module/actor-sheet.js index a1fb2b80..2c8c2110 100644 --- a/module/actor-sheet.js +++ b/module/actor-sheet.js @@ -106,28 +106,51 @@ export class RdDActorSheet extends ActorSheet { // Mise à jour de l'encombrement total this.actor.computeEncombrementTotal(); - // low is normal, this the base used to compute the grid. - data.data.fatigueHTML = "" + RdDUtility.makeHTMLfatigueMatrix( data.data.sante.fatigue.value, data.data.sante.endurance.max ).html() + "
"; - data.data.materiel = this._checkNull(data.itemsByType['objet']); - data.data.conteneur = this._checkNull(data.itemsByType['conteneur']); - data.data.armes = this._checkNull(data.itemsByType['arme']); - data.data.armures = this._checkNull(data.itemsByType['armure']); - data.data.livres = this._checkNull(data.itemsByType['livre']); - data.data.potions = this._checkNull(data.itemsByType['potions']); - data.data.ingredients = this._checkNull(data.itemsByType['ingredient']); - data.data.munitions = this._checkNull(data.itemsByType['munition']); - data.data.herbes = this._checkNull(data.itemsByType['herbes']); - data.data.sorts = this._checkNull(data.itemsByType['sort']); - data.data.queues = this._checkNull(data.itemsByType['queue']); - data.data.souffles = this._checkNull(data.itemsByType['souffle']); - data.data.ombres = this._checkNull(data.itemsByType['ombre']); - data.data.tetes = this._checkNull(data.itemsByType['tete']); - data.data.objets = 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); + // Common data data.data.competenceByCategory = data.competenceByCategory; data.data.encombrementTotal = this.actor.encombrementTotal; data.data.isGM = game.user.isGM; data.bonusmalusTable = CONFIG.RDD.bonusmalus; + // low is normal, this the base used to compute the grid. + data.data.fatigueHTML = "" + RdDUtility.makeHTMLfatigueMatrix( data.data.sante.fatigue.value, data.data.sante.endurance.max ).html() + "
"; + data.data.materiel = this._checkNull(data.itemsByType['objet']); + data.data.conteneurs = this._checkNull(data.itemsByType['conteneur']); + data.data.armes = this._checkNull(data.itemsByType['arme']); + data.data.armures = this._checkNull(data.itemsByType['armure']); + data.data.livres = this._checkNull(data.itemsByType['livre']); + data.data.potions = this._checkNull(data.itemsByType['potions']); + data.data.ingredients = this._checkNull(data.itemsByType['ingredient']); + data.data.munitions = this._checkNull(data.itemsByType['munition']); + data.data.herbes = this._checkNull(data.itemsByType['herbes']); + data.data.sorts = this._checkNull(data.itemsByType['sort']); + data.data.queues = this._checkNull(data.itemsByType['queue']); + data.data.souffles = this._checkNull(data.itemsByType['souffle']); + data.data.ombres = this._checkNull(data.itemsByType['ombre']); + data.data.tetes = this._checkNull(data.itemsByType['tete']); + 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); + + this.objetVersConteneur = {}; // Table de hash locale pour recupération rapide du conteneur parent (si existant) + // Attribution des objets aux conteneurs + for (let conteneur of data.data.conteneurs) { + conteneur.subItems = []; + conteneur.encTotal = 0; + if (conteneur.data.contenu) { + for (let id of conteneur.data.contenu) { + let objet = data.data.objets.find( objet => (id == objet._id) ); + if (objet) { + objet.estContenu = true; // Permet de filtrer ce qui est porté dans le template + this.objetVersConteneur[id] = conteneur._id; + conteneur.encTotal += objet.data.encombrement; + conteneur.subItems.push( objet ); + } + } + } + } + // Construit la liste des conteneurs de niveau 1 (c'est à dire non contenu eux-même dans un conteneur) + let newConteneurs = data.data.conteneurs.filter(function(conteneur, index, arr) { return !conteneur.estContenu } ); + data.data.conteneurs = newConteneurs; + return data; } @@ -140,10 +163,18 @@ export class RdDActorSheet extends ActorSheet { /* -------------------------------------------- */ async _onDrop(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; + this.actor.enleverDeConteneur(objetId, this.objetVersConteneur[objetId]); + this.actor.ajouterAConteneur(objetId, dropID); + } + console.log("DATA !!!!", dragData, dropID ); super._onDrop(event) this.actor.computeEncombrementTotal(); - } + /* -------------------------------------------- */ /** @override */ activateListeners(html) { diff --git a/module/actor.js b/module/actor.js index 685ba338..c99fd5e6 100644 --- a/module/actor.js +++ b/module/actor.js @@ -371,14 +371,40 @@ export class RdDActor extends Actor { await this.update( {"data.compteurs": compteurs } ); } + /* -------------------------------------------- */ + /** Supprime un item d'un conteneur, sur la base + * de leurs ID */ + async enleverDeConteneur( itemId, conteneurId ) { + if ( !conteneurId ) return; // pas de conteneur (porté sur soi) + let conteneur = this.items.find( conteneur => conteneurId == conteneur._id); // recup conteneur + if ( conteneur ) { // Si présent + //console.log("Suppression du conteneur1", conteneurId, itemId, conteneur.data.data.contenu); + let newContenu = conteneur.data.data.contenu.filter( function(value, index, arr) { return value != itemId } ); + //console.log("Suppression du conteneur2", conteneurId, itemId, newContenu); + let update = {_id: conteneurId, "data.contenu": newContenu }; + await this.updateEmbeddedEntity("OwnedItem", update); + } + } + + /* -------------------------------------------- */ + /** Ajoute un item dans un conteneur, sur la base + * de leurs ID */ + async ajouterAConteneur( itemId, conteneurId ) { + if ( !conteneurId ) return; // pas de conteneur (porté sur soi) + let conteneur = this.items.find( conteneur => conteneurId == conteneur._id); + if ( conteneur && conteneur.type == 'conteneur' ) { + conteneur.data.data.contenu.push( itemId ); + await this.updateEmbeddedEntity("OwnedItem", conteneur.data ); + } + } + /* -------------------------------------------- */ detectSurEncombrement( ) { let diffEnc = Number(this.encombrementTotal) - Number(this.data.data.attributs.encombrement.value); - console.log("Sur enc", this.encombrementTotal, diffEnc); if ( diffEnc > 0 ) { // Sur-encombrement let malus = Math.round( diffEnc); malus = (malus == 0) ? 1 : malus; // Always 1 at least - console.log("Sur enc malus", malus); + //console.log("Sur enc malus", malus); return malus; } return 0; diff --git a/module/rdd-main.js b/module/rdd-main.js index 80f1ab46..2ef7a5e3 100644 --- a/module/rdd-main.js +++ b/module/rdd-main.js @@ -136,6 +136,9 @@ Hooks.once("init", async function() { Items.unregisterSheet("core", ItemSheet); Items.registerSheet("foundryvtt-reve-de-dragon", RdDItemSheet, {makeDefault: true}); + // Handlebar function pour container + Handlebars.registerHelper('buildConteneur', (objet) => { return RdDUtility.buildConteneur(objet); }); + // Patch the initiative formula _patch_initiative(); }); diff --git a/module/rdd-utility.js b/module/rdd-utility.js index d0d57571..67f8a785 100644 --- a/module/rdd-utility.js +++ b/module/rdd-utility.js @@ -155,12 +155,30 @@ export class RdDUtility { 'systems/foundryvtt-reve-de-dragon/templates/dialog-roll-encaisser.html', 'systems/foundryvtt-reve-de-dragon/templates/dialog-tmr.html', // Calendrier - 'systems/foundryvtt-reve-de-dragon/templates/calendar_template.html' + 'systems/foundryvtt-reve-de-dragon/templates/calendar_template.html', + // Conteneur/item in Actor sheet + 'systems/foundryvtt-reve-de-dragon/templates/actor-inventaire-conteneur.html' ]; return loadTemplates(templatePaths); } + /************************************************************************************/ + static buildConteneur( objet, niveau ) { + if (!niveau) niveau = 1; + let prefix = ""; + for (let i = 0; i < niveau; i++) prefix += " "; + let str = Handlebars.partials['systems/foundryvtt-reve-de-dragon/templates/actor-inventaire-conteneur.html']( { item: objet} ); + if (objet.type == 'conteneur') { + str = str + "
    "; + for (let subItem of objet.subItems) { + str = str + this.buildConteneur(subItem, niveau+1); + } + str = str + "
"; + } + return new Handlebars.SafeString(str); + } + /* -------------------------------------------- */ static buildResolutionTable( ) { let tableRes = [] diff --git a/system.json b/system.json index 2f1359d1..3343ce64 100644 --- a/system.json +++ b/system.json @@ -5,7 +5,7 @@ "version": "0.9.32", "minimumCoreVersion": "0.7.5", "compatibleCoreVersion": "0.7.6", - "templateVersion": 41, + "templateVersion": 43, "author": "LeRatierBretonnien", "esmodules": [ "module/rdd-main.js", "module/hook-renderChatLog.js" ], "styles": ["styles/simple.css"], diff --git a/template.json b/template.json index 7ec212ab..73a4ddd1 100644 --- a/template.json +++ b/template.json @@ -467,6 +467,7 @@ "encombrement": 0, "equipe": false, "qualite": 0, + "contenu": [], "cout": 0 }, "arme": { diff --git a/templates/actor-inventaire-conteneur.html b/templates/actor-inventaire-conteneur.html new file mode 100644 index 00000000..6e924503 --- /dev/null +++ b/templates/actor-inventaire-conteneur.html @@ -0,0 +1,8 @@ +
  • + +

    {{item.name}}

    +
    + + +
    +
  • diff --git a/templates/actor-sheet.html b/templates/actor-sheet.html index de13d622..926fcc04 100644 --- a/templates/actor-sheet.html +++ b/templates/actor-sheet.html @@ -422,8 +422,12 @@ {{!-- Equipment Tab --}}
    Encombrement total/max : {{data.encombrementTotal}} / {{data.attributs.encombrement.value}} +
    + Porté sur soi
      - {{#each data.armes_seules as |item id|}} + {{#each data.objets as |item id|}} + {{#unless item.estContenu}} + {{#if (ne item.type 'conteneur')}}
    1. {{item.name}}

      @@ -433,61 +437,15 @@
    + {{/if}} + {{/unless}} {{/each}} -
      - {{#each data.armures as |item id|}} -
    1. - -

      {{item.name}}

      - -
    2. - {{/each}} -
    -
      - {{#each data.conteneur as |item id|}} -
    1. - -

      {{item.name}}

      - -
    2. + {{#each data.conteneurs as |conteneur id|}} + {{buildConteneur this}} {{/each}} -
    -
      - {{#each data.materiel as |item id|}} -
    1. - -

      {{item.name}}

      - -
    2. - {{/each}} -
    -
      - {{#each data.livres as |item id|}} -
    1. - -

      {{item.name}}

      - -
    2. - {{/each}} -
    - + + {{!-- Biography Tab --}}