Tie active effects to equipped flag for token actors, too

This commit is contained in:
Johannes Loher 2021-02-16 02:07:34 +01:00
parent 6cdbcc6899
commit 1b715031a8
2 changed files with 107 additions and 39 deletions

View file

@ -204,24 +204,12 @@ export class DS4Actor extends Actor<DS4ActorData, DS4Item> {
options?: Record<string, unknown>,
): Promise<DS4ItemData> | Promise<ActiveEffect.Data> {
if (embeddedName === "OwnedItem") {
this._preCreateOwnedItem(data);
preCreateOwnedItem.bind(this)(data);
return super.createEmbeddedEntity(embeddedName, data, options);
}
return super.createEmbeddedEntity(embeddedName, data, options);
}
/**
* If the item that is going to be created is equipable, set it to be non equipped and disable all ActiveEffects
* contained in the item
* @param itemData - The data of the item to be created
*/
protected _preCreateOwnedItem(itemData: DeepPartial<DS4ItemData>): void {
if (itemData.data && "equipped" in itemData.data) {
itemData.effects = itemData.effects?.map((effect) => ({ ...effect, disabled: true }));
itemData.data.equipped = false;
}
}
/** @override */
// TODO(types): Improve typing once it's fixed in upstream
updateEmbeddedEntity(embeddedName: string, data: unknown[], options?: Entity.UpdateOptions): Promise<unknown[]>;
@ -232,32 +220,113 @@ export class DS4Actor extends Actor<DS4ActorData, DS4Item> {
options?: Record<string, unknown>,
): Promise<unknown> {
if (embeddedName === "OwnedItem") {
this._preUpdateOwnedItem(updateData as DeepPartial<DS4ItemData> | Array<DeepPartial<DS4ItemData>>);
preUpdateOwnedItem.bind(this)(updateData as DeepPartial<DS4ItemData> | Array<DeepPartial<DS4ItemData>>);
}
return super.updateEmbeddedEntity(embeddedName, updateData, options);
}
/**
* If the equipped flag of one or more items changed, update all ActiveEffects originating from those items
* accordingly.
* @param updateData - The change that is going to be applied to the owned item(s)
*/
private _preUpdateOwnedItem(updateData: DeepPartial<DS4ItemData> | Array<DeepPartial<DS4ItemData>>): void {
const dataArray = updateData instanceof Array ? updateData : [updateData];
dataArray.forEach((data) => {
if (data.data && "equipped" in data.data) {
const equipped = data.data.equipped;
const origin = `Actor.${this.id}.OwnedItem.${data._id}`;
const effects = this.effects
.filter((e) => e.data.origin === origin)
.map((e) => {
const effectData = duplicate(e.data);
effectData.disabled = !(equipped ?? true);
return effectData;
});
if (effects.length > 0)
this.updateEmbeddedEntity("ActiveEffect", (effects as unknown) as Record<string, unknown>);
}
});
}
}
/**
* If the item that is going to be created is equipable, set it to be non equipped and disable all ActiveEffects
* contained in the item
* @param itemData - The data of the item to be created
*/
function preCreateOwnedItem(itemData: DeepPartial<DS4ItemData> | DeepPartial<DS4ItemData>[]): void {
const dataArray = itemData instanceof Array ? itemData : [itemData];
dataArray.forEach((data) => {
if (data.data && "equipped" in data.data) {
data.effects = data.effects?.map((effect) => ({ ...effect, disabled: true }));
data.data.equipped = false;
}
});
console.log(itemData);
}
/**
* If the equipped flag of one or more items changed, update all ActiveEffects originating from those items
* accordingly.
* @param updateData - The change that is going to be applied to the owned item(s)
*/
function preUpdateOwnedItem<T extends Actor>(
this: T,
updateData: DeepPartial<DS4ItemData> | Array<DeepPartial<DS4ItemData>>,
): void {
const dataArray = updateData instanceof Array ? updateData : [updateData];
dataArray.forEach((data) => {
if (data.data && "equipped" in data.data) {
const equipped = data.data.equipped;
const origin = `Actor.${this.id}.OwnedItem.${data._id}`;
const effects = this.effects
.filter((e) => e.data.origin === origin)
.map((e) => {
const effectData = duplicate(e.data);
effectData.disabled = !(equipped ?? true);
return effectData;
});
if (effects.length > 0)
this.updateEmbeddedEntity("ActiveEffect", (effects as unknown) as Record<string, unknown>);
}
});
}
const oldTokenCreateEmbeddedEntity = ActorTokenHelpers.prototype.createEmbeddedEntity;
const oldTokenUpdateEmbeddedEntity = ActorTokenHelpers.prototype.updateEmbeddedEntity;
function tokenCreateEmbeddedEntity<T extends Actor, U>(
this: T,
embeddedName: "OwnedItem" | "ActiveEffect",
data: Expanded<U> extends DeepPartial<ActiveEffect.Data> | DeepPartial<DS4ItemData>
? U | U[]
:
| DeepPartial<ActiveEffect.Data>
| DeepPartial<ActiveEffect.Data>[]
| DeepPartial<DS4ItemData>
| DeepPartial<DS4ItemData>[],
options?: Entity.UpdateOptions,
) {
if (embeddedName === "OwnedItem") {
const itemDataArray =
data instanceof Array
? data.map((it) => expandObject(it) as DS4ItemData)
: [expandObject(data as Record<string, unknown>) as DS4ItemData];
preCreateOwnedItem.bind(this)(itemDataArray);
// eslint-disable-next-line
// @ts-ignore
return oldTokenCreateEmbeddedEntity.bind(this)(embeddedName, itemDataArray, options);
}
// eslint-disable-next-line
// @ts-ignore
return oldTokenCreateEmbeddedEntity.bind(this)(embeddedName, data, options);
}
function tokenUpdateEmbeddedEntity<T extends Actor, U>(
this: T,
embeddedName: "OwnedItem" | "ActiveEffect",
data: Expanded<U> extends
| (DeepPartial<ActiveEffect.Data> & { _id: string })
| (DeepPartial<DS4ItemData> & { _id: string })
? U | U[]
:
| (DeepPartial<ActiveEffect.Data> & { _id: string })
| (DeepPartial<ActiveEffect.Data> & { _id: string })[]
| (DeepPartial<DS4ItemData> & { _id: string })
| (DeepPartial<DS4ItemData> & { _id: string })[],
options?: Entity.UpdateOptions,
) {
if (embeddedName === "OwnedItem") {
const itemDataArray =
data instanceof Array
? data.map((it) => expandObject(it) as DS4ItemData)
: [expandObject(data as Record<string, unknown>) as DS4ItemData];
preUpdateOwnedItem.bind(this)(itemDataArray);
// eslint-disable-next-line
// @ts-ignore
return oldTokenUpdateEmbeddedEntity.bind(this)(embeddedName, itemDataArray, options);
}
// eslint-disable-next-line
// @ts-ignore
return oldTokenUpdateEmbeddedEntity.bind(this)(embeddedName, data, options);
}
ActorTokenHelpers.prototype.createEmbeddedEntity = tokenCreateEmbeddedEntity;
ActorTokenHelpers.prototype.updateEmbeddedEntity = tokenUpdateEmbeddedEntity;

View file

@ -127,7 +127,6 @@ export class DS4ActorSheet extends ActorSheet<ActorSheet.Data<DS4Actor>> {
*/
protected _onItemChange(ev: JQuery.ChangeEvent): void {
ev.preventDefault();
console.log("Current target:", $(ev.currentTarget).get(0)["name"]);
const el: HTMLFormElement = $(ev.currentTarget).get(0);
const id = $(ev.currentTarget).parents(".item").data("itemId");
const item = duplicate<DS4Item, "lenient">(this.actor.getOwnedItem(id));