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

Instant, painless type rename with document#migrateData #727

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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: 2 additions & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ module.exports = {
},
plugins: ['@typescript-eslint'],
rules: {
// LoFD, the target of most of our augmentations, uses namespaces. More generally, namespaces are necessary to augment static class members.
'@typescript-eslint/no-namespace': 'off',
'@typescript-eslint/no-unused-vars': [1, { argsIgnorePattern: '^_' }],
'@typescript-eslint/explicit-module-boundary-types': 'off',
'@typescript-eslint/ban-ts-comment': 'off',
Expand Down
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ Hooks.once('init', async () => {
})

Items.registerSheet('ironsworn', SFMoveSheet, {
types: ['sfmove'],
types: ['move'],
label: 'IRONSWORN.ITEM.TypeMove'
})

Expand Down Expand Up @@ -175,15 +175,15 @@ Hooks.once('init', async () => {
asset: 'IRONSWORN.ITEM.TypeAsset',
progress: 'IRONSWORN.ITEM.TypeProgressTrack',
bondset: 'IRONSWORN.ITEM.TypeBondset',
sfmove: 'IRONSWORN.ITEM.TypeMove',
move: 'IRONSWORN.ITEM.TypeMove',
'delve-domain': 'IRONSWORN.ITEM.TypeDelveDomain',
'delve-theme': 'IRONSWORN.ITEM.TypeDelveTheme'
})
CONFIG.Item.typeIcons = mergeObject(CONFIG.Item.typeIcons, {
asset: 'fa-solid fa-cards-blank',
progress: 'fa-solid fa-asterisk',
bondset: 'fa-solid fa-handshake',
sfmove: 'icon isicon-d10-tilt',
move: 'icon isicon-d10-tilt',
// FIXME ideally, these would be distinct from assets, but all three card types are abstract enough than an icon is tricky
'delve-domain': 'fa-duotone fa-cards-blank',
'delve-theme': 'fa-duotone fa-cards-blank'
Expand Down
4 changes: 2 additions & 2 deletions src/module/chat/cards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class IronswornChatCard {

const fPack = game.packs.get(pack)
const fItem = fPack?.get(id) as IronswornItem
if (fItem?.type !== 'sfmove') return []
if (fItem?.type !== 'move') return []

const system = fItem.system as SFMoveDataPropertiesData
const oracleIds = system.Oracles ?? []
Expand Down Expand Up @@ -98,7 +98,7 @@ export class IronswornChatCard {
const { uuid } = ev.currentTarget.dataset

const item = (await fromUuid(uuid)) as IronswornItem
if (item?.type !== 'sfmove') {
if (item?.type !== 'move') {
console.log('falling through')
return // (TextEditor as any)._onClickContentLink(ev)
}
Expand Down
2 changes: 1 addition & 1 deletion src/module/dataforged/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ function movesForCategories(
console.log(move.Name, move.$id)
movesToCreate.push({
_id: hashLookup(cleanMove.dfid),
type: 'sfmove',
type: 'move',
name: move.Name,
img: 'icons/dice/d10black.svg',
system: cleanMove
Expand Down
2 changes: 1 addition & 1 deletion src/module/features/custommoves.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ async function augmentWithFolderContents(categories: MoveCategory[]) {

const customMoves = [] as Move[]
for (const moveItem of folder.contents) {
if (moveItem.documentName !== 'Item' || moveItem.type !== 'sfmove') continue
if (moveItem.documentName !== 'Item' || moveItem.type !== 'move') continue
customMoves.push({
color,
displayName: moveItem.name ?? '(move)',
Expand Down
24 changes: 23 additions & 1 deletion src/module/item/item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,19 @@ import type {
SFMoveDataPropertiesData
} from './itemtypes'

declare global {
namespace Item {
/**
* Migrate candidate source data for this DataModel which may require initial cleaning or transformations.
* @param source - The candidate source data from which the model will be constructed
* @returns Migrated source data, if necessary
*/
function migrateData(
source: Record<string, unknown>
): Record<string, unknown>
}
}

/**
* Extend the base Item entity
* @extends {Item}
Expand All @@ -21,6 +34,15 @@ export class IronswornItem extends Item {
declare system: typeof this.data.data
declare sort: typeof this.data.sort

static override migrateData(source: Record<string, unknown>) {
// run tihs first so we don't have to worry about e.g. data => system
source = super.migrateData(source)

/** Migration 6: rename type since it's the only kind of move we use now */
if (source.type === 'sfmove') source.type = 'move'
return source
}

protected override _onCreate(
data: this['data']['_source'],
options: DocumentModificationOptions,
Expand Down Expand Up @@ -126,7 +148,7 @@ export class IronswornItem extends Item {
* Move methods
*/
isProgressMove(): boolean | undefined {
if (this.type !== 'sfmove') return
if (this.type !== 'move') return

const sfMoveSystem = this.system as SFMoveDataPropertiesData
return sfMoveSystem.Trigger.Options?.some(
Expand Down
4 changes: 2 additions & 2 deletions src/module/item/itemtypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,11 @@ export interface SFMoveDataPropertiesData extends IMove {
}

export interface SFMoveDataSource {
type: 'sfmove'
type: 'move'
data: SFMoveDataPropertiesData
}
export interface SFMoveDataProperties {
type: 'sfmove'
type: 'move'
data: SFMoveDataPropertiesData
}

Expand Down
4 changes: 2 additions & 2 deletions src/module/rolls/ironsworn-roll-message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ export class IronswornRollMessage {
this.roll.postRollOptions.replacedOutcome?.source
}
const move = await this.roll.moveItem
if (move?.type !== 'sfmove') return ret
if (move?.type !== 'move') return ret

const key = DfRollOutcome[theOutcome]
const moveSystem = move.system as SFMoveDataPropertiesData
Expand Down Expand Up @@ -319,7 +319,7 @@ export class IronswornRollMessage {

private async oraclesData(): Promise<any> {
const move = await this.roll.moveItem
if (move?.type !== 'sfmove') return {}
if (move?.type !== 'move') return {}

const system = move.system as SFMoveDataPropertiesData
const dfids = system.Oracles ?? []
Expand Down
4 changes: 2 additions & 2 deletions src/module/rolls/preroll-dialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ function rollableOptions(trigger: IMoveTrigger) {
}

export function moveHasRollableOptions(move: IronswornItem) {
if (move.type !== 'sfmove') return false
if (move.type !== 'move') return false
const data = move.system as SFMoveDataPropertiesData
const options = rollableOptions(data.Trigger)
return options.length > 0
Expand Down Expand Up @@ -285,7 +285,7 @@ export class IronswornPrerollDialog extends Dialog<
}

static async showForMove(move: IronswornItem, opts?: showForMoveOpts) {
if (move.type !== 'sfmove') {
if (move.type !== 'move') {
throw new Error('this only works with SF moves')
}

Expand Down
2 changes: 1 addition & 1 deletion src/module/vue/components/with-rolllisteners.vue
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ async function click(ev: JQuery.ClickEvent) {
const { uuid, dfid } = ev.currentTarget.dataset
if (uuid) {
const gameItem = (await fromUuid(uuid)) as IronswornItem | IronswornActor
if (gameItem?.type === 'sfmove') {
if (gameItem?.type === 'move') {
$emit('moveclick', gameItem)
return !!$attrs['onMoveclick']
}
Expand Down
4 changes: 2 additions & 2 deletions system/template.json
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@
"asset",
"progress",
"bondset",
"sfmove",
"move",
"delve-theme",
"delve-domain"
],
Expand Down Expand Up @@ -234,7 +234,7 @@
],
"conditions": []
},
"sfmove": {
"move": {
"dfid": "",
"Category": "",
"Progress Move": false,
Expand Down