Tirer dans les compendiums selon les fréquences

This commit is contained in:
Vincent Vandemeulebrouck
2022-11-26 03:13:45 +01:00
parent 970be67537
commit 0dacbefd6b
15 changed files with 370 additions and 165 deletions

View File

@ -1,4 +1,7 @@
import { SYSTEM_RDD } from "../constants.js";
import { HIDE_DICE, SYSTEM_RDD } from "../constants.js";
import { RdDItem } from "../item.js";
import { Misc } from "../misc.js";
import { RdDDice } from "../rdd-dice.js";
const COMPENDIUM_SETTING_PREFIX = 'compendium-';
@ -63,6 +66,7 @@ export class SystemCompendiums extends FormApplication {
case 'vehicule': return [];
}
}
/* -------------------------------------------- */
static async getWorldOrCompendiumItems(itemType, compendium) {
let items = game.items.filter(it => it.type == itemType);
@ -80,7 +84,34 @@ export class SystemCompendiums extends FormApplication {
static async getItems(compendium, itemType = undefined) {
const items = await SystemCompendiums.getContent(compendium, 'Item');
return itemType ? items.filter(it => it.type == itemType) : items;
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)
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);
}
static async getDefaultItems(compendium) {
@ -125,11 +156,13 @@ export class SystemCompendiums extends FormApplication {
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
} });
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
@ -145,5 +178,102 @@ export class SystemCompendiums extends FormApplication {
game.settings.set(SYSTEM_RDD, systemCompendium.setting, value);
});
}
}
}
export class SystemCompendiumTable {
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
}
typeName() {
return game.i18n.localize(`${this.type.toUpperCase()}.Type${Misc.upperFirst(this.subType)}`);
}
applyType(filter) {
return it => it.type == this.subType && filter(it);
}
async getRandom(toChat = true, itemFrequence = it => it.system.frequence, filter = it => true, forcedRoll = undefined) {
const table = await this.$buildTable(itemFrequence, filter);
if (table.length == 0) {
ui.notifications.warn(`Aucun ${this.typeName()} dans ${this.sourceCompendium}`);
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);
}
/* -------------------------------------------- */
async $selectRow(table, forcedRoll = undefined) {
if (table.length == 0) {
return undefined
}
const total = table[0].total;
const formula = `1d${total}`;
if (forcedRoll == undefined && (forcedRoll > total || forcedRoll <= 0)) {
ui.notifications.warn(`Jet de rencontre ${forcedRoll} en dehors de la table [1..${total}], le jet est relancé`);
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;
}
/* -------------------------------------------- */
async $chatRolledResult(row) {
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,
percentages,
typeName: this.typeName(),
sourceCompendium: this.sourceCompendium,
isGM: game.user.isGM,
});
const messageData = {
// flavor: flavorContent,
user: game.user.id,
type: CONST.CHAT_MESSAGE_TYPES.ROLL,
roll: row.roll,
sound: CONFIG.sounds.dice,
content: flavorContent
};
ChatMessage.create(messageData, { rollMode: "gmroll" });
}
/* -------------------------------------------- */
async $chatSystemCompendiumTable(table, typeName) {
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,
table,
isGM: game.user.isGM,
});
ChatMessage.create({
user: game.user.id,
whisper: game.user.id,
content: flavorContent
}, { rollMode: "gmroll" });
}
}