Skip to content

Commit

Permalink
Extend Document from DataModel (foundryvtt#10636)
Browse files Browse the repository at this point in the history
  • Loading branch information
stwlam committed Oct 10, 2023
1 parent 45124e3 commit 0f7b602
Show file tree
Hide file tree
Showing 130 changed files with 1,380 additions and 1,254 deletions.
2 changes: 1 addition & 1 deletion build/lib/compendium-pack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ class CompendiumPack {

if (isItemSource(docSource)) {
docSource.effects = [];
docSource.flags.core = { sourceId: this.#sourceIdOf(docSource._id, { docType: "Item" }) };
docSource.flags.core = { sourceId: this.#sourceIdOf(docSource._id!, { docType: "Item" }) };
docSource.system.slug = sluggify(docSource.name);
docSource.system._migration = { version: MigrationRunnerBase.LATEST_SCHEMA_VERSION, previous: null };

Expand Down
2 changes: 1 addition & 1 deletion build/lib/extractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ class PackExtractor {
if (key === "_id") {
topLevel = docSource;
if (docSource.folder === null) {
delete docSource.folder;
delete (docSource as { folder?: null }).folder;
}
delete (docSource as { _stats?: unknown })._stats;

Expand Down
4 changes: 2 additions & 2 deletions src/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import type {
ScenePF2e,
TileDocumentPF2e,
TokenDocumentPF2e,
} from "@scene/index.ts";
} from "@scene";
import type { ActorDeltaPF2e } from "@scene/token-document/actor-delta.ts";
import type { PF2ECONFIG, StatusEffectIconTheme } from "@scripts/config/index.ts";
import type { DicePF2e } from "@scripts/dice.ts";
Expand All @@ -60,9 +60,9 @@ import type { ModuleArt } from "@system/module-art.ts";
import type { CustomDamageData, HomebrewTag, HomebrewTraitSettingsKey } from "@system/settings/homebrew/index.ts";
import type { TextEditorPF2e } from "@system/text-editor.ts";
import type { sluggify } from "@util";
import type Peggy from "peggy";
import type EnJSON from "static/lang/en.json";
import type ReEnJSON from "static/lang/re-en.json";
import type Peggy from "peggy";

interface GamePF2e
extends Game<
Expand Down
26 changes: 9 additions & 17 deletions src/module/actor/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,7 @@ class ActorPF2e<TParent extends TokenDocumentPF2e | null = TokenDocumentPF2e | n
});
}

static override getDefaultArtwork(
actorData: foundry.documents.ActorSource | PreDocumentId<foundry.documents.ActorSource>
): {
static override getDefaultArtwork(actorData: foundry.documents.ActorSource): {
img: ImageFilePath;
texture: { src: ImageFilePath | VideoFilePath };
} {
Expand Down Expand Up @@ -582,11 +580,11 @@ class ActorPF2e<TParent extends TokenDocumentPF2e | null = TokenDocumentPF2e | n

static override updateDocuments<TDocument extends foundry.abstract.Document>(
this: ConstructorOf<TDocument>,
updates?: DocumentUpdateData<TDocument>[],
updates?: Record<string, unknown>[],
context?: DocumentUpdateContext<TDocument["parent"]>
): Promise<TDocument[]>;
static override async updateDocuments(
updates: DocumentUpdateData<ActorPF2e>[] = [],
updates: Record<string, unknown>[] = [],
context: DocumentModificationContext<TokenDocumentPF2e | null> = {}
): Promise<Actor<TokenDocument<Scene | null> | null>[]> {
// Process rule element hooks for each actor update
Expand Down Expand Up @@ -1764,7 +1762,7 @@ class ActorPF2e<TParent extends TokenDocumentPF2e | null = TokenDocumentPF2e | n
/* -------------------------------------------- */

protected override _applyDefaultTokenSettings(
data: PreDocumentId<this["_source"]>,
data: this["_source"],
options?: { fromCompendium?: boolean }
): DeepPartial<this["_source"]> {
const diff = super._applyDefaultTokenSettings(data, options);
Expand Down Expand Up @@ -1869,9 +1867,9 @@ interface ActorPF2e<TParent extends TokenDocumentPF2e | null = TokenDocumentPF2e

prototypeToken: PrototypeTokenPF2e<this>;

get sheet(): ActorSheetPF2e<this>;
get sheet(): ActorSheetPF2e<ActorPF2e>;

update(data: DocumentUpdateData<this>, options?: ActorUpdateContext<TParent>): Promise<this>;
update(data: Record<string, unknown>, options?: ActorUpdateContext<TParent>): Promise<this>;

getActiveTokens(linked: boolean | undefined, document: true): TokenDocumentPF2e<ScenePF2e>[];
getActiveTokens(linked?: boolean | undefined, document?: false): TokenPF2e<TokenDocumentPF2e<ScenePF2e>>[];
Expand All @@ -1880,12 +1878,6 @@ interface ActorPF2e<TParent extends TokenDocumentPF2e | null = TokenDocumentPF2e
document?: boolean
): TokenDocumentPF2e<ScenePF2e>[] | TokenPF2e<TokenDocumentPF2e<ScenePF2e>>[];

_preCreate(
data: PreDocumentId<this["_source"]>,
options: DocumentModificationContext<TParent>,
user: UserPF2e
): Promise<boolean | void>;

/** See implementation in class */
createEmbeddedDocuments(
embeddedName: "ActiveEffect",
Expand All @@ -1906,17 +1898,17 @@ interface ActorPF2e<TParent extends TokenDocumentPF2e | null = TokenDocumentPF2e
/** See implementation in class */
updateEmbeddedDocuments(
embeddedName: "ActiveEffect",
updateData: EmbeddedDocumentUpdateData<ActiveEffectPF2e<this>>[],
updateData: EmbeddedDocumentUpdateData[],
options?: DocumentUpdateContext<this>
): Promise<ActiveEffectPF2e<this>[]>;
updateEmbeddedDocuments(
embeddedName: "Item",
updateData: EmbeddedDocumentUpdateData<ItemPF2e<this>>[],
updateData: EmbeddedDocumentUpdateData[],
options?: DocumentUpdateContext<this>
): Promise<ItemPF2e<this>[]>;
updateEmbeddedDocuments(
embeddedName: "ActiveEffect" | "Item",
updateData: EmbeddedDocumentUpdateData<ActiveEffectPF2e<this> | ItemPF2e<this>>[],
updateData: EmbeddedDocumentUpdateData[],
options?: DocumentUpdateContext<this>
): Promise<ActiveEffectPF2e<this>[] | ItemPF2e<this>[]>;

Expand Down
4 changes: 2 additions & 2 deletions src/module/actor/character/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ import type { FeatGroup } from "./feats.ts";
import { WeaponAuxiliaryAction } from "./helpers.ts";
import { CharacterSheetTabVisibility } from "./sheet.ts";

interface CharacterSource extends BaseCreatureSource<"character", CharacterSystemSource> {
type CharacterSource = BaseCreatureSource<"character", CharacterSystemSource> & {
flags: DeepPartial<CharacterFlags>;
}
};

type CharacterFlags = ActorFlagsPF2e & {
pf2e: {
Expand Down
12 changes: 7 additions & 5 deletions src/module/actor/character/document.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ActorPF2e, CreaturePF2e, FamiliarPF2e } from "@actor";
import { ActorPF2e, CreaturePF2e, type FamiliarPF2e } from "@actor";
import { Abilities, CreatureSpeeds, LabeledSpeed, SkillAbbreviation } from "@actor/creature/data.ts";
import { CreatureUpdateContext } from "@actor/creature/types.ts";
import { ALLIANCES, SAVING_THROW_DEFAULT_ATTRIBUTES } from "@actor/creature/values.ts";
Expand Down Expand Up @@ -28,18 +28,17 @@ import {
SKILL_DICTIONARY,
SKILL_EXPANDED,
} from "@actor/values.ts";
import {
import type {
AncestryPF2e,
BackgroundPF2e,
ClassPF2e,
ConsumablePF2e,
DeityPF2e,
FeatPF2e,
HeritagePF2e,
ItemPF2e,
PhysicalItemPF2e,
WeaponPF2e,
} from "@item";
import { ItemPF2e, WeaponPF2e } from "@item";
import { ActionTrait } from "@item/ability/types.ts";
import { ARMOR_CATEGORIES } from "@item/armor/values.ts";
import { ItemType, PhysicalItemSource } from "@item/data/index.ts";
Expand Down Expand Up @@ -335,7 +334,10 @@ class CharacterPF2e<TParent extends TokenDocumentPF2e | null = TokenDocumentPF2e
mergeObject({ mod: 0 }, this.system.abilities?.[a] ?? {}),
]);

const systemData: DeepPartial<CharacterSystemData> & { abilities: Abilities } = this.system;
type SystemDataPartial = DeepPartial<
Pick<CharacterSystemData, "build" | "crafting" | "proficiencies" | "saves">
> & { abilities: Abilities };
const systemData: SystemDataPartial = this.system;
const existingBoosts = systemData.build?.attributes?.boosts;
const isABP = game.pf2e.variantRules.AutomaticBonusProgression.isEnabled(this);

Expand Down
2 changes: 1 addition & 1 deletion src/module/actor/character/elemental-blast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { ElementTrait, elementTraits } from "@scripts/config/traits.ts";
import { eventToRollParams } from "@scripts/sheet-util.ts";
import { CheckRoll } from "@system/check/index.ts";
import { DamagePF2e } from "@system/damage/damage.ts";
import { DamageModifierDialog } from "@system/damage/dialog.ts";
import { createDamageFormula } from "@system/damage/formula.ts";
import { applyDamageDiceOverrides } from "@system/damage/helpers.ts";
import { DamageRoll } from "@system/damage/roll.ts";
Expand All @@ -34,7 +35,6 @@ import type {
StringField,
} from "types/foundry/common/data/fields.d.ts";
import type { CharacterPF2e } from "./document.ts";
import { DamageModifierDialog } from "@system/damage/dialog.ts";

class ElementalBlast {
actor: CharacterPF2e;
Expand Down
2 changes: 1 addition & 1 deletion src/module/actor/character/sheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1431,7 +1431,7 @@ class CharacterSheetPF2e<TActor extends CharacterPF2e> extends CreatureSheetPF2e
itemSource: ItemSourcePF2e
): Promise<CollectionValue<TActor["items"]>[]>;
protected override async _onSortItem(event: DragEvent, itemSource: ItemSourcePF2e): Promise<ItemPF2e<ActorPF2e>[]> {
const item = this.actor.items.get(itemSource._id);
const item = this.actor.items.get(itemSource._id!);
if (item?.isOfType("feat")) {
const featSlot = this.#getNearestFeatSlotId(event);
if (featSlot) {
Expand Down
12 changes: 6 additions & 6 deletions src/module/actor/creature/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import { isCycle } from "@item/container/helpers.ts";
import { ArmorSource, ItemType } from "@item/data/index.ts";
import { EquippedData, ItemCarryType } from "@item/physical/data.ts";
import { isEquipped } from "@item/physical/usage.ts";
import { ActiveEffectPF2e } from "@module/active-effect.ts";
import type { ActiveEffectPF2e } from "@module/active-effect.ts";
import { Rarity, SIZES, SIZE_SLUGS } from "@module/data.ts";
import { RollNotePF2e } from "@module/notes.ts";
import { extractModifiers } from "@module/rules/helpers.ts";
import { RuleElementSynthetics } from "@module/rules/index.ts";
import { BaseSpeedSynthetic } from "@module/rules/synthetics.ts";
import { UserPF2e } from "@module/user/index.ts";
import type { UserPF2e } from "@module/user/index.ts";
import { LightLevels } from "@scene/data.ts";
import type { TokenDocumentPF2e } from "@scene/index.ts";
import { eventToRollParams } from "@scripts/sheet-util.ts";
Expand Down Expand Up @@ -737,22 +737,22 @@ interface CreaturePF2e<TParent extends TokenDocumentPF2e | null = TokenDocumentP
get hitPoints(): HitPointsSummary;

/** Expand DocumentModificationContext for creatures */
update(data: DocumentUpdateData<this>, options?: CreatureUpdateContext<TParent>): Promise<this>;
update(data: Record<string, unknown>, options?: CreatureUpdateContext<TParent>): Promise<this>;

/** See implementation in class */
updateEmbeddedDocuments(
embeddedName: "ActiveEffect",
updateData: EmbeddedDocumentUpdateData<ActiveEffectPF2e<this>>[],
updateData: EmbeddedDocumentUpdateData[],
options?: DocumentUpdateContext<this>
): Promise<ActiveEffectPF2e<this>[]>;
updateEmbeddedDocuments(
embeddedName: "Item",
updateData: EmbeddedDocumentUpdateData<ItemPF2e<this>>[],
updateData: EmbeddedDocumentUpdateData[],
options?: DocumentUpdateContext<this>
): Promise<ItemPF2e<this>[]>;
updateEmbeddedDocuments(
embeddedName: "ActiveEffect" | "Item",
updateData: EmbeddedDocumentUpdateData<ActiveEffectPF2e<this> | ItemPF2e<this>>[],
updateData: EmbeddedDocumentUpdateData[],
options?: DocumentUpdateContext<this>
): Promise<ActiveEffectPF2e<this>[] | ItemPF2e<this>[]>;

Expand Down
2 changes: 1 addition & 1 deletion src/module/actor/creature/sheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ export abstract class CreatureSheetPF2e<TActor extends CreaturePF2e> extends Act
const dropSlotType = dropItemEl?.dataset.itemType;
const dropContainerType = dropContainerEl?.dataset.containerType;

const item = this.actor.items.get(itemSource._id);
const item = this.actor.items.get(itemSource._id!);
if (!item) return [];

// if they are dragging onto another spell, it's just sorting the spells
Expand Down
2 changes: 1 addition & 1 deletion src/module/actor/creature/spell-preparation-sheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ class SpellPreparationSheet<TActor extends CreaturePF2e> extends ActorSheet<TAct
protected override async _onSortItem(event: DragEvent, itemData: ItemSourcePF2e): Promise<ItemPF2e<ActorPF2e>[]> {
if (itemData.type !== "spell") return [];

const spell = this.actor.items.get(itemData._id);
const spell = this.actor.items.get(itemData._id!);
if (itemData.system.location.value !== this.item.id && spell?.isOfType("spell")) {
const addedSpell = await this.item.spells?.addSpell(spell);
return [addedSpell ?? []].flat();
Expand Down
20 changes: 11 additions & 9 deletions src/module/actor/data/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,22 @@ import { ActorType } from "./index.ts";
import type { Immunity, ImmunitySource, Resistance, ResistanceSource, Weakness, WeaknessSource } from "./iwr.ts";

/** Base interface for all actor data */
interface BaseActorSourcePF2e<TType extends ActorType, TSystemSource extends ActorSystemSource = ActorSystemSource>
extends foundry.documents.ActorSource<TType, TSystemSource, ItemSourcePF2e> {
type BaseActorSourcePF2e<
TType extends ActorType,
TSystemSource extends ActorSystemSource = ActorSystemSource
> = foundry.documents.ActorSource<TType, TSystemSource, ItemSourcePF2e> & {
flags: DeepPartial<ActorFlagsPF2e>;
prototypeToken: PrototypeTokenSourcePF2e;
}
};

interface ActorFlagsPF2e extends foundry.documents.ActorFlags {
type ActorFlagsPF2e = foundry.documents.ActorFlags & {
pf2e: {
rollOptions: RollOptionFlags;
/** IDs of granted items that are tracked */
trackedItems: Record<string, string>;
[key: string]: unknown;
};
}
};

interface ActorSystemSource {
details?: ActorDetailsSource;
Expand Down Expand Up @@ -262,17 +264,17 @@ interface Rollable {
roll: RollFunction;
}

interface PrototypeTokenSourcePF2e extends foundry.data.PrototypeTokenSource {
flags: foundry.data.PrototypeToken<ActorPF2e>["flags"] & {
type PrototypeTokenSourcePF2e = foundry.data.PrototypeTokenSource & {
flags: {
pf2e?: {
linkToActorSize?: boolean;
autoscale?: boolean;
};
};
}
};

interface PrototypeTokenPF2e<TParent extends ActorPF2e | null> extends foundry.data.PrototypeToken<TParent> {
flags: foundry.data.PrototypeToken<NonNullable<TParent>>["flags"] & {
flags: DocumentFlags & {
pf2e: {
linkToActorSize: boolean;
autoscale: boolean;
Expand Down
12 changes: 6 additions & 6 deletions src/module/actor/inventory/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { ActorPF2e } from "@actor";
import { ItemProxyPF2e, KitPF2e, PhysicalItemPF2e, TreasurePF2e } from "@item";
import { ItemProxyPF2e, KitPF2e, PhysicalItemPF2e } from "@item";
import { ItemSourcePF2e } from "@item/data/index.ts";
import { Coins } from "@item/physical/data.ts";
import { CoinsPF2e, coinCompendiumIds } from "@item/physical/helpers.ts";
import { DENOMINATIONS } from "@item/physical/values.ts";
import { coinCompendiumIds, CoinsPF2e } from "@item/physical/helpers.ts";
import { DelegatedCollection, ErrorPF2e, groupBy } from "@util";
import { InventoryBulk } from "./bulk.ts";
import { ItemSourcePF2e } from "@item/data/index.ts";

class ActorInventory<TActor extends ActorPF2e> extends DelegatedCollection<PhysicalItemPF2e<TActor>> {
declare actor: TActor;
declare bulk: InventoryBulk;
actor: TActor;
bulk: InventoryBulk;

constructor(actor: TActor, entries?: PhysicalItemPF2e<TActor>[]) {
super(entries?.map((entry) => [entry.id, entry]));
Expand Down Expand Up @@ -165,7 +165,7 @@ class ActorInventory<TActor extends ActorPF2e> extends DelegatedCollection<Physi
let quantityToRemove = coinsToRemove[denomination];
const coinItems = coinsByDenomination.get(denomination);
if (!!quantityToRemove && coinItems) {
const itemsToUpdate: EmbeddedDocumentUpdateData<TreasurePF2e>[] = [];
const itemsToUpdate: EmbeddedDocumentUpdateData[] = [];
const itemsToDelete: string[] = [];
for (const item of coinItems) {
if (quantityToRemove === 0) break;
Expand Down
4 changes: 2 additions & 2 deletions src/module/actor/npc/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ import type { MeleePF2e } from "@item";
import { Rarity, Size } from "@module/data.ts";
import type { ArmorClassTraceData, StatisticTraceData } from "@system/statistic/index.ts";

interface NPCSource extends BaseCreatureSource<"npc", NPCSystemSource> {
type NPCSource = BaseCreatureSource<"npc", NPCSystemSource> & {
flags: DeepPartial<NPCFlags>;
}
};

type NPCFlags = ActorFlagsPF2e & {
pf2e: { lootable: boolean };
Expand Down
2 changes: 1 addition & 1 deletion src/module/actor/party/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class PartyPF2e<TParent extends TokenDocumentPF2e | null = TokenDocumentPF2e | n
}

override updateSource(
data?: DocumentUpdateData<PartyPF2e>,
data?: Record<string, unknown>,
options?: DocumentSourceUpdateContext
): DeepPartial<this["_source"]> {
if (!this.campaign) return super.updateSource(data, options);
Expand Down
2 changes: 1 addition & 1 deletion src/module/actor/party/kingdom/sheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ class KingdomSheetPF2e extends ActorSheetPF2e<PartyPF2e> {
event: ElementDragEvent,
itemSource: ItemSourcePF2e
): Promise<ItemPF2e<PartyPF2e>[]> {
const item = this.actor.items.get(itemSource._id);
const item = this.actor.items.get(itemSource._id!);
if (item?.isOfType("campaignFeature") && (item.isFeat || item.isFeature)) {
const featSlot = this.#getNearestFeatSlotId(event);
if (!featSlot) return [];
Expand Down
2 changes: 1 addition & 1 deletion src/module/actor/sheet/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1355,7 +1355,7 @@ abstract class ActorSheetPF2e<TActor extends ActorPF2e> extends ActorSheet<TActo
return super._onSubmit(event, { updateData, preventClose, preventRender });
}

protected override _getSubmitData(updateData?: DocumentUpdateData<TActor>): Record<string, unknown> {
protected override _getSubmitData(updateData?: Record<string, unknown>): Record<string, unknown> {
const data = super._getSubmitData(updateData);
processTagifyInSubmitData(this.form, data);

Expand Down
4 changes: 1 addition & 3 deletions src/module/actor/spellcasting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,7 @@ export class ActorSpellcasting<TActor extends ActorPF2e> extends Collection<Base
itemUpdates: ((Record<string, unknown> | Partial<SpellcastingEntrySource>) & { _id: string })[];
actorUpdates: { "system.resources.focus.value": number } | null;
} {
type SpellcastingUpdate =
| EmbeddedDocumentUpdateData<SpellcastingEntryPF2e>
| EmbeddedDocumentUpdateData<SpellcastingEntryPF2e>[];
type SpellcastingUpdate = EmbeddedDocumentUpdateData | EmbeddedDocumentUpdateData[];

const itemUpdates = this.contents.flatMap((entry): SpellcastingUpdate => {
if (!(entry instanceof SpellcastingEntryPF2e)) return [];
Expand Down
4 changes: 2 additions & 2 deletions src/module/chat-message/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import { DamageRollContext } from "@system/damage/types.ts";
import { DegreeAdjustmentsRecord, DegreeOfSuccessString } from "@system/degree-of-success.ts";
import type { ChatMessageFlags } from "types/foundry/common/documents/chat-message.d.ts";

interface ChatMessageSourcePF2e extends foundry.documents.ChatMessageSource {
type ChatMessageSourcePF2e = foundry.documents.ChatMessageSource & {
flags: ChatMessageFlagsPF2e;
}
};

export interface ItemOriginFlag {
type: ItemType;
Expand Down
Loading

0 comments on commit 0f7b602

Please sign in to comment.