2021-03-25 03:18:27 +01:00
|
|
|
import { Misc } from "./misc.js";
|
2022-10-07 19:05:56 +02:00
|
|
|
import { LOG_HEAD } from "./constants.js";
|
2021-03-19 22:20:01 +01:00
|
|
|
|
2022-10-07 19:05:56 +02:00
|
|
|
const MONNAIE_ETAIN = {
|
2022-12-17 17:26:11 +01:00
|
|
|
name: "Denier (étain)", type: 'monnaie',
|
2022-10-07 19:05:56 +02:00
|
|
|
img: "systems/foundryvtt-reve-de-dragon/icons/objets/piece_etain_poisson.webp",
|
2022-11-29 00:01:54 +01:00
|
|
|
system: { quantite: 0, cout: 0.01, encombrement: 0.001, description: "" }
|
2022-10-07 19:05:56 +02:00
|
|
|
};
|
|
|
|
const MONNAIE_BRONZE = {
|
2022-12-17 17:26:11 +01:00
|
|
|
name: "Sou (bronze)", type: 'monnaie',
|
2022-10-07 19:05:56 +02:00
|
|
|
img: "systems/foundryvtt-reve-de-dragon/icons/objets/piece_bronze_epees.webp",
|
2022-11-29 00:01:54 +01:00
|
|
|
system: { quantite: 0, cout: 0.10, encombrement: 0.002, description: "" }
|
2022-10-07 19:05:56 +02:00
|
|
|
};
|
|
|
|
const MONNAIE_ARGENT = {
|
2022-12-17 17:26:11 +01:00
|
|
|
name: "Sol (argent)", type: 'monnaie',
|
2022-10-07 19:05:56 +02:00
|
|
|
img: "systems/foundryvtt-reve-de-dragon/icons/objets/piece_argent_sol.webp",
|
2022-11-29 00:01:54 +01:00
|
|
|
system: { quantite: 0, cout: 1, encombrement: 0.003, description: "" }
|
2022-10-07 19:05:56 +02:00
|
|
|
};
|
|
|
|
const MONNAIE_OR = {
|
2022-12-17 17:26:11 +01:00
|
|
|
name: "Dreagon (or)", type: 'monnaie',
|
2022-10-07 19:05:56 +02:00
|
|
|
img: "systems/foundryvtt-reve-de-dragon/icons/objets/piece_or_sol.webp",
|
2022-11-29 00:01:54 +01:00
|
|
|
system: { quantite: 0, cout: 10, encombrement: 0.004, description: "" }
|
2022-10-07 19:05:56 +02:00
|
|
|
};
|
2021-03-19 22:20:01 +01:00
|
|
|
|
2022-10-07 19:05:56 +02:00
|
|
|
const MONNAIES_STANDARD = [MONNAIE_ETAIN, MONNAIE_BRONZE, MONNAIE_ARGENT, MONNAIE_OR];
|
2022-12-12 22:51:38 +01:00
|
|
|
const VALEUR_DENIERS = sols => Math.max(Math.floor((sols ?? 0) * 100), 0);
|
2021-03-19 22:20:01 +01:00
|
|
|
|
2022-10-07 19:05:56 +02:00
|
|
|
export class Monnaie {
|
2021-06-02 17:39:16 +02:00
|
|
|
|
2022-10-07 19:05:56 +02:00
|
|
|
static monnaiesStandard() {
|
2022-09-07 18:47:56 +02:00
|
|
|
return MONNAIES_STANDARD;
|
2021-03-19 22:20:01 +01:00
|
|
|
}
|
|
|
|
|
2022-09-19 20:16:16 +02: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)));
|
2022-09-16 02:22:09 +02:00
|
|
|
if (manquantes.length > 0) {
|
2022-09-19 20:16:16 +02:00
|
|
|
console.error(`${LOG_HEAD} monnaiesManquantes pour ${actor.name}`, manquantes, ' avec monnaies', disponibles, MONNAIES_STANDARD);
|
2022-09-07 18:47:56 +02:00
|
|
|
}
|
2022-09-16 02:22:09 +02:00
|
|
|
return manquantes;
|
2021-03-25 03:18:27 +01:00
|
|
|
}
|
|
|
|
|
2022-09-07 18:47:56 +02:00
|
|
|
static deValeur(monnaie, valeur) {
|
2022-12-12 22:51:38 +01:00
|
|
|
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
|
|
|
|
2022-11-29 00:01:54 +01:00
|
|
|
static triValeurEntiere() {
|
2022-12-12 22:51:38 +01:00
|
|
|
return Misc.ascending(item => VALEUR_DENIERS(item.system.cout))
|
2021-06-05 02:05:51 +02:00
|
|
|
}
|
2022-10-07 19:05:56 +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;
|
|
|
|
}
|
|
|
|
|
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);
|
2022-12-18 19:16:23 +01:00
|
|
|
}
|
|
|
|
|
2022-11-29 00:01:54 +01:00
|
|
|
static async optimiserFortune(actor, fortune) {
|
2022-12-12 22:51:38 +01:00
|
|
|
let resteEnDeniers = Math.round(fortune * 100);
|
2022-10-07 19:05:56 +02:00
|
|
|
let monnaies = actor.itemTypes['monnaie'];
|
|
|
|
let updates = [];
|
2022-12-28 23:36:48 +01:00
|
|
|
Monnaie.validerMonnaies(monnaies, actor);
|
2022-12-12 22:51:38 +01:00
|
|
|
|
|
|
|
let parValeur = Misc.classifyFirst(monnaies, it => VALEUR_DENIERS(it.system.cout));
|
|
|
|
for (let valeurDeniers of [1000, 100, 10, 1]) {
|
2022-11-29 00:01:54 +01:00
|
|
|
const itemPiece = parValeur[valeurDeniers];
|
2022-10-14 00:43:37 +02:00
|
|
|
if (itemPiece) {
|
2022-11-29 00:01:54 +01:00
|
|
|
const quantite = Math.floor(resteEnDeniers / valeurDeniers);
|
2022-11-16 02:53:38 +01:00
|
|
|
if (quantite != itemPiece.system.quantite) {
|
2022-11-29 00:01:54 +01:00
|
|
|
updates.push({ _id: parValeur[valeurDeniers].id, 'system.quantite': quantite });
|
2022-10-14 00:43:37 +02:00
|
|
|
}
|
2022-12-12 22:51:38 +01:00
|
|
|
resteEnDeniers -= quantite * valeurDeniers;
|
2022-10-07 19:05:56 +02:00
|
|
|
}
|
|
|
|
}
|
2022-11-29 00:01:54 +01:00
|
|
|
console.log('Monnaie.optimiserFortune', actor.name, 'total', fortune, 'parValeur', parValeur, 'updates', updates, 'reste', resteEnDeniers);
|
2022-10-07 19:05:56 +02:00
|
|
|
if (updates.length > 0) {
|
|
|
|
await actor.updateEmbeddedDocuments('Item', updates);
|
|
|
|
}
|
2022-12-12 22:51:38 +01:00
|
|
|
if (resteEnDeniers > 0) {
|
2022-10-07 19:05:56 +02:00
|
|
|
// créer le reste en deniers fortune en deniers
|
2022-11-29 00:01:54 +01:00
|
|
|
await Monnaie.creerMonnaiesDeniers(actor, resteEnDeniers);
|
2022-10-07 19:05:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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!`)
|
2022-12-12 22:51:38 +01:00
|
|
|
.forEach(message => {
|
|
|
|
ui.notifications.warn(message);
|
|
|
|
console.warn(message);
|
|
|
|
});
|
|
|
|
}
|
2021-03-19 22:20:01 +01:00
|
|
|
}
|