Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use kit items as testbed for TypeDataModel-based system data #14910

Merged
merged 1 commit into from
Jun 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build/lib/compendium-pack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ class CompendiumPack {
if (itemIsOfType(source, "feat", "action") && source.system.selfEffect) {
source.system.selfEffect.uuid = CompendiumPack.convertUUID(source.system.selfEffect.uuid, convertOptions);
} else if (itemIsOfType(source, "ancestry", "background", "class", "kit")) {
const items: Record<string, { uuid: string; items?: Record<string, { uuid: string }> }> =
const items: Record<string, { uuid: string; items?: Record<string, { uuid: string }> | null }> =
source.system.items;
for (const entry of Object.values(items)) {
entry.uuid = CompendiumPack.convertUUID(entry.uuid, convertOptions);
Expand Down
6 changes: 3 additions & 3 deletions src/module/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ interface NewDocumentMigrationRecord {
}

interface MigratedDocumentMigrationRecord {
version: number;
version: number | null;
previous: {
schema: number | null;
system?: string;
foundry?: string;
system: string | null;
foundry: string | null;
} | null;
}

Expand Down
118 changes: 118 additions & 0 deletions src/module/item/base/data/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import { RuleElementSource } from "@module/rules/index.ts";
import { SlugField } from "@system/schema-data-fields.ts";
import type {
ArrayField,
BooleanField,
NumberField,
ObjectField,
SchemaField,
StringField,
} from "types/foundry/common/data/fields.d.ts";
import type { ItemPF2e } from "../document.ts";
import { ItemDescriptionData } from "./system.ts";

abstract class ItemSystemModel<TParent extends ItemPF2e, TSchema extends ItemSystemSchema> extends foundry.abstract
.TypeDataModel<TParent, TSchema> {
static override defineSchema(): ItemSystemSchema {
const fields = foundry.data.fields;

const anyStringField = (): StringField<string, string, true, false, true> =>
new fields.StringField({ required: true, nullable: false, initial: "" });

return {
description: new fields.SchemaField({
value: anyStringField(),
gm: anyStringField(),
}),
publication: new fields.SchemaField({
title: anyStringField(),
authors: anyStringField(),
license: new fields.StringField({
required: true,
nullable: false,
choices: ["OGL", "ORC"],
initial: "OGL",
}),
remaster: new fields.BooleanField({ required: true, nullable: false, initial: false }),
}),
rules: new fields.ArrayField(new fields.ObjectField({ required: true, nullable: false }), {
required: true,
nullable: false,
}),
slug: new SlugField({ required: true, nullable: true, initial: null }),
traits: new fields.SchemaField({
otherTags: new fields.ArrayField(
new SlugField({ required: true, nullable: false, initial: undefined }),
),
}),
_migration: new fields.SchemaField({
version: new fields.NumberField({
required: true,
nullable: true,
positive: true,
initial: null,
}),
previous: new fields.SchemaField(
{
foundry: new fields.StringField({ required: true, nullable: true, initial: null }),
system: new fields.StringField({ required: true, nullable: true, initial: null }),
schema: new fields.NumberField({
required: true,
nullable: true,
positive: true,
initial: null,
}),
},
{ required: true, nullable: true, initial: null },
),
}),
};
}
}

interface ItemSystemModel<TParent extends ItemPF2e, TSchema extends ItemSystemSchema>
extends foundry.abstract.TypeDataModel<TParent, TSchema> {
description: ItemDescriptionData;
}

type ItemSystemSchema = {
description: SchemaField<{
value: StringField<string, string, true, false, true>;
gm: StringField<string, string, true, false, true>;
}>;
publication: SchemaField<{
title: StringField<string, string, true, false, true>;
authors: StringField<string, string, true, false, true>;
license: StringField<"OGL" | "ORC", "OGL" | "ORC", true, false, true>;
remaster: BooleanField<boolean, boolean, true, false, true>;
}>;
rules: ArrayField<
ObjectField<RuleElementSource, RuleElementSource, true, false, false>,
RuleElementSource[],
RuleElementSource[],
true,
false,
true
>;
slug: SlugField<true, true, true>;
traits: SchemaField<{
otherTags: ArrayField<SlugField<true, false, false>, string[], string[], true, false, true>;
}>;
_migration: SchemaField<{
version: NumberField<number, number, true, true, true>;
previous: SchemaField<
{
foundry: StringField<string, string, true, true, true>;
system: StringField<string, string, true, true, true>;
schema: NumberField<number, number, true, true, true>;
},
{ foundry: string | null; system: string | null; schema: number | null },
{ foundry: string | null; system: string | null; schema: number | null },
true,
true,
true
>;
}>;
};

export { ItemSystemModel, type ItemSystemSchema };
4 changes: 2 additions & 2 deletions src/module/item/base/data/system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,15 @@ type ItemSystemSource = {
/** A record of this actor's current world schema version as well a log of the last migration to occur */
_migration: MigrationRecord;
/** Legacy location of `MigrationRecord` */
schema?: Readonly<{ version: number | null; lastMigration: object | null }>;
schema?: object;
};

interface ItemDescriptionSource {
gm: string;
value: string;
}

interface ItemSystemData extends ItemSystemSource {
interface ItemSystemData extends Omit<ItemSystemSource, "schema"> {
description: ItemDescriptionData;
}

Expand Down
9 changes: 7 additions & 2 deletions src/module/item/base/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ class ItemPF2e<TParent extends ActorPF2e | null = ActorPF2e | null> extends Item

/** The recorded schema version of this item, updated after each data migration */
get schemaVersion(): number | null {
return Number(this.system._migration?.version ?? this.system.schema?.version) || null;
const legacyValue = R.isPlainObject(this._source.system.schema)
? Number(this._source.system.schema.version) || null
: null;
return Number(this._source.system._migration?.version) ?? legacyValue;
}

get description(): string {
Expand Down Expand Up @@ -714,7 +717,9 @@ class ItemPF2e<TParent extends ActorPF2e | null = ActorPF2e | null> extends Item
}

// Remove any rule elements that request their own removal upon item creation
this._source.system.rules = this._source.system.rules.filter((r) => !r.removeUponCreate);
this._source.system.rules = this._source.system.rules.filter(
(r) => !("removeUponCreate" in r) || !r.removeUponCreate,
);

return super._preCreate(data, options, user);
}
Expand Down
160 changes: 143 additions & 17 deletions src/module/item/kit/data.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,150 @@
import type { BaseItemSourcePF2e, ItemSystemData, ItemSystemSource } from "@item/base/data/system.ts";
import type { PartialPrice, PhysicalItemTrait, PhysicalItemTraits } from "@item/physical/data.ts";
import type { ItemPF2e } from "@item";
import { ItemSystemModel, ItemSystemSchema } from "@item/base/data/schema.ts";
import type { BaseItemSourcePF2e, ItemSystemSource } from "@item/base/data/system.ts";
import type { ClassTrait } from "@item/class/types.ts";
import { PriceField } from "@item/physical/schema.ts";
import type { RuleElementSource } from "@module/rules/index.ts";
import { NullField, RecordField, SlugField } from "@system/schema-data-fields.ts";
import type {
ArrayField,
BooleanField,
DocumentUUIDField,
FilePathField,
NumberField,
SchemaField,
StringField,
} from "types/foundry/common/data/fields.d.ts";

type KitSource = BaseItemSourcePF2e<"kit", KitSystemSource>;
class KitEntriesField extends RecordField<
StringField<string, string, true, false, false>,
SchemaField<KitEntryValueSchema>,
true,
false,
true,
true
> {
/**
* @param depth The recursion depth of this field:must be between 0 and 2
*/
constructor(depth = 0) {
const fields = foundry.data.fields;

type KitEntryValueSchema = {
uuid: DocumentUUIDField<ItemUUID, true, false, false>;
img: FilePathField<ImageFilePath, ImageFilePath, true, false, false>;
quantity: NumberField<number, number, true, false, false>;
name: StringField<string, string, true, false, false>;
isContainer: BooleanField<boolean, boolean, true, false, false>;
items: KitEntriesField | NullField;
};
const hasNestedItems = depth <= 2;
const valueSchemaData = (): KitEntryValueSchema => ({
uuid: new fields.DocumentUUIDField<ItemUUID, true, false, false>({
required: true,
nullable: false,
initial: undefined,
}),
img: new fields.FilePathField<ImageFilePath, ImageFilePath, true, false, false>({
categories: ["IMAGE"],
base64: false,
required: true,
nullable: false,
initial: undefined,
}),
quantity: new fields.NumberField({
required: true,
integer: true,
positive: true,
nullable: false,
initial: undefined,
}),
name: new fields.StringField({ required: true, nullable: false, blank: false, initial: undefined }),
isContainer: new fields.BooleanField({
required: true,
nullable: false,
initial: undefined,
}),
items: hasNestedItems ? new KitEntriesField(depth + 1) : new NullField(),
});

interface KitEntryData {
uuid: ItemUUID;
img: ImageFilePath;
quantity: number;
name: string;
isContainer: boolean;
items?: Record<string, KitEntryData>;
super(
new fields.StringField({ required: true, nullable: false, blank: false, initial: undefined }),
new fields.SchemaField(valueSchemaData(), {
required: true,
nullable: false,
initial: undefined,
}),
);
}
}

interface KitSystemSource extends ItemSystemSource {
traits: PhysicalItemTraits<PhysicalItemTrait>;
items: Record<string, KitEntryData>;
price: PartialPrice;
level?: never;
class KitSystemData extends ItemSystemModel<ItemPF2e, KitSystemSchema> {
static override defineSchema(): KitSystemSchema {
const fields = foundry.data.fields;

const traitChoices: Record<ClassTrait, string> = {
...CONFIG.PF2E.classTraits,
};

return {
...super.defineSchema(),
traits: new fields.SchemaField({
otherTags: new fields.ArrayField(
new SlugField({ required: true, nullable: false, initial: undefined }),
),
value: new fields.ArrayField(
new fields.StringField({
required: true,
nullable: false,
choices: traitChoices,
initial: undefined,
}),
),
}),
items: new KitEntriesField(),
price: new PriceField(),
};
}
}

interface KitSystemData extends Omit<KitSystemSource, "description" | "traits">, Omit<ItemSystemData, "level"> {}
interface KitSystemData
extends ItemSystemModel<ItemPF2e, KitSystemSchema>,
Omit<ModelPropsFromSchema<KitSystemSchema>, "description"> {}

type KitEntryData = NonNullable<KitSystemData["items"][string]>;

type KitEntryValueSchema = {
uuid: DocumentUUIDField<ItemUUID, true, false, false>;
img: FilePathField<ImageFilePath, ImageFilePath, true, false, false>;
quantity: NumberField<number, number, true, false, false>;
name: StringField<string, string, true, false, false>;
isContainer: BooleanField<boolean, boolean, true, false, false>;
items: KitEntriesField | NullField;
};

type KitSystemSchema = Omit<ItemSystemSchema, "traits"> & {
traits: SchemaField<{
otherTags: ArrayField<SlugField<true, false, false>, string[], string[], true, false, true>;
value: ArrayField<
StringField<ClassTrait, ClassTrait, true, false, false>,
ClassTrait[],
ClassTrait[],
true,
false,
true
>;
}>;
items: KitEntriesField;
price: PriceField;
};

type KitSystemSource = Omit<SourceFromSchema<KitSystemSchema>, "rules"> & {
level?: never;
schema?: ItemSystemSource["schema"];
rules: RuleElementSource[];
};

type KitSource = BaseItemSourcePF2e<"kit", KitSystemSource>;

export type { KitEntryData, KitSource, KitSystemData, KitSystemSource };
export { KitSystemData };
export type { KitEntryData, KitSource };
8 changes: 2 additions & 6 deletions src/module/item/kit/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ import { ActorSizePF2e } from "@actor/data/size.ts";
import { ItemPF2e, type PhysicalItemPF2e } from "@item";
import type { ClassTrait } from "@item/class/types.ts";
import { Price } from "@item/physical/data.ts";
import { CoinsPF2e } from "@item/physical/helpers.ts";
import { DENOMINATIONS } from "@item/physical/values.ts";
import { Size } from "@module/data.ts";
import type { UserPF2e } from "@module/user/index.ts";
import { ErrorPF2e, isObject } from "@util";
import { UUIDUtils } from "@util/uuid.ts";
import { KitEntryData, KitSource, KitSystemData } from "./data.ts";
import { KitSource, KitSystemData, type KitEntryData } from "./data.ts";

class KitPF2e<TParent extends ActorPF2e | null = ActorPF2e | null> extends ItemPF2e<TParent> {
static override get validTraits(): Record<ClassTrait, string> {
Expand All @@ -21,10 +20,7 @@ class KitPF2e<TParent extends ActorPF2e | null = ActorPF2e | null> extends ItemP
}

get price(): Price {
return {
value: new CoinsPF2e(this.system.price.value),
per: this.system.price.per ?? 1,
};
return this.system.price;
}

/** Expand a tree of kit entry data into a list of physical items */
Expand Down
4 changes: 2 additions & 2 deletions src/module/item/physical/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,12 @@ interface PhysicalItemHitPoints extends PhysicalItemHPSource {
brokenThreshold: number;
}

interface Coins {
type Coins = {
pp?: number;
gp?: number;
sp?: number;
cp?: number;
}
};

interface PartialPrice {
value: Coins;
Expand Down
Loading
Loading