diff --git a/module/actor/actor.js b/module/actor/actor.js index 969f368..ffa8b4c 100644 --- a/module/actor/actor.js +++ b/module/actor/actor.js @@ -1,3 +1,5 @@ +import { fromIdUuid } from "../mosh.js"; + /** * Extend the base Actor entity by defining a custom roll data structure which is ideal for the Simple system. * @extends {Actor} @@ -1375,29 +1377,8 @@ export class MothershipActor extends Actor { //A script to return the data from a table. async getRollTableData(tableId){ - let currentLocation = ''; - let tableLocation = ''; - //find where this table is located - //get current compendium - let compendium = game.packs; - //loop through each compendium - compendium.forEach(function(pack){ - //is this a pack of rolltables? - if (pack.metadata.type === 'RollTable') { - //log where we are - currentLocation = pack.metadata.id; - //loop through each pack to find the right table - pack.index.forEach(function(table) { - //is this our table? - if (table._id === tableId) { - //grab the table location - tableLocation = currentLocation; - } - }); - } - }); - //get table data - let tableData = await game.packs.get(tableLocation).getDocument(tableId); + + let tableData = await fromIdUuid(tableId,{type:"RollTable"}); //get table name let tableName = tableData.name; //get table name @@ -1545,27 +1526,9 @@ export class MothershipActor extends Actor { //set variables rollString = chosenRollType[0]; } - //find where this table is located + + let tableData = await fromIdUuid(tableId,{type:"RollTable"}); //get current compendium - let compendium = game.packs; - //loop through each compendium - compendium.forEach(function(pack){ - //is this a pack of rolltables? - if (pack.metadata.type === 'RollTable') { - //log where we are - currentLocation = pack.metadata.id; - //loop through each pack to find the right table - pack.index.forEach(function(table) { - //is this our table? - if (table._id === tableId) { - //grab the table location - tableLocation = currentLocation; - } - }); - } - }); - //get table data - let tableData = await game.packs.get(tableLocation).getDocument(tableId); //get table name let tableName = tableData.name; //get table name @@ -2625,29 +2588,9 @@ export class MothershipActor extends Actor { //prepare attribute label attributeLabel = 'Bankruptcy'; //get the bankruptcy table - //get current compendium - let compendium = game.packs; - let currentLocation = ``; - let tableLocation = ``; - let tableId = game.settings.get('mosh','table1eBankruptcy'); - //loop through each compendium - compendium.forEach(function(pack){ - //is this a pack of rolltables? - if (pack.metadata.type === 'RollTable') { - //log where we are - currentLocation = pack.metadata.id; - //loop through each pack to find the right table - pack.index.forEach(function(table) { - //is this our table? - if (table._id === tableId) { - //grab the table location - tableLocation = currentLocation; - } - }); - } - }); - //get table data - let tableData = await game.packs.get(tableLocation).getDocument(tableId); + let tableId = game.settings.get('mosh','table1eBankruptcy'); + //get Table Data + let tableData = await fromIdUuid(tableId,{type:"RollTable"}); //prep text for success if (parsedRollResult.success && parsedRollResult.critical) { //flavor text @@ -3121,27 +3064,8 @@ export class MothershipActor extends Actor { let newValue = 0; let flavorText = ``; let chatId = (game.release.generation >= 12 ? foundry.utils.randomID(): randomID()) - //find where this item is located - //get current compendium - let compendium = game.packs; - //loop through each compendium - compendium.forEach(function(pack){ - //is this a pack of items? - if (pack.metadata.type === 'Item') { - //log where we are - currentLocation = pack.metadata.id; - //loop through each pack to find the right table - pack.index.forEach(function(item) { - //is this our table? - if (item._id === itemId) { - //grab the table location - itemLocation = currentLocation; - } - }); - } - }); - //get table data - let itemData = await game.packs.get(itemLocation).getDocument(itemId); + //get item data + let itemData = await fromIdUuid(itemId,{type:"Item"}); //add or increase the count of the item, depending on type, if the actor has it if (this.items.getName(itemData.name)) { //if this is an item, increase the count diff --git a/module/actor/ship-sheet-sbt.js b/module/actor/ship-sheet-sbt.js index 8f925dd..c68fd79 100644 --- a/module/actor/ship-sheet-sbt.js +++ b/module/actor/ship-sheet-sbt.js @@ -1,3 +1,4 @@ +import { fromIdUuid } from "../mosh.js"; /** * Extend the basic ActorSheet with some very simple modifications @@ -179,29 +180,8 @@ export class MothershipShipSheetSBT extends ActorSheet { const actorData = sheetData; //A script to return the data from a table. let tableId = game.settings.get('mosh', 'table1eMegadamageEffects'); - let currentLocation = ''; - let tableLocation = ''; - //find where this table is located - //get current compendium - let compendium = game.packs; - //loop through each compendium - compendium.forEach(function (pack) { - //is this a pack of rolltables? - if (pack.metadata.type === 'RollTable') { - //log where we are - currentLocation = pack.metadata.id; - //loop through each pack to find the right table - pack.index.forEach(function (table) { - //is this our table? - if (table._id === tableId) { - //grab the table location - tableLocation = currentLocation; - } - }); - } - }); //get table data - let tableData = await game.packs.get(tableLocation).getDocument(tableId); + let tableData = await fromIdUuid(tableId,{type:"RollTable"}); let megadamageHTML = ""; diff --git a/module/mosh.js b/module/mosh.js index fac2334..fcd75d9 100644 --- a/module/mosh.js +++ b/module/mosh.js @@ -933,4 +933,135 @@ async function noShipSelected() { //log what was done console.log(`Told the user that no character was selected.`); }); +} + + +/** + * get element from world or compendiums by id or UUID, filtering by specific type. + * @param {string} id_uuid The id or the full uuid of the element we want to retieve. + * @param {object} options General search options for this function and for 'fromUuid' + * @param {string} [options.type] A string to filter the compendium type to search or the world element type. Valid values =["RollTable","Item","Macro","Actor","Adventure","Cards","JournalEntry","Playlist","Scene"] + * @returns {Promise} Returns the Document if it could be found, otherwise null. + */ +export async function fromIdUuid(id_uuid, options={}){ + let type = options.type; + //first we try to find from UUID, asuming the parameter(id_uuid) is an UUID. + let item = await fromUuid(id_uuid,options); + if(item != null){ + //we found the item with the id_uuid, it probably was an uuid. + return item; + } + + //we need to manualy find the item + let currentLocation = ''; + let objectLocation = ''; + //first loop through each compendium + game.packs.forEach(function(pack){ + //is this a pack of rolltables? + if (pack.metadata.type === type) { + //log where we are + currentLocation = pack.metadata.id; + //loop through each pack to find the right table + pack.index.forEach(function(pack_item) { + //is this our table? + if (pack_item._id === id_uuid) { + //grab the table location + objectLocation = currentLocation; + } + }); + } + }); + if (objectLocation){ + // Item found in a compendium -> get document data + return await game.packs.get(objectLocation).getDocument(id_uuid); + }else{ + //if we dont find it in a compendium, its probable a world item: + //Lets filtery by type to search the relevant elements only. + switch (type) { + case "RollTable": + return getTableFromId(id_uuid); + case "Item": + return getItemFromId(id_uuid); + case "Macro": + return getMacroFromId(id_uuid); + case "Actor": + return getActorFromId(id_uuid); + case "Adventure": + //adventures can only be defined in compendiums and not in the world (i think) + return null; + case "Cards": + return getCardFromId(id_uuid); + case "JournalEntry": + return getJournalFromId(id_uuid); + case "Playlist": + return getPlaylistFromId(id_uuid); + case "Scene": + return getSceneFromId(id_uuid); + + default: + //type is not defined, and we could not find it in a compendium, + //now we search all world elements for the ID. + //this could lead to conflicts since ID could not be unique. + let tableData = getTableFromId(id_uuid); + if (tableData){ + return tableData; + } + let itemData = getItemFromId(id_uuid); + if (itemData){ + return itemData; + } + let macroData = getMacroFromId(id_uuid); + if (macroData){ + return macroData; + } + let actorData = getActorFromId(id_uuid); + if (actorData){ + return actorData; + } + let cardData = getCardFromId(id_uuid); + if (cardData){ + return cardData; + } + let journalData = getJournalFromId(id_uuid); + if (journalData){ + return journalData; + } + let scenneData = getSceneFromId(id_uuid); + if (scenneData){ + return scenneData; + } + let playlistData = getPlaylistFromId(id_uuid); + if (playlistData){ + return playlistData; + } + } + //if we get here we have not found anything with that id. + return null; + } + /**functions to get world defined elements by type and ID */ + function getSceneFromId(sceneId){ + return game.scenes.filter(i=> i.id == sceneId)[0]; + } + function getPlaylistFromId(playlistId){ + return game.playlists.filter(i=> i.id == playlistId)[0]; + } + function getJournalFromId(journalId){ + return game.journal.filter(i=> i.id == journalId)[0]; + } + function getCardFromId(cardId){ + return game.cards.filter(i=> i.id == cardId)[0]; + } + function getActorFromId(actorId){ + return game.actors.filter(i=> i.id == actorId)[0]; + } + function getTableFromId(tableId){ + return game.tables.filter(i=> i.id == tableId)[0]; + } + function getItemFromId(itemId){ + return game.items.filter(i=> i.id == itemId)[0]; + } + function getMacroFromId(macroId){ + return game.macros.filter(i=> i.id == macroId)[0]; + } + } \ No newline at end of file diff --git a/module/windows/ship-megadamage.js b/module/windows/ship-megadamage.js index 16b2887..712a8d3 100644 --- a/module/windows/ship-megadamage.js +++ b/module/windows/ship-megadamage.js @@ -1,3 +1,4 @@ +import { fromIdUuid } from "../mosh.js"; export class DLShipMegaDamage extends FormApplication { static get defaultOptions() { const options = super.defaultOptions; @@ -41,29 +42,8 @@ export class DLShipMegaDamage extends FormApplication { //A script to return the data from a table. let tableId = game.settings.get('mosh','table1eMegadamageEffects'); - let currentLocation = ''; - let tableLocation = ''; - //find where this table is located - //get current compendium - let compendium = game.packs; - //loop through each compendium - compendium.forEach(function(pack){ - //is this a pack of rolltables? - if (pack.metadata.type === 'RollTable') { - //log where we are - currentLocation = pack.metadata.id; - //loop through each pack to find the right table - pack.index.forEach(function(table) { - //is this our table? - if (table._id === tableId) { - //grab the table location - tableLocation = currentLocation; - } - }); - } - }); //get table data - let tableData = await game.packs.get(tableLocation).getDocument(tableId); + let tableData = await fromIdUuid(tableId,{type:"RollTable"}); let entries = Array.from(tableData.results.entries());