2020-04-24 15:30:51 +00:00
|
|
|
export class BladesHelpers {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes a duplicate item type from charlist.
|
2021-04-22 08:28:03 +00:00
|
|
|
*
|
|
|
|
* @param {Object} item_data
|
|
|
|
* @param {Entity} actor
|
2020-04-24 15:30:51 +00:00
|
|
|
*/
|
2020-04-28 12:19:50 +00:00
|
|
|
static removeDuplicatedItemType(item_data, actor) {
|
2020-04-24 15:30:51 +00:00
|
|
|
|
2020-05-04 16:28:55 +00:00
|
|
|
let distinct_types = ["crew_reputation", "class", "vice", "background", "heritage"];
|
|
|
|
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.
|
2020-04-28 12:19:50 +00:00
|
|
|
actor.items.forEach(i => {
|
2020-05-04 16:28:55 +00:00
|
|
|
let has_double = (item_data.type === i.data.type);
|
|
|
|
if (i.data.name === item_data.name || (should_be_distinct && has_double)) {
|
2020-04-28 12:19:50 +00:00
|
|
|
actor.deleteOwnedItem(i.id);
|
|
|
|
}
|
|
|
|
});
|
2020-04-24 15:30:51 +00:00
|
|
|
}
|
|
|
|
|
2020-04-28 10:46:04 +00:00
|
|
|
/**
|
|
|
|
* Add item modification if logic exists.
|
2021-04-22 08:28:03 +00:00
|
|
|
* @param {Object} item_data
|
|
|
|
* @param {Entity} entity
|
2020-04-28 10:46:04 +00:00
|
|
|
*/
|
2021-04-22 08:28:03 +00:00
|
|
|
static async callItemLogic(item_data, entity) {
|
2020-04-28 10:46:04 +00:00
|
|
|
|
2020-08-21 15:03:37 +00:00
|
|
|
if ('logic' in item_data.data && item_data.data.logic !== '') {
|
2020-04-28 10:46:04 +00:00
|
|
|
let logic = JSON.parse(item_data.data.logic);
|
|
|
|
|
2020-08-21 15:03:37 +00:00
|
|
|
// Should be an array to support multiple expressions
|
|
|
|
if (!Array.isArray(logic)) {
|
|
|
|
logic = [logic];
|
|
|
|
}
|
|
|
|
|
2020-04-28 12:19:50 +00:00
|
|
|
if (logic) {
|
2021-04-22 08:28:03 +00:00
|
|
|
let logic_update = { "_id": entity.data._id };
|
2020-08-21 15:03:37 +00:00
|
|
|
logic.forEach(expression => {
|
|
|
|
|
|
|
|
// Different logic behav. dep on operator.
|
|
|
|
switch (expression.operator) {
|
2021-04-22 08:28:03 +00:00
|
|
|
|
2020-08-21 15:03:37 +00:00
|
|
|
// Add when creating.
|
|
|
|
case "addition":
|
2021-04-22 08:28:03 +00:00
|
|
|
mergeObject(
|
|
|
|
logic_update,
|
2021-04-22 08:29:37 +00:00
|
|
|
{[expression.attribute]: Number(BladesHelpers.getNestedProperty(entity, prefix + expression.attribute)) + expression.value},
|
2021-04-22 08:28:03 +00:00
|
|
|
{insertKeys: true}
|
|
|
|
);
|
|
|
|
break;
|
2020-08-21 15:03:37 +00:00
|
|
|
|
|
|
|
// Change name property.
|
|
|
|
case "attribute_change":
|
2021-04-22 08:28:03 +00:00
|
|
|
mergeObject(
|
|
|
|
logic_update,
|
|
|
|
{[expression.attribute]: expression.value},
|
|
|
|
{insertKeys: true}
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
|
2020-08-21 15:03:37 +00:00
|
|
|
}
|
|
|
|
});
|
2021-04-22 08:28:03 +00:00
|
|
|
await Actor.update( logic_update );
|
2020-04-28 10:46:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Undo Item modifications when item is removed.
|
2020-08-18 14:22:07 +00:00
|
|
|
* @todo
|
|
|
|
* - Remove all items and then Add them back to
|
|
|
|
* sustain the logic mods
|
2021-04-22 08:28:03 +00:00
|
|
|
* @param {Object} item_data
|
|
|
|
* @param {Entity} entity
|
2020-04-28 10:46:04 +00:00
|
|
|
*/
|
2021-04-22 08:28:03 +00:00
|
|
|
static async undoItemLogic(item_data, entity) {
|
2020-04-28 10:46:04 +00:00
|
|
|
|
2020-08-21 15:03:37 +00:00
|
|
|
if ('logic' in item_data.data && item_data.data.logic !== '') {
|
2020-04-28 10:46:04 +00:00
|
|
|
let logic = JSON.parse(item_data.data.logic)
|
|
|
|
|
2020-08-21 15:03:37 +00:00
|
|
|
// Should be an array to support multiple expressions
|
|
|
|
if (!Array.isArray(logic)) {
|
|
|
|
logic = [logic];
|
|
|
|
}
|
|
|
|
|
2020-04-28 12:19:50 +00:00
|
|
|
if (logic) {
|
2021-04-22 08:28:03 +00:00
|
|
|
let logic_update = { "_id": entity.data._id };
|
2020-08-21 15:03:37 +00:00
|
|
|
var entity_data = entity.data;
|
|
|
|
|
|
|
|
logic.forEach(expression => {
|
|
|
|
// Different logic behav. dep on operator.
|
|
|
|
switch (expression.operator) {
|
|
|
|
|
|
|
|
// Subtract when removing.
|
|
|
|
case "addition":
|
2021-04-22 08:28:03 +00:00
|
|
|
mergeObject(
|
|
|
|
logic_update,
|
2021-04-22 08:29:37 +00:00
|
|
|
{[expression.attribute]: Number(BladesHelpers.getNestedProperty(entity, prefix + expression.attribute)) - expression.value},
|
2021-04-22 08:28:03 +00:00
|
|
|
{insertKeys: true}
|
|
|
|
);
|
|
|
|
break;
|
2020-08-21 15:03:37 +00:00
|
|
|
|
|
|
|
// Change name back to default.
|
|
|
|
case "attribute_change":
|
|
|
|
// Get the array path to take data.
|
|
|
|
let default_expression_attribute_path = expression.attribute + '_default';
|
|
|
|
let default_name = default_expression_attribute_path.split(".").reduce((o, i) => o[i], entity_data);
|
|
|
|
|
2021-04-22 08:28:03 +00:00
|
|
|
mergeObject(
|
|
|
|
logic_update,
|
|
|
|
{[expression.attribute]: default_name},
|
|
|
|
{insertKeys: true}
|
|
|
|
);
|
|
|
|
|
|
|
|
break;
|
2020-08-21 15:03:37 +00:00
|
|
|
}
|
|
|
|
});
|
2021-04-22 08:28:03 +00:00
|
|
|
await Actor.update( logic_update );
|
2020-04-28 10:46:04 +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
|
|
|
|
};
|
|
|
|
return actor.createEmbeddedEntity("OwnedItem", data);
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
let compendium_content = await pack.getContent();
|
|
|
|
compendium_items = compendium_content.map(e => {return e.data});
|
|
|
|
|
|
|
|
list_of_items = game_items.concat(compendium_items);
|
|
|
|
|
|
|
|
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
|
|
|
|
2020-08-05 13:20:52 +00:00
|
|
|
for (var attibute_name in attributes) {
|
|
|
|
attribute_labels[attibute_name] = attributes[attibute_name].label;
|
|
|
|
for (var skill_name in attributes[attibute_name].skills) {
|
|
|
|
attribute_labels[skill_name] = attributes[attibute_name].skills[skill_name].label;
|
|
|
|
}
|
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
|
|
|
|
* @returns {bool}
|
|
|
|
*/
|
|
|
|
static isAttributeAction(attribute_name) {
|
|
|
|
let attribute_labels = {};
|
|
|
|
const attributes = game.system.model.Actor.character.attributes;
|
|
|
|
|
|
|
|
return !(attribute_name in attributes);
|
|
|
|
}
|
2020-08-05 13:20:52 +00:00
|
|
|
|
|
|
|
/* -------------------------------------------- */
|
|
|
|
|
2020-04-24 15:30:51 +00:00
|
|
|
}
|