2020-04-24 15:30:51 +00:00
|
|
|
export class BladesHelpers {
|
|
|
|
|
|
|
|
/**
|
2021-06-01 06:56:25 +00:00
|
|
|
* Identifies duplicate items by type and returns a array of item ids to remove
|
2021-04-22 08:28:03 +00:00
|
|
|
*
|
|
|
|
* @param {Object} item_data
|
2021-06-01 06:56:25 +00:00
|
|
|
* @param {Document} actor
|
|
|
|
* @returns {Array}
|
|
|
|
*
|
2020-04-24 15:30:51 +00:00
|
|
|
*/
|
2020-04-28 12:19:50 +00:00
|
|
|
static removeDuplicatedItemType(item_data, actor) {
|
2021-06-01 06:56:25 +00:00
|
|
|
let dupe_list = [];
|
2020-05-04 16:28:55 +00:00
|
|
|
let distinct_types = ["crew_reputation", "class", "vice", "background", "heritage"];
|
2021-06-01 06:56:25 +00:00
|
|
|
let allowed_types = ["item"];
|
2020-05-04 16:28:55 +00:00
|
|
|
let should_be_distinct = distinct_types.includes(item_data.type);
|
2020-04-28 12:19:50 +00:00
|
|
|
// If the Item has the exact same name - remove it from list.
|
2020-05-04 16:28:55 +00:00
|
|
|
// Remove Duplicate items from the array.
|
2021-06-01 06:56:25 +00:00
|
|
|
actor.items.forEach( i => {
|
2020-05-04 16:28:55 +00:00
|
|
|
let has_double = (item_data.type === i.data.type);
|
2021-06-01 06:56:25 +00:00
|
|
|
if ( ( ( i.name === item_data.name ) || ( should_be_distinct && has_double ) ) && !( allowed_types.includes( item_data.type ) ) && ( item_data._id !== i.id ) ) {
|
|
|
|
dupe_list.push (i.id);
|
2020-04-28 12:19:50 +00:00
|
|
|
}
|
|
|
|
});
|
2021-06-01 06:56:25 +00:00
|
|
|
|
|
|
|
return dupe_list;
|
2020-04-24 15:30:51 +00:00
|
|
|
}
|
|
|
|
|
2020-04-28 12:19:50 +00:00
|
|
|
/**
|
|
|
|
* Get a nested dynamic attribute.
|
2021-04-22 08:28:03 +00:00
|
|
|
* @param {Object} obj
|
|
|
|
* @param {string} property
|
2020-04-28 12:19:50 +00:00
|
|
|
*/
|
2020-04-28 10:46:04 +00:00
|
|
|
static getNestedProperty(obj, property) {
|
|
|
|
return property.split('.').reduce((r, e) => {
|
|
|
|
return r[e];
|
|
|
|
}, obj);
|
|
|
|
}
|
|
|
|
|
2020-05-04 10:10:57 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Add item functionality
|
|
|
|
*/
|
|
|
|
static _addOwnedItem(event, actor) {
|
|
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
const a = event.currentTarget;
|
|
|
|
const item_type = a.dataset.itemType;
|
|
|
|
|
|
|
|
let data = {
|
|
|
|
name: randomID(),
|
|
|
|
type: item_type
|
|
|
|
};
|
2021-05-25 10:27:40 +00:00
|
|
|
return actor.createEmbeddedDocuments("Item", [data]);
|
2020-05-04 10:10:57 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 12:21:18 +00:00
|
|
|
/**
|
|
|
|
* Get the list of all available ingame items by Type.
|
2021-04-22 08:28:03 +00:00
|
|
|
*
|
|
|
|
* @param {string} item_type
|
|
|
|
* @param {Object} game
|
2020-05-11 12:21:18 +00:00
|
|
|
*/
|
|
|
|
static async getAllItemsByType(item_type, game) {
|
|
|
|
|
|
|
|
let list_of_items = [];
|
|
|
|
let game_items = [];
|
|
|
|
let compendium_items = [];
|
2021-04-22 08:28:03 +00:00
|
|
|
|
2020-05-11 12:21:18 +00:00
|
|
|
game_items = game.items.filter(e => e.type === item_type).map(e => {return e.data});
|
|
|
|
|
|
|
|
let pack = game.packs.find(e => e.metadata.name === item_type);
|
2021-05-25 10:27:40 +00:00
|
|
|
let compendium_content = await pack.getDocuments();
|
2020-05-11 12:21:18 +00:00
|
|
|
compendium_items = compendium_content.map(e => {return e.data});
|
|
|
|
|
|
|
|
list_of_items = game_items.concat(compendium_items);
|
2021-06-23 01:59:00 +00:00
|
|
|
list_of_items.sort(function(a, b) {
|
|
|
|
let nameA = a.name.toUpperCase();
|
|
|
|
let nameB = b.name.toUpperCase();
|
|
|
|
return nameA.localeCompare(nameB);
|
|
|
|
});
|
2020-05-11 12:21:18 +00:00
|
|
|
return list_of_items;
|
|
|
|
|
|
|
|
}
|
2020-05-25 16:00:53 +00:00
|
|
|
|
2020-05-11 12:21:18 +00:00
|
|
|
/* -------------------------------------------- */
|
|
|
|
|
2020-08-05 13:20:52 +00:00
|
|
|
/**
|
|
|
|
* Returns the label for attribute.
|
|
|
|
*
|
2021-04-22 08:28:03 +00:00
|
|
|
* @param {string} attribute_name
|
2020-08-05 13:20:52 +00:00
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
static getAttributeLabel(attribute_name) {
|
|
|
|
let attribute_labels = {};
|
|
|
|
const attributes = game.system.model.Actor.character.attributes;
|
2021-04-22 08:28:03 +00:00
|
|
|
|
2021-06-02 12:09:14 +00:00
|
|
|
for (const att_name in attributes) {
|
|
|
|
attribute_labels[att_name] = attributes[att_name].label;
|
|
|
|
for (const skill_name in attributes[att_name].skills) {
|
|
|
|
attribute_labels[skill_name] = attributes[att_name].skills[skill_name].label;
|
2020-08-05 13:20:52 +00:00
|
|
|
}
|
2021-04-22 08:28:03 +00:00
|
|
|
|
2020-08-05 13:20:52 +00:00
|
|
|
}
|
2021-04-22 08:28:03 +00:00
|
|
|
|
2020-08-05 13:20:52 +00:00
|
|
|
return attribute_labels[attribute_name];
|
|
|
|
}
|
2021-04-22 10:30:56 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if the attribute is an action
|
|
|
|
*
|
|
|
|
* @param {string} attribute_name
|
2021-06-01 06:56:25 +00:00
|
|
|
* @returns {Boolean}
|
2021-04-22 10:30:56 +00:00
|
|
|
*/
|
|
|
|
static isAttributeAction(attribute_name) {
|
|
|
|
const attributes = game.system.model.Actor.character.attributes;
|
|
|
|
|
|
|
|
return !(attribute_name in attributes);
|
|
|
|
}
|
2020-08-05 13:20:52 +00:00
|
|
|
|
|
|
|
/* -------------------------------------------- */
|
|
|
|
|
2021-08-07 18:28:16 +00:00
|
|
|
/**
|
|
|
|
* Creates options for faction clocks.
|
|
|
|
*
|
|
|
|
* @param {int[]} sizes
|
|
|
|
* array of possible clock sizes
|
|
|
|
* @param {int} default_size
|
|
|
|
* default clock size
|
|
|
|
* @param {int} current_size
|
|
|
|
* current clock size
|
|
|
|
* @returns {string}
|
|
|
|
* html-formatted option string
|
|
|
|
*/
|
|
|
|
static createListOfClockSizes( sizes, default_size, current_size ) {
|
|
|
|
|
|
|
|
let text = ``;
|
|
|
|
|
|
|
|
sizes.forEach( size => {
|
|
|
|
text += `<option value="${size}"`;
|
|
|
|
if ( !( current_size ) && ( size === default_size ) ) {
|
|
|
|
text += ` selected`;
|
|
|
|
} else if ( size === current_size ) {
|
|
|
|
text += ` selected`;
|
|
|
|
}
|
|
|
|
|
|
|
|
text += `>${size}</option>`;
|
|
|
|
});
|
|
|
|
|
|
|
|
return text;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-04-24 15:30:51 +00:00
|
|
|
}
|