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 = {
|
|
|
|
name: "Etain (1 denier)", type: 'monnaie',
|
|
|
|
img: "systems/foundryvtt-reve-de-dragon/icons/objets/piece_etain_poisson.webp",
|
|
|
|
system: { quantite: 0, valeur_deniers: 1, encombrement: 0.001, description: "" }
|
|
|
|
};
|
|
|
|
const MONNAIE_BRONZE = {
|
|
|
|
name: "Bronze (10 deniers)", type: 'monnaie',
|
|
|
|
img: "systems/foundryvtt-reve-de-dragon/icons/objets/piece_bronze_epees.webp",
|
|
|
|
system: { quantite: 0, valeur_deniers: 10, encombrement: 0.002, description: "" }
|
|
|
|
};
|
|
|
|
const MONNAIE_ARGENT = {
|
|
|
|
name: "Argent (1 sol)", type: 'monnaie',
|
|
|
|
img: "systems/foundryvtt-reve-de-dragon/icons/objets/piece_argent_sol.webp",
|
|
|
|
system: { quantite: 0, valeur_deniers: 100, encombrement: 0.003, description: "" }
|
|
|
|
};
|
|
|
|
const MONNAIE_OR = {
|
|
|
|
name: "Or (10 sols)", type: 'monnaie',
|
|
|
|
img: "systems/foundryvtt-reve-de-dragon/icons/objets/piece_or_sol.webp",
|
|
|
|
system: { quantite: 0, valeur_deniers: 1000, encombrement: 0.004, description: "" }
|
|
|
|
};
|
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];
|
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-09-07 18:47:56 +02:00
|
|
|
const manquantes = MONNAIES_STANDARD.filter(standard => !disponibles.find(disponible => Monnaie.deValeur(disponible, standard.system?.valeur_deniers)));
|
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) {
|
|
|
|
return valeur == monnaie.system.valeur_deniers
|
2021-03-19 22:20:01 +01:00
|
|
|
}
|
2021-05-07 17:27:02 +02:00
|
|
|
|
|
|
|
static arrondiDeniers(sols) {
|
2022-10-07 19:05:56 +02:00
|
|
|
return Number(sols).toFixed(2);
|
2021-05-07 17:27:02 +02:00
|
|
|
}
|
2021-06-05 02:05:51 +02:00
|
|
|
|
|
|
|
static triValeurDenier() {
|
2022-05-01 00:38:09 +02:00
|
|
|
return Misc.ascending(item => item.system.valeur_deniers)
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
static async optimiser(actor, fortune) {
|
|
|
|
let reste = fortune;
|
|
|
|
let monnaies = actor.itemTypes['monnaie'];
|
|
|
|
let updates = [];
|
|
|
|
let parValeur = Misc.classifyFirst(monnaies, it => it.system.valeur_deniers);
|
|
|
|
for (let valeur of [1000, 100, 10, 1]) {
|
|
|
|
if (parValeur[valeur]) {
|
|
|
|
const piecesDeCetteValeur = Math.floor(reste / valeur);
|
|
|
|
updates.push({ _id: parValeur[valeur].id, 'system.quantite': piecesDeCetteValeur });
|
|
|
|
reste -= piecesDeCetteValeur*valeur;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
console.log('Monnaie.optimiser', actor.name, 'total', fortune, 'parValeur', parValeur, 'updates', updates, 'reste', reste);
|
|
|
|
if (updates.length > 0) {
|
|
|
|
await actor.updateEmbeddedDocuments('Item', updates);
|
|
|
|
}
|
|
|
|
if (reste>0){
|
|
|
|
// créer le reste en deniers fortune en deniers
|
|
|
|
await Monnaie.creerMonnaiesDeniers(actor, reste);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-19 22:20:01 +01:00
|
|
|
}
|