foundryvtt-reve-de-dragon/module/rdd-rolltables.js

110 lines
4.2 KiB
JavaScript
Raw Normal View History

import { Grammar } from "./grammar.js";
import { CompendiumTable, CompendiumTableHelpers, SystemCompendiums } from "./settings/system-compendiums.js";
2022-11-06 21:39:47 +01:00
export class RdDRollTables {
2020-11-12 18:42:41 +01:00
/* -------------------------------------------- */
2020-11-12 18:42:41 +01:00
static async genericGetTableResult(tableName, toChat) {
let table = RdDRollTables.getWorldTable(tableName) ?? (await RdDRollTables.getSystemTable(tableName));
const draw = await table.draw({ displayChat: toChat, rollMode: "gmroll" });
2021-05-11 21:21:33 +02:00
return draw.results.length > 0 ? draw.results[0] : undefined;
}
static getWorldTable(tableName) {
return game.tables.find(table => table.name.toLowerCase() == tableName.toLowerCase());
}
static async getSystemTable(tableName) {
2022-11-06 21:39:47 +01:00
const pack = SystemCompendiums.getPack("tables-diverses");
const index = await pack.getIndex();
const entry = index.find(e => e.name === tableName);
return await pack.getDocument(entry._id);
}
2020-11-12 18:42:41 +01:00
/* -------------------------------------------- */
2021-05-11 21:21:33 +02:00
static async drawItemFromRollTable(tableName, toChat = false) {
const drawResult = await RdDRollTables.genericGetTableResult(tableName, toChat);
2022-06-12 12:14:55 +02:00
const pack = game.packs.get(drawResult.documentCollection)
2022-11-06 21:39:47 +01:00
return await pack.getDocument(drawResult.documentId)
}
2020-12-17 12:29:54 +01:00
/* -------------------------------------------- */
static async drawTextFromRollTable(tableName, toChat) {
2021-05-11 21:21:33 +02:00
const drawResult = await RdDRollTables.genericGetTableResult(tableName, toChat);
return drawResult.text;
2020-12-17 12:29:54 +01:00
}
/* -------------------------------------------- */
static async getCompetence(toChat = false) {
if (toChat == 'liste') {
return await RdDRollTables.listOrRoll('competences', 'Item', ['competence'], toChat, it => 1);
}
else {
return await RdDRollTables.drawItemFromRollTable("Détermination aléatoire de compétence", toChat);
}
}
/* -------------------------------------------- */
static async getSouffle(toChat = false) {
return await RdDRollTables.listOrRoll('souffles-de-dragon', 'Item', ['souffle'], toChat);
}
/* -------------------------------------------- */
static async getQueue(toChat = false) {
return await RdDRollTables.listOrRoll('queues-de-dragon', 'Item', ['queue'], toChat);
}
static async getDesirLancinant(toChat = false) {
return await RdDRollTables.listOrRoll('queues-de-dragon', 'Item', ['queue'], toChat,
it => it.system.frequence,
it => it.system.categorie == 'lancinant');
}
static async getIdeeFixe(toChat = false) {
return await RdDRollTables.listOrRoll('queues-de-dragon', 'Item', ['queue'], toChat,
it => it.system.frequence,
it => it.system.categorie == 'ideefixe');
}
/* -------------------------------------------- */
static async getTeteHR(toChat = false) {
return await RdDRollTables.listOrRoll('tetes-de-dragon-pour-haut-revants', 'Item', ['tete'], toChat);
}
2020-11-12 18:42:41 +01:00
/* -------------------------------------------- */
static async getTete(toChat = false) {
return await RdDRollTables.listOrRoll('tetes-de-dragon-pour-tous-personnages', 'Item', ['tete'], toChat);
}
/* -------------------------------------------- */
static async getOmbre(toChat = false) {
return await RdDRollTables.listOrRoll('ombres-de-thanatos', 'Item', ['ombre'], toChat);
}
/* -------------------------------------------- */
2020-11-12 18:42:41 +01:00
static async getTarot(toChat = true) {
return await RdDRollTables.listOrRoll('tarot-draconique', 'Item', ['tarot'], toChat);
}
/* -------------------------------------------- */
static async listOrRoll(compendium, type, subTypes, toChat, itemFrequence = it => it.system.frequence, filter = it => true) {
const table = new CompendiumTable(compendium, type, subTypes);
if (toChat == 'liste') {
return await table.toChatMessage(itemFrequence, filter);
}
const row = await table.getRandom(itemFrequence, filter);
if (row) {
await CompendiumTableHelpers.tableRowToChatMessage(row, type);
}
return row;
}
2020-12-17 12:29:54 +01:00
/* -------------------------------------------- */
static async getMaladresse(options = { toChat: false, arme: false }) {
2020-12-17 12:29:54 +01:00
return await RdDRollTables.drawTextFromRollTable(
options.arme ? "Maladresse armé" : "Maladresses non armé",
options.toChat);
}
2020-11-12 18:42:41 +01:00
}