From 6508a529e585680226f8fddbcab656fcceb76810 Mon Sep 17 00:00:00 2001 From: sladecraven Date: Wed, 2 Jun 2021 17:39:16 +0200 Subject: [PATCH] Fix monnaie deletion --- module/actor-sheet.js | 3 +++ module/actor-vehicule-sheet.js | 18 ++++++++++++++++++ module/actor.js | 1 + module/item-monnaie.js | 11 +++++++++-- module/rdd-utility.js | 7 +++++++ 5 files changed, 38 insertions(+), 2 deletions(-) diff --git a/module/actor-sheet.js b/module/actor-sheet.js index 775c367b..fd9fb336 100644 --- a/module/actor-sheet.js +++ b/module/actor-sheet.js @@ -38,6 +38,9 @@ export class RdDActorSheet extends ActorSheet { /* -------------------------------------------- */ async getData() { const objectData = Misc.data(this.object); + + //this.actor.checkMonnaiePresence(this.actor.data.items); // Always check + let formData = { title: this.title, id: objectData.id, diff --git a/module/actor-vehicule-sheet.js b/module/actor-vehicule-sheet.js index 840f4540..b07c666c 100644 --- a/module/actor-vehicule-sheet.js +++ b/module/actor-vehicule-sheet.js @@ -80,6 +80,15 @@ export class RdDActorVehiculeSheet extends ActorSheet { await this.actor.createEmbeddedDocuments('Item', [{ name: name, type: type }], { renderSheet: true }); } + /* -------------------------------------------- */ + async monnaieIncDec(id, value) { + let monnaie = this.getMonnaie(id); + if (monnaie) { + const quantite = Math.max(0, Misc.templateData(monnaie).quantite + value); + await this.updateEmbeddedDocuments('Item', [{ _id: monnaie.id, 'data.quantite': quantite }]); + } + } + /* -------------------------------------------- */ /** @override */ activateListeners(html) { @@ -109,6 +118,15 @@ export class RdDActorVehiculeSheet extends ActorSheet { this.actor.nettoyerConteneurs(); }); + html.find('.monnaie-plus').click(async event => { + const li = $(event.currentTarget).parents(".item"); + this.actor.monnaieIncDec(li.data("item-id"), 1); + }); + html.find('.monnaie-moins').click(async event => { + const li = $(event.currentTarget).parents(".item"); + this.actor.monnaieIncDec(li.data("item-id"), -1); + }); + // Display info about queue html.find('.conteneur-name a').click((event) => { let myID = event.currentTarget.attributes['data-item-id'].value; diff --git a/module/actor.js b/module/actor.js index d1dd3c83..f7c47d6e 100644 --- a/module/actor.js +++ b/module/actor.js @@ -198,6 +198,7 @@ export class RdDActor extends Actor { async checkMonnaiePresence(items) { // Ajout opportuniste si les pièces n'existent pas. if (!items) return; // Sanity check during import let manquantes = Monnaie.monnaiesManquantes(items); + console.log("Manque : ", manquantes); if (manquantes.length > 0) { await this.createEmbeddedDocuments('Item', manquantes, { renderSheet: false }); } diff --git a/module/item-monnaie.js b/module/item-monnaie.js index 5850a3e3..741765de 100644 --- a/module/item-monnaie.js +++ b/module/item-monnaie.js @@ -25,6 +25,11 @@ const monnaiesData = [ export class Monnaie { + static isSystemMonnaie(item) { + let present = monnaiesData.find( monnaie => monnaie.data.valeur_deniers == item.data?.data?.valeur_deniers); + return present; + } + static monnaiesData() { return monnaiesData; } @@ -35,9 +40,11 @@ export class Monnaie { static monnaiesManquantes(items) { const valeurs = Monnaie.filtrerMonnaies(items) - .map(it => Misc.templateData(it).valeur_deniers) + .map(it => Misc.templateData(it).valeur_deniers); const manquantes = monnaiesData.filter(monnaie => !valeurs.find(v => v != Misc.templateData(monnaie).valeur_deniers)); - return manquantes; + //const manquantes = monnaiesData.filter(monnaie => !valeurs.find(v => v != Misc.templateData(monnaie).valeur_deniers) ); + console.log("Valeurs : ", valeurs, manquantes); + return []; //manquantes; } static deValeur(monnaie, v) { diff --git a/module/rdd-utility.js b/module/rdd-utility.js index eb4c0289..e275a5e1 100644 --- a/module/rdd-utility.js +++ b/module/rdd-utility.js @@ -9,6 +9,7 @@ import { DialogItemAchat } from "./dialog-item-achat.js"; import { ReglesOptionelles } from "./regles-optionelles.js"; import { RdDDice } from "./rdd-dice.js"; import { RdDItem } from "./item.js"; +import { Monnaie } from "./item-monnaie.js"; /* -------------------------------------------- */ // This table starts at 0 -> niveau -10 @@ -781,6 +782,12 @@ export class RdDUtility { static async confirmerSuppression(actorSheet, li) { let itemId = li.data("item-id"); let objet = actorSheet.actor.getObjet(itemId); + + if ( objet.type == 'monnaie' && Monnaie.isSystemMonnaie(objet) ) { + ui.notifications.warn("Suppression des monnaies de base impossible"); + return; + } + let msgTxt = "

Etes vous certain de vouloir supprimer cet objet ?"; let buttons = { delete: {