foundryvtt-reve-de-dragon/module/item-monnaie.js

119 lines
4.1 KiB
JavaScript
Raw Normal View History

2021-03-25 03:18:27 +01:00
import { Misc } from "./misc.js";
import { LOG_HEAD } from "./constants.js";
2021-03-19 22:20:01 +01:00
const MONNAIE_ETAIN = {
name: "Denier (étain)", type: 'monnaie',
img: "systems/foundryvtt-reve-de-dragon/icons/objets/piece_etain_poisson.webp",
system: { quantite: 0, cout: 0.01, encombrement: 0.001, description: "" }
};
const MONNAIE_BRONZE = {
name: "Sou (bronze)", type: 'monnaie',
img: "systems/foundryvtt-reve-de-dragon/icons/objets/piece_bronze_epees.webp",
system: { quantite: 0, cout: 0.10, encombrement: 0.002, description: "" }
};
const MONNAIE_ARGENT = {
name: "Sol (argent)", type: 'monnaie',
img: "systems/foundryvtt-reve-de-dragon/icons/objets/piece_argent_sol.webp",
system: { quantite: 0, cout: 1, encombrement: 0.003, description: "" }
};
const MONNAIE_OR = {
name: "Dreagon (or)", type: 'monnaie',
img: "systems/foundryvtt-reve-de-dragon/icons/objets/piece_or_sol.webp",
system: { quantite: 0, cout: 10, encombrement: 0.004, description: "" }
};
2021-03-19 22:20:01 +01:00
const MONNAIES_STANDARD = [MONNAIE_ETAIN, MONNAIE_BRONZE, MONNAIE_ARGENT, MONNAIE_OR];
const VALEUR_DENIERS = sols => Math.max(Math.floor((sols ?? 0) * 100), 0);
2021-03-19 22:20:01 +01:00
export class Monnaie {
2021-06-02 17:39:16 +02:00
static monnaiesStandard() {
return MONNAIES_STANDARD;
2021-03-19 22:20:01 +01:00
}
static monnaiesManquantes(actor) {
const disponibles = actor.itemTypes['monnaie'];
2022-11-28 11:31:55 +01:00
const manquantes = MONNAIES_STANDARD.filter(standard => !disponibles.find(disponible => Monnaie.deValeur(disponible, standard.system?.cout)));
if (manquantes.length > 0) {
console.error(`${LOG_HEAD} monnaiesManquantes pour ${actor.name}`, manquantes, ' avec monnaies', disponibles, MONNAIES_STANDARD);
}
return manquantes;
2021-03-25 03:18:27 +01:00
}
static deValeur(monnaie, valeur) {
return VALEUR_DENIERS(valeur) == VALEUR_DENIERS(monnaie.system.cout)
2021-05-07 17:27:02 +02:00
}
2021-06-05 02:05:51 +02:00
static triValeurEntiere() {
return Misc.ascending(item => VALEUR_DENIERS(item.system.cout))
2021-06-05 02:05:51 +02:00
}
static async creerMonnaiesStandard(actor) {
await actor.createEmbeddedDocuments('Item', MONNAIES_STANDARD, { renderSheet: false });
}
static async creerMonnaiesDeniers(actor, fortune) {
await actor.createEmbeddedDocuments('Item', [Monnaie.creerDeniers(fortune)], { renderSheet: false });
}
static creerDeniers(fortune) {
const deniers = duplicate(MONNAIE_ETAIN);
deniers.system.quantite = fortune;
return deniers;
}
static getFortune(actor) {
if (actor) {
Monnaie.validerMonnaies(actor);
return actor.itemTypes['monnaie']
.map(m => Number(m.system.cout) * Number(m.system.quantite))
.reduce(Misc.sum(), 0);
}
return 0;
}
static getFortuneSolsDeniers(actor) {
const fortune = Monnaie.getFortune(actor);
return {
sols: Math.floor(fortune),
deniers: Math.round(100 * (fortune - Math.floor(fortune)))
};
}
static async optimiserFortune(actor, fortune) {
let resteEnDeniers = Math.round(fortune * 100);
let monnaies = actor.itemTypes['monnaie'];
let updates = [];
Monnaie.validerMonnaies(actor);
let parValeur = Misc.classifyFirst(monnaies, it => VALEUR_DENIERS(it.system.cout));
for (let valeurDeniers of [1000, 100, 10, 1]) {
const itemPiece = parValeur[valeurDeniers];
if (itemPiece) {
const quantite = Math.floor(resteEnDeniers / valeurDeniers);
2022-11-16 02:53:38 +01:00
if (quantite != itemPiece.system.quantite) {
updates.push({ _id: parValeur[valeurDeniers].id, 'system.quantite': quantite });
}
resteEnDeniers -= quantite * valeurDeniers;
}
}
console.log('Monnaie.optimiserFortune', actor.name, 'total', fortune, 'parValeur', parValeur, 'updates', updates, 'reste', resteEnDeniers);
if (updates.length > 0) {
await actor.updateEmbeddedDocuments('Item', updates);
}
if (resteEnDeniers > 0) {
// créer le reste en deniers fortune en deniers
await Monnaie.creerMonnaiesDeniers(actor, resteEnDeniers);
}
}
static validerMonnaies(actor) {
actor.itemTypes['monnaie'].filter(it => VALEUR_DENIERS(it.system.cout) == 0)
.map(it => `La monnaie ${it.name} de l'acteur ${actor.name} a une valeur de 0!`)
.forEach(message => {
ui.notifications.warn(message);
console.warn(message);
});
}
2021-03-19 22:20:01 +01:00
}