Skip to content

Commit

Permalink
replace ChallengeRankField with ChallengeRank
Browse files Browse the repository at this point in the history
  • Loading branch information
rsek committed Jun 25, 2023
1 parent 25c1be1 commit fb9401b
Show file tree
Hide file tree
Showing 16 changed files with 62 additions and 128 deletions.
1 change: 0 additions & 1 deletion src/module/actor/subtypes/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,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
1 change: 0 additions & 1 deletion src/module/actor/subtypes/starship.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export class StarshipModel extends foundry.abstract.TypeDataModel<

static override defineSchema(): DataSchema<StarshipDataSourceData> {
return {
// @ts-expect-error
debility: new foundry.data.fields.SchemaField<
StarshipDataSourceData['debility']
>({
Expand Down
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
50 changes: 0 additions & 50 deletions src/module/fields/ChallengeRankField.ts

This file was deleted.

9 changes: 0 additions & 9 deletions src/module/helpers/util.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
import { ChallengeRank } from '../constants'
import type { Metadata } from '@league-of-foundry-developers/foundry-vtt-types/src/foundry/common/abstract/document.mjs'
import type { KeysWithValuesOfType } from 'dataforged'

/**
* @returns A localized string label for the challenge rank.
*/
export function localizeRank(rank: ChallengeRank | keyof typeof ChallengeRank) {
const key = typeof rank === 'string' ? rank : ChallengeRank[rank]
return game.i18n.localize(`IRONSWORN.CHALLENGERANK.${key}`)
}

/**
* @remarks A document-subtype-sensitive replacement for the FVTT document deletion dialog.
* @see {@link ClientDocumentMixin#deleteDialog}
Expand Down
4 changes: 2 additions & 2 deletions src/module/item/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { DelveDomainModel } from './subtypes/delve-domain'
import { DelveThemeModel } from './subtypes/delve-theme'
import { ProgressModel } from './subtypes/progress'
import { SFMoveModel } from './subtypes/sfmove'
import type { ChallengeRank } from '../constants'
import type { AssetDataProperties, AssetDataSource } from './subtypes/asset'
import type {
BondsetDataProperties,
Expand All @@ -26,6 +25,7 @@ import type {
ProgressDataSource
} from './subtypes/progress'
import type { SFMoveDataProperties, SFMoveDataSource } from './subtypes/sfmove'
import { ChallengeRank } from '../fields/ChallengeRank'

const dataModels: Partial<
Record<
Expand Down Expand Up @@ -82,7 +82,7 @@ export default config

export interface ProgressBase {
description: string
rank: ChallengeRank
rank: ChallengeRank.Value
current: number
completed: boolean
}
Expand Down
1 change: 0 additions & 1 deletion src/module/item/subtypes/asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ export class AssetModel extends foundry.abstract.TypeDataModel<
value: new fields.StringField()
})
),
// @ts-expect-error
track: new AssetConditionMeterField()
}
}
Expand Down
7 changes: 3 additions & 4 deletions src/module/item/subtypes/progress.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { clamp } from 'lodash-es'
import { RANK_INCREMENTS } from '../../constants'
import { ChallengeRankField } from '../../fields/ChallengeRankField'
import { ChallengeRank } from '../../fields/ChallengeRank'
import { ProgressTicksField } from '../../fields/ProgressTicksField'
import type { DataSchema } from '../../fields/utils'
import { localizeRank } from '../../helpers/util'
import { IronswornPrerollDialog } from '../../rolls'
import type { IronswornItem } from '../item'
import type { ProgressBase } from '../config'
Expand Down Expand Up @@ -68,7 +67,7 @@ export class ProgressModel extends foundry.abstract.TypeDataModel<

/** Provide a localized label for this progress track's challenge rank. */
localizeRank() {
return localizeRank(this.rank)
return ChallengeRank.localizeValue(this.rank)
}

static override defineSchema(): DataSchema<ProgressDataSourceData> {
Expand All @@ -91,7 +90,7 @@ export class ProgressModel extends foundry.abstract.TypeDataModel<
completed: new fields.BooleanField({ initial: false }),
current: new ProgressTicksField(),
description: new fields.HTMLField(),
rank: new ChallengeRankField()
rank: new ChallengeRank()
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/module/journal/journal-entry-page-types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ISettingTruthOption } from 'dataforged'
import type { ChallengeRank } from '../constants'
import type { ChallengeRank } from '../fields/ChallengeRank'
import type { IronswornJournalPage } from './journal-entry-page'

interface CounterBase {
Expand All @@ -21,7 +21,7 @@ interface Countdown extends CounterBase {

interface ProgressTrack {
ticks: number
rank: ChallengeRank
rank: ChallengeRank.Value
/**
* For Threat/Menace from Ironsworn: Delve.
*/
Expand Down
5 changes: 2 additions & 3 deletions src/module/journal/journal-entry-page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { TableResultDataConstructorData } from '@league-of-foundry-develope
import type { BaseUser } from '@league-of-foundry-developers/foundry-vtt-types/src/foundry/common/documents.mjs'
import type { IRow } from 'dataforged'
import { clamp } from 'lodash-es'
import { ChallengeRank, RANK_INCREMENTS } from '../constants'
import { RANK_INCREMENTS } from '../constants'
import { typedDeleteDialog } from '../helpers/util'
import { OracleTable } from '../roll-table/oracle-table'
import { OracleTableResult } from '../roll-table/oracle-table-result'
Expand Down Expand Up @@ -87,11 +87,10 @@ export class IronswornJournalPage<
async markProgress(progressUnits = 1) {
if (this.type !== 'progress') return
const system = this.system as ProgressTrackDataPropertiesData
const legacyRank = ChallengeRank[system.rank]
const oldTicks = system.ticks ?? 0
const minTicks = 0
const maxTicks = 40
const increment = RANK_INCREMENTS[legacyRank] * progressUnits
const increment = RANK_INCREMENTS[system.rank] * progressUnits
const newValue = clamp(oldTicks + increment, minTicks, maxTicks)
return await this.update({ 'system.ticks': newValue })
}
Expand Down
15 changes: 8 additions & 7 deletions src/module/journal/sheet/progress-page.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { fill, range } from 'lodash-es'
import { ChallengeRank, RANK_INCREMENTS } from '../../constants'
import { localizeRank } from '../../helpers/util'
import { RANK_INCREMENTS } from '../../constants'
import { ChallengeRank } from '../../fields/ChallengeRank'
import { IronswornPrerollDialog } from '../../rolls'

export class JournalProgressPageSheet extends JournalPageSheet {
Expand Down Expand Up @@ -33,12 +33,12 @@ export class JournalProgressPageSheet extends JournalPageSheet {
getData(options?: Partial<DocumentSheetOptions> | undefined): any {
const data = super.getData(options) as any

data.currentRank = localizeRank(
data.data.system.rank ?? ChallengeRank.Troublesome
data.currentRank = ChallengeRank.localizeValue(
data.data.system.rank ?? ChallengeRank.RANK.Troublesome
)
data.rankButtons = range(1, 6).map((rank) => ({
data.rankButtons = Object.values(ChallengeRank.RANK).map((rank) => ({
rank,
i18nRank: localizeRank(rank),
i18nRank: ChallengeRank.localizeValue(rank),
selected: data.data.system.rank === rank
}))

Expand Down Expand Up @@ -100,7 +100,8 @@ export class JournalProgressPageSheet extends JournalPageSheet {
}

function increment(object: any, direction: 1 | -1) {
const rank: ChallengeRank = object.system.rank ?? ChallengeRank.Troublesome
const rank: ChallengeRank.Value =
object.system.rank ?? ChallengeRank.RANK.Troublesome
const increment = RANK_INCREMENTS[rank]
const currentValue = object.system.ticks || 0
const newValue = currentValue + increment * direction
Expand Down
4 changes: 2 additions & 2 deletions src/module/vue/components/progress/progress-track.vue
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
<script setup lang="ts">
import { computed } from 'vue'
import { fill } from 'lodash-es'
import type { ChallengeRank } from '../../../constants.js'
import ProgressTrackBox from './progress-track-box.vue'
import { ProgressModel } from '../../../item/subtypes/progress'
import type { ChallengeRank } from '../../../fields/ChallengeRank'
const props = defineProps<{
/**
Expand All @@ -41,7 +41,7 @@ const props = defineProps<{
/**
* Use 'null' if it's an unranked track, such as a Legacy or classic Bonds.
*/
rank: ChallengeRank | null
rank: ChallengeRank.Value | null
legacyOverflow?: boolean
/**
* When true, renders the progress bar for more compact display.
Expand Down
Loading

0 comments on commit fb9401b

Please sign in to comment.