Paramétrage des compendiums de recherche

This commit is contained in:
Vincent Vandemeulebrouck 2023-01-17 21:51:49 +01:00
parent f7595a1bfe
commit f4d074fa31
8 changed files with 237 additions and 104 deletions

View File

@ -99,8 +99,7 @@ export class Environnement {
if (!milieux) { if (!milieux) {
milieux = await this.milieux() milieux = await this.milieux()
} }
const frequence = item => item.getRarete(milieux)?.frequence ?? 0; return await this.table.buildTable(it => it.getFrequence(), filter);
return await this.table.buildTable(frequence, it => frequence(it) > 0 && filter(it));
} }
async getElements(itemFrequence, filter) { async getElements(itemFrequence, filter) {

View File

@ -186,28 +186,19 @@ export class RdDItem extends Item {
? this.system?.environnement.filter(env => !milieux || milieux.includes(env.milieu)) ? this.system?.environnement.filter(env => !milieux || milieux.includes(env.milieu))
: [] : []
} }
getEnvRarete(milieux = undefined) {
if (this.isEnvironnement()) {
const list = this.getEnvironnements(milieux);
const frequenceMax = Math.max(...list.map(env => env.frequence));
return list.find(env => env.frequence == frequenceMax);
}
return {}
}
getRarete(milieux = undefined) { getRarete(milieux = undefined) {
if (this.isEnvironnement()) { if (this.isEnvironnement()) {
const env = this.getEnvRarete(milieux); return RdDRaretes.rareteEnvironnement(this, milieux)
return RdDRaretes.getRarete(env.rarete);
} }
if (this.isInventaire()) { if (this.isInventaire()) {
return RdDRaretes.rareteEquipement(this) return RdDRaretes.rareteEquipement(this)
} }
return RdDRaretes.getRareteFrequente(); return RdDRaretes.rareteFrequente();
} }
getFrequence(milieux = undefined) { getFrequence(milieux = undefined) {
const frequence = this.getRarete(milieux)?.frequence; return this.getRarete(milieux)?.frequence ?? 0;
return frequence == undefined ? 1 : frequence;
} }
getItemGroup() { getItemGroup() {

View File

@ -511,7 +511,7 @@ export class RdDCommands {
} }
async tirage() { async tirage() {
new FenetreRechercheTirage({}).render(true); FenetreRechercheTirage.create();
} }
} }

View File

