2024-08-01 01:15:26 +02:00
|
|
|
import { ChatUtility } from "../chat-utility.js";
|
2022-11-26 03:13:45 +01:00
|
|
|
import { HIDE_DICE, SYSTEM_RDD } from "../constants.js";
|
|
|
|
import { RdDItem } from "../item.js";
|
|
|
|
import { Misc } from "../misc.js";
|
|
|
|
import { RdDDice } from "../rdd-dice.js";
|
2022-11-06 21:39:47 +01:00
|
|
|
|
|
|
|
const COMPENDIUM_SETTING_PREFIX = 'compendium-';
|
|
|
|
|
|
|
|
const CONFIGURABLE_COMPENDIUMS = {
|
|
|
|
'tables-diverses': { label: "Tables aléatoires", type: "RollTable" },
|
|
|
|
'competences': { label: "Compétences", type: "Item" },
|
2022-11-16 03:00:38 +01:00
|
|
|
'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" },
|
2022-11-30 01:11:31 +01:00
|
|
|
'faune-flore-mineraux': { label: "Herbes & plantes", type: "Item" },
|
2022-11-28 11:31:19 +01:00
|
|
|
'equipement': { label: "Equipements", type: "Item" },
|
2022-11-06 21:39:47 +01:00
|
|
|
}
|
|
|
|
|
2022-11-28 11:31:19 +01:00
|
|
|
/**
|
|
|
|
* ======= Gestion des accès aux compendiums systèmes (ou surchargés) =======
|
|
|
|
*/
|
2022-11-06 21:39:47 +01:00
|
|
|
export class SystemCompendiums extends FormApplication {
|
2024-10-14 03:46:09 +02:00
|
|
|
static initSettings() {
|
2022-11-06 21:39:47 +01:00
|
|
|
Object.keys(CONFIGURABLE_COMPENDIUMS).forEach(compendium => {
|
|
|
|
const definition = CONFIGURABLE_COMPENDIUMS[compendium];
|
2024-05-01 09:13:21 +02:00
|
|
|
foundry.utils.mergeObject(definition, {
|
2022-11-06 21:39:47 +01:00
|
|
|
compendium: compendium,
|
|
|
|
default: SystemCompendiums._getDefaultCompendium(compendium),
|
|
|
|
setting: SystemCompendiums._getSettingCompendium(compendium)
|
2024-12-02 22:07:20 +01:00
|
|
|
})
|
2022-11-06 21:39:47 +01:00
|
|
|
|
|
|
|
game.settings.register(SYSTEM_RDD, definition.setting, {
|
|
|
|
name: definition.label,
|
|
|
|
default: definition.default,
|
|
|
|
scope: "world",
|
|
|
|
config: false,
|
|
|
|
type: String
|
2024-12-02 22:07:20 +01:00
|
|
|
})
|
|
|
|
})
|
2022-11-06 21:39:47 +01:00
|
|
|
|
|
|
|
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",
|
2023-07-07 00:46:48 +02:00
|
|
|
restricted: true,
|
2022-11-06 21:39:47 +01:00
|
|
|
type: SystemCompendiums
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
static getPack(compendium) {
|
2023-01-17 21:51:49 +01:00
|
|
|
const pack = game.packs.get(compendium);
|
|
|
|
if (pack) {
|
|
|
|
return pack;
|
|
|
|
}
|
2022-12-03 01:22:30 +01:00
|
|
|
return game.packs.get(SystemCompendiums.getCompendium(compendium)) ?? game.packs.get(SystemCompendiums._getDefaultCompendium(compendium));
|
2022-11-06 21:39:47 +01:00
|
|
|
}
|
|
|
|
|
2022-11-28 11:31:19 +01:00
|
|
|
static async getPackContent(compendium, docType) {
|
2022-11-06 21:39:47 +01:00
|
|
|
const pack = SystemCompendiums.getPack(compendium);
|
2023-01-18 01:37:22 +01:00
|
|
|
if (pack?.metadata.type == docType) {
|
2022-11-06 21:39:47 +01:00
|
|
|
return await pack.getDocuments();
|
|
|
|
}
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2022-11-23 22:39:48 +01:00
|
|
|
static async getCompetences(actorType) {
|
|
|
|
switch (actorType ?? 'personnage') {
|
2024-12-02 22:07:20 +01:00
|
|
|
case 'personnage':
|
|
|
|
return await SystemCompendiums.getWorldOrCompendiumItems('competence', 'competences')
|
|
|
|
case 'entite':
|
|
|
|
case 'creature':
|
|
|
|
return await SystemCompendiums.getWorldOrCompendiumItems('competencecreature', 'competences-creatures')
|
2022-11-23 22:39:48 +01:00
|
|
|
case 'vehicule': return [];
|
|
|
|
}
|
|
|
|
}
|
2022-11-26 03:13:45 +01:00
|
|
|
|
2022-11-23 22:39:48 +01:00
|
|
|
/* -------------------------------------------- */
|
|
|
|
static async getWorldOrCompendiumItems(itemType, compendium) {
|
2024-12-02 22:07:20 +01:00
|
|
|
let items = game.items.filter(it => it.type == itemType)
|
2022-11-23 22:39:48 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-12-23 00:34:50 +01:00
|
|
|
static async loadDocument(document) {
|
|
|
|
const pack = game.packs.get(document.pack);
|
|
|
|
return await pack.getDocument(document.id ?? document._id);
|
|
|
|
}
|
|
|
|
|
2022-11-16 03:00:38 +01:00
|
|
|
static async getItems(compendium, itemType = undefined) {
|
2022-11-28 11:31:19 +01:00
|
|
|
const items = await SystemCompendiums.getPackContent(compendium, 'Item');
|
2022-11-26 03:13:45 +01:00
|
|
|
return (itemType ? items.filter(it => it.type == itemType) : items);
|
|
|
|
}
|
|
|
|
|
2022-11-28 11:31:19 +01:00
|
|
|
static async getContent(compendium, type, filter, itemFrequence, sorting) {
|
|
|
|
let elements = await SystemCompendiums.getPackContent(compendium, type);
|
|
|
|
elements = elements.filter(filter).filter(it => itemFrequence(it) > 0);
|
2022-11-26 03:13:45 +01:00
|
|
|
if (sorting) {
|
|
|
|
elements = elements.sort(sorting);
|
|
|
|
}
|
2022-11-28 11:31:19 +01:00
|
|
|
return elements;
|
2022-11-06 21:39:47 +01:00
|
|
|
}
|
|
|
|
|
2024-11-08 19:57:36 +01:00
|
|
|
/* -------------------------------------------- */
|
|
|
|
static async loadCompendiumData(compendium) {
|
|
|
|
const pack = game.packs.get(compendium);
|
|
|
|
return await pack?.getDocuments() ?? [];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* -------------------------------------------- */
|
|
|
|
static async loadCompendium(compendium, filter = item => true) {
|
|
|
|
let compendiumData = await SystemCompendiums.loadCompendiumData(compendium);
|
|
|
|
return compendiumData.filter(filter);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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;
|
2024-05-01 09:13:21 +02:00
|
|
|
foundry.utils.mergeObject(options, {
|
2022-11-06 21:39:47 +01:00
|
|
|
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)
|
2024-06-01 01:53:14 +02:00
|
|
|
.map(it => foundry.utils.mergeObject(it, { value: SystemCompendiums.getCompendium(it.compendium) }, { inplace: false }))
|
2022-11-26 03:13:45 +01:00
|
|
|
const availableCompendiums = game.packs.map(pack => {
|
|
|
|
return {
|
|
|
|
name: pack.collection,
|
|
|
|
path: pack.collection.replace('.', " / "),
|
|
|
|
type: pack.metadata.type
|
|
|
|
}
|
|
|
|
});
|
2024-05-01 09:13:21 +02:00
|
|
|
return foundry.utils.mergeObject(super.getData(), {
|
2022-11-06 21:39:47 +01:00
|
|
|
systemCompendiums: systemCompendiums,
|
|
|
|
availableCompendiums: availableCompendiums
|
2024-06-01 01:53:14 +02:00
|
|
|
}, { inplace: false })
|
2022-11-06 21:39:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
}
|
2022-11-26 03:13:45 +01:00
|
|
|
}
|
|
|
|
|
2022-11-28 11:31:19 +01:00
|
|
|
/**
|
|
|
|
* ======= Gestion de jets dans une table correspondant à un compendium =======
|
|
|
|
*/
|
|
|
|
export class CompendiumTable {
|
2022-11-26 03:13:45 +01:00
|
|
|
|
2023-01-13 04:54:29 +01:00
|
|
|
constructor(compendium, type, subTypes = undefined, sorting = undefined) {
|
2022-11-26 03:13:45 +01:00
|
|
|
this.compendium = compendium;
|
|
|
|
this.type = type;
|
2022-11-30 01:11:31 +01:00
|
|
|
this.subTypes = subTypes;
|
2022-11-28 11:31:19 +01:00
|
|
|
this.sorting = sorting ?? Misc.ascending(it => it.name);
|
2022-11-26 03:13:45 +01:00
|
|
|
}
|
|
|
|
|
2022-11-28 11:31:19 +01:00
|
|
|
async getContent(itemFrequence = it => it.system.frequence, filter = it => true) {
|
|
|
|
return await SystemCompendiums.getContent(this.compendium,
|
|
|
|
this.type,
|
2023-01-17 21:51:49 +01:00
|
|
|
it => (!this.subTypes || this.subTypes.includes(it.type)) && itemFrequence(it) > 0 && filter(it),
|
2022-11-28 11:31:19 +01:00
|
|
|
itemFrequence,
|
|
|
|
this.sorting);
|
2022-11-26 03:13:45 +01:00
|
|
|
}
|
2022-11-28 11:31:19 +01:00
|
|
|
|
|
|
|
async buildTable(itemFrequence = it => it.system.frequence, filter = it => true) {
|
2023-01-13 04:54:29 +01:00
|
|
|
const elements = await this.getContent(itemFrequence, filter);
|
2022-11-28 11:31:19 +01:00
|
|
|
return CompendiumTableHelpers.buildTable(elements, itemFrequence);
|
2022-11-26 03:13:45 +01:00
|
|
|
}
|
|
|
|
|
2022-11-28 11:31:19 +01:00
|
|
|
async getRandom(itemFrequence = it => it.system.frequence, filter = it => true, forcedRoll = undefined) {
|
|
|
|
const table = await this.buildTable(itemFrequence, filter);
|
2022-11-30 01:11:31 +01:00
|
|
|
return await CompendiumTableHelpers.getRandom(table, this.type, this.subTypes, forcedRoll, SystemCompendiums.getCompendium(compendium));
|
2022-11-26 03:13:45 +01:00
|
|
|
}
|
2022-11-06 21:39:47 +01:00
|
|
|
|
2022-11-28 11:31:19 +01:00
|
|
|
async toChatMessage(itemFrequence = it => it.system.frequence, filter = it => true, typeName = undefined) {
|
|
|
|
const table = await this.buildTable(itemFrequence, filter);
|
2022-11-30 01:11:31 +01:00
|
|
|
await CompendiumTableHelpers.tableToChatMessage(table, this.type, this.subTypes, typeName);
|
2022-11-28 11:31:19 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ======= Gestion de tables correspondant à un compendium =======
|
|
|
|
*/
|
|
|
|
export class CompendiumTableHelpers {
|
|
|
|
|
|
|
|
static buildTable(elements, itemFrequence) {
|
|
|
|
let max = 0;
|
|
|
|
const total = elements.map(it => itemFrequence(it)).reduce(Misc.sum(), 0);
|
|
|
|
return elements.map(it => {
|
2022-12-03 01:22:30 +01:00
|
|
|
const frequence = itemFrequence(it);
|
|
|
|
let row = { document: it, frequence: frequence, min: max + 1, max: max + frequence, total: total };
|
|
|
|
max += frequence;
|
|
|
|
return row;
|
|
|
|
});
|
2022-11-26 03:13:45 +01:00
|
|
|
}
|
|
|
|
|
2023-01-13 04:54:29 +01:00
|
|
|
static concatTables(...tables) {
|
|
|
|
const rows = tables.reduce((a, b) => a.concat(b));
|
|
|
|
let max = 0;
|
|
|
|
const total = rows.map(it => it.frequence).reduce(Misc.sum(), 0);
|
|
|
|
return rows.map(row => {
|
2024-10-16 23:29:18 +02:00
|
|
|
const frequence = row.frequence
|
|
|
|
row.min = max + 1
|
|
|
|
row.max = max + frequence
|
2023-01-13 04:54:29 +01:00
|
|
|
row.total = total
|
2024-10-16 23:29:18 +02:00
|
|
|
max += frequence
|
|
|
|
return row
|
2023-01-13 04:54:29 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
static async getRandom(table, type, subTypes = ['objet'], forcedRoll = undefined, localisation = undefined) {
|
2022-11-28 11:31:19 +01:00
|
|
|
if (table.length == 0) {
|
2023-01-13 04:54:29 +01:00
|
|
|
const typeName = Misc.typeName(type, subTypes[0]);
|
|
|
|
ui.notifications.warn(`Aucun ${typeName} trouvé dans ${localisation ?? ' les compendiums'}`);
|
2022-11-28 11:31:19 +01:00
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
return await CompendiumTableHelpers.selectRow(table, forcedRoll);
|
2022-11-26 03:13:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* -------------------------------------------- */
|
2022-11-28 11:31:19 +01:00
|
|
|
static async selectRow(table, forcedRoll = undefined) {
|
2022-11-26 03:13:45 +01:00
|
|
|
if (table.length == 0) {
|
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
const total = table[0].total;
|
|
|
|
const formula = `1d${total}`;
|
2024-10-16 23:29:18 +02:00
|
|
|
if (forcedRoll != undefined && (forcedRoll > total || forcedRoll <= 0)) {
|
|
|
|
ui.notifications.warn(`Jet forcé ${forcedRoll} en dehors de la table [1..${total}], le jet est relancé`);
|
2022-11-26 03:13:45 +01:00
|
|
|
forcedRoll = undefined;
|
|
|
|
}
|
|
|
|
const roll = forcedRoll ? { total: forcedRoll, formula } : await RdDDice.roll(formula, { showDice: HIDE_DICE });
|
|
|
|
const row = table.find(it => it.min <= roll.total && roll.total <= it.max);
|
|
|
|
row.roll = roll;
|
|
|
|
return row;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* -------------------------------------------- */
|
2024-12-02 22:07:20 +01:00
|
|
|
static async tableRowToChatMessage(row, type, options = { showSource: true }) {
|
2022-11-28 11:31:19 +01:00
|
|
|
if (!row) {
|
|
|
|
return;
|
|
|
|
}
|
2024-10-16 23:29:18 +02:00
|
|
|
const flavorContent = await renderTemplate('systems/foundryvtt-reve-de-dragon/templates/chat-compendium-table-roll.hbs', {
|
2022-11-26 03:13:45 +01:00
|
|
|
roll: row.roll,
|
2022-11-28 11:31:19 +01:00
|
|
|
document: row.document,
|
2023-01-13 04:54:29 +01:00
|
|
|
typeName: Misc.typeName(type, row.document?.type ?? 'objet'),
|
2022-11-26 03:13:45 +01:00
|
|
|
isGM: game.user.isGM,
|
2024-11-28 00:43:39 +01:00
|
|
|
options
|
2022-11-26 03:13:45 +01:00
|
|
|
});
|
|
|
|
const messageData = {
|
|
|
|
user: game.user.id,
|
2024-11-28 00:43:39 +01:00
|
|
|
rolls: [row.roll],
|
2022-11-26 03:13:45 +01:00
|
|
|
sound: CONFIG.sounds.dice,
|
|
|
|
content: flavorContent
|
|
|
|
};
|
2024-10-16 23:18:15 +02:00
|
|
|
await ChatUtility.createChatWithRollMode(messageData)
|
2022-11-26 03:13:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* -------------------------------------------- */
|
2022-11-30 01:11:31 +01:00
|
|
|
static async tableToChatMessage(table, type, subTypes, typeName = undefined) {
|
2024-10-16 23:29:18 +02:00
|
|
|
const flavorContent = await renderTemplate('systems/foundryvtt-reve-de-dragon/templates/chat-compendium-table.hbs', {
|
2022-11-30 01:11:31 +01:00
|
|
|
img: RdDItem.getDefaultImg(subTypes[0]),
|
|
|
|
typeName: typeName ?? Misc.typeName(type, subTypes[0]),
|
2022-11-26 03:13:45 +01:00
|
|
|
table,
|
|
|
|
isGM: game.user.isGM,
|
|
|
|
});
|
2023-11-21 16:03:26 +01:00
|
|
|
const messageData = {
|
2022-11-26 03:13:45 +01:00
|
|
|
user: game.user.id,
|
2024-10-16 23:18:15 +02:00
|
|
|
whisper: [game.user],
|
2022-11-26 03:13:45 +01:00
|
|
|
content: flavorContent
|
2023-11-21 16:03:26 +01:00
|
|
|
};
|
2024-10-16 23:18:15 +02:00
|
|
|
await ChatUtility.createChatWithRollMode(messageData)
|
2022-11-26 03:13:45 +01:00
|
|
|
}
|
|
|
|
|
2022-11-28 11:31:19 +01:00
|
|
|
}
|