Skip to content

Commit

Permalink
add an inline roll action on compendium to allow rolling without crea…
Browse files Browse the repository at this point in the history
…ting rolltable
  • Loading branch information
Daimakaicho committed Jul 14, 2021
1 parent 78f8112 commit e97f7a7
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
35 changes: 35 additions & 0 deletions scripts/better-tables.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import { StoryBuilder } from './story/story-builder.js';
import { StoryChatCard } from './story/story-chat-card.js';
import { BRTBuilder } from './core/brt-builder.js';
import { BetterResults } from './core/brt-table-results.js';
import { getRandomItemFromCompendium } from "./core/utils.js";
import { BRTCONFIG } from "./core/config.js";


export class BetterTables {
__constructor() {
this._spellCache = undefined;
Expand Down Expand Up @@ -175,6 +177,14 @@ export class BetterTables {
BetterTables.menuCallBackCreateTable(li.data('pack'));
}
});

options.push({
"name": "Roll on compendium",
"icon": '<i class="fas fa-dice-d20"></i>',
"callback": li => {
BetterTables.menuCallBackRollCompendium(li.data('pack'));
}
});
}

/**
Expand Down Expand Up @@ -208,4 +218,29 @@ export class BetterTables {
const rolltable = game.tables.get(rolltable_id);
await game.betterTables.betterTableRoll(rolltable);
}

/**
*
* @param {String} compendium ID of the compendium to roll
*/
static async menuCallBackRollCompendium(compendium) {
// Get random item from compendium
const item = await getRandomItemFromCompendium(compendium);

// prepare card data
const fontSize = Math.max(60, 100 - Math.max(0, item.name.length - 27) * 2);
const chatCardData = {
itemsData: [
{ item: item, quantity: 1, fontSize: fontSize }
]
};
const cardHtml = await renderTemplate("modules/better-rolltables/templates/loot-chat-card.hbs", chatCardData);
const chatData = {
flavor: `Rolled from compendium ${item.pack}`,
sound: "sounds/dice.wav",
user: game.user.data._id,
content: cardHtml
};
ChatMessage.create(chatData);
}
}
18 changes: 18 additions & 0 deletions scripts/core/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,22 @@ export function separateIdComendiumName(stringWithComendium) {
*/
export async function getItemFromCompendium(item) {
return findInCompendiumByName(item.collection, item.text);
}

/**
*
* @param {object} compendium reference to compendium to roll
* @returns {object} item from compendium
*/
export async function getRandomItemFromCompendium(compendium) {
const pack = game.packs.get(compendium);
if (!pack) return;
const size = pack.index.size;
if (size === 0) {
ui.notifications.warn(`Compendium ${pack.title} is empty.`);
return;
}
const randonIndex = Math.floor(Math.random() * size);
const randomItem = pack.index.contents[randonIndex];
return pack.getDocument(randomItem._id);
}

0 comments on commit e97f7a7

Please sign in to comment.