@ -50,6 +50,7 @@ import { RdDConteneurItemSheet } from "./item-conteneur-sheet.js";
import { RdDSigneDraconiqueItemSheet } from "./item-signedraconique-sheet.js"; import { RdDSigneDraconiqueItemSheet } from "./item-signedraconique-sheet.js";
import { TMRRencontres } from "./tmr-rencontres.js"; import { TMRRencontres } from "./tmr-rencontres.js";
import { FenetreRechercheTirage } from "./tirage/fenetre-recherche-tirage.js";
/** /**
* RdD system * RdD system
@ -182,6 +183,7 @@ export class SystemReveDeDragon {
RdDPossession.init(); RdDPossession.init();
TMRRencontres.init(); TMRRencontres.init();
Environnement.init(); Environnement.init();
FenetreRechercheTirage.init();
Hooks.once('ready', () => this.onReady()); Hooks.once('ready', () => this.onReady());
} }
@ -298,7 +300,7 @@ export class SystemReveDeDragon {
} }
/* -------------------------------------------- */ /* -------------------------------------------- */
messageDeBienvenue() { messageDeBienvenue() {
if (game.user.isGM) { if (game.user.isGM) {
ChatUtility.removeChatMessageContaining('<div id="message-bienvenue-rdd">'); ChatUtility.removeChatMessageContaining('<div id="message-bienvenue-rdd">');
ChatMessage.create({ ChatMessage.create({

View File

@ -52,6 +52,10 @@ export class SystemCompendiums extends FormApplication {
} }
static getPack(compendium) { static getPack(compendium) {
const pack = game.packs.get(compendium);
if (pack) {
return pack;
}
return game.packs.get(SystemCompendiums.getCompendium(compendium)) ?? game.packs.get(SystemCompendiums._getDefaultCompendium(compendium)); return game.packs.get(SystemCompendiums.getCompendium(compendium)) ?? game.packs.get(SystemCompendiums._getDefaultCompendium(compendium));
} }
@ -187,7 +191,7 @@ export class CompendiumTable {
async getContent(itemFrequence = it => it.system.frequence, filter = it => true) { async getContent(itemFrequence = it => it.system.frequence, filter = it => true) {
return await SystemCompendiums.getContent(this.compendium, return await SystemCompendiums.getContent(this.compendium,
this.type, this.type,
it => (!this.subTypes || this.subTypes.includes(it.type)) && filter(it), it => (!this.subTypes || this.subTypes.includes(it.type)) && itemFrequence(it) > 0 && filter(it),
itemFrequence, itemFrequence,
this.sorting); this.sorting);
} }

View File

@ -3,6 +3,7 @@ import { HtmlUtility } from '../html-utility.js';
import { Misc } from "../misc.js"; import { Misc } from "../misc.js";
import { CompendiumTable, CompendiumTableHelpers, SystemCompendiums } from '../settings/system-compendiums.js'; import { CompendiumTable, CompendiumTableHelpers, SystemCompendiums } from '../settings/system-compendiums.js';
import { RdDRaretes } from './raretes.js'; import { RdDRaretes } from './raretes.js';
import { SYSTEM_RDD } from '../constants.js';
const FILTER_GROUPS = [ const FILTER_GROUPS = [
{ group: 'type', label: "Type d'objet" }, { group: 'type', label: "Type d'objet" },
@ -50,8 +51,75 @@ const FILTERS = [
{ group: "prix", code: "dragons", label: "Dragons (or) 1+ ", check: item => item.isInventaire() && 10 <= item.system.cout }, { 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));
}
export class FenetreRechercheTirage extends Application { 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
});
}
static get defaultOptions() { static get defaultOptions() {
return mergeObject(super.defaultOptions, { return mergeObject(super.defaultOptions, {
template: "systems/foundryvtt-reve-de-dragon/templates/tirage/fenetre-recherche-tirage.hbs", template: "systems/foundryvtt-reve-de-dragon/templates/tirage/fenetre-recherche-tirage.hbs",
@ -64,72 +132,32 @@ export class FenetreRechercheTirage extends Application {
}); });
} }
static async $filterMilieux() { static async create() {
const milieux = await game.system.rdd.environnement.milieux(); const parameters = {
return milieux.map(m => { return { group: 'milieu', code: m, label: m, check: item => item.isPresentDansMilieux(m) } }) milieux: await game.system.rdd.environnement.milieux(),
filterGroups: duplicate(FILTER_GROUPS).filter(it => it.group),
}
const options = {}
$loadFilters(parameters);
new FenetreRechercheTirage(parameters, options).render(true);
} }
static $filterRarete() { constructor(parameters, options) {
return RdDRaretes.raretes()
.map(r => { return { group: 'rarete', code: r.code, label: r.label, check: item => item.getRarete()?.code == r.code }; });
}
static $filterTypes() {
return [
{ group: 'type', code: 'inventaire', label: 'Inventaire', check: item => item.isInventaire() && !item.isEnvironnement() },
]
.concat(['arme', 'armure'].map(it => FenetreRechercheTirage.$typeToFilter(it)))
.concat([{ group: 'type', code: 'environement', label: 'Faune, Flore, Ingrédients', check: item => item.isEnvironnement() }])
.concat(RdDItem.getItemTypesEnvironnement().map(it => FenetreRechercheTirage.$typeToFilter(it)))
}
static $typeToFilter(type) { return { group: 'type', code: type, label: Misc.typeName('Item', type), check: item => item.type == type }; }
static async create(options) {
new FenetreRechercheTirage(options).render(true);
}
constructor(options) {
super(options); super(options);
this.tirage = {}; this.parameters = parameters;
this.compendiums = [ this.buildCompendiumTables();
SystemCompendiums.getCompendium('faune-flore-mineraux'), }
SystemCompendiums.getCompendium('equipement')
] buildCompendiumTables() {
this.parameters.compendiums = game.settings.get(SYSTEM_RDD, COMPENDIUMS_RECHERCHE);
this.parameters.compendiumTables = this.parameters.compendiums.map(it => new CompendiumTable(it, 'Item'));
} }
async getData() { async getData() {
const filterGroups = duplicate(FILTER_GROUPS);
FILTERS
.concat(FenetreRechercheTirage.$filterTypes())
.concat(await FenetreRechercheTirage.$filterMilieux())
.concat(FenetreRechercheTirage.$filterRarete())
.forEach(f => addFilterToGroup(filterGroups, f))
mergeObject(this.tirage,
{
filterGroups: filterGroups.filter(it => it.group)
})
let formData = super.getData(); let formData = super.getData();
mergeObject(formData, this.tirage) mergeObject(formData, this.parameters)
return formData; return formData;
function addFilterToGroup(filterGroups, filter) {
if (filter.group && filter.code && filter.label) {
let fg = filterGroups.find(g => g.group == filter.group);
if (fg == undefined) {
filterGroups.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);
}
}
} }
_canDragStart() { _canDragStart() {
@ -138,6 +166,18 @@ export class FenetreRechercheTirage extends Application {
_onDragStart(event) { _onDragStart(event) {
console.log('_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
}
activateListeners(html) { activateListeners(html) {
super.activateListeners(html); super.activateListeners(html);
@ -179,17 +219,17 @@ export class FenetreRechercheTirage extends Application {
} }
async buildTable() { async buildTable() {
const filter = this.getSelectedItemsFilter(); const filter = this.buildCheckedItemsFilter();
const equipementCompendiumTable = new CompendiumTable('equipement', 'Item'); const allTables = await Promise.all(
const equipements = await equipementCompendiumTable.buildTable(it => it.getFrequence(), filter) this.parameters.compendiumTables.map(async compTable => await compTable.buildTable(it => it.getFrequence(), filter))
const environnements = await game.system.rdd.environnement.buildEnvironnementTable(undefined, filter); );
return CompendiumTableHelpers.concatTables(environnements, equipements); return CompendiumTableHelpers.concatTables(...allTables);
} }
getSelectedItemsFilter() { buildCheckedItemsFilter() {
const byGroup = this.getSelectedFiltersByGroup(); const byGroup = this.getCheckedFiltersByGroup();
const groupSummaries = Object.entries(byGroup).map(([key, list]) => { const groupSummaries = Object.entries(byGroup).map(([key, list]) => {
const group = this.tirage.filterGroups.find(g => key == g.group); const group = this.parameters.filterGroups.find(g => key == g.group);
const filters = list.map(it => it.code).map(code => group.filters.find(f => code == f.code)) const filters = list.map(it => it.code).map(code => group.filters.find(f => code == f.code))
return filters return filters
.map(f => f.check) .map(f => f.check)
@ -210,9 +250,9 @@ export class FenetreRechercheTirage extends Application {
} }
changeListeFiltresActifs() { changeListeFiltresActifs() {
const byGroup = this.getSelectedFiltersByGroup(); const byGroup = this.getCheckedFiltersByGroup();
const groupSummaries = Object.entries(byGroup).map(([key, list]) => { const groupSummaries = Object.entries(byGroup).map(([key, list]) => {
const group = this.tirage.filterGroups.find(g => key == g.group); const group = this.parameters.filterGroups.find(g => key == g.group);
const filters = list.map(it => it.code).map(code => group.filters.find(f => code == f.code)) const filters = list.map(it => it.code).map(code => group.filters.find(f => code == f.code))
return group.label + ': ' + filters return group.label + ': ' + filters
.map(f => f.label) .map(f => f.label)
@ -222,12 +262,70 @@ export class FenetreRechercheTirage extends Application {
this.html.find('span.liste-filtres-actifs').text(fullText); this.html.find('span.liste-filtres-actifs').text(fullText);
} }
getSelectedFiltersByGroup() { getCheckedFiltersByGroup() {
const selectedFilters = jQuery.map(this.html.find('input.activate-filter:checked'), it => { const selectedFilters = jQuery.map(this.html.find('input.activate-filter:checked'), it => {
const element = this.html.find(it); const element = this.html.find(it);
return { group: element.data('group'), code: element.data('code') }; return { group: element.data('group'), code: element.data('code') };
}); });
const byGroup = Misc.classify(selectedFilters, it => it.group); return Misc.classify(selectedFilters, it => it.group);
return byGroup; }
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)
} }
} }

View File

@ -1,15 +1,21 @@
const RARETE_COMMUNE = { code: 'Commune', label: 'Commune', frequence: 54, min: 27, max: 108 };
const RARETE_FREQUENTE = { code: 'Frequente', label: 'Fréquente', frequence: 18, min: 9, max: 36 };
const RARETE_RARE = { code: 'Rare', label: 'Rare', frequence: 6, min: 3, max: 12 };
const RARETE_RARISSIME = { code: 'Rarissime', label: 'Rarissime', frequence: 2, min: 1, max: 4 };
const RARETE_INEXISTANT = { code: 'Inexistant', label: 'Inexistant', frequence: 0, min: 0, max: 0 };
const RARETES = [ const RARETES = [
{ code: 'Commune', label: 'Commune', frequence: 54, min: 27, max: 108 }, RARETE_COMMUNE,
{ code: 'Frequente', label: 'Fréquente', frequence: 18, min: 9, max: 36 }, RARETE_FREQUENTE,
{ code: 'Rare', label: 'Rare', frequence: 6, min: 3, max: 12 }, RARETE_RARE,
{ code: 'Rarissime', label: 'Rarissime', frequence: 2, min: 1, max: 4 }] RARETE_RARISSIME,
const DEFAULT_RARETE = 1; RARETE_INEXISTANT,
]
export class RdDRaretes { export class RdDRaretes {
static getRarete(code = undefined) { static getRarete(code = undefined) {
return RARETES.find(it => it.code == code) ?? RARETES[DEFAULT_RARETE]; return RARETES.find(it => it.code == code) ?? RARETE_FREQUENTE;
} }
static getChamp(rarete, field = undefined) { static getChamp(rarete, field = undefined) {
@ -17,8 +23,8 @@ export class RdDRaretes {
return field ? selected[field] : selected[frequence]; return field ? selected[field] : selected[frequence];
} }
static getRareteFrequente() { static rareteFrequente() {
return RARETES[DEFAULT_RARETE]; return RARETE_FREQUENTE;
} }
static raretes() { static raretes() {
@ -29,17 +35,28 @@ export class RdDRaretes {
return RdDRaretes.rareteEquipement(item).frequence return RdDRaretes.rareteEquipement(item).frequence
} }
static rareteEnvironnement(item, milieux = undefined) {
const list = item.getEnvironnements(milieux);
const freqMax = Math.max(0, ...list.map(env => env.frequence));
const env = list.find(env => env.frequence == freqMax);
return env ? RdDRaretes.getRarete(env.rarete) : RARETE_INEXISTANT;
}
static rareteEquipement(item) { static rareteEquipement(item) {
const qualite = item.system.qualite ?? 0; const qualite = item.system.qualite ?? 0;
if (qualite <= 0) { if (qualite <= 0) {
return RARETES[0] return RARETE_COMMUNE
} }
if (qualite <= 3) { if (qualite <= 3) {
return RARETES[1] return RARETE_FREQUENTE
} }
if (qualite <= 6) { if (qualite <= 6) {
return RARETES[2] return RARETE_RARE
} }
return RARETES[3] if (qualite <= 9) {
return RARETE_RARISSIME
}
return RARETE_INEXISTANT
} }
} }

View File

@ -0,0 +1,22 @@
<form class="fenetre-recherche-configuration" autocomplete="off" onsubmit="event.preventDefault();">
<div class="form-group">
<h3>Recherche dans les compendiums</h3>
</div>
<div class="form-group">
<ul>
{{#each compendiums as |compendium|}}
<li class="flexrow">
<input type="checkbox" class="select-compendium" name="{{compendium.id}}"
data-id="{{compendium.id}}" {{#if compendium.selected}}checked{{/if}}/>
<label for="{{compendium.id}}">{{compendium.label}}
<label class="poesie-reference">({{compendium.id}})</label>
</label>
</li>
{{/each}}
</ul>
</div>
<div class="form-group">
<button class="configuration-save">Sauvegarder</button>
</div>
</form>