Cleanup compendium et monnaies

This commit is contained in:
Vincent Vandemeulebrouck 2021-03-19 22:20:01 +01:00
parent c99ac0675a
commit 441259efb6
5 changed files with 102 additions and 95 deletions

View File

@ -26,6 +26,7 @@ import { Poetique } from "./poetique.js";
import { EffetsDraconiques } from "./tmr/effets-draconiques.js"; import { EffetsDraconiques } from "./tmr/effets-draconiques.js";
import { Draconique } from "./tmr/draconique.js"; import { Draconique } from "./tmr/draconique.js";
import { RdDCarac } from "./rdd-carac.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) { static async create(data, options) {
// Case of compendium global import // Case of compendium global import
if (data instanceof Array) { if (data instanceof Array) {
return super.create(data, options); 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 the created actor has items (only applicable to duplicated actors) bypass the new actor creation logic
if (data.items) { if (data.items) {
let actor = super.create(data, options); let actor = super.create(data, options);
if (data.type == "personnage") { if (isPersonnage) {
await actor.checkMonnaiePresence(data.items); await actor.checkMonnaiePresence(data.items);
} }
return actor; return actor;
} }
let compendiumName; data.items = await RdDUtility.loadCompendium(RdDItemCompetence.actorCompendium(data.type));
if (data.type == "personnage") { if (isPersonnage) {
compendiumName = "foundryvtt-reve-de-dragon.competences"; data.items = data.items.concat(Monnaie.monnaiesData());
} else if (data.type == "creature") {
compendiumName = "foundryvtt-reve-de-dragon.competences-creatures";
} else if (data.type == "entite") {
compendiumName = "foundryvtt-reve-de-dragon.competences-entites";
} }
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); 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. async checkMonnaiePresence(items) { // Ajout opportuniste si les pièces n'existent pas.
if (!items) return; // Sanity check during import if (!items) return; // Sanity check during import
let piece = items.find(item => item.type == 'monnaie' && Number(item.data.valeur_deniers) == 1); let manquantes = Monnaie.monnaiesManquantes(items);
let newMonnaie = []; if (manquantes.length > 0) {
if (!piece) { await this.createOwnedItem(manquantes);
newMonnaie.push(RdDUtility.createMonnaie("Etain (1 denier)", 1, "systems/foundryvtt-reve-de-dragon/icons/objets/piece_etain_poisson.webp"));
} }
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.useMoral) return;
if (rollData.rolled.isEchec || if (rollData.rolled.isEchec ||
(rollData.ajustements.diviseurSignificative && (rollData.rolled.roll * rollData.ajustements.diviseurSignificative > rollData.score))) { (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 */ 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; return;
} }
/* -------------------------------------------- */ /* -------------------------------------------- */
async optimizeArgent(sumDenier) { async optimizeArgent(sumDenier, monnaies) {
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 parValeur = Misc.classifyFirst(monnaies, it => it.data.valeur_deniers);
let piece = this.data.items.find(item => item.type == 'monnaie' && item.data.valeur_deniers == 1000); let fortune = {
if (piece) { 1000: Math.floor(sumDenier / 1000), // or
let update = { _id: piece._id, 'data.quantite': nbOr }; 100: Math.floor(sumDenier / 100) % 10, // argent
const updated = await this.updateEmbeddedEntity("OwnedItem", update); 10: Math.floor(sumDenier / 10) % 10, // bronze
1: sumDenier % 10 // étain
} }
for (const [valeur, nombre] of Object.entries(fortune)) {
piece = this.data.items.find(item => item.type == 'monnaie' && item.data.valeur_deniers == 100); let piece = parValeur[valeur];
if (piece) { await this.updateEmbeddedEntity("OwnedItem", { _id: piece._id, 'data.quantite': nombre });
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);
} }
} }
/* -------------------------------------------- */ /* -------------------------------------------- */
async payerDenier(sumDenier, dataObj = undefined, quantite = 1) { 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); sumDenier = Number(sumDenier);
let denierDisponible = 0; let denierDisponible = 0;
@ -2912,19 +2858,18 @@ export class RdDActor extends Actor {
let isPayed = false; let isPayed = false;
if (denierDisponible >= sumDenier) { if (denierDisponible >= sumDenier) {
denierDisponible -= sumDenier; denierDisponible -= sumDenier;
this.optimizeArgent(denierDisponible); this.optimizeArgent(denierDisponible, monnaies);
msg = `Vous avez payé <strong>${sumDenier} Deniers</strong>, qui ont été soustraits de votre argent.`; msg = `Vous avez payé <strong>${sumDenier} Deniers</strong>, qui ont été soustraits de votre argent.`;
RdDAudio.PlayContextAudio("argent"); // Petit son RdDAudio.PlayContextAudio("argent"); // Petit son
isPayed = true; 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 += `<br>Et l'objet <strong>${dataObj.payload.name}</strong> a été ajouté à votre inventaire.`;
}
} else { } else {
msg = "Vous n'avez pas assez d'argent pour paye cette somme !"; msg = "Vous n'avez pas assez d'argent pour payer 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 += `<br>Et l'objet <strong>${dataObj.payload.name}</strong> a été ajouté à votre inventaire.`;
} }
let message = { let message = {
@ -2938,10 +2883,8 @@ export class RdDActor extends Actor {
async monnaieIncDec(id, value) { async monnaieIncDec(id, value) {
let monnaie = this.data.items.find(item => item.type == 'monnaie' && item._id == id); let monnaie = this.data.items.find(item => item.type == 'monnaie' && item._id == id);
if (monnaie) { if (monnaie) {
monnaie.data.quantite += value; const quantite = Math.max(0, monnaie.data.quantite + value);
if (monnaie.data.quantite < 0) monnaie.data.quantite = 0; // Sanity check await this.updateEmbeddedEntity("OwnedItem", { _id: monnaie._id, 'data.quantite': quantite });
const update = { _id: monnaie._id, 'data.quantite': monnaie.data.quantite };
const updated = await this.updateEmbeddedEntity("OwnedItem", update);
} }
} }

View File

@ -31,6 +31,13 @@ const categorieCompetences = {
"lancer": { level: "-8", label: "Lancer" } "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() { function _buildCumulXP() {
let cumulXP = { "-11": 0 }; let cumulXP = { "-11": 0 };
let cumul = 0; let cumul = 0;
@ -46,6 +53,10 @@ const competence_xp_cumul = _buildCumulXP();
export class RdDItemCompetence extends Item { export class RdDItemCompetence extends Item {
static actorCompendium(actorType) {
return compendiumCompetences[actorType];
}
static getCategorieCompetences() { static getCategorieCompetences() {
return categorieCompetences; return categorieCompetences;
} }

40
module/item-monnaie.js Normal file
View File

@ -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)));
}
}

View File

@ -47,6 +47,17 @@ export class Misc {
return itemsBy; 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) { static classifyInto(itemsBy, items, classifier = it => it.type, transform = it => it) {
for (const item of items) { for (const item of items) {
const classification = classifier(item); const classification = classifier(item);

View File

@ -485,8 +485,10 @@ export class RdDUtility {
/* -------------------------------------------- */ /* -------------------------------------------- */
static async loadCompendium(compendium, filter = item => true) { static async loadCompendium(compendium, filter = item => true) {
if (!compendium){
return [];
}
let compendiumItems = await RdDUtility.loadCompendiumNames(compendium); let compendiumItems = await RdDUtility.loadCompendiumNames(compendium);
const pack = game.packs.get(compendium); const pack = game.packs.get(compendium);
let list = []; let list = [];
for (let compendiumItem of compendiumItems) { for (let compendiumItem of compendiumItems) {