Skip to content

Commit

Permalink
Merge pull request #881 from rsek/compendium-folders
Browse files Browse the repository at this point in the history
Add folders to compendium packs
  • Loading branch information
ben authored Sep 29, 2023
2 parents 9971d75 + 3dc6dfa commit 9a5035b
Show file tree
Hide file tree
Showing 71 changed files with 183 additions and 946 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,9 @@ node_modules/
# Generated files
.DS_Store
dist

# Uneeded LevelDB files. "*.ldb", "MANIFEST-*", and "CURRENT" are required for FVTT to correctly read the database.
system/packs/**/*.log
system/packs/**/LOG
system/packs/**/LOG.old
system/packs/**/LOCK
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Next Release

- Add compendium folders for oracles, moves, and assets
- these don't affect our custom oracle/move/asset tree displays yet, but this change lays some groundwork for it

## 1.22.4

- Fix a bug with "highest-of" stat selection, as with _Endure Stress_ ([#880](https://github.com/ben/foundry-ironsworn/pull/880))
Expand Down
2 changes: 0 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import { StarshipSheet } from './module/actor/sheets/starshipsheet'
import { FirstStartDialog } from './module/applications/firstStartDialog'
import { IronswornChatCard } from './module/chat/cards'
import { registerChatAlertHooks } from './module/features/chat-alert'
import { registerCompendiumCategoryHook } from './module/features/compendium-categories'
import { registerDragAndDropHooks } from './module/features/drag-and-drop'
import { primeCommonPackCaches } from './module/features/pack-cache'
import { activateSceneButtonListeners } from './module/features/sceneButtons'
Expand Down Expand Up @@ -210,7 +209,6 @@ Hooks.once('init', async () => {
IronswornHandlebarsHelpers.registerHelpers()
IronswornChatCard.registerHooks()
patchZIndex()
registerCompendiumCategoryHook()
await registerTokenHUDButtons()
activateSceneButtonListeners()
})
Expand Down
165 changes: 141 additions & 24 deletions src/module/dataforged/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,19 @@ import type {
IOracle,
IOracleCategory,
Ironsworn,
IRow,
ISettingTruth,
Starforged
} from 'dataforged'
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { starforged, ironsworn } from 'dataforged'
import { isArray, isObject, max } from 'lodash-es'
import { isArray, isObject } from 'lodash-es'
import shajs from 'sha.js'
import { renderLinksInMove, renderLinksInStr } from '.'
import { IronswornActor } from '../actor/actor'
import type { IronswornItem } from '../item/item'
import { OracleTable } from '../roll-table/oracle-table'
import { IronswornJournalEntry } from '../journal/journal-entry'
import { IronswornJournalPage } from '../journal/journal-entry-page'
import { OracleTableResult } from '../roll-table/oracle-table-result'
import {
ISAssetTypes,
ISMoveCategories,
Expand All @@ -31,6 +29,8 @@ import {
} from './data'
import { DATAFORGED_ICON_MAP } from './images'
import { renderMarkdown } from './rendering'
import type { FolderDataConstructorData } from '@league-of-foundry-developers/foundry-vtt-types/src/foundry/common/data/data.mjs/folderData'
import { MoveCategoryColor } from '../features/custommoves'

export function cleanDollars(obj): any {
if (isArray(obj)) {
Expand Down Expand Up @@ -93,7 +93,15 @@ export async function importFromDataforged() {

// Delete all the contents
const idsToDelete = pack.index.map((x) => x._id)
await Item.deleteDocuments(idsToDelete, { pack: key })
await getDocumentClass(pack.metadata.type).deleteDocuments(idsToDelete, {
pack: key
})
// @ts-expect-error outdated typing
if (pack.folders.size > 0)
// @ts-expect-error outdated typing
await Folder.deleteDocuments(Array.from(pack.folders.keys()), {
pack: key
})
}

await Promise.all([
Expand All @@ -120,13 +128,31 @@ export async function importFromDataforged() {
* MOVES
*/

function getMoveFolderData(
moveCategory: IMoveCategory
): FolderDataConstructorData {
return {
name: `${moveCategory.Name} Moves`,
type: 'Item',
_id: hashLookup(moveCategory.$id),
// workaround for incorrect move colors in DFv1
color: MoveCategoryColor[moveCategory.Name],
description: moveCategory.Description,
sort:
(moveCategory.Source.Page ?? 0) +
(moveCategory.Source.Title.includes('Delve') ? 1000 : 0),
flags: { 'foundry-ironsworn': { dfid: moveCategory.$id } }
}
}

function movesForCategories(
categories: IMoveCategory[]
): Array<ItemDataConstructorData & Record<string, unknown>> {
const movesToCreate = [] as Array<
ItemDataConstructorData & Record<string, unknown>
>
for (const category of categories) {
const folder = hashLookup(category.$id)
for (const move of category.Moves) {
renderLinksInMove(move)
const cleanMove = cleanDollars(move)
Expand All @@ -135,26 +161,38 @@ function movesForCategories(
movesToCreate.push({
_id: hashLookup(cleanMove.dfid),
type: 'sfmove',
name: move.Name,
name: move.Name + (move.$id.endsWith('_alt') ? ' (alt)' : ''),
img: 'icons/dice/d10black.svg',
system: cleanMove
system: cleanMove,
sort: move.Source.Page,
folder
})
}
}
return movesToCreate
}

async function processISMoves() {
const pack = 'foundry-ironsworn.ironswornmoves'
await Folder.createDocuments(
ISMoveCategories.map((moveCategory) => getMoveFolderData(moveCategory)),
{ pack, keepId: true }
)
const movesToCreate = movesForCategories(ISMoveCategories)
await Item.createDocuments(movesToCreate, {
pack: 'foundry-ironsworn.ironswornmoves',
pack,
keepId: true
})
}
async function processSFMoves() {
const pack = 'foundry-ironsworn.starforgedmoves'
await Folder.createDocuments(
SFMoveCategories.map((moveCategory) => getMoveFolderData(moveCategory)),
{ pack, keepId: true }
)
const movesToCreate = movesForCategories(SFMoveCategories)
await Item.createDocuments(movesToCreate, {
pack: 'foundry-ironsworn.starforgedmoves',
pack,
keepId: true
})
}
Expand All @@ -163,11 +201,24 @@ async function processSFMoves() {
* ASSSETS
*/

function getAssetFolderData(assetType: IAssetType): FolderDataConstructorData {
return {
name: assetType.Name,
color: assetType.Display.Color,
description: assetType.Description,
type: 'Item',
_id: hashLookup(assetType.$id),
sort: assetType.Source.Page,
flags: { 'foundry-ironsworn': { dfid: assetType.$id } }
}
}

function assetsForTypes(types: IAssetType[]) {
const assetsToCreate = [] as Array<
ItemDataConstructorData & Record<string, unknown>
>
for (const assetType of types) {
const folder = hashLookup(assetType.$id)
for (const asset of assetType.Assets) {
// Inputs map to fields and exclusive options
const fields = [] as Array<{ name: string; value: string }>
Expand Down Expand Up @@ -220,6 +271,7 @@ function assetsForTypes(types: IAssetType[]) {
assetsToCreate.push({
type: 'asset',
_id: hashLookup(asset.$id),
folder,
name: asset.Name,
system: data
})
Expand All @@ -229,64 +281,129 @@ function assetsForTypes(types: IAssetType[]) {
}

async function processSFAssets() {
const pack = 'foundry-ironsworn.starforgedassets'
await Folder.createDocuments(
SFAssetTypes.map((assetType) => getAssetFolderData(assetType)),
{ pack, keepId: true }
)
const assetsToCreate = assetsForTypes(SFAssetTypes)
await Item.createDocuments(assetsToCreate, {
pack: 'foundry-ironsworn.starforgedassets',
pack,
keepId: true
})
}

async function processISAssets() {
const pack = 'foundry-ironsworn.ironswornassets'
await Folder.createDocuments(
ISAssetTypes.map((assetType) => getAssetFolderData(assetType)),
{ pack, keepId: true }
)
const assetsToCreate = assetsForTypes(ISAssetTypes)
await Item.createDocuments(assetsToCreate, {
pack: 'foundry-ironsworn.ironswornassets',
pack,
keepId: true
})
}

/**
* ORACLES
*/

function getOracleFolderData(
oracleBranch: IOracleCategory | IOracle,
parent?: string
): FolderDataConstructorData {
if ('Oracles' in oracleBranch)
return {
name: oracleBranch.Name,
_id: hashLookup(oracleBranch.$id),
type: 'RollTable',
description: oracleBranch.Description,
sort:
(oracleBranch.Source.Page ?? 0) +
(oracleBranch.Source.Title.includes('Delve') ? 1000 : 0),
flags: { 'foundry-ironsworn': { dfid: oracleBranch.$id } },
parent
}
console.log(oracleBranch)
throw new Error("Data isn't an oracle tree branch")
}

async function processOracle(
oracle: IOracle,
output: RollTableDataConstructorData[]
output: {
RollTable: RollTableDataConstructorData[]
Folder: FolderDataConstructorData[]
},
folder: string
) {
// Oracles JSON is a tree we wish to iterate through depth first adding
// parents prior to their children, and children in order
if (oracle.Table != null)
output.push(OracleTable.getConstructorData(oracle as any))
output.RollTable.push({
...OracleTable.getConstructorData(oracle as any),
folder,
name: oracle.Name + (oracle.$id.endsWith('_alt') ? ' (alt)' : ''),
sort:
(oracle.Source.Page ?? 0) +
(oracle.Source.Title.includes('Delve') ? 1000 : 0)
})

for (const child of oracle.Oracles ?? []) await processOracle(child, output)
if ('Oracles' in oracle)
output.Folder.push(getOracleFolderData(oracle, folder))

for (const child of oracle.Oracles ?? [])
await processOracle(child, output, hashLookup(oracle.$id))
}
async function processOracleCategory(
cat: IOracleCategory,
output: RollTableDataConstructorData[]
output: {
RollTable: RollTableDataConstructorData[]
Folder: FolderDataConstructorData[]
},
/** The Foundry ID of the parent folder, if any. */
parent?: string
) {
for (const oracle of cat.Oracles ?? []) await processOracle(oracle, output)
const folderData = getOracleFolderData(cat, parent)
output.Folder.push(folderData)
for (const oracle of cat.Oracles ?? [])
await processOracle(oracle, output, folderData._id as string)
for (const child of cat.Categories ?? [])
await processOracleCategory(child, output)
await processOracleCategory(child, output, folderData._id as string)
}

async function processSFOracles() {
const oraclesToCreate: RollTableDataConstructorData[] = []
const toCreate: {
RollTable: RollTableDataConstructorData[]
Folder: FolderDataConstructorData[]
} = { RollTable: [], Folder: [] }
const pack = 'foundry-ironsworn.starforgedoracles'

for (const category of SFOracleCategories) {
await processOracleCategory(category, oraclesToCreate)
await processOracleCategory(category, toCreate)
}
await OracleTable.createDocuments(oraclesToCreate, {
pack: 'foundry-ironsworn.starforgedoracles',
await Folder.createDocuments(toCreate.Folder, { pack, keepId: true })
await OracleTable.createDocuments(toCreate.RollTable, {
pack,
keepId: true
})
}

async function processISOracles() {
const oraclesToCreate: RollTableDataConstructorData[] = []
const toCreate: {
RollTable: RollTableDataConstructorData[]
Folder: FolderDataConstructorData[]
} = { RollTable: [], Folder: [] }

const pack = 'foundry-ironsworn.ironswornoracles'

for (const category of ISOracleCategories) {
await processOracleCategory(category, oraclesToCreate)
await processOracleCategory(category, toCreate)
}
await OracleTable.createDocuments(oraclesToCreate, {
pack: 'foundry-ironsworn.ironswornoracles',
await Folder.createDocuments(toCreate.Folder, { pack, keepId: true })
await OracleTable.createDocuments(toCreate.RollTable, {
pack,
keepId: true
})
}
Expand Down
23 changes: 0 additions & 23 deletions src/module/features/compendium-categories.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/module/features/custommoves.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export async function createStarforgedMoveTree(): Promise<MoveCategory[]> {
}

// TODO dataforged has a key for move colours...., but they appear to have changed significantly since the last time i updated them! they'll be fixed for 2.0, but until then, here's a workaround.
enum MoveCategoryColor {
export enum MoveCategoryColor {
Adventure = '#206087',
Combat = '#818992',
Connection = '#4A5791',
Expand Down
Loading

0 comments on commit 9a5035b

Please sign in to comment.