2022-11-28 11:31:19 +01:00
|
|
|
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";
|
2022-11-28 11:31:19 +01:00
|
|
|
|
|
|
|
|
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 {
|
2024-10-14 03:46:09 +02:00
|
|
|
static initSettings() {
|
2023-01-18 00:11:10 +01:00
|
|
|
game.settings.register(SYSTEM_RDD, COMPENDIUMS_RECHERCHE, {
|
|
|
|
name: COMPENDIUMS_RECHERCHE,
|
|
|
|
default: [
|
|
|
|
SystemCompendiums.getCompendium('faune-flore-mineraux'),
|
2023-01-20 03:25:40 +01:00
|
|
|
SystemCompendiums.getCompendium('meditations-et-ecrits'),
|
2023-01-18 00:11:10 +01:00
|
|
|
SystemCompendiums.getCompendium('equipement')
|
|
|
|
],
|
2022-11-28 11:31:19 +01:00
|
|
|
scope: "world",
|
2023-01-18 00:11:10 +01:00
|
|
|
config: false,
|
|
|
|
type: Object
|
2022-11-28 11:31:19 +01:00
|
|
|
});
|
2023-01-18 00:11:10 +01:00
|
|
|
|
2022-11-28 11:31:19 +01:00
|
|
|
game.system.rdd.environnement = new Environnement();
|
2023-01-18 00:11:10 +01:00
|
|
|
Hooks.once('ready', () => game.system.rdd.environnement.onReady());
|
2022-11-28 11:31:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
constructor() {
|
2023-01-18 00:11:10 +01:00
|
|
|
this.compendiums = [];
|
|
|
|
this.compendiumTables = [];
|
|
|
|
this.mapMilieux = {}
|
|
|
|
}
|
|
|
|
|
|
|
|
async onReady() {
|
|
|
|
await this.$prepareCompendiums()
|
2022-11-28 11:31:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async milieux() {
|
2023-01-18 00:11:10 +01:00
|
|
|
return Object.values(this.mapMilieux);
|
2022-12-03 01:24:56 +01:00
|
|
|
}
|
|
|
|
|
2023-01-18 00:11:10 +01:00
|
|
|
async saveCompendiums(compendiumIds) {
|
|
|
|
game.settings.set(SYSTEM_RDD, COMPENDIUMS_RECHERCHE, compendiumIds);
|
|
|
|
await this.$prepareCompendiums();
|
2022-12-03 01:24:56 +01:00
|
|
|
}
|
|
|
|
|
2023-01-18 00:11:10 +01:00
|
|
|
async $prepareCompendiums() {
|
2023-01-18 01:37:22 +01:00
|
|
|
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);
|
2022-12-03 01:24:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2022-12-03 01:24:56 +01:00
|
|
|
.filter(it => !milieuxExistants.includes(it))
|
2023-01-18 00:11:10 +01:00
|
|
|
.map(it => this.mapMilieux[it]);
|
2022-11-28 11:31:19 +01:00
|
|
|
}
|
|
|
|
|
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))
|
|
|
|
);
|
2023-01-20 01:39:31 +01:00
|
|
|
const elements = compendiumsElement.reduce((a, b) => a.concat(b));
|
|
|
|
elements.sort(Misc.ascending(it => it.name))
|
|
|
|
return elements;
|
2022-11-28 11:31:19 +01:00
|
|
|
}
|
|
|
|
|
2023-01-18 00:11:10 +01:00
|
|
|
async buildTable(itemFrequence, filter = it => true) {
|
|
|
|
if (!itemFrequence) {
|
|
|
|
itemFrequence = it => it.getFrequence()
|
2022-11-30 13:32:42 +01:00
|
|
|
}
|
2023-01-18 00:11:10 +01:00
|
|
|
const elements = await this.getElements(itemFrequence, filter);;
|
|
|
|
return CompendiumTableHelpers.buildTable(elements, itemFrequence);
|
2022-11-28 11:31:19 +01:00
|
|
|
}
|
|
|
|
}
|