Skip to content

Commit

Permalink
Merge pull request #824 from ben/ae-and-progress-type-updates
Browse files Browse the repository at this point in the history
ActiveEffect and progress-related type/field updates
  • Loading branch information
rsek authored Jun 25, 2023
2 parents b9a9e4b + fb9401b commit d25f2fb
Show file tree
Hide file tree
Showing 24 changed files with 249 additions and 229 deletions.
39 changes: 30 additions & 9 deletions src/module/active-effect/types.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import type { ActiveEffectDataSource } from '@league-of-foundry-developers/foundry-vtt-types/src/foundry/common/data/data.mjs/activeEffectData'
import type {
EffectChangeData,
EffectChangeDataConstructorData
EffectChangeDataConstructorData,
EffectChangeDataProperties
} from '@league-of-foundry-developers/foundry-vtt-types/src/foundry/common/data/data.mjs/effectChangeData'
import type { EffectDurationDataConstructorData } from '@league-of-foundry-developers/foundry-vtt-types/src/foundry/common/data/data.mjs/effectDurationData'
import type {
ConfiguredDocumentClassForName,
ConfiguredFlags
} from '@league-of-foundry-developers/foundry-vtt-types/src/types/helperTypes'
EffectDurationDataConstructorData,
EffectDurationDataSource
} from '@league-of-foundry-developers/foundry-vtt-types/src/foundry/common/data/data.mjs/effectDurationData'
import type { ConfiguredFlags } from '@league-of-foundry-developers/foundry-vtt-types/src/types/helperTypes'
import type { ConfiguredDocumentClass } from '../../types/helperTypes'

export type ImpactCategoryStarforged = 'conditions' | 'banes' | 'burdens'
export type DebilityCategoryClassic =
Expand Down Expand Up @@ -68,8 +71,18 @@ declare global {
statuses?: Set<string> | string[]
flags?: ConfiguredFlags<'ActiveEffect'>
}
export interface StatusEffect extends ActiveEffectDataConstructorData {
export interface StatusEffectV11 {
id: string
name: string
changes?: EffectChangeDataProperties[]
disabled?: boolean
duration?: EffectDurationDataSource
description?: string
statuses?: string[] | Set<string>
icon: string
overlay?: boolean
tint?: string
flags?: ConfiguredFlags<'ActiveEffect'>
}
export namespace foundry {
export namespace data {
Expand All @@ -88,9 +101,15 @@ declare global {

statuses: Set<string>
}
export interface ActiveEffectData extends ActiveEffectDataProperties {}

type ActiveEffectDataSourceV11 = ActiveEffectDataSource & {
name: string
statuses: string[]
}
export interface ActiveEffectData extends ActiveEffectDataProperties {
toJSON(): ActiveEffectDataSourceV11
}
}
export namespace documents {}
}

export interface ActiveEffect
Expand All @@ -104,7 +123,9 @@ declare global {
/**
* Retrieve the Document that this ActiveEffect targets for modification.
*/
get target(): InstanceType<ConfiguredDocumentClassForName<'Actor'>> | null
get target(): InstanceType<
ConfiguredDocumentClass<typeof foundry.documents.BaseActor>
> | null
/**
* Whether the Active Effect currently applying its changes to the target.
*/
Expand Down
8 changes: 2 additions & 6 deletions src/module/actor/subtypes/shared.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import {
ConditionMeterField,
ConditionMeterSource
} from '../../fields/MeterField'
import { MeterValueField } from '../../fields/MeterValueField'
import type { ConditionMeterSource } from '../../fields/MeterField'
import { ConditionMeterField } from '../../fields/MeterField'
import type { DataSchema } from '../../fields/utils'
import type { IronswornActor } from '../actor'

Expand All @@ -16,7 +13,6 @@ export class SharedModel extends foundry.abstract.TypeDataModel<
static override defineSchema(): DataSchema<SharedDataSourceData> {
return {
biography: new foundry.data.fields.HTMLField(),
// @ts-expect-error
supply: new ConditionMeterField({ label: 'IRONSWORN.Supply' })
}
}
Expand Down
7 changes: 3 additions & 4 deletions src/module/actor/subtypes/site.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { TableResultDataConstructorData } from '@league-of-foundry-developers/foundry-vtt-types/src/foundry/common/data/data.mjs/tableResultData'
import type { ChallengeRank } from 'dataforged'
import { ChallengeRankField } from '../../fields/ChallengeRankField'
import { ChallengeRank } from '../../fields/ChallengeRank'
import { ProgressTicksField } from '../../fields/ProgressTicksField'
import type { TableResultStub } from '../../fields/TableResultField'
import { TableResultField } from '../../fields/TableResultField'
Expand Down Expand Up @@ -114,7 +113,7 @@ export class SiteModel extends foundry.abstract.TypeDataModel<
static override defineSchema(): DataSchema<SiteDataSourceData> {
const fields = foundry.data.fields
return {
rank: new ChallengeRankField(),
rank: new ChallengeRank(),
current: new ProgressTicksField(),
objective: new fields.HTMLField(),
description: new fields.HTMLField(),
Expand Down Expand Up @@ -181,7 +180,7 @@ interface SiteDataSourceData {
objective: string
description: string
notes: string
rank: ChallengeRank
rank: ChallengeRank.Value
current: number
denizens: TableResultStub[]
}
Expand Down
4 changes: 0 additions & 4 deletions src/module/actor/subtypes/starship.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { MeterValueField } from '../../fields/MeterValueField'
import { ImpactField } from '../../fields/ImpactField'
import type { IronswornActor } from '../actor'
import type { DataSchema } from '../../fields/utils'
Expand All @@ -12,8 +11,6 @@ export class StarshipModel extends foundry.abstract.TypeDataModel<

static override defineSchema(): DataSchema<StarshipDataSourceData> {
return {
health: new MeterValueField({ label: 'IRONSWORN.Integrity' }),
// @ts-expect-error
debility: new foundry.data.fields.SchemaField<
StarshipDataSourceData['debility']
>({
Expand All @@ -26,7 +23,6 @@ export class StarshipModel extends foundry.abstract.TypeDataModel<
export interface StarshipModel extends StarshipDataSourceData {}

interface StarshipDataSourceData {
health: number
debility: {
battered: boolean
cursed: boolean
Expand Down
9 changes: 9 additions & 0 deletions src/module/application/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
declare global {
namespace Application {
interface HeaderButton {
tooltip?: string
}
}
}

export {}
28 changes: 11 additions & 17 deletions src/module/constants.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
export enum ChallengeRank {
Troublesome = 1,
Dangerous = 2,
Formidable = 3,
Extreme = 4,
Epic = 5
}
import { ChallengeRank } from './fields/ChallengeRank'

/**
* The number of ticks in one unit of progress.
*/
export const RANK_INCREMENTS = {
[ChallengeRank.Troublesome]: 12,
[ChallengeRank.Dangerous]: 8,
[ChallengeRank.Formidable]: 4,
[ChallengeRank.Extreme]: 2,
[ChallengeRank.Epic]: 1
[ChallengeRank.RANK.Troublesome]: 12,
[ChallengeRank.RANK.Dangerous]: 8,
[ChallengeRank.RANK.Formidable]: 4,
[ChallengeRank.RANK.Extreme]: 2,
[ChallengeRank.RANK.Epic]: 1
}

/**
Expand All @@ -25,11 +19,11 @@ export const RANK_REWARDS_SF = {
* A troublesome reward that's been downgraded.
*/
0: 0,
[ChallengeRank.Troublesome]: 1,
[ChallengeRank.Dangerous]: 2,
[ChallengeRank.Formidable]: 4,
[ChallengeRank.Extreme]: 8,
[ChallengeRank.Epic]: 12,
[ChallengeRank.RANK.Troublesome]: 1,
[ChallengeRank.RANK.Dangerous]: 2,
[ChallengeRank.RANK.Formidable]: 4,
[ChallengeRank.RANK.Extreme]: 8,
[ChallengeRank.RANK.Epic]: 12,
/**
* An epic reward that's been upgraded.
*/
Expand Down
10 changes: 5 additions & 5 deletions src/module/features/chat-alert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import type { ChatMessageDataConstructorData } from '@league-of-foundry-develope
import { compact, get } from 'lodash-es'
import type { DocumentSubTypes } from '../../types/helperTypes'
import type { IronswornActor } from '../actor/actor'
import { ChallengeRank } from '../fields/ChallengeRank'
import { IronswornSettings } from '../helpers/settings'
import { localizeRank } from '../helpers/util'
import type { IronswornItem } from '../item/item'

type ActorTypeHandler<T extends DocumentSubTypes<'Actor'> = any> = (
Expand Down Expand Up @@ -248,8 +248,8 @@ const ACTOR_TYPE_HANDLERS: ActorTypeHandlers = {
site: (actor, data) => {
if (data.system?.rank != null) {
return game.i18n.format('IRONSWORN.ChatAlert.RankChanged', {
old: localizeRank(actor.system.rank),
new: localizeRank(data.system.rank)
old: ChallengeRank.localizeValue(actor.system.rank),
new: ChallengeRank.localizeValue(data.system.rank)
})
}
if (data.system?.current !== undefined) {
Expand All @@ -266,8 +266,8 @@ const ITEM_TYPE_HANDLERS: ItemTypeHandlers = {
progress: (item, data) => {
if (data.system?.rank) {
return game.i18n.format('IRONSWORN.ChatAlert.rankChanged', {
old: localizeRank(item.system.rank),
new: localizeRank(data.system.rank)
old: ChallengeRank.localizeValue(item.system.rank),
new: ChallengeRank.localizeValue(data.system.rank)
})
}
if (data.system?.current !== undefined) {
Expand Down
71 changes: 71 additions & 0 deletions src/module/fields/ChallengeRank.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/** Represents a challenge rank, usually for a progress track. */
export class ChallengeRank extends foundry.data.fields.NumberField {
/**
* Enumerates challenge ranks.
* @enum
*/
static readonly RANK = {
Troublesome: 1,
Dangerous: 2,
Formidable: 3,
Extreme: 4,
Epic: 5
} as const

static readonly MIN = this.RANK.Troublesome
static readonly MAX = this.RANK.Epic

static readonly i18nKeys = Object.fromEntries(
Object.entries(ChallengeRank.RANK).map(([key, numericValue]) => [
numericValue,
`IRONSWORN.CHALLENGERANK.${key}`
])
) as Record<ChallengeRank.Value, string>

static localizeValue(rank: ChallengeRank.Value) {
return game.i18n.localize(ChallengeRank.i18nKeys[rank])
}

constructor(
options?: Partial<
Omit<
foundry.data.fields.NumberField.Options,
'choices' | 'step' | 'integer' | 'max' | 'min' | 'positive'
>
>
) {
super({
label: 'IRONSWORN.ChallengeRank',
choices: ChallengeRank.i18nKeys,
initial: ChallengeRank.MIN as number,
integer: true,
min: ChallengeRank.MIN,
max: ChallengeRank.MAX,
...options
})
}

override _cast(value: unknown) {
switch (true) {
case value === 'formidible':
return ChallengeRank.RANK.Formidable
case typeof value === 'string':
return ChallengeRank.RANK[
(value as string).capitalize() as keyof typeof ChallengeRank.RANK
]
default: {
return super._cast(value)
}
}
}
}
export interface ChallengeRank extends foundry.data.fields.NumberField {
choices: {
[R in ChallengeRank.Value]: string
}
}

export namespace ChallengeRank {
/** A numeric challenge rank value. */
export type Value = ValueOf<(typeof ChallengeRank)['RANK']>
}
50 changes: 0 additions & 50 deletions src/module/fields/ChallengeRankField.ts

This file was deleted.

18 changes: 0 additions & 18 deletions src/module/fields/MeterValueField.ts

This file was deleted.

7 changes: 4 additions & 3 deletions src/module/fields/types/DataField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ declare global {
namespace data {
namespace fields {
export abstract class DataField<
SourceData = unknown,
SourceData = any,
ConcreteData = SourceData,
Options extends DataField.Options<
SourceData,
Expand Down Expand Up @@ -174,7 +174,7 @@ declare global {
protected _getField(path: string[]): undefined | this
}
export interface DataField<
SourceData = unknown,
SourceData = any,
ConcreteData = SourceData,
Options extends DataField.Options<
SourceData,
Expand Down Expand Up @@ -233,7 +233,8 @@ declare global {
*/
initial?:
| ConcreteData
| ((data: SourceData) => ConcreteData)
| Partial<ConcreteData>
| ((data: SourceData) => ConcreteData | Partial<ConcreteData>)
| undefined
/**
* A data validation function which accepts one argument with the current value.
Expand Down
Loading

0 comments on commit d25f2fb

Please sign in to comment.