From 312638fbd3789f8f800632676769cb168a6996d1 Mon Sep 17 00:00:00 2001 From: Melon-PieldeSapo Date: Sat, 14 Dec 2024 21:23:12 +0100 Subject: [PATCH 1/7] game.tables.filter for when item or table not from compendium --- module/actor/actor.js | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/module/actor/actor.js b/module/actor/actor.js index 969f368..c2ea4c9 100644 --- a/module/actor/actor.js +++ b/module/actor/actor.js @@ -1396,8 +1396,13 @@ export class MothershipActor extends Actor { }); } }); - //get table data - let tableData = await game.packs.get(tableLocation).getDocument(tableId); + if (tableLocation){ + // Item found in a compendium -> get table data + let tableData = await game.packs.get(tableLocation).getDocument(tableId); + }else{ + //try to find the item in the world. (user defined) + let tableData = game.tables.filter(i=> i.id == tableId) + } //get table name let tableName = tableData.name; //get table name @@ -1564,8 +1569,13 @@ export class MothershipActor extends Actor { }); } }); - //get table data + if (tableLocation){ + // Table found in a compendium -> get table data let tableData = await game.packs.get(tableLocation).getDocument(tableId); + }else{ + //try to find the item in the world. (user defined) + let tableData = game.tables.filter(i=> i.id == tableId) + } //get table name let tableName = tableData.name; //get table name @@ -2646,8 +2656,13 @@ export class MothershipActor extends Actor { }); } }); - //get table data + if (tableLocation){ + // Table found in a compendium -> get table data let tableData = await game.packs.get(tableLocation).getDocument(tableId); + }else{ + //try to find the item in the world. (user defined) + let tableData = game.tables.filter(i=> i.id == tableId) + } //prep text for success if (parsedRollResult.success && parsedRollResult.critical) { //flavor text @@ -3140,8 +3155,13 @@ export class MothershipActor extends Actor { }); } }); - //get table data - let itemData = await game.packs.get(itemLocation).getDocument(itemId); + if (itemLocation){ + // Item found in a compendium -> get table data + let itemData = await game.packs.get(itemLocation).getDocument(itemId); + }else{ + //try to find the item in the world. (user defined) + let itemData = game.items.filter(i=> i.id == itemId) + } //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 From 1892d749cc349eb0a645049cfcd960da40517718 Mon Sep 17 00:00:00 2001 From: Melon-PieldeSapo Date: Mon, 16 Dec 2024 15:40:14 +0100 Subject: [PATCH 2/7] Centalize compendium searchs and make them compatible with uuid --- module/actor/actor.js | 116 +++--------------------------- module/actor/ship-sheet-sbt.js | 23 +----- module/mosh.js | 54 ++++++++++++++ module/windows/ship-megadamage.js | 23 +----- 4 files changed, 65 insertions(+), 151 deletions(-) diff --git a/module/actor/actor.js b/module/actor/actor.js index c2ea4c9..c0db14c 100644 --- a/module/actor/actor.js +++ b/module/actor/actor.js @@ -1375,34 +1375,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; - } - }); - } - }); - if (tableLocation){ - // Item found in a compendium -> get table data - let tableData = await game.packs.get(tableLocation).getDocument(tableId); - }else{ - //try to find the item in the world. (user defined) - let tableData = game.tables.filter(i=> i.id == tableId) - } + + let tableData = await fromIdUuid(tableId,type="RollTable"); //get table name let tableName = tableData.name; //get table name @@ -1550,32 +1524,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; - } - }); - } - }); - if (tableLocation){ - // Table found in a compendium -> get table data - let tableData = await game.packs.get(tableLocation).getDocument(tableId); - }else{ - //try to find the item in the world. (user defined) - let tableData = game.tables.filter(i=> i.id == tableId) - } //get table name let tableName = tableData.name; //get table name @@ -2635,34 +2586,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; - } - }); - } - }); - if (tableLocation){ - // Table found in a compendium -> get table data - let tableData = await game.packs.get(tableLocation).getDocument(tableId); - }else{ - //try to find the item in the world. (user defined) - let tableData = game.tables.filter(i=> i.id == 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 @@ -3136,32 +3062,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; - } - }); - } - }); - if (itemLocation){ - // Item found in a compendium -> get table data - let itemData = await game.packs.get(itemLocation).getDocument(itemId); - }else{ - //try to find the item in the world. (user defined) - let itemData = game.items.filter(i=> i.id == 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..e3a0a74 100644 --- a/module/actor/ship-sheet-sbt.js +++ b/module/actor/ship-sheet-sbt.js @@ -179,29 +179,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..2ba8b2f 100644 --- a/module/mosh.js +++ b/module/mosh.js @@ -933,4 +933,58 @@ async function noShipSelected() { //log what was done console.log(`Told the user that no character was selected.`); }); +} + + +//get item from world or compendiums by id or UUID. +export async function fromIdUuid(id_uuid,type="", options={}){ + let item = await fromUuid(id_uuid,options); + if(item != null){ + //we found the item with the id, it probably was an uuid. + return item; + } + + //we need to manualy find the item: + let currentLocation = ''; + let objectLocation = ''; + //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(table) { + //is this our table? + if (table._id === id_uuid) { + //grab the table location + objectLocation = currentLocation; + } + }); + } + }); + if (objectLocation){ + // Item found in a compendium -> get table data + return await game.packs.get(objectLocation).getDocument(id_uuid); + }else{ + //it is a world item. + switch (type) { + case "RollTable": + return game.tables.filter(i=> i.id == id_uuid); + case "Item": + return game.items.filter(i=> i.id == id_uuid); + default: + //type is not defined, and we could not find it in a compendium, + //now we search all world elements. + let itemData = game.tables.filter(i=> i.id == id_uuid); + if (itemData){ + return itemData + } + itemData = game.items.filter(i=> i.id == id_uuid); + if (itemData){ + return itemData + } + } + } + } \ No newline at end of file diff --git a/module/windows/ship-megadamage.js b/module/windows/ship-megadamage.js index 16b2887..3ff2610 100644 --- a/module/windows/ship-megadamage.js +++ b/module/windows/ship-megadamage.js @@ -41,29 +41,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()); From 9105420656a61913a08ca672c4ab52c6d0be8631 Mon Sep 17 00:00:00 2001 From: Melon-PieldeSapo Date: Mon, 16 Dec 2024 16:10:48 +0100 Subject: [PATCH 3/7] Fix arguments and import --- module/actor/actor.js | 10 ++++++---- module/actor/ship-sheet-sbt.js | 3 ++- module/mosh.js | 11 ++++++----- module/windows/ship-megadamage.js | 3 ++- 4 files changed, 16 insertions(+), 11 deletions(-) diff --git a/module/actor/actor.js b/module/actor/actor.js index c0db14c..a8fa578 100644 --- a/module/actor/actor.js +++ b/module/actor/actor.js @@ -1,3 +1,5 @@ +import { fromIdUuid } from "../mosh"; + /** * Extend the base Actor entity by defining a custom roll data structure which is ideal for the Simple system. * @extends {Actor} @@ -1376,7 +1378,7 @@ export class MothershipActor extends Actor { //A script to return the data from a table. async getRollTableData(tableId){ - let tableData = await fromIdUuid(tableId,type="RollTable"); + let tableData = await fromIdUuid(tableId,{type:"RollTable"}); //get table name let tableName = tableData.name; //get table name @@ -1525,7 +1527,7 @@ export class MothershipActor extends Actor { rollString = chosenRollType[0]; } - let tableData = await fromIdUuid(tableId,type="RollTable"); + let tableData = await fromIdUuid(tableId,{type:"RollTable"}); //get current compendium //get table name let tableName = tableData.name; @@ -2588,7 +2590,7 @@ export class MothershipActor extends Actor { //get the bankruptcy table let tableId = game.settings.get('mosh','table1eBankruptcy'); //get Table Data - let tableData = await fromIdUuid(tableId,type="RollTable"); + let tableData = await fromIdUuid(tableId,{type:"RollTable"}); //prep text for success if (parsedRollResult.success && parsedRollResult.critical) { //flavor text @@ -3063,7 +3065,7 @@ export class MothershipActor extends Actor { let flavorText = ``; let chatId = (game.release.generation >= 12 ? foundry.utils.randomID(): randomID()) //get item data - let itemData = await fromIdUuid(itemId,type="Item"); + 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 e3a0a74..76b7582 100644 --- a/module/actor/ship-sheet-sbt.js +++ b/module/actor/ship-sheet-sbt.js @@ -1,3 +1,4 @@ +import { fromIdUuid } from "../mosh"; /** * Extend the basic ActorSheet with some very simple modifications @@ -180,7 +181,7 @@ export class MothershipShipSheetSBT extends ActorSheet { //A script to return the data from a table. let tableId = game.settings.get('mosh', 'table1eMegadamageEffects'); //get table data - let tableData = await fromIdUuid(tableId,type="RollTable"); + let tableData = await fromIdUuid(tableId,{type:"RollTable"}); let megadamageHTML = ""; diff --git a/module/mosh.js b/module/mosh.js index 2ba8b2f..07c5e5b 100644 --- a/module/mosh.js +++ b/module/mosh.js @@ -937,7 +937,8 @@ async function noShipSelected() { //get item from world or compendiums by id or UUID. -export async function fromIdUuid(id_uuid,type="", options={}){ +export async function fromIdUuid(id_uuid, options={}){ + let type = options.type; let item = await fromUuid(id_uuid,options); if(item != null){ //we found the item with the id, it probably was an uuid. @@ -970,19 +971,19 @@ export async function fromIdUuid(id_uuid,type="", options={}){ //it is a world item. switch (type) { case "RollTable": - return game.tables.filter(i=> i.id == id_uuid); + return game.tables.filter(i=> i.id == id_uuid)[0]; case "Item": - return game.items.filter(i=> i.id == id_uuid); + return game.items.filter(i=> i.id == id_uuid)[0]; default: //type is not defined, and we could not find it in a compendium, //now we search all world elements. let itemData = game.tables.filter(i=> i.id == id_uuid); if (itemData){ - return itemData + return itemData[0]; } itemData = game.items.filter(i=> i.id == id_uuid); if (itemData){ - return itemData + return itemData[0]; } } } diff --git a/module/windows/ship-megadamage.js b/module/windows/ship-megadamage.js index 3ff2610..8c780db 100644 --- a/module/windows/ship-megadamage.js +++ b/module/windows/ship-megadamage.js @@ -1,3 +1,4 @@ +import { fromIdUuid } from "../mosh"; export class DLShipMegaDamage extends FormApplication { static get defaultOptions() { const options = super.defaultOptions; @@ -42,7 +43,7 @@ export class DLShipMegaDamage extends FormApplication { //A script to return the data from a table. let tableId = game.settings.get('mosh','table1eMegadamageEffects'); //get table data - let tableData = await fromIdUuid(tableId,type="RollTable"); + let tableData = await fromIdUuid(tableId,{type:"RollTable"}); let entries = Array.from(tableData.results.entries()); From 8a5c076ba90bf6d5e905c54339cd12e7d61f146a Mon Sep 17 00:00:00 2001 From: Melon-PieldeSapo Date: Mon, 16 Dec 2024 16:14:14 +0100 Subject: [PATCH 4/7] fix import (again) --- module/actor/actor.js | 2 +- module/actor/ship-sheet-sbt.js | 2 +- module/windows/ship-megadamage.js | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/module/actor/actor.js b/module/actor/actor.js index a8fa578..ffa8b4c 100644 --- a/module/actor/actor.js +++ b/module/actor/actor.js @@ -1,4 +1,4 @@ -import { fromIdUuid } from "../mosh"; +import { fromIdUuid } from "../mosh.js"; /** * Extend the base Actor entity by defining a custom roll data structure which is ideal for the Simple system. diff --git a/module/actor/ship-sheet-sbt.js b/module/actor/ship-sheet-sbt.js index 76b7582..c68fd79 100644 --- a/module/actor/ship-sheet-sbt.js +++ b/module/actor/ship-sheet-sbt.js @@ -1,4 +1,4 @@ -import { fromIdUuid } from "../mosh"; +import { fromIdUuid } from "../mosh.js"; /** * Extend the basic ActorSheet with some very simple modifications diff --git a/module/windows/ship-megadamage.js b/module/windows/ship-megadamage.js index 8c780db..712a8d3 100644 --- a/module/windows/ship-megadamage.js +++ b/module/windows/ship-megadamage.js @@ -1,4 +1,4 @@ -import { fromIdUuid } from "../mosh"; +import { fromIdUuid } from "../mosh.js"; export class DLShipMegaDamage extends FormApplication { static get defaultOptions() { const options = super.defaultOptions; From e776667142478d4019cbb133611d0b2bf09cc32f Mon Sep 17 00:00:00 2001 From: Melon-PieldeSapo Date: Tue, 17 Dec 2024 14:21:35 +0100 Subject: [PATCH 5/7] Add all the other pack.type --- module/mosh.js | 85 ++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 75 insertions(+), 10 deletions(-) diff --git a/module/mosh.js b/module/mosh.js index 07c5e5b..ae21b6e 100644 --- a/module/mosh.js +++ b/module/mosh.js @@ -955,9 +955,9 @@ export async function fromIdUuid(id_uuid, options={}){ //log where we are currentLocation = pack.metadata.id; //loop through each pack to find the right table - pack.index.forEach(function(table) { + pack.index.forEach(function(pack_item) { //is this our table? - if (table._id === id_uuid) { + if (pack_item._id === id_uuid) { //grab the table location objectLocation = currentLocation; } @@ -971,21 +971,86 @@ export async function fromIdUuid(id_uuid, options={}){ //it is a world item. switch (type) { case "RollTable": - return game.tables.filter(i=> i.id == id_uuid)[0]; + return getTableFromId(id_uuid); case "Item": - return game.items.filter(i=> i.id == id_uuid)[0]; + 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. - let itemData = game.tables.filter(i=> i.id == id_uuid); - if (itemData){ - return itemData[0]; + //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; } - itemData = game.items.filter(i=> i.id == id_uuid); + let itemData = getItemFromId(id_uuid); if (itemData){ - return itemData[0]; + 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; } } } + 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 From 5f75a209d94dd1f065c179cfc595ba883f79e8c0 Mon Sep 17 00:00:00 2001 From: Melon-PieldeSapo Date: Tue, 17 Dec 2024 14:28:08 +0100 Subject: [PATCH 6/7] add default return --- module/mosh.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/module/mosh.js b/module/mosh.js index ae21b6e..60a82bc 100644 --- a/module/mosh.js +++ b/module/mosh.js @@ -1027,6 +1027,8 @@ export async function fromIdUuid(id_uuid, options={}){ return playlistData; } } + //if we get here we have not found anything with that id. + return null; } function getSceneFromId(sceneId){ return game.scenes.filter(i=> i.id == sceneId)[0]; From bebf4c3a7bdde46fac4210c1eb246085c44071b4 Mon Sep 17 00:00:00 2001 From: Melon-PieldeSapo Date: Tue, 17 Dec 2024 14:36:46 +0100 Subject: [PATCH 7/7] Improved comments --- module/mosh.js | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/module/mosh.js b/module/mosh.js index 60a82bc..fcd75d9 100644 --- a/module/mosh.js +++ b/module/mosh.js @@ -936,19 +936,26 @@ async function noShipSelected() { } -//get item from world or compendiums by id or UUID. +/** + * 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, it probably was an uuid. + //we found the item with the id_uuid, it probably was an uuid. return item; } - //we need to manualy find the item: + //we need to manualy find the item let currentLocation = ''; let objectLocation = ''; - //loop through each compendium + //first loop through each compendium game.packs.forEach(function(pack){ //is this a pack of rolltables? if (pack.metadata.type === type) { @@ -965,10 +972,11 @@ export async function fromIdUuid(id_uuid, options={}){ } }); if (objectLocation){ - // Item found in a compendium -> get table data + // Item found in a compendium -> get document data return await game.packs.get(objectLocation).getDocument(id_uuid); }else{ - //it is a world item. + //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); @@ -1030,6 +1038,7 @@ export async function fromIdUuid(id_uuid, options={}){ //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]; }