foundryvtt-reve-de-dragon/module/settings/system-compendiums.js

149 lines
5.3 KiB
JavaScript
Raw Normal View History

2022-11-06 21:39:47 +01:00
import { SYSTEM_RDD } from "../constants.js";
const COMPENDIUM_SETTING_PREFIX = 'compendium-';
const CONFIGURABLE_COMPENDIUMS = {
'tables-diverses': { label: "Tables aléatoires", type: "RollTable" },
'competences': { label: "Compétences", type: "Item" },
'extrait-poetique': { label: "Extraits poetiques", type: "Item" },
2022-11-06 21:39:47 +01:00
'queues-de-dragon': { label: "Queues de dragon", type: "Item" },
'ombres-de-thanatos': { label: "Ombres de Thanatos", type: "Item" },
'souffles-de-dragon': { label: "Souffles de Dragon", type: "Item" },
'tarot-draconique': { label: "Tarots draconiques", type: "Item" },
2022-11-06 22:10:09 +01:00
'rencontres': { label: "Rencontres dans les TMR", type: "Item" },
2022-11-06 21:39:47 +01:00
'tetes-de-dragon-pour-haut-revants': { label: "Têtes de dragons (haut-rêvant)", type: "Item" },
'tetes-de-dragon-pour-tous-personnages': { label: "Têtes de dragons (tous)", type: "Item" },
}
export class SystemCompendiums extends FormApplication {
static init() {
Object.keys(CONFIGURABLE_COMPENDIUMS).forEach(compendium => {
const definition = CONFIGURABLE_COMPENDIUMS[compendium];
mergeObject(definition, {
compendium: compendium,
default: SystemCompendiums._getDefaultCompendium(compendium),
setting: SystemCompendiums._getSettingCompendium(compendium)
});
game.settings.register(SYSTEM_RDD, definition.setting, {
name: definition.label,
default: definition.default,
scope: "world",
config: false,
type: String
});
});
game.settings.registerMenu(SYSTEM_RDD, "compendium-settings", {
name: "Choisir les compendiums système",
label: "Compendiums système",
hint: "Ouvre la fenêtre de sélection des compendiums système",
icon: "fas fa-bars",
type: SystemCompendiums
})
}
static getPack(compendium) {
return game.packs.get(SystemCompendiums.getCompendium(compendium));
}
static async getContent(compendium, docType) {
const pack = SystemCompendiums.getPack(compendium);
2022-11-07 00:04:43 +01:00
if (pack.metadata.type == docType) {
2022-11-06 21:39:47 +01:00
return await pack.getDocuments();
}
return [];
}
static async getCompetences(actorType) {
switch (actorType ?? 'personnage') {
case 'personnage': return await SystemCompendiums.getWorldOrCompendiumItems('competence', 'competences');
case 'creature': return await SystemCompendiums.getWorldOrCompendiumItems('competencecreature', 'competences-creatures');
case 'entite': return await SystemCompendiums.getWorldOrCompendiumItems('competencecreature', 'competences-entites');
case 'vehicule': return [];
}
}
/* -------------------------------------------- */
static async getWorldOrCompendiumItems(itemType, compendium) {
let items = game.items.filter(it => it.type == itemType);
if (compendium) {
const ids = items.map(it => it.id);
const names = items.map(it => it.name.toLowerCase());
const compendiumItems = await SystemCompendiums.getItems(compendium);
items = items.concat(compendiumItems
.filter(it => it.type == itemType)
.filter(it => !ids.includes(it.id))
.filter(it => !names.includes(it.name.toLowerCase())));
}
return items;
}
static async getItems(compendium, itemType = undefined) {
const items = await SystemCompendiums.getContent(compendium, 'Item');
return itemType ? items.filter(it => it.type == itemType) : items;
2022-11-06 21:39:47 +01:00
}
2022-11-07 00:04:43 +01:00
static async getDefaultItems(compendium) {
const pack = game.packs.get(SystemCompendiums._getDefaultCompendium(compendium));
if (pack.metadata.type == 'Item') {
return await pack.getDocuments();
}
return [];
}
2022-11-06 21:39:47 +01:00
static getCompendium(compendium) {
const setting = CONFIGURABLE_COMPENDIUMS[compendium]?.setting;
return setting ? game.settings.get(SYSTEM_RDD, setting) : SystemCompendiums._getDefaultCompendium(compendium);
}
static _getSettingCompendium(compendium) {
return COMPENDIUM_SETTING_PREFIX + compendium;
}
static _getDefaultCompendium(compendium) {
return `${SYSTEM_RDD}.${compendium}`;
}
constructor(...args) {
super(...args);
}
static get defaultOptions() {
const options = super.defaultOptions;
mergeObject(options, {
id: "system-compendiums",
template: "systems/foundryvtt-reve-de-dragon/templates/settings/system-compendiums.html",
height: 'fit-content',
width: 600,
minimizable: false,
closeOnSubmit: true,
title: "Compendiums système"
});
return options;
}
getData() {
const systemCompendiums = Object.values(CONFIGURABLE_COMPENDIUMS)
.map(it => mergeObject(it, { value: SystemCompendiums.getCompendium(it.compendium) }));
const availableCompendiums = game.packs.map(pack => { return {
name: pack.collection,
path: pack.collection.replace('.', " / "),
type: pack.metadata.type
} });
return mergeObject(super.getData(), {
systemCompendiums: systemCompendiums,
availableCompendiums: availableCompendiums
});
}
activateListeners(html) {
html.find("select.system-compendium-setting").change((event) => {
const compendium = $(event.currentTarget).data('compendium')
const value = $(event.currentTarget).val();
const systemCompendium = CONFIGURABLE_COMPENDIUMS[compendium];
game.settings.set(SYSTEM_RDD, systemCompendium.setting, value);
});
}
}