Skip to content

Commit

Permalink
Merge pull request #874 from rsek/meter-migration-fixes
Browse files Browse the repository at this point in the history
Meter migration fixes
  • Loading branch information
ben authored Sep 24, 2023
2 parents c52b114 + ea17336 commit 9f78db2
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 635 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Next Release

- Fixed a bug that sometimes caused health, supply, momentum, or spirit to reset when loading an older world ([#865](https://github.com/ben/foundry-ironsworn/issues/865))

## 1.22.1

- Fixed a bug where pronouns and callsign wouldn't save ([#870](https://github.com/ben/foundry-ironsworn/pull/870))
Expand Down
7 changes: 2 additions & 5 deletions src/module/actor/subtypes/character.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { StatField } from '../../fields/StatField'
import { ImpactField } from '../../fields/ImpactField'
import type { IronswornActor } from '../actor'
import { ProgressTicksField } from '../../fields/ProgressTicksField'
import type { DataSchema } from '../../fields/utils'
import type {
ConditionMeterSource,
MomentumSource
Expand Down Expand Up @@ -30,15 +29,13 @@ export class CharacterModel extends foundry.abstract.TypeDataModel<
async burnMomentum(this: CharacterModel) {
if (this.canBurnMomentum) {
await this.parent.update({
system: { 'momentum.value': this.parent.system.momentum.resetValue }
system: { 'momentum.value': this.parent.system.momentumReset }
})
}
}

get canBurnMomentum() {
return (
this.parent.system.momentum.value > this.parent.system.momentum.resetValue
)
return this.parent.system.momentum.value > this.parent.system.momentumReset
}

get #impactCount() {
Expand Down
36 changes: 12 additions & 24 deletions src/module/fields/MeterField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,19 @@ export abstract class MeterField<
super(schema, options)
}

migrateSource(sourceData: object, fieldData: any) {
if (typeof fieldData === 'number') {
fieldData = { value: fieldData }
}
override _cast(value: unknown): any {
// Cast numberish values to a meter-shaped object
return Number.isNumeric(value)
? { value: Number(value) }
: super._cast(value)
}

override migrateSource(sourceData: object, fieldData: any) {
// migrate legacy asset condition meters
IronswornActor._addDataFieldMigration(fieldData, 'current', 'value')

// the _cast method above handles migrations from simple number values

return super.migrateSource(sourceData, fieldData)
}
}
Expand Down Expand Up @@ -81,6 +87,8 @@ export class MomentumField extends MeterField<MomentumSource> {
label: 'IRONSWORN.Momentum'
},
{
// it's for later use by ActiveEffect to model impact/debility behavior
// if you need to get at the resetValue, use the getter Actor.system.momentumReset instead. otherwise, it won't be sensitive to impacts.
resetValue: new fields.NumberField({
initial: MomentumField.INITIAL,
min: MomentumField.RESET_MIN,
Expand All @@ -90,25 +98,5 @@ export class MomentumField extends MeterField<MomentumSource> {
}
)
}

override migrateSource(
sourceData: CharacterDataSourceData,
fieldData: MomentumSource
): void {
super.migrateSource(sourceData, fieldData)
if (typeof sourceData?.momentum === 'number') {
console.log('Migrating sourceData, fieldData', sourceData, fieldData)
IronswornActor._addDataFieldMigration(
sourceData,
'momentumReset',
'momentum.resetValue'
)
IronswornActor._addDataFieldMigration(
sourceData,
'momentumMax',
'momentum.max'
)
}
}
}
export interface MomentumField extends MomentumSource {}
9 changes: 0 additions & 9 deletions src/module/vue/components/impact/impact-checkbox.vue
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,6 @@ async function input(value: boolean) {
}
await $actor?.update(data)
await nextTick()
const numDebilitiesMarked = Object.values(actor.value.system.debility).filter(
(x) => x === true
).length
await $actor?.update({
system: {
momentumMax: 10 - numDebilitiesMarked,
momentumReset: Math.max(0, 2 - numDebilitiesMarked)
}
})
if (props.global) {
await IronswornSettings.updateGlobalAttribute(data, [
Expand Down
Loading

0 comments on commit 9f78db2

Please sign in to comment.