foundryvtt-reve-de-dragon/module/tirage/fenetre-recherche-tirage.js

332 lines
14 KiB
JavaScript
Raw Normal View History

2023-01-13 04:54:29 +01:00
import { RdDItem } from '../item.js';
import { HtmlUtility } from '../html-utility.js';
import { Misc } from "../misc.js";
import { CompendiumTable, CompendiumTableHelpers, SystemCompendiums } from '../settings/system-compendiums.js';
import { RdDRaretes } from './raretes.js';
import { SYSTEM_RDD } from '../constants.js';
2023-01-13 04:54:29 +01:00
const FILTER_GROUPS = [
{ group: 'type', label: "Type d'objet" },
{ group: 'comestible', label: 'Alimentaire' },
{ group: 'categorie', label: 'Utilisation' },
{ group: 'milieu', label: 'Milieu' },
{ group: 'rarete', label: 'Rarete' },
{ group: 'qualite', label: 'Qualité' },
{ group: 'enc', label: 'Encombrement' },
{ group: 'prix', label: 'Prix' },
]
const FILTERS = [
2023-01-15 15:54:40 +01:00
{ group: 'comestible', code: 'pret', label: 'Préparé', check: item => item.getUtilisationCuisine() == 'pret' },
{ group: 'comestible', code: 'comestible', label: 'Comestible', check: item => item.getUtilisation() == 'cuisine' },
2023-01-13 04:54:29 +01:00
{ group: 'comestible', code: 'boisson', label: 'Boisson', check: item => item.isNourritureBoisson() && item.system.boisson },
2023-01-15 15:54:40 +01:00
{ group: 'comestible', code: 'alcool', label: 'Alcool', check: item => item.isAlcool() },
{ group: 'comestible', code: 'brut', label: 'A préparer', check: item => item.getUtilisationCuisine() == 'brut' },
{ group: 'comestible', code: 'non', label: 'Immangeable', check: item => item.isEnvironnement() && !item.getUtilisationCuisine() },
2023-01-13 04:54:29 +01:00
2023-01-15 15:54:40 +01:00
{ group: 'categorie', code: 'alchimie', label: 'Alchimique', check: item => item.isEnvironnement() && item.getUtilisation() == 'alchimie' },
{ group: 'categorie', code: 'cuisine', label: 'Cuisine', check: item => item.isEnvironnement() && item.getUtilisation() == 'cuisine' },
{ group: 'categorie', code: 'soins', label: 'Médical', check: item => item.isEnvironnement() && item.getUtilisation() == 'soins' },
{ group: 'categorie', code: 'cpoison', label: 'Toxique', check: item => item.isEnvironnement() && item.getUtilisation() == 'poison' },
{ group: 'categorie', code: 'autres', label: 'Autres', check: item => !item.isEnvironnement() || item.getUtilisation() == '' },
2023-01-13 04:54:29 +01:00
{ group: "qualite", code: "mauvaise", label: "Mauvaise (négative)", check: item => item.isInventaire() && item.system.qualite < 0 },
{ group: "qualite", code: "quelconque", label: "Quelconque (0)", check: item => item.isInventaire() && item.system.qualite == 0 },
{ group: "qualite", code: "correcte", label: "Correcte (1-3)", check: item => item.isInventaire() && 1 <= item.system.qualite && item.system.qualite <= 3 },
{ group: "qualite", code: "bonne", label: "Bonne (4-6)", check: item => item.isInventaire() && 4 <= item.system.qualite && item.system.qualite <= 6 },
{ group: "qualite", code: "excellente", label: "Excellente (7-9)", check: item => item.isInventaire() && 7 <= item.system.qualite && item.system.qualite <= 9 },
{ group: "qualite", code: "mythique", label: "Mythique (10+)", check: item => item.isInventaire() && 10 <= item.system.qualite },
{ group: "enc", code: "negligeable", label: "Négligeable (jusqu'à 0.1)", check: item => item.isInventaire() && item.system.encombrement <= 0.1 },
{ group: "enc", code: "leger", label: "Léger (0.1 à 0.5)", check: item => item.isInventaire() && 0.1 < item.system.encombrement && item.system.encombrement <= 0.5 },
{ group: "enc", code: "moyen", label: "Moyen (0.5 à 1.5)", check: item => item.isInventaire() && 0.5 < item.system.encombrement && item.system.encombrement <= 1.5 },
{ group: "enc", code: "lourd", label: "Lourd (1.5 à 3)", check: item => item.isInventaire() && 1.5 < item.system.encombrement && item.system.encombrement <= 3 },
{ group: "enc", code: "massif", label: "Massif (3 à 10)", check: item => item.isInventaire() && 3 < item.system.encombrement && item.system.encombrement <= 10 },
{ group: "enc", code: "anemort", label: "Un âne mort (plus de 10)", check: item => item.isInventaire() && 10 < item.system.encombrement },
{ group: "prix", code: "gratuit", label: "Gratuit", check: item => item.isInventaire() && item.system.cout == 0 },
{ group: "prix", code: "deniers", label: "Deniers (étain) 1-9", check: item => item.isInventaire() && 0 < item.system.cout && item.system.cout < 0.1 },
{ group: "prix", code: "bronze", label: "Bronzes 1-9", check: item => item.isInventaire() && 0.1 <= item.system.cout && item.system.cout < 1 },
{ group: "prix", code: "sols", label: "Sols (argent) 1-9", check: item => item.isInventaire() && 1 <= item.system.cout && item.system.cout < 10 },
{ group: "prix", code: "dragons", label: "Dragons (or) 1+ ", check: item => item.isInventaire() && 10 <= item.system.cout },
]
const COMPENDIUMS_RECHERCHE = 'compendiums-recherche';
function $typeToFilter(type) { return { group: 'type', code: type, label: Misc.typeName('Item', type), check: item => item.type == type }; }
function $filterMilieux(milieux) {
return milieux.map(m => { return { group: 'milieu', code: m, label: m, check: item => item.isPresentDansMilieux(m) } })
}
function $filterRarete() {
return RdDRaretes.raretes()
.filter(it => it.frequence > 0)
.map(r => { return { group: 'rarete', code: r.code, label: r.label, check: item => item.getRarete()?.code == r.code }; });
}
function $filterTypes() {
return [
{ group: 'type', code: 'inventaire', label: 'Inventaire', check: item => item.isInventaire() && !item.isEnvironnement() },
]
.concat(['arme', 'armure'].map(it => $typeToFilter(it)))
.concat([{ group: 'type', code: 'environement', label: 'Faune, Flore, Ingrédients', check: item => item.isEnvironnement() }])
.concat(RdDItem.getItemTypesEnvironnement().map(it => $typeToFilter(it)))
}
function $getAllFilters(milieux) {
return FILTERS
.concat($filterTypes())
.concat($filterMilieux(milieux))
.concat($filterRarete());
}
function $addFilterToGroup(groups, filter) {
if (filter.group && filter.code && filter.label) {
let fg = groups.find(g => g.group == filter.group);
if (fg == undefined) {
groups.push({ group: filter.group, label: filter.group, filters: [filter] })
}
else if (fg.filters == undefined) {
fg.filters = [filter];
}
else {
fg.filters.push(filter);
}
}
else {
console.warn("Filtre incorrect, pas de groupe/code/label", filter);
}
}
function $loadFilters(parameters) {
$getAllFilters(parameters.milieux).forEach(f => $addFilterToGroup(parameters.filterGroups, f));
}
2023-01-13 04:54:29 +01:00
export class FenetreRechercheTirage extends Application {
static init() {
game.settings.register(SYSTEM_RDD, COMPENDIUMS_RECHERCHE, {
name: COMPENDIUMS_RECHERCHE,
default: [
SystemCompendiums.getCompendium('faune-flore-mineraux'),
SystemCompendiums.getCompendium('equipement')
],
scope: "world",
config: false,
type: Object
});
}
2023-01-13 04:54:29 +01:00
static get defaultOptions() {
return mergeObject(super.defaultOptions, {
template: "systems/foundryvtt-reve-de-dragon/templates/tirage/fenetre-recherche-tirage.hbs",
title: `Recherches et tirages`,
width: 600,
height: 600,
popOut: true,
2023-01-15 23:21:19 +01:00
dragDrop: [{ dragSelector: "a.content-link" }],
2023-01-13 04:54:29 +01:00
resizable: true
});
}
static async create() {
const parameters = {
milieux: await game.system.rdd.environnement.milieux(),
filterGroups: duplicate(FILTER_GROUPS).filter(it => it.group),
}
const options = {}
$loadFilters(parameters);
2023-01-13 04:54:29 +01:00
new FenetreRechercheTirage(parameters, options).render(true);
2023-01-13 04:54:29 +01:00
}
constructor(parameters, options) {
super(options);
this.parameters = parameters;
this.buildCompendiumTables();
2023-01-13 04:54:29 +01:00
}
buildCompendiumTables() {
this.parameters.compendiums = game.settings.get(SYSTEM_RDD, COMPENDIUMS_RECHERCHE);
this.parameters.compendiumTables = this.parameters.compendiums.map(it => new CompendiumTable(it, 'Item'));
2023-01-13 04:54:29 +01:00
}
async getData() {
let formData = super.getData();
mergeObject(formData, this.parameters)
2023-01-13 04:54:29 +01:00
return formData;
}
2023-01-15 23:21:19 +01:00
_canDragStart() {
return true;
}
_onDragStart(event) {
console.log('_onDragStart', event)
}
_getHeaderButtons() {
let buttons = super._getHeaderButtons();
if (game.user.isGM) {
buttons.unshift({
class: "configurer",
label: "Configurer",
icon: "fas fa-cogs",
onclick: ev => this.configurer()
});
}
return buttons
}
2023-01-13 04:54:29 +01:00
activateListeners(html) {
super.activateListeners(html);
this.html = html;
HtmlUtility.showControlWhen(this.html.find('div.group-filters'), false);
HtmlUtility.showControlWhen(this.html.find('i.filter-group-hide'), false);
HtmlUtility.showControlWhen(this.html.find('i.filter-group-show'), true);
this.html.find("a.filter-group-toggle").click(event => {
const groupDiv = this.html.find(event.currentTarget)?.parents('div.filter-group').first();
const visible = groupDiv.find('div.group-filters').first().is(":visible");
this.showFilterGroup(groupDiv, !visible)
});
this.html.find("input.activate-filter").change(event => this.changeListeFiltresActifs())
2023-01-15 23:30:32 +01:00
this.html.find("a.supprimer-filtres").click(async event => this.supprimerFiltres())
2023-01-13 04:54:29 +01:00
2023-01-15 23:21:19 +01:00
this.html.find("a.recherche-filtres").click(async event => await this.recherche())
2023-01-13 04:54:29 +01:00
this.html.find("a.tirage-filtres").click(async event => {
const table = await this.buildTable();
const row = await CompendiumTableHelpers.getRandom(table, 'Item')
await CompendiumTableHelpers.tableRowToChatMessage(row, 'Item');
})
}
2023-01-15 23:30:32 +01:00
supprimerFiltres() {
this.html.find('div.liste-resultats').html('');
return this.html.find('input.activate-filter:checked').prop("checked", false);
}
2023-01-15 23:21:19 +01:00
async recherche() {
const table = await this.buildTable();
const htmlResultats = await renderTemplate(`systems/foundryvtt-reve-de-dragon/templates/tirage/liste-resultats.hbs`, { resultats: table });
this.html.find('div.liste-resultats').html(htmlResultats);
this._dragDrop.forEach(dragDropHandler => dragDropHandler.bind(this.element[0]))
}
2023-01-13 04:54:29 +01:00
async buildTable() {
const filter = this.buildCheckedItemsFilter();
const allTables = await Promise.all(
this.parameters.compendiumTables.map(async compTable => await compTable.buildTable(it => it.getFrequence(), filter))
);
return CompendiumTableHelpers.concatTables(...allTables);
2023-01-13 04:54:29 +01:00
}
buildCheckedItemsFilter() {
const byGroup = this.getCheckedFiltersByGroup();
2023-01-13 04:54:29 +01:00
const groupSummaries = Object.entries(byGroup).map(([key, list]) => {
const group = this.parameters.filterGroups.find(g => key == g.group);
2023-01-13 04:54:29 +01:00
const filters = list.map(it => it.code).map(code => group.filters.find(f => code == f.code))
return filters
.map(f => f.check)
.reduce((a, b) => { return it => a(it) || b(it) });;
});
if (groupSummaries.length == 0) {
return it => true;
}
return groupSummaries.reduce((a, b) => { return it => a(it) && b(it) })
}
showFilterGroup(groupDiv, show) {
if (groupDiv) {
HtmlUtility.showControlWhen(groupDiv.find('div.group-filters'), show);
HtmlUtility.showControlWhen(groupDiv.find('i.filter-group-hide'), show);
HtmlUtility.showControlWhen(groupDiv.find('i.filter-group-show'), !show);
}
}
changeListeFiltresActifs() {
const byGroup = this.getCheckedFiltersByGroup();
2023-01-13 04:54:29 +01:00
const groupSummaries = Object.entries(byGroup).map(([key, list]) => {
const group = this.parameters.filterGroups.find(g => key == g.group);
2023-01-13 04:54:29 +01:00
const filters = list.map(it => it.code).map(code => group.filters.find(f => code == f.code))
return group.label + ': ' + filters
.map(f => f.label)
.reduce(Misc.joining(', '));;
});
const fullText = groupSummaries.length == 0 ? "" : groupSummaries.reduce(Misc.joining(' - '));
this.html.find('span.liste-filtres-actifs').text(fullText);
}
getCheckedFiltersByGroup() {
2023-01-13 04:54:29 +01:00
const selectedFilters = jQuery.map(this.html.find('input.activate-filter:checked'), it => {
const element = this.html.find(it);
return { group: element.data('group'), code: element.data('code') };
});
return Misc.classify(selectedFilters, it => it.group);
}
async configurer() {
const itemPacks = game.packs.filter(it => it.metadata.type == 'Item');
console.log('packs1', itemPacks);
const configuration = {
compendiums: itemPacks.map(it => duplicate(it.metadata)).map(it => mergeObject(it, { selected: this.parameters.compendiums.includes(it.id) }))
}
FenetreRechercheConfiguration.create(configuration, c => this.setConfiguration(c));
}
setConfiguration(configuration) {
const compendiums = configuration.compendiums.filter(it => it.selected).map(it => it.id);
game.settings.set(SYSTEM_RDD, COMPENDIUMS_RECHERCHE, compendiums);
this.buildCompendiumTables()
}
}
class FenetreRechercheConfiguration extends Dialog {
static async create(configuration, onSave) {
const html = await renderTemplate("systems/foundryvtt-reve-de-dragon/templates/tirage/fenetre-recherche-configuration.hbs", configuration);
new FenetreRechercheConfiguration(html, configuration, onSave).render(true);
}
constructor(html, configuration, onSave) {
const options = {
classes: ["fenetre-recherche-configuration"],
width: 600,
height: 'fit-content',
'max-height': 600,
height: 'fit-content',
'z-index': 99999
};
const conf = {
title: 'Configuration de la recherche',
content: html,
buttons: {
}
};
super(conf, options)
this.configuration = configuration;
this.onSave = onSave;
}
activateListeners(html) {
this.html = html;
super.activateListeners(html);
this.html.find("button.configuration-save").click(event => this.sauvegarder())
}
async sauvegarder() {
const compendiumIds = jQuery.map(this.html.find("input.select-compendium:checked"), it => {
return this.html.find(it).data('id');
});
this.configuration.compendiums.forEach(c => {
c.selected = compendiumIds.includes(c.id)
})
this.onSave(this.configuration)
2023-01-13 04:54:29 +01:00
}
}