From 441259efb603b60cc762a4440d70abe11e072734 Mon Sep 17 00:00:00 2001 From: Vincent Vandemeulebrouck Date: Fri, 19 Mar 2021 22:20:01 +0100 Subject: [PATCH] Cleanup compendium et monnaies --- module/actor.js | 131 +++++++++++--------------------------- module/item-competence.js | 11 ++++ module/item-monnaie.js | 40 ++++++++++++ module/misc.js | 11 ++++ module/rdd-utility.js | 4 +- 5 files changed, 102 insertions(+), 95 deletions(-) create mode 100644 module/item-monnaie.js diff --git a/module/actor.js b/module/actor.js index ee1e39af..c78b6419 100644 --- a/module/actor.js +++ b/module/actor.js @@ -26,6 +26,7 @@ import { Poetique } from "./poetique.js"; import { EffetsDraconiques } from "./tmr/effets-draconiques.js"; import { Draconique } from "./tmr/draconique.js"; import { RdDCarac } from "./rdd-carac.js"; +import { Monnaie } from "./item-monnaie.js"; /* -------------------------------------------- */ @@ -56,36 +57,25 @@ export class RdDActor extends Actor { */ static async create(data, options) { - // Case of compendium global import if (data instanceof Array) { return super.create(data, options); } + + const isPersonnage = data.type == "personnage"; // If the created actor has items (only applicable to duplicated actors) bypass the new actor creation logic if (data.items) { let actor = super.create(data, options); - if (data.type == "personnage") { + if (isPersonnage) { await actor.checkMonnaiePresence(data.items); } return actor; } - let compendiumName; - if (data.type == "personnage") { - compendiumName = "foundryvtt-reve-de-dragon.competences"; - } else if (data.type == "creature") { - compendiumName = "foundryvtt-reve-de-dragon.competences-creatures"; - } else if (data.type == "entite") { - compendiumName = "foundryvtt-reve-de-dragon.competences-entites"; + data.items = await RdDUtility.loadCompendium(RdDItemCompetence.actorCompendium(data.type)); + if (isPersonnage) { + data.items = data.items.concat(Monnaie.monnaiesData()); } - if (compendiumName) { - data.items = await RdDUtility.loadCompendium(compendiumName); - } - // Ajout monnaie - if (data.type == "personnage" && data.items) { - await RdDActor.ajouterMonnaie(data.items); - } - return super.create(data, options); } @@ -151,41 +141,10 @@ 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 piece = items.find(item => item.type == 'monnaie' && Number(item.data.valeur_deniers) == 1); - let newMonnaie = []; - if (!piece) { - newMonnaie.push(RdDUtility.createMonnaie("Etain (1 denier)", 1, "systems/foundryvtt-reve-de-dragon/icons/objets/piece_etain_poisson.webp")); + let manquantes = Monnaie.monnaiesManquantes(items); + if (manquantes.length > 0) { + await this.createOwnedItem(manquantes); } - piece = items.find(item => item.type == 'monnaie' && Number(item.data.valeur_deniers) == 10); - if (!piece) { - newMonnaie.push(RdDUtility.createMonnaie("Bronze (10 deniers)", 10, "systems/foundryvtt-reve-de-dragon/icons/objets/piece_bronze_epees.webp")); - } - piece = items.find(item => item.type == 'monnaie' && Number(item.data.valeur_deniers) == 100); - if (!piece) { - newMonnaie.push(RdDUtility.createMonnaie("Argent (1 sol)", 100, "systems/foundryvtt-reve-de-dragon/icons/objets/piece_argent_sol.webp")); - } - piece = items.find(item => item.type == 'monnaie' && Number(item.data.valeur_deniers) == 1000); - if (!piece) { - newMonnaie.push(RdDUtility.createMonnaie("Or (10 sols)", 1000, "systems/foundryvtt-reve-de-dragon/icons/objets/piece_or_sol.webp")); - } - if (newMonnaie.length > 0) { - await this.createOwnedItem(newMonnaie); - } - } - - /* -------------------------------------------- */ - static async ajouterMonnaie(items) { // Creation auto à la création du personnage - let etain = RdDUtility.createMonnaie("Etain (1 denier)", 1, "systems/foundryvtt-reve-de-dragon/icons/objets/piece_etain_poisson.webp"); - items.push(etain); - - let bronze = RdDUtility.createMonnaie("Bronze (10 deniers)", 10, "systems/foundryvtt-reve-de-dragon/icons/objets/piece_bronze_epees.webp"); - items.push(bronze); - - let argent = RdDUtility.createMonnaie("Argent (1 sol)", 100, "systems/foundryvtt-reve-de-dragon/icons/objets/piece_argent_sol.webp"); - items.push(argent); - - let or = RdDUtility.createMonnaie("Or (10 sols)", 1000, "systems/foundryvtt-reve-de-dragon/icons/objets/piece_or_sol.webp"); - items.push(or); } /* -------------------------------------------- */ @@ -1774,7 +1733,7 @@ export class RdDActor extends Actor { if (!rollData.useMoral) return; if (rollData.rolled.isEchec || (rollData.ajustements.diviseurSignificative && (rollData.rolled.roll * rollData.ajustements.diviseurSignificative > rollData.score))) { - rollData.perteMoralEchec = rollData.moral<=-3? 'dissolution' : 'perte'; + rollData.perteMoralEchec = rollData.moral <= -3 ? 'dissolution' : 'perte'; rollData.moral = await this.moralIncDec(-1); /* L'appel au moral a échoué. Le personnage perd un point de moral */ } } @@ -2863,42 +2822,29 @@ export class RdDActor extends Actor { 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); + async optimizeArgent(sumDenier, monnaies) { - // 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); + let parValeur = Misc.classifyFirst(monnaies, it => it.data.valeur_deniers); + let fortune = { + 1000: Math.floor(sumDenier / 1000), // or + 100: Math.floor(sumDenier / 100) % 10, // argent + 10: Math.floor(sumDenier / 10) % 10, // bronze + 1: sumDenier % 10 // étain } - - 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); + for (const [valeur, nombre] of Object.entries(fortune)) { + let piece = parValeur[valeur]; + await this.updateEmbeddedEntity("OwnedItem", { _id: piece._id, 'data.quantite': nombre }); } } /* -------------------------------------------- */ async payerDenier(sumDenier, dataObj = undefined, quantite = 1) { + let monnaies = Monnaie.filtrerMonnaies(this.data.items); + if (monnaies.length < 4) { + ui.notifications.warn("Problème de monnaies manquantes, impossible de payer correctement!") + return; + } + sumDenier = Number(sumDenier); let denierDisponible = 0; @@ -2912,19 +2858,18 @@ export class RdDActor extends Actor { let isPayed = false; if (denierDisponible >= sumDenier) { denierDisponible -= sumDenier; - this.optimizeArgent(denierDisponible); + this.optimizeArgent(denierDisponible, monnaies); msg = `Vous avez payé ${sumDenier} Deniers, qui ont été soustraits de votre argent.`; RdDAudio.PlayContextAudio("argent"); // Petit son isPayed = true; + if (dataObj) { + dataObj.payload.data.cout = sumDenier / 100; // Mise à jour du prix en sols , avec le prix acheté + dataObj.payload.data.quantite = quantite; + await this.createOwnedItem(dataObj.payload); + msg += `
Et l'objet ${dataObj.payload.name} a été ajouté à votre inventaire.`; + } } else { - msg = "Vous n'avez pas assez d'argent pour paye cette somme !"; - } - - if (dataObj && isPayed) { - dataObj.payload.data.cout = sumDenier / 100; // Mise à jour du prix en sols , avec le prix acheté - dataObj.payload.data.quantite = quantite; - await this.createOwnedItem(dataObj.payload); - msg += `
Et l'objet ${dataObj.payload.name} a été ajouté à votre inventaire.`; + msg = "Vous n'avez pas assez d'argent pour payer cette somme !"; } let message = { @@ -2938,10 +2883,8 @@ export class RdDActor extends Actor { 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); + const quantite = Math.max(0, monnaie.data.quantite + value); + await this.updateEmbeddedEntity("OwnedItem", { _id: monnaie._id, 'data.quantite': quantite }); } } diff --git a/module/item-competence.js b/module/item-competence.js index c7be84c7..98a422f6 100644 --- a/module/item-competence.js +++ b/module/item-competence.js @@ -31,6 +31,13 @@ const categorieCompetences = { "lancer": { level: "-8", label: "Lancer" } } +const compendiumCompetences = { + "personnage": "foundryvtt-reve-de-dragon.competences", + "creature": "foundryvtt-reve-de-dragon.competences-creatures", + "entite": "foundryvtt-reve-de-dragon.competences-entites" +}; + + function _buildCumulXP() { let cumulXP = { "-11": 0 }; let cumul = 0; @@ -46,6 +53,10 @@ const competence_xp_cumul = _buildCumulXP(); export class RdDItemCompetence extends Item { + static actorCompendium(actorType) { + return compendiumCompetences[actorType]; + } + static getCategorieCompetences() { return categorieCompetences; } diff --git a/module/item-monnaie.js b/module/item-monnaie.js new file mode 100644 index 00000000..b0a76110 --- /dev/null +++ b/module/item-monnaie.js @@ -0,0 +1,40 @@ + +const monnaiesData = [ + { + _id: randomID(16), name: "Etain (1 denier)", type: 'monnaie', + img: "systems/foundryvtt-reve-de-dragon/icons/objets/piece_etain_poisson.webp", + data: { quantite: 0, valeur_deniers: 1, encombrement: 0.001, description: "" } + }, + { + _id: randomID(16), name: "Bronze (10 deniers)", type: 'monnaie', + img: "systems/foundryvtt-reve-de-dragon/icons/objets/piece_bronze_epees.webp", + data: { quantite: 0, valeur_deniers: 10, encombrement: 0.002, description: "" } + }, + { + _id: randomID(16), name: "Argent (1 sol)", type: 'monnaie', + img: "systems/foundryvtt-reve-de-dragon/icons/objets/piece_argent_sol.webp", + data: { quantite: 0, valeur_deniers: 100, encombrement: 0.003, description: "" } + }, + { + _id: randomID(16), name: "Or (10 sols)", type: 'monnaie', + img: "systems/foundryvtt-reve-de-dragon/icons/objets/piece_or_sol.webp", + data: { quantite: 0, valeur_deniers: 1000, encombrement: 0.004, description: "" } + } +] + +export class Monnaie { + + static monnaiesData() { + return monnaiesData; + } + + static filtrerMonnaies(items) { + return items.filter(it => it.type == 'monnaie'); + } + + static monnaiesManquantes(items) { + const valeurs = Monnaie.filtrerMonnaies(items) + .map(it => it.data.valeur_deniers) + return duplicate(monnaiesData.filter(monnaie => !valeurs.find(v => v != monnaie.data.valeur_deniers))); + } +} diff --git a/module/misc.js b/module/misc.js index f1220c35..8f972c7b 100644 --- a/module/misc.js +++ b/module/misc.js @@ -47,6 +47,17 @@ export class Misc { return itemsBy; } + static classifyFirst(items, classifier) { + let itemsBy = {}; + for (const item of items) { + const classification = classifier(item); + if (!itemsBy[classification]) { + itemsBy[classification] = item; + } + } + return itemsBy; + } + static classifyInto(itemsBy, items, classifier = it => it.type, transform = it => it) { for (const item of items) { const classification = classifier(item); diff --git a/module/rdd-utility.js b/module/rdd-utility.js index 939c2d4c..34573582 100644 --- a/module/rdd-utility.js +++ b/module/rdd-utility.js @@ -485,8 +485,10 @@ export class RdDUtility { /* -------------------------------------------- */ static async loadCompendium(compendium, filter = item => true) { + if (!compendium){ + return []; + } let compendiumItems = await RdDUtility.loadCompendiumNames(compendium); - const pack = game.packs.get(compendium); let list = []; for (let compendiumItem of compendiumItems) {