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

114 lines
4.0 KiB
JavaScript
Raw Permalink 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 = {
2023-01-15 12:42:41 +01:00
name: "Dragon (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) {
2024-05-02 14:08:02 +02:00
const deniers = foundry.utils.duplicate(MONNAIE_ETAIN);
deniers.system.quantite = fortune;
return deniers;
}
2023-01-28 16:35:45 +01:00
static toSolsDeniers(fortune) {
return {
sols: Math.floor(fortune),
deniers: Math.round(100 * (fortune - Math.floor(fortune)))
};
}
2022-12-28 23:36:48 +01:00
static getFortune(monnaies) {
return (monnaies??[])
.map(m => Number(m.system.cout) * Number(m.system.quantite))
.reduce(Misc.sum(), 0);
}
static async optimiserFortune(actor, fortune) {
let resteEnDeniers = Math.round(fortune * 100);
let monnaies = actor.itemTypes['monnaie'];
let updates = [];
2022-12-28 23:36:48 +01:00
Monnaie.validerMonnaies(monnaies, 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);
}
}
2022-12-28 23:36:48 +01:00
static validerMonnaies(monnaies, actor = undefined) {
monnaies.filter(it => VALEUR_DENIERS(it.system.cout) == 0)
.map(it => `La monnaie ${it.name} de l'acteur ${actor?.name ?? 'sélectionné'} a une valeur de 0!`)
.forEach(message => {
ui.notifications.warn(message);
console.warn(message);
});
}
2021-03-19 22:20:01 +01:00
}