Skip to content

Commit

Permalink
Merge pull request #99 from Daimakaicho/master
Browse files Browse the repository at this point in the history
Roll from everywhere!
  • Loading branch information
ultrakorne authored Jul 15, 2021
2 parents 22648bc + e97f7a7 commit 197017d
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 1 deletion.
61 changes: 60 additions & 1 deletion 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,13 +177,70 @@ 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'));
}
});
}

/**
*
* @param {String} compendium
* @param {String} compendium_id
*/
static async menuCallBackCreateTable(compendium_id){
await game.betterTables.createTableFromCompendium('BRT | '+ compendium_id,compendium_id);
}

/**
* Add a roll option in context menu of rolltables
* @param {html} html
* @param {Array} options
*/
static async enhanceRolltableContextMenu(html, options) {
options.push({
"name": "Roll table",
"icon": '<i class="fas fa-dice-d20"></i>',
"callback": li => {
BetterTables.menuCallBackRollTable(li.data("entityId"));
}
});
}

/**
*
* @param {String} rolltable_id ID of the rolltable to roll
*/
static async menuCallBackRollTable(rolltable_id){
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);
}
}
1 change: 1 addition & 0 deletions scripts/brt-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Hooks.on("updateCompendium", async function (pack, documents, option, userId) {

Hooks.on("renderRollTableConfig", BetterRT.enhanceRollTableView);
Hooks.on('getCompendiumDirectoryEntryContext', BetterTables.enhanceCompendiumContextMenu);
Hooks.on('getRollTableDirectoryEntryContext', BetterTables.enhanceRolltableContextMenu);

function registerSettings() {
let defaultLootSheet = "dnd5e.LootSheet5eNPC";
Expand Down
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 197017d

Please sign in to comment.