foundryvtt-reve-de-dragon/module/environnement.js

76 lines
2.4 KiB
JavaScript
Raw Normal View History

import { SYSTEM_RDD } from "./constants.js";
import { Grammar } from "./grammar.js";
import { Misc } from "./misc.js";
2023-01-18 00:11:10 +01:00
import { CompendiumTableHelpers, CompendiumTable, SystemCompendiums } from "./settings/system-compendiums.js";
2023-01-18 00:11:10 +01:00
const COMPENDIUMS_RECHERCHE = 'compendiums-recherche';
2023-01-15 15:54:40 +01:00
2023-01-18 00:11:10 +01:00
export class Environnement {
static init() {
2023-01-18 00:11:10 +01:00
game.settings.register(SYSTEM_RDD, COMPENDIUMS_RECHERCHE, {
name: COMPENDIUMS_RECHERCHE,
default: [
SystemCompendiums.getCompendium('faune-flore-mineraux'),
SystemCompendiums.getCompendium('equipement')
],
scope: "world",
2023-01-18 00:11:10 +01:00
config: false,
type: Object
});
2023-01-18 00:11:10 +01:00
game.system.rdd.environnement = new Environnement();
2023-01-18 00:11:10 +01:00
Hooks.once('ready', () => game.system.rdd.environnement.onReady());
}
constructor() {
2023-01-18 00:11:10 +01:00
this.compendiums = [];
this.compendiumTables = [];
this.mapMilieux = {}
}
async onReady() {
await this.$prepareCompendiums()
}
async milieux() {
2023-01-18 00:11:10 +01:00
return Object.values(this.mapMilieux);
}
2023-01-18 00:11:10 +01:00
async saveCompendiums(compendiumIds) {
game.settings.set(SYSTEM_RDD, COMPENDIUMS_RECHERCHE, compendiumIds);
await this.$prepareCompendiums();
}
2023-01-18 00:11:10 +01:00
async $prepareCompendiums() {
this.compendiums = game.settings.get(SYSTEM_RDD, COMPENDIUMS_RECHERCHE).filter(c => SystemCompendiums.getPack(c));
2023-01-18 00:11:10 +01:00
this.compendiumTables = this.compendiums.map(it => new CompendiumTable(it, 'Item'));
const compendiumItems = await this.getElements(it => 1, it => it.isInventaire());
const fromCompendiums = Misc.concat(compendiumItems.map(it => it.getMilieux().filter(m => m)));
this.mapMilieux = Misc.indexLowercase(fromCompendiums);
}
async autresMilieux(item) {
2023-01-18 00:11:10 +01:00
const milieuxExistants = item.getMilieux().map(it => Grammar.toLowerCaseNoAccent(it));
return Object.keys(this.mapMilieux)
.filter(it => !milieuxExistants.includes(it))
2023-01-18 00:11:10 +01:00
.map(it => this.mapMilieux[it]);
}
2023-01-18 00:11:10 +01:00
async getElements(itemFrequence, filter) {
const compendiumsElement = await Promise.all(
this.compendiumTables.map(async compTable => await compTable.getContent(itemFrequence, filter))
);
return compendiumsElement.reduce((a, b) => a.concat(b))
}
2023-01-18 00:11:10 +01:00
async buildTable(itemFrequence, filter = it => true) {
if (!itemFrequence) {
itemFrequence = it => it.getFrequence()
}
2023-01-18 00:11:10 +01:00
const elements = await this.getElements(itemFrequence, filter);;
return CompendiumTableHelpers.buildTable(elements, itemFrequence);
}
}