forked from public/foundryvtt-reve-de-dragon
Fréquences par milieu pour l'environnement
Les herbes et les ingrédients peuvent être cherchées/tirées
This commit is contained in:
@ -16,8 +16,13 @@ const CONFIGURABLE_COMPENDIUMS = {
|
||||
'rencontres': { label: "Rencontres dans les TMR", type: "Item" },
|
||||
'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" },
|
||||
'botanique': { label: "Herbes & plantes", type: "Item" },
|
||||
'equipement': { label: "Equipements", type: "Item" },
|
||||
}
|
||||
|
||||
/**
|
||||
* ======= Gestion des accès aux compendiums systèmes (ou surchargés) =======
|
||||
*/
|
||||
export class SystemCompendiums extends FormApplication {
|
||||
static init() {
|
||||
Object.keys(CONFIGURABLE_COMPENDIUMS).forEach(compendium => {
|
||||
@ -50,7 +55,7 @@ export class SystemCompendiums extends FormApplication {
|
||||
return game.packs.get(SystemCompendiums.getCompendium(compendium));
|
||||
}
|
||||
|
||||
static async getContent(compendium, docType) {
|
||||
static async getPackContent(compendium, docType) {
|
||||
const pack = SystemCompendiums.getPack(compendium);
|
||||
if (pack.metadata.type == docType) {
|
||||
return await pack.getDocuments();
|
||||
@ -83,35 +88,17 @@ export class SystemCompendiums extends FormApplication {
|
||||
}
|
||||
|
||||
static async getItems(compendium, itemType = undefined) {
|
||||
const items = await SystemCompendiums.getContent(compendium, 'Item');
|
||||
const items = await SystemCompendiums.getPackContent(compendium, 'Item');
|
||||
return (itemType ? items.filter(it => it.type == itemType) : items);
|
||||
}
|
||||
|
||||
static async buildTable(compendium, itemFrequence, filter, type = 'Item', sorting = undefined) {
|
||||
let elements = await SystemCompendiums.getContent(compendium, type);
|
||||
elements = elements.filter(filter).filter(it => itemFrequence(it) > 0)
|
||||
static async getContent(compendium, type, filter, itemFrequence, sorting) {
|
||||
let elements = await SystemCompendiums.getPackContent(compendium, type);
|
||||
elements = elements.filter(filter).filter(it => itemFrequence(it) > 0);
|
||||
if (sorting) {
|
||||
elements = elements.sort(sorting);
|
||||
}
|
||||
let max = 0;
|
||||
const table = elements
|
||||
.map(it => {
|
||||
const frequence = itemFrequence(it)
|
||||
let row = { document: it, frequence: frequence, min: max + 1, max: max + frequence }
|
||||
max += frequence;
|
||||
return row;
|
||||
});
|
||||
table.forEach(it => it.total = max);
|
||||
return table;
|
||||
}
|
||||
|
||||
static async getRandom(compendium, type, subType, toChat = true, itemFrequence = it => it.system.frequence, filter = it => true) {
|
||||
const table = new SystemCompendiumTable(compendium, type, subType);
|
||||
return await table.getRandom(toChat, itemFrequence, filter);
|
||||
}
|
||||
static async chatTableItems(compendium, type, subType, itemFrequence = it => it.system.frequence, filter = it => true) {
|
||||
const table = new SystemCompendiumTable(compendium, type, subType, itemFrequence);
|
||||
await table.chatTable(itemFrequence, filter);
|
||||
return elements;
|
||||
}
|
||||
|
||||
static async getDefaultItems(compendium) {
|
||||
@ -180,48 +167,69 @@ export class SystemCompendiums extends FormApplication {
|
||||
}
|
||||
}
|
||||
|
||||
export class SystemCompendiumTable {
|
||||
/**
|
||||
* ======= Gestion de jets dans une table correspondant à un compendium =======
|
||||
*/
|
||||
export class CompendiumTable {
|
||||
|
||||
constructor(compendium, type, subType, sorting = undefined) {
|
||||
this.compendium = compendium;
|
||||
this.type = type;
|
||||
this.subType = subType;
|
||||
this.compendium = compendium;
|
||||
this.sourceCompendium = SystemCompendiums.getCompendium(compendium);
|
||||
this.sorting = sorting
|
||||
this.sorting = sorting ?? Misc.ascending(it => it.name);
|
||||
}
|
||||
|
||||
typeName() {
|
||||
return Misc.typeName(this.type, this.subType);
|
||||
}
|
||||
applyType(filter) {
|
||||
return it => it.type == this.subType && filter(it);
|
||||
async getContent(itemFrequence = it => it.system.frequence, filter = it => true) {
|
||||
return await SystemCompendiums.getContent(this.compendium,
|
||||
this.type,
|
||||
it => this.subType == it.type && filter(it),
|
||||
itemFrequence,
|
||||
this.sorting);
|
||||
}
|
||||
|
||||
async getRandom(toChat = true, itemFrequence = it => it.system.frequence, filter = it => true, forcedRoll = undefined) {
|
||||
const table = await this.$buildTable(itemFrequence, filter);
|
||||
async buildTable(itemFrequence = it => it.system.frequence, filter = it => true) {
|
||||
const elements = await this.getContent(filter, itemFrequence);
|
||||
return CompendiumTableHelpers.buildTable(elements, itemFrequence);
|
||||
}
|
||||
|
||||
async getRandom(itemFrequence = it => it.system.frequence, filter = it => true, forcedRoll = undefined) {
|
||||
const table = await this.buildTable(itemFrequence, filter);
|
||||
return await CompendiumTableHelpers.getRandom(table, this.type, this.subType, forcedRoll, SystemCompendiums.getCompendium(compendium));
|
||||
}
|
||||
|
||||
async toChatMessage(itemFrequence = it => it.system.frequence, filter = it => true, typeName = undefined) {
|
||||
const table = await this.buildTable(itemFrequence, filter);
|
||||
await CompendiumTableHelpers.tableToChatMessage(table, this.type, this.subType, typeName);
|
||||
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 => {
|
||||
const frequence = itemFrequence(it);
|
||||
let row = { document: it, frequence: frequence, min: max + 1, max: max + frequence, total: total };
|
||||
max += frequence;
|
||||
return row;
|
||||
});
|
||||
}
|
||||
|
||||
static async getRandom(table, type, subType, forcedRoll = undefined, localisation = undefined) {
|
||||
if (table.length == 0) {
|
||||
ui.notifications.warn(`Aucun ${this.typeName()} dans ${this.sourceCompendium}`);
|
||||
ui.notifications.warn(`Aucun ${Misc.typeName(type, subType)} trouvé dans ${localisation ?? ' les compendiums'}`);
|
||||
return undefined;
|
||||
}
|
||||
const row = await this.$selectRow(table, forcedRoll);
|
||||
if (row && toChat) {
|
||||
await this.$chatRolledResult(row);
|
||||
}
|
||||
return row;
|
||||
}
|
||||
|
||||
async chatTable(itemFrequence = it => it.system.frequence, filter = it => true, typeName = undefined) {
|
||||
const table = await this.$buildTable(itemFrequence, filter);
|
||||
await this.$chatSystemCompendiumTable(table, typeName);
|
||||
}
|
||||
|
||||
async $buildTable(itemFrequence, filter) {
|
||||
return await SystemCompendiums.buildTable(this.compendium, itemFrequence, this.applyType(filter), this.type, this.sorting);
|
||||
return await CompendiumTableHelpers.selectRow(table, forcedRoll);
|
||||
}
|
||||
|
||||
/* -------------------------------------------- */
|
||||
async $selectRow(table, forcedRoll = undefined) {
|
||||
static async selectRow(table, forcedRoll = undefined) {
|
||||
if (table.length == 0) {
|
||||
return undefined
|
||||
}
|
||||
@ -238,14 +246,16 @@ export class SystemCompendiumTable {
|
||||
}
|
||||
|
||||
/* -------------------------------------------- */
|
||||
async $chatRolledResult(row) {
|
||||
static async tableRowToChatMessage(row, type = 'Item') {
|
||||
if (!row) {
|
||||
return;
|
||||
}
|
||||
const percentages = (row.total == 100) ? '%' : ''
|
||||
const flavorContent = await renderTemplate('systems/foundryvtt-reve-de-dragon/templates/chat-compendium-table-roll.html', {
|
||||
roll: row.roll,
|
||||
document: row?.document,
|
||||
document: row.document,
|
||||
percentages,
|
||||
typeName: this.typeName(),
|
||||
sourceCompendium: this.sourceCompendium,
|
||||
typeName: Misc.typeName(type, row.document.type),
|
||||
isGM: game.user.isGM,
|
||||
});
|
||||
const messageData = {
|
||||
@ -260,11 +270,10 @@ export class SystemCompendiumTable {
|
||||
}
|
||||
|
||||
/* -------------------------------------------- */
|
||||
async $chatSystemCompendiumTable(table, typeName) {
|
||||
static async tableToChatMessage(table, type, subType, typeName = undefined) {
|
||||
const flavorContent = await renderTemplate('systems/foundryvtt-reve-de-dragon/templates/chat-compendium-table.html', {
|
||||
img: RdDItem.getDefaultImg(this.subType),
|
||||
typeName: typeName ?? this.typeName(),
|
||||
sourceCompendium: this.sourceCompendium,
|
||||
img: RdDItem.getDefaultImg(subType),
|
||||
typeName: typeName ?? Misc.typeName(type, subType),
|
||||
table,
|
||||
isGM: game.user.isGM,
|
||||
});
|
||||
@ -275,5 +284,4 @@ export class SystemCompendiumTable {
|
||||
}, { rollMode: "gmroll" });
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user