2022-11-28 11:31:19 +01:00
|
|
|
import { SYSTEM_RDD } from "./constants.js";
|
|
|
|
import { Grammar } from "./grammar.js";
|
|
|
|
import { Misc } from "./misc.js";
|
2022-12-03 01:24:56 +01:00
|
|
|
import { CompendiumTableHelpers, CompendiumTable } 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
|
|
|
|
|
|
|
const SETTINGS_LISTE_MILIEUX = "liste-milieux";
|
|
|
|
const MILIEUX = [
|
|
|
|
"Collines",
|
2022-12-03 01:03:15 +01:00
|
|
|
"Cours d'eau",
|
2022-11-28 11:31:19 +01:00
|
|
|
"Déserts",
|
|
|
|
"Forêts",
|
|
|
|
"Marais",
|
2022-12-02 21:20:37 +01:00
|
|
|
"Maritimes",
|
2022-11-28 11:31:19 +01:00
|
|
|
"Montagnes",
|
|
|
|
"Plaines",
|
2022-12-03 01:03:15 +01:00
|
|
|
"Sous-sols"
|
2022-11-28 11:31:19 +01:00
|
|
|
]
|
|
|
|
const ITEM_ENVIRONNEMENT_TYPES = [
|
2022-12-03 18:31:05 +01:00
|
|
|
'herbe', 'ingredient', 'faune'
|
2022-11-28 11:31:19 +01:00
|
|
|
]
|
|
|
|
|
|
|
|
export class Environnement {
|
|
|
|
|
|
|
|
static init() {
|
|
|
|
game.settings.register(SYSTEM_RDD, SETTINGS_LISTE_MILIEUX, {
|
|
|
|
name: "Liste des milieux proposés",
|
2022-11-30 01:11:31 +01:00
|
|
|
hint: "Liste des milieux proposés pour la faune&flore, séparés par des virgules",
|
2022-11-28 11:31:19 +01:00
|
|
|
scope: "world",
|
|
|
|
config: true,
|
|
|
|
default: MILIEUX.reduce(Misc.joining(',')),
|
|
|
|
type: String
|
|
|
|
});
|
|
|
|
game.system.rdd.environnement = new Environnement();
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
this.table = new CompendiumTable('faune-flore-mineraux', 'Item', ITEM_ENVIRONNEMENT_TYPES)
|
|
|
|
}
|
|
|
|
|
|
|
|
async milieux() {
|
2022-12-03 01:24:56 +01:00
|
|
|
return Object.values(await this.mapMilieux());
|
|
|
|
}
|
|
|
|
|
|
|
|
async mapMilieux() {
|
|
|
|
const compendiumItems = await this.getElements(it => 1, it => ITEM_ENVIRONNEMENT_TYPES.includes(it.type));
|
|
|
|
return Misc.indexLowercase(this.getMilieuxSettings().concat(Environnement.listMilieux(compendiumItems)));
|
|
|
|
}
|
|
|
|
|
|
|
|
static listMilieux(items) {
|
|
|
|
return Misc.concat(items.map(it => Environnement.$itemToMilieux(it).filter(m => m)));
|
|
|
|
}
|
|
|
|
|
|
|
|
async autresMilieux(item) {
|
|
|
|
const mapMilieux = await this.mapMilieux();
|
|
|
|
const milieuxExistants = Environnement.$itemToMilieux(item).map(it => Grammar.toLowerCaseNoAccent(it));
|
|
|
|
return Object.keys(mapMilieux)
|
|
|
|
.filter(it => !milieuxExistants.includes(it))
|
|
|
|
.map(it => mapMilieux[it]);
|
|
|
|
}
|
|
|
|
|
|
|
|
static $itemToMilieux(item) {
|
|
|
|
return item.system.environnement.map(env => env.milieu);
|
2022-11-28 11:31:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
getMilieuxSettings() {
|
2022-12-03 01:24:56 +01:00
|
|
|
return game.settings.get(SYSTEM_RDD, SETTINGS_LISTE_MILIEUX).split(',').map(it => it.trim()).filter(it => it != '');
|
2022-11-28 11:31:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async findEnvironnementsLike(search) {
|
2022-12-03 01:24:56 +01:00
|
|
|
const milieux = await this.mapMilieux();
|
|
|
|
const searchLower = Grammar.toLowerCaseNoAccent(search);
|
|
|
|
const keys = Object.keys(milieux).filter(it => it.includes(searchLower));
|
|
|
|
if (keys.length > 1) {
|
|
|
|
const milieuExact = milieux[searchLower];
|
2022-11-30 13:32:42 +01:00
|
|
|
if (milieuExact) {
|
|
|
|
return [milieuExact];
|
|
|
|
}
|
|
|
|
}
|
2022-12-03 01:24:56 +01:00
|
|
|
return keys.map(k => milieux[k]);
|
2022-11-28 11:31:19 +01:00
|
|
|
}
|
|
|
|
|
2022-11-30 13:32:42 +01:00
|
|
|
async searchToChatMessage(milieux, typeName) {
|
|
|
|
const table = await this.buildEnvironnementTable(milieux);
|
|
|
|
await CompendiumTableHelpers.tableToChatMessage(table, 'Item', ITEM_ENVIRONNEMENT_TYPES, typeName);
|
2022-11-28 11:31:19 +01:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2022-11-30 13:32:42 +01:00
|
|
|
async getRandom(milieux, typeName) {
|
|
|
|
const table = await this.buildEnvironnementTable(milieux);
|
|
|
|
return await CompendiumTableHelpers.getRandom(table, 'Item', ITEM_ENVIRONNEMENT_TYPES, undefined, typeName);
|
2022-11-28 11:31:19 +01:00
|
|
|
}
|
|
|
|
|
2023-01-13 04:54:29 +01:00
|
|
|
async buildEnvironnementTable(milieux, filter) {
|
|
|
|
if (!milieux){
|
|
|
|
milieux = await this.milieux()
|
2022-11-30 13:32:42 +01:00
|
|
|
}
|
2023-01-13 04:54:29 +01:00
|
|
|
const frequence = item => item.getRarete(milieux)?.frequence ?? 0;
|
|
|
|
return await this.table.buildTable(frequence, it => frequence(it) > 0 && filter(it));
|
2022-11-28 11:31:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async getElements(itemFrequence, filter) {
|
2022-11-30 01:11:31 +01:00
|
|
|
return await this.table.getContent(itemFrequence, filter);
|
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) {
|
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
|
|
|
}
|
|
|
|
|
|
|
|
}
|