Skip to content

Commit

Permalink
challenge rank field from progress model branch
Browse files Browse the repository at this point in the history
  • Loading branch information
rsek committed Jun 25, 2023
1 parent 796aafb commit 25c1be1
Showing 1 changed file with 71 additions and 0 deletions.
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']>
}

0 comments on commit 25c1be1

Please sign in to comment.