2022-11-28 11:31:19 +01:00
|
|
|
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";
|
2022-11-28 11:31:19 +01:00
|
|
|
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";
|
2022-11-28 11:31:19 +01:00
|
|
|
|
|
|
|
|
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 {
|
2022-11-28 11:31:19 +01:00
|
|
|
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')
|
|
|
|
],
|
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() {
|
|
|
|
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);
|
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))
|
|
|
|
);
|
|
|
|
return compendiumsElement.reduce((a, b) => a.concat(b))
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class EnvironmentSheetHelper {
|
2022-12-03 18:29:29 +01:00
|
|
|
static defaultOptions(defaultOptions) {
|
2022-11-28 11:31:19 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* -------------------------------------------- */
|
2022-12-03 01:24:56 +01:00
|
|
|
static async getData(sheet, formData) {
|
2022-12-03 18:29:29 +01:00
|
|
|
return mergeObject(formData, {
|
|
|
|
milieux: await game.system.rdd.environnement.autresMilieux(sheet.item)
|
2022-11-28 11:31:19 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-12-09 02:00:31 +01:00
|
|
|
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));
|
|
|
|
|
2022-11-28 11:31:19 +01:00
|
|
|
if (!sheet.options.editable) return;
|
2022-12-03 22:01:06 +01:00
|
|
|
|
2022-12-09 02:00:31 +01:00
|
|
|
sheet.html.find("input.input-selection-milieu").keypress(event => {
|
2022-12-02 21:21:21 +01:00
|
|
|
if (event.keyCode == '13') {
|
2022-12-09 02:00:31 +01:00
|
|
|
EnvironmentSheetHelper.onAddMilieu(sheet, event);
|
2022-12-02 21:21:21 +01:00
|
|
|
}
|
|
|
|
event.stopPropagation();
|
|
|
|
})
|
2022-12-09 02:00:31 +01:00
|
|
|
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)));
|
2022-12-02 21:21:21 +01:00
|
|
|
}
|
|
|
|
|
2022-12-09 02:00:31 +01:00
|
|
|
static $changeFrequence(sheet, event, updated) {
|
|
|
|
updated.frequence = Number(sheet.html.find(event.currentTarget).val());
|
2022-12-02 21:21:21 +01:00
|
|
|
}
|
|
|
|
|
2022-12-09 02:00:31 +01:00
|
|
|
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;
|
2022-12-03 01:24:56 +01:00
|
|
|
updated.frequence = rarete.frequence;
|
2022-11-28 11:31:19 +01:00
|
|
|
}
|
|
|
|
|
2022-12-09 02:00:31 +01:00
|
|
|
static async onAddMilieu(sheet, event) {
|
|
|
|
const milieu = sheet.html.find('input.input-selection-milieu').val();
|
2022-11-28 11:31:19 +01:00
|
|
|
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))
|
2022-11-28 11:31:19 +01:00
|
|
|
await sheet.item.update({ 'system.environnement': newList })
|
|
|
|
}
|
|
|
|
|
|
|
|
static async onDeleteMilieu(sheet, event) {
|
2022-12-09 12:01:44 +01:00
|
|
|
const milieu = EnvironmentSheetHelper.$getEventMilieu(sheet, event);
|
2022-12-02 21:21:21 +01:00
|
|
|
if (milieu != undefined) {
|
2022-11-28 11:31:19 +01:00
|
|
|
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;
|
2022-12-09 02:00:31 +01:00
|
|
|
const milieu = EnvironmentSheetHelper.$getEventMilieu(sheet, event);
|
2022-11-28 11:31:19 +01:00
|
|
|
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 });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-09 02:00:31 +01:00
|
|
|
static $getEventMilieu(sheet, event) {
|
|
|
|
return sheet.html.find(event.currentTarget)?.parents("div.environnement-milieu").data("milieu");
|
2022-11-28 11:31:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|