Skip to content

Commit

Permalink
Merge pull request #160 from ultrakorne/DanielBoettner-pf1-missingSpe…
Browse files Browse the repository at this point in the history
…llCompendiumError

PF1 support, API fixes and eslint optimization
  • Loading branch information
DanielBoettner authored Dec 30, 2021
2 parents 6bcbedb + 6d99bd7 commit a1fc4b5
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 77 deletions.
8 changes: 8 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,13 @@ module.exports = {
Application: 'readonly'
},
rules: {
'no-unused-vars': ['error'],
'one-var': [2, 'consecutive'],
'comma-dangle': ['error', 'never'],
semi: [2, 'always'],
'semi-spacing': 2,
'no-extra-semi': 2,
'no-unexpected-multiline': 2,
indent: [4, 'error']
}
}
2 changes: 1 addition & 1 deletion module.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "better-rolltables",
"title": "Better Roll Tables",
"description": "Adding functionality to roll tables, especially to roll treasure and magic items",
"version": "1.8.92",
"version": "1.8.93",
"manifestPlusVersion": "1.2.0",
"authors": [
{
Expand Down
35 changes: 14 additions & 21 deletions scripts/API.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,19 @@ import { getRandomItemFromCompendium } from './core/utils.js';
* Create a new API class and export it as default
*/
class API {
constructor(version, config) {
this.version = version;
this.config = config;
this.endpoints = {};
this.methods = {};
}

/**
* Get better rolltable tags from settings
*
*
*/
getTags() {
static getTags() {
return game.settings.get(MODULE.ns, BRTCONFIG.TAGS.USE);
}

/**
*
* @param {*} tableEntity
*/
async generateLoot(tableEntity, options = {}) {
*
* @param {*} tableEntity
*/
static async generateLoot(tableEntity, options = {}) {
const builder = new BRTBuilder(tableEntity),
results = await builder.betterRoll(),
br = new BetterResults(results),
Expand Down Expand Up @@ -77,28 +70,28 @@ class API {
}

/**
* @module BetterRolltables.API.createTableFromCompendium
*
* @module BetterRolltables.API.createRolltableFromCompendium
*
* @description Create a new RollTable by extracting entries from a compendium.
*
* @version 1.0.0
* @version 1.0.1
* @since 1.8.7
*
*
* @param {string} compendiumName the name of the compendium to use for the table generation
* @param {string} tableName the name of the table entity that will be created
* @param {function(Document)} weightPredicate a function that returns a weight (number) that will be used
* for the tableResult weight for that given entity. returning 0 will exclude the entity from appearing in the table
*
*
* @returns {Promise<Document>} the table entity that was created
*/
async createRolltableFromCompendium(
static async createRolltableFromCompendium(
compendiumName,
tableName = compendiumName + ' RollTable',
{ weightPredicate = null } = {}
) {
const compendium = game.packs.get(compendiumName);
let msg = { name: compendiumName, tableName: tableName };
api_msg = MODULE.ns + '.api | ';
let msg = { name: compendiumName, tableName: tableName },
api_msg = MODULE.ns + '.api | ';

if (compendium === undefined) {
api.msg += game.i18n.format('BRT.api.msg.compendiumNotFound', msg);
Expand Down
22 changes: 11 additions & 11 deletions scripts/better-tables.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ export class BetterTables {
}

/**
*
* @param {*} tableEntity
*
* @param {*} tableEntity
*/
async generateLoot(tableEntity, options = {}) {
const brtBuilder = new BRTBuilder(tableEntity),
Expand All @@ -45,11 +45,11 @@ export class BetterTables {

/**
* Roll a table an add the resulting loot to a given token.
*
* @param {RollTable} tableEntity
* @param {TokenDocument} token
*
* @param {RollTable} tableEntity
* @param {TokenDocument} token
* @param {options} object
* @returns
* @returns
*/
async addLootToSelectedToken(tableEntity, token = null, options = null) {
let tokenstack = [];
Expand Down Expand Up @@ -147,7 +147,7 @@ export class BetterTables {
* @param {string} compendiumName the name of the compendium to use for the table generation
* @param {function(Document)} weightPredicate a function that returns a weight (number) that will be used
* for the tableResult weight for that given entity. returning 0 will exclude the entity from appearing in the table
*
*
* @deprecated use api.createRolltableFromCompendium instead
*/

Expand Down Expand Up @@ -218,7 +218,7 @@ export class BetterTables {
name: i18n('BRT.api.msg.generateRolltableFromCompendium'),
icon: '<i class="fas fa-th-list"></i>',
callback: li => {
api.createTableFromCompendium(li.data('pack'))
api.createRolltableFromCompendium(li.data('pack'))
}
})

Expand Down Expand Up @@ -262,11 +262,11 @@ export class BetterTables {

/**
* Create card content from compendium content
*
*
* @param {String} compendium compendium name
*
*
* @returns {Promise<{flavor: string, sound: string, user: *, content: *}>}
*
*
* @deprecated use api.rollCompendiumAsRolltable instead
*/
static async rollCompendiumAsRolltable(compendium) {
Expand Down
27 changes: 19 additions & 8 deletions scripts/core/settingsConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { MODULE , BRTCONFIG } from './config.js';
import { i18n } from './utils.js';

const WORLD = 'world',
GROUP_DEFAULT = 'defaults',
GROUP_DEFAULT = 'defaults',
GROUP_UI = 'UI',
GROUP_LOOT = 'Loot',
GROUP_TAGS = 'Tags';
Expand All @@ -21,7 +21,7 @@ export class Settings {
defaultSpellCompendium = 'dnd5e.spells';

if (game.system.id === 'pf2e') {
defaultLootSheet = 'pf2e.LootSheetNPC';
defaultLootSheet = 'pf2e.LootSheetPF2e';
defaultSpellCompendium = 'pf2e.spells-srd';

BRTCONFIG.QUANTITY_PROPERTY_PATH = 'data.quantity.value';
Expand All @@ -32,6 +32,17 @@ export class Settings {
BRTCONFIG.SCROLL_REGEX = /\s*Scroll\s*of\s*(\d+)/gi;
}

if (game.system.id === 'pf1') {
defaultLootSheet = 'PF1.ActorSheetPFNPCLoot';
defaultSpellCompendium = 'pf1.spells';

BRTCONFIG.QUANTITY_PROPERTY_PATH = 'data.quantity';
BRTCONFIG.PRICE_PROPERTY_PATH = 'data.price';
BRTCONFIG.SPELL_LEVEL_PATH = 'data.level';
BRTCONFIG.ITEM_LOOT_TYPE = 'loot';
BRTCONFIG.SCROLL_REGEX = /\s*Scroll\s*of\s*(\d+)/gi;
}

game.settings.registerMenu(MODULE.ns, "helpersOptions", {
name: i18n("User Interface Integration"),
label: i18n("BRT.Settings.Module.AdvancedSettings.Title"),
Expand Down Expand Up @@ -96,7 +107,7 @@ export class Settings {
config: false,
default: false,
type: Boolean
});
});

game.settings.register(MODULE.ns, BRTCONFIG.SHOW_OPEN_BUTTONS, {
name: i18n('BRT.Buttons.Open.Title'),
Expand Down Expand Up @@ -165,7 +176,7 @@ export class Settings {
}

/**
*
*
*/
static _registerTagsSettings() {
game.settings.register(MODULE.ns, BRTCONFIG.TAGS.USE, {
Expand Down Expand Up @@ -210,7 +221,7 @@ class BetterRolltableSettingsConfig extends FormApplication {
`${MODULE.path}/templates/partials/settings.hbs`,
`${MODULE.path}/templates/partials/menu.hbs`,
]);

return this;
}

Expand Down Expand Up @@ -277,7 +288,7 @@ class BetterRolltableSettingsConfig extends FormApplication {
formData = expandObject(formData)[MODULE.ns];
for(let [k,v] of Object.entries(formData)){
await game.settings.set(MODULE.ns, k, v);
}
}
}

/* -------------------------------------------- */
Expand Down Expand Up @@ -338,13 +349,13 @@ class BetterRolltableSettingsConfig extends FormApplication {
i18nName: i18n("BRT.Settings.Module.AdvancedSettings.Menu.UI.Title"),
class: "fas fa-cog", menus: [], settings: []
},
{
{
name: GROUP_LOOT,
description: i18n("BRT.Settings.Module.AdvancedSettings.Menu.Loot.Description"),
i18nName: i18n("BRT.Settings.Module.AdvancedSettings.Menu.Loot.Title"),
class: "fas fa-cog", menus: [], settings: []
},
{
{
name: GROUP_TAGS,
description: i18n("BRT.Settings.Module.AdvancedSettings.Menu.Tags.Description"),
i18nName: i18n("BRT.Settings.Module.AdvancedSettings.Menu.Tags.Title"),
Expand Down
7 changes: 7 additions & 0 deletions scripts/core/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ export function addRollModeToChatData (chatData, rollMode) {
}
}

/**
*
* @param {string} compendiumName
* @param {string} entityName
*
* @returns {Item}
*/
export async function findInCompendiumByName (compendiumName, entityName) {
const compendium = game.packs.get(compendiumName)
if (compendium) {
Expand Down
28 changes: 17 additions & 11 deletions scripts/hooks/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ import { getIconByEntityType } from '../core/utils.js';
/**
* @module BetterRollTables.BetterRolltableHooks
* @typicalname BetterRolltableHooks
*
*
* @version 1.0.0
*
*
*/
class BetterRolltableHooks {

/**
* Hooks on game hooks and attaches methods
* Hooks on game hooks and attaches methods
*/
static init() {
Hooks.once("init", BetterRolltableHooks.foundryInit);
Expand All @@ -29,11 +29,17 @@ class BetterRolltableHooks {
}

static foundrySetup() {
const moduleData = game.modules.get(MODULE.ns);
moduleData.public = { API };
const moduleData = game.modules.get(MODULE.ns);

// Freeze the public API so it can't be modified.
Object.freeze(moduleData.public);
/**
* @type {API}
*/
moduleData.public = {
API
};

// Freeze the public API so it can't be modified.
Object.freeze(moduleData.public);
}

static async foundryReady() {
Expand All @@ -53,7 +59,7 @@ class BetterRolltableHooks {
if (game.user.isGM && VersionCheck.check(MODULE.ns)) {
renderWelcomeScreen();
}

/** Register Handlebar helpers **/
/** checks if the first argument is equal to any of the subsequent arguments */
Handlebars.registerHelper('ifcontain', function () {
Expand All @@ -63,7 +69,7 @@ class BetterRolltableHooks {
}
return options.inverse(this)
});

/** checks if the first argument is greater than the second argument */
Handlebars.registerHelper('ifgt', function (a, b, options) {
return a > b ? options.fn(this) : options.inverse(this)
Expand Down Expand Up @@ -113,7 +119,7 @@ class BetterRolltableHooks {
return options.fn(this)
}
});

await game.betterTables.updateSpellCache();
}

Expand All @@ -123,7 +129,7 @@ class BetterRolltableHooks {

/**
* Register with AIP
*
*
* Register fields with autocomplete inline properties
*/
static async onAIPSetup() {
Expand Down
34 changes: 20 additions & 14 deletions scripts/loot/loot-manipulation.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,41 @@
import { findInCompendiumById } from '../core/utils.js'
import { MODULE, BRTCONFIG } from '../core/config.js'

export class LootManipulator {

/**
*
* @param {number} level
*
* @returns {Item}
*/
async _getRandomSpell (level) {
const spells = game.betterTables.getSpellCache().filter(spell => spell.data.level === level)
const randomIndex = Math.floor(Math.random() * spells.length)
const spell = spells[randomIndex]
const spells = game.betterTables.getSpellCache().filter(spell => BRTCONFIG.SPELL_LEVEL_PATH === level),
spell = spells[Math.floor(Math.random() * spells.length)]
return findInCompendiumById(spell.collection, spell._id)
}

/**
*
* @param {*} itemData
*
* @returns
*/
async preItemCreationDataManipulation (itemData) {
// we duplicate item now in order to modify it
itemData = duplicate(itemData)

// const match = BRTCONFIG.SCROLL_REGEX.exec(itemData.name);
let match = /\s*Spell\s*Scroll\s*(\d+|cantrip)/gi.exec(itemData.name)
const match = BRTCONFIG.SCROLL_REGEX.exec(itemData.name);

if (!match) {
// pf2e temporary FIXME add this in a proper config
match = /\s*Scroll\s*of\s*(\d+)/gi.exec(itemData.name)
}
itemData = duplicate(itemData);

if (!match) {
return itemData
}

// if its a scorll then open compendium
// If it is a scroll then open the compendium
const level = match[1].toLowerCase() === 'cantrip' ? 0 : parseInt(match[1])
const itemEntity = await this._getRandomSpell(level)

if (!itemEntity) {
ui.notifications.warn(`no spell of level ${level} found in compendium ${itemEntity.collection} `)
ui.notifications.warn(MODULE.ns + ` | No spell of level ${level} found in compendium ${itemEntity.collection} `)
return itemData
}

Expand Down
Loading

0 comments on commit a1fc4b5

Please sign in to comment.