From 5175be2801a1cc654659f216d581be9b49f2dc89 Mon Sep 17 00:00:00 2001 From: sladecraven Date: Thu, 31 Dec 2020 00:55:02 +0100 Subject: [PATCH] #88 Gestion argent --- module/actor-sheet.js | 11 +++ module/actor.js | 113 +++++++++++++++++++++++++++--- module/rdd-main.js | 3 +- module/rdd-utility.js | 44 ++++++++++-- styles/simple.css | 2 +- templates/actor-sheet.html | 13 ++++ templates/item-monnaie-sheet.html | 29 ++++++++ 7 files changed, 198 insertions(+), 17 deletions(-) create mode 100644 templates/item-monnaie-sheet.html diff --git a/module/actor-sheet.js b/module/actor-sheet.js index 90af6b3b..7ec9930c 100644 --- a/module/actor-sheet.js +++ b/module/actor-sheet.js @@ -478,6 +478,17 @@ export class RdDActorSheet extends ActorSheet { this.actor.jetVie(); this.render(true); }); + + html.find('.monnaie-plus').click((event) => { + const li = $(event.currentTarget).parents(".item"); + this.actor.monnaieIncDec( li.data("item-id"), 1 ); + this.render(true); + }); + html.find('.monnaie-moins').click((event) => { + const li = $(event.currentTarget).parents(".item"); + this.actor.monnaieIncDec( li.data("item-id"), -1 ); + this.render(true); + }); html.find('#vie-plus').click((event) => { this.actor.santeIncDec("vie", 1); diff --git a/module/actor.js b/module/actor.js index dfa81ead..a824d6d2 100644 --- a/module/actor.js +++ b/module/actor.js @@ -14,6 +14,7 @@ import { Grammar } from "./grammar.js"; import { RdDEncaisser } from "./rdd-roll-encaisser.js"; import { RdDCombat } from "./rdd-combat.js"; +/* -------------------------------------------- */ /** * Extend the base Actor entity by defining a custom roll data structure which is ideal for the Simple system. * @extends {Actor} @@ -47,8 +48,6 @@ export class RdDActor extends Actor { let compendiumName = ""; if (data.type == "personnage") { compendiumName = "foundryvtt-reve-de-dragon.competences"; - } else if (data.type == "humanoide") { - compendiumName = "foundryvtt-reve-de-dragon.competences-humanoides"; } else if (data.type == "creature") { compendiumName = "foundryvtt-reve-de-dragon.competences-creatures"; } else if (data.type == "entite") { @@ -64,12 +63,14 @@ export class RdDActor extends Actor { data.items.push(compItem); } + // Ajout monnaie + if (data.type == "personnage" ) { + await RdDActor.ajouterMonnaie( data.items ); + } + return super.create(data, options); } - /* -------------------------------------------- */ - - /* -------------------------------------------- */ prepareData() { super.prepareData(); @@ -103,6 +104,37 @@ export class RdDActor extends Actor { this.computeEncombrementTotalEtMalusArmure(); this.computeEtatGeneral(); } + + /* -------------------------------------------- */ + static async ajouterMonnaie( items ) { + let etain = { name: "Etain (1 denier)", type: 'monnaie', _id: randomID(), + data: { + quantite: 0, + valeur_deniers: 1, + encombrement: 0.01, + description: "" + } + } + items.push(etain); + + let bronze = duplicate(etain); + bronze.id = randomID(); + bronze.name = "Bronze (10 deniers)"; + bronze.data.valeur_deniers = 10; + items.push(bronze); + + let argent = duplicate(etain); + argent.id = randomID(); + argent.name = "Argent (1 sol)"; + argent.data.valeur_deniers = 100; + items.push(argent); + + let or = duplicate(argent); + or.id = randomID(); + or.name = "Or (10 sols)"; + or.data.valeur_deniers = 1000; + items.push(or); + } /* -------------------------------------------- */ isCreature() { @@ -1355,7 +1387,7 @@ export class RdDActor extends Actor { isConnaissanceFleuve( ) { return this.data.items.find( item => item.type == 'tete' && item.name.toLowerCase().includes( 'connaissance du fleuve' ) ); } - + /* -------------------------------------------- */ isReserveEnSecurite() { let reserveSecurite = this.data.items.find( item => item.type == 'tete' && item.name.toLowerCase().includes( ' en sécurité' ) ); @@ -1996,7 +2028,6 @@ export class RdDActor extends Actor { this.sheet.render(true); } - /* -------------------------------------------- */ /** @override */ getRollData() { @@ -2004,7 +2035,6 @@ export class RdDActor extends Actor { return data; } - /* -------------------------------------------- */ /* -- entites -- */ /* retourne true si on peut continuer, false si on ne peut pas continuer */ @@ -2075,6 +2105,73 @@ export class RdDActor extends Actor { await this.update( {"data.sante.resonnance": resonnance}); return; } + /* -------------------------------------------- */ + async optimizeArgent( sumDenier ) { + let sols = Math.floor(sumDenier / 100); + let deniers = sumDenier - (sols*100); + let nbOr = Math.floor(sols / 10); + let nbArgent = sols - (nbOr*10); + let nbBronze = Math.floor(deniers / 10 ); + let nbEtain = deniers - (nbBronze*10); + + // console.log("ARGENT", nbOr, nbArgent, nbBronze, nbEtain); + let piece = this.data.items.find( item => item.type =='monnaie' && item.data.valeur_deniers == 1000); + if (piece ) { + let update = { _id: piece._id, 'data.quantite': nbOr}; + const updated = await this.updateEmbeddedEntity("OwnedItem", update); + } + + piece = this.data.items.find( item => item.type =='monnaie' && item.data.valeur_deniers == 100); + if (piece ) { + let update = { _id: piece._id, 'data.quantite': nbArgent}; + const updated = await this.updateEmbeddedEntity("OwnedItem", update); + } + + piece = this.data.items.find( item => item.type =='monnaie' && item.data.valeur_deniers == 10); + if (piece ) { + let update = { _id: piece._id, 'data.quantite': nbBronze}; + const updated = await this.updateEmbeddedEntity("OwnedItem", update); + } + + piece = this.data.items.find( item => item.type =='monnaie' && item.data.valeur_deniers == 1); + if (piece ) { + let update = { _id: piece._id, 'data.quantite': nbEtain}; + const updated = await this.updateEmbeddedEntity("OwnedItem", update); + } + } + + /* -------------------------------------------- */ + payerDenier( sumDenier ) { + sumDenier = Number(sumDenier); + let denierDisponible = 0; + let monnaie = this.data.items.filter( item => item.type =='monnaie'); + for ( let piece of monnaie ) { + denierDisponible += piece.data.valeur_deniers * Number(piece.data.quantite); + } + console.log("DENIER", game.user.character, sumDenier, denierDisponible); + if ( denierDisponible >= sumDenier) { + denierDisponible -= sumDenier; + this.optimizeArgent(denierDisponible); + } else { + let message = { + whisper: ChatUtility.getWhisperRecipientsAndGMs( this.name ), + content : "Vous n'avez pas assez d'argent pour paye cette somme !" + }; + ChatMessage.create( message ); + } + } + + /* -------------------------------------------- */ + async monnaieIncDec( id, value) { + let monnaie = this.data.items.find( item => item.type =='monnaie' && item._id == id ); + if (monnaie ) { + monnaie.data.quantite += value; + if ( monnaie.data.quantite < 0 ) monnaie.data.quantite = 0; // Sanity check + const update = {_id: monnaie._id, 'data.quantite': monnaie.data.quantite }; + const updated = await this.updateEmbeddedEntity("OwnedItem", update); + } + } + } diff --git a/module/rdd-main.js b/module/rdd-main.js index 1452d74d..a6be6ccb 100644 --- a/module/rdd-main.js +++ b/module/rdd-main.js @@ -271,8 +271,7 @@ Hooks.on("preCreateToken", (scene, tokenData, options) => { /* Foundry VTT Initialization */ /* -------------------------------------------- */ Hooks.on("chatMessage", (html, content, msg) => { - let regExp; - regExp = /(\S+)/g; + let regExp = /(\S+)/g; let commands = content.match(regExp); return RdDUtility.processChatCommand( commands, content, msg ); diff --git a/module/rdd-utility.js b/module/rdd-utility.js index 18099930..46031101 100644 --- a/module/rdd-utility.js +++ b/module/rdd-utility.js @@ -164,6 +164,7 @@ export class RdDUtility { 'systems/foundryvtt-reve-de-dragon/templates/item-tarot-sheet.html', 'systems/foundryvtt-reve-de-dragon/templates/item-tete-sheet.html', 'systems/foundryvtt-reve-de-dragon/templates/item-ombre-sheet.html', + 'systems/foundryvtt-reve-de-dragon/templates/item-monnaie-sheet.html', 'systems/foundryvtt-reve-de-dragon/templates/competence-categorie.html', 'systems/foundryvtt-reve-de-dragon/templates/competence-carac-defaut.html', 'systems/foundryvtt-reve-de-dragon/templates/competence-base.html', @@ -245,6 +246,7 @@ export class RdDUtility { data.data.ombres = this.checkNull(data.itemsByType['ombre']); data.data.tetes = this.checkNull(data.itemsByType['tete']); data.data.taches = this.checkNull(data.itemsByType['tache']); + data.data.monnaie = this.checkNull(data.itemsByType['monnaie']); 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); } @@ -743,7 +745,7 @@ export class RdDUtility { return; } if ((game.user.isGM && !defenderToken.actor.hasPlayerOwner) || (defenderToken.actor.hasPlayerOwner && (game.user.character.id == defenderToken.actor.data._id))) { - console.log("User is pushing message...", game.user.name); + //console.log("User is pushing message...", game.user.name); game.system.rdd.rollDataHandler[data.attackerId] = duplicate(data.rollData); data.whisper = [game.user]; data.blind = true; @@ -873,7 +875,7 @@ export class RdDUtility { let actor = game.actors.get( actorId ); actor.tmrApp.forceDemiRevePosition(coord); }); - // Gestio spécifique des sorts en réserve multiples (ie têtes) + // Gestion spécifique des sorts en réserve multiples (ie têtes) html.on("click", '#sort-reserve', event => { let coord = event.currentTarget.attributes['data-tmr-coord'].value; let sortId = event.currentTarget.attributes['data-sort-id'].value; @@ -881,6 +883,12 @@ export class RdDUtility { let actor = game.actors.get( actorId ); actor.tmrApp.lancerSortEnReserve(coord, sortId); }); + // Gestion du bouton payer + html.on("click", '#payer-button', event => { + let sumdenier = event.currentTarget.attributes['data-somme-denier'].value; + if (game.user.character ) + game.user.character.payerDenier(sumdenier); + }); } /* -------------------------------------------- */ @@ -893,6 +901,27 @@ export class RdDUtility { } ChatMessage.create( msg ); } + + /* -------------------------------------------- */ + static afficherDemandePayer(som1, som2) { + som1 = (som1) ? som1.toLowerCase() : "0d"; + som2 = (som2) ? som2.toLowerCase() : "0d"; + let regExp = /(\d+)(\w+)/g; + let p1 = regExp.exec( som1); + regExp = /(\d+)(\w+)/g; + let p2 = regExp.exec( som2); + let sumd = 0; + let sums = 0; + if (p1[2] == 'd') sumd += Number(p1[1]); + if (p1[2] == 's') sums += Number(p1[1]); + if (p2[2] == 'd') sumd += Number(p2[1]); + if (p2[2] == 's') sums += Number(p2[1]); + + let sumtotald = sumd + (sums*100); + let msgPayer = "La somme de "+sums+" Sols et "+sumd+" Deniers est à payer, cliquer sur le lien ci-dessous si besoin.
"; + msgPayer += "Payer" + ChatMessage.create( { content: msgPayer } ); + } /* -------------------------------------------- */ /* Manage chat commands */ @@ -915,11 +944,14 @@ export class RdDUtility { } return false } else if (command === "/tmrr") { - TMRUtility.getRencontre(commands[1], commands[2] ) - return false + TMRUtility.getRencontre(commands[1], commands[2] ) + return false } else if (command === "/tmra") { - TMRUtility.getTMRAleatoire( ) - return false + TMRUtility.getTMRAleatoire( ) + return false + } else if (command === "/payer") { + RdDUtility.afficherDemandePayer( commands[1], commands[2] ) + return false } return true; diff --git a/styles/simple.css b/styles/simple.css index 6df502b9..71bfe4f6 100644 --- a/styles/simple.css +++ b/styles/simple.css @@ -451,7 +451,7 @@ section.sheet-body:after { width: 2rem; } -#vie-plus, #vie-moins, #endurance-plus, #endurance-moins, #fatigue-plus, #fatigue-moins, #ptreve-actuel-plus, #ptreve-actuel-moins { +#vie-plus, #vie-moins, #endurance-plus, #endurance-moins, #fatigue-plus, #fatigue-moins, #ptreve-actuel-plus, #ptreve-actuel-moins, .monnaie-plus, .monnaie-moins { display: inline-block; width: 1.25rem; background: rgba(30, 25, 20, 1); diff --git a/templates/actor-sheet.html b/templates/actor-sheet.html index 07804020..e7805231 100644 --- a/templates/actor-sheet.html +++ b/templates/actor-sheet.html @@ -614,6 +614,19 @@ {{!-- Equipment Tab --}}
+ Encombrement total/max : {{numberFormat data.encTotal decimals=1}} / {{data.attributs.encombrement.value}} {{data.surEncombrementMessage}} - Créer un objet