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

173 lines
6.5 KiB
JavaScript
Raw Normal View History

import { SYSTEM_RDD } from "./constants.js";
import { Grammar } from "./grammar.js";
2023-01-18 00:11:10 +01:00
import { HtmlUtility } from "./html-utility.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-13 04:54:29 +01:00
import { RdDRaretes } from "./tirage/raretes.js";
2023-01-18 00:11:10 +01:00
const COMPENDIUMS_RECHERCHE = 'compendiums-recherche';
const TYPE_ITEMS_NATURELS = ["faune", "herbe", "plante", "ingredient"];
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);
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);
}
}
export class EnvironmentSheetHelper {
static defaultOptions(defaultOptions) {
return mergeObject(defaultOptions, {
tabs: [{ navSelector: ".sheet-tabs", contentSelector: ".sheet-body", initial: "informations" }]
});
}
static setPosition(sheet, superPosition) {
const position = superPosition;
const sheetHeader = sheet.element.find(".sheet-header");
const sheetBody = sheet.element.find(".sheet-body");
sheetBody.css("height", position.height - sheetHeader[0].clientHeight)
return position;
}
/* -------------------------------------------- */
static async getData(sheet, formData) {
return mergeObject(formData, {
milieux: await game.system.rdd.environnement.autresMilieux(sheet.item)
});
}
static activateListeners(sheet) {
2023-01-18 00:11:10 +01:00
HtmlUtility.showControlWhen(sheet.html.find("div.description-milieu"), TYPE_ITEMS_NATURELS.includes(sheet.item.type));
if (!sheet.options.editable) return;
2022-12-03 22:01:06 +01:00
sheet.html.find("input.input-selection-milieu").keypress(event => {
if (event.keyCode == '13') {
EnvironmentSheetHelper.onAddMilieu(sheet, event);
}
event.stopPropagation();
})
sheet.html.find("a.milieu-add").click(event => EnvironmentSheetHelper.onAddMilieu(sheet, event));
sheet.html.find("div.environnement-milieu a.milieu-delete").click(event => EnvironmentSheetHelper.onDeleteMilieu(sheet, event));
sheet.html.find("div.environnement-milieu select.environnement-rarete").change(event => EnvironmentSheetHelper.onChange(sheet, event,
updated => EnvironmentSheetHelper.$changeRarete(sheet, event, updated)));
sheet.html.find("div.environnement-milieu input[name='environnement-frequence']").change(event => EnvironmentSheetHelper.onChange(sheet, event,
updated => EnvironmentSheetHelper.$changeFrequence(sheet, event, updated)));
}
static $changeFrequence(sheet, event, updated) {
updated.frequence = Number(sheet.html.find(event.currentTarget).val());
}
static $changeRarete(sheet, event, updated) {
const name = sheet.html.find(event.currentTarget).val();
2023-01-13 04:54:29 +01:00
const rarete = RdDRaretes.getRarete(name);
updated.rarete = rarete.code;
updated.frequence = rarete.frequence;
}
static async onAddMilieu(sheet, event) {
const milieu = sheet.html.find('input.input-selection-milieu').val();
if (!milieu) {
ui.notifications.warn(`Choisissez le milieu dans lequel se trouve le/la ${sheet.item.name}`);
return
}
const list = sheet.item.system.environnement;
const exists = list.find(it => it.milieu == milieu);
if (exists) {
ui.notifications.warn(`${sheet.item.name} a déjà une rareté ${exists.rarete} en ${milieu} (fréquence: ${exists.frequence})`);
return
}
2023-01-13 04:54:29 +01:00
const rarete = RdDRaretes.getRarete();
const newList = [...list, { milieu, rarete: rarete.code, frequence: rarete.frequence }].sort(Misc.ascending(it => it.milieu))
await sheet.item.update({ 'system.environnement': newList })
}
static async onDeleteMilieu(sheet, event) {
const milieu = EnvironmentSheetHelper.$getEventMilieu(sheet, event);
if (milieu != undefined) {
const newList = sheet.item.system.environnement.filter(it => it.milieu != milieu)
.sort(Misc.ascending(it => it.milieu));
await sheet.item.update({ 'system.environnement': newList });
}
}
static async onChange(sheet, event, doMutation) {
const list = sheet.item.system.environnement;
const milieu = EnvironmentSheetHelper.$getEventMilieu(sheet, event);
const updated = list.find(it => it.milieu == milieu);
if (updated) {
doMutation(updated);
const newList = [...list.filter(it => it.milieu != milieu), updated]
.sort(Misc.ascending(it => it.milieu));
await sheet.item.update({ 'system.environnement': newList });
}
}
static $getEventMilieu(sheet, event) {
return sheet.html.find(event.currentTarget)?.parents("div.environnement-milieu").data("milieu");
}
}