mirror of
https://github.com/Foundryborne/daggerheart.git
synced 2026-01-12 03:31:07 +01:00
* Initial * Added Character-Settings * Finalized Character-Settings * Hide CharacterSetup if any part is done manually * Fixed class/subclass drag-drop * Fixed relinking of Features from items created on Character * Adding features on CharacterItems now adds them on the Character and relinks * Made suggested items inactive in the Class sheet if rendered from inside a Character * Added hope to CharacterSetting * add style to textarea element, add spellcasting and domain class into char sheet and move rest buttons to another place * Fixed characterCreation experience description --------- Co-authored-by: moliloo <dev.murilobrito@gmail.com>
184 lines
6.9 KiB
JavaScript
184 lines
6.9 KiB
JavaScript
/**
|
|
* Describes metadata about the item data model type
|
|
* @typedef {Object} ItemDataModelMetadata
|
|
* @property {string} label - A localizable label used on application.
|
|
* @property {string} type - The system type that this data model represents.
|
|
* @property {boolean} hasDescription - Indicates whether items of this type have description field
|
|
* @property {boolean} isQuantifiable - Indicates whether items of this type have quantity field
|
|
* @property {boolean} isInventoryItem- Indicates whether items of this type is a Inventory Item
|
|
*/
|
|
|
|
import { addLinkedItemsDiff, updateLinkedItemApps } from '../../helpers/utils.mjs';
|
|
import { ActionsField } from '../fields/actionField.mjs';
|
|
import FormulaField from '../fields/formulaField.mjs';
|
|
|
|
const fields = foundry.data.fields;
|
|
|
|
export default class BaseDataItem extends foundry.abstract.TypeDataModel {
|
|
static LOCALIZATION_PREFIXES = ['DAGGERHEART.ITEMS'];
|
|
|
|
/** @returns {ItemDataModelMetadata}*/
|
|
static get metadata() {
|
|
return {
|
|
label: 'Base Item',
|
|
type: 'base',
|
|
hasDescription: false,
|
|
hasResource: false,
|
|
isQuantifiable: false,
|
|
isInventoryItem: false,
|
|
hasActions: false
|
|
};
|
|
}
|
|
|
|
/**@returns {ItemDataModelMetadata}*/
|
|
get metadata() {
|
|
return this.constructor.metadata;
|
|
}
|
|
|
|
/** @inheritDoc */
|
|
static defineSchema() {
|
|
const schema = {};
|
|
|
|
if (this.metadata.hasDescription) schema.description = new fields.HTMLField({ required: true, nullable: true });
|
|
|
|
if (this.metadata.hasResource) {
|
|
schema.resource = new fields.SchemaField(
|
|
{
|
|
type: new fields.StringField({
|
|
choices: CONFIG.DH.ITEM.itemResourceTypes,
|
|
initial: CONFIG.DH.ITEM.itemResourceTypes.simple
|
|
}),
|
|
value: new fields.NumberField({ integer: true, min: 0, initial: 0 }),
|
|
max: new FormulaField({ nullable: true, initial: null, deterministic: true }),
|
|
icon: new fields.StringField(),
|
|
recovery: new fields.StringField({
|
|
choices: CONFIG.DH.GENERAL.refreshTypes,
|
|
initial: null,
|
|
nullable: true
|
|
}),
|
|
diceStates: new fields.TypedObjectField(
|
|
new fields.SchemaField({
|
|
value: new fields.NumberField({ integer: true, initial: 1, min: 1 }),
|
|
used: new fields.BooleanField({ initial: false })
|
|
})
|
|
),
|
|
dieFaces: new fields.StringField({
|
|
choices: CONFIG.DH.GENERAL.diceTypes,
|
|
initial: CONFIG.DH.GENERAL.diceTypes.d4
|
|
})
|
|
},
|
|
{ nullable: true, initial: null }
|
|
);
|
|
}
|
|
|
|
if (this.metadata.isQuantifiable)
|
|
schema.quantity = new fields.NumberField({ integer: true, initial: 1, min: 0, required: true });
|
|
|
|
if (this.metadata.hasActions) schema.actions = new ActionsField();
|
|
|
|
return schema;
|
|
}
|
|
|
|
/**
|
|
* Convenient access to the item's actor, if it exists.
|
|
* @returns {foundry.documents.Actor | null}
|
|
*/
|
|
get actor() {
|
|
return this.parent.actor;
|
|
}
|
|
|
|
get actionsList() {
|
|
return this.actions;
|
|
}
|
|
|
|
/**
|
|
* Obtain a data object used to evaluate any dice rolls associated with this Item Type
|
|
* @param {object} [options] - Options which modify the getRollData method.
|
|
* @returns {object}
|
|
*/
|
|
getRollData(options = {}) {
|
|
const actorRollData = this.actor?.getRollData() ?? {};
|
|
const data = { ...actorRollData, item: { ...this } };
|
|
return data;
|
|
}
|
|
|
|
async _preCreate(data, options, user) {
|
|
// Skip if no initial action is required or actions already exist
|
|
if (this.metadata.hasInitialAction && foundry.utils.isEmpty(this.actions)) {
|
|
const metadataType = this.metadata.type;
|
|
const actionType = { weapon: 'attack' }[metadataType];
|
|
const ActionClass = game.system.api.models.actions.actionsTypes[actionType];
|
|
|
|
const action = new ActionClass(
|
|
{
|
|
_id: foundry.utils.randomID(),
|
|
type: actionType,
|
|
name: game.i18n.localize(CONFIG.DH.ACTIONS.actionTypes[actionType].name),
|
|
...ActionClass.getSourceConfig(this.parent)
|
|
},
|
|
{
|
|
parent: this.parent
|
|
}
|
|
);
|
|
|
|
this.updateSource({ actions: [action] });
|
|
}
|
|
|
|
if (this.actor && this.actor.type === 'character' && this.features) {
|
|
const featureUpdates = {};
|
|
for (let f of this.features) {
|
|
const fBase = f.item ?? f;
|
|
const feature = fBase.system ? fBase : await foundry.utils.fromUuid(fBase.uuid);
|
|
const createData = foundry.utils.mergeObject(
|
|
feature.toObject(),
|
|
{
|
|
system: {
|
|
originItemType: this.parent.type,
|
|
originId: data._id,
|
|
identifier: this.isMulticlass ? 'multiclass' : null
|
|
}
|
|
},
|
|
{ inplace: false }
|
|
);
|
|
const [doc] = await this.actor.createEmbeddedDocuments('Item', [createData]);
|
|
|
|
if (!featureUpdates.features)
|
|
featureUpdates.features = this.features.map(x => (x.item ? { ...x, item: x.item.uuid } : x.uuid));
|
|
|
|
if (f.item) {
|
|
const existingFeature = featureUpdates.features.find(x => x.item === f.item.uuid);
|
|
existingFeature.item = doc.uuid;
|
|
} else {
|
|
const replaceIndex = featureUpdates.features.findIndex(x => x === f.uuid);
|
|
featureUpdates.features.splice(replaceIndex, 1, doc.uuid);
|
|
}
|
|
}
|
|
|
|
await this.updateSource(featureUpdates);
|
|
}
|
|
}
|
|
|
|
async _preDelete() {
|
|
if (!this.actor || this.actor.type !== 'character') return;
|
|
|
|
const items = this.actor.items.filter(item => item.system.originId === this.parent.id);
|
|
if (items.length > 0)
|
|
await this.actor.deleteEmbeddedDocuments(
|
|
'Item',
|
|
items.map(x => x.id)
|
|
);
|
|
}
|
|
|
|
async _preUpdate(changed, options, userId) {
|
|
const allowed = await super._preUpdate(changed, options, userId);
|
|
if (allowed === false) return false;
|
|
|
|
addLinkedItemsDiff(changed.system?.features, this.features, options);
|
|
}
|
|
|
|
_onUpdate(changed, options, userId) {
|
|
super._onUpdate(changed, options, userId);
|
|
|
|
updateLinkedItemApps(options, this.parent.sheet);
|
|
}
|
|
}
|