Skip to content

Commit

Permalink
New import script
Browse files Browse the repository at this point in the history
  • Loading branch information
ben committed Mar 6, 2024
1 parent 04da8c9 commit 910b72e
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ package-lock.json
main.js
import-from-archmage.js
import-from-archmage-db.js
import-from-archmage-yaml.js

# Exclude sourcemaps
*.map
Expand Down
2 changes: 1 addition & 1 deletion esbuild.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ esbuild.build({
banner: {
js: banner,
},
entryPoints: ['main.ts', 'import-from-archmage.ts', 'import-from-archmage-db.ts'],
entryPoints: ['main.ts', 'import-from-archmage.ts', 'import-from-archmage-db.ts', 'import-from-archmage-yaml.ts'],
bundle: true,
external: ['obsidian', 'electron', ...builtins],
format: 'cjs',
Expand Down
111 changes: 111 additions & 0 deletions import-from-archmage-yaml.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import { readFile, writeFile, readdir } from 'fs/promises'
import yaml from 'yaml'

const debug = require('debug')('import')

function stripFoundryMarkup (input?: string): string {
return (input ?? '')
.replace(/\[\[(d20)?/g, '') // Remove [[d20 and [[
.replace(/\]\]/g, '') // Remove ]]
.replace(/\+\s*(\d+)/g, '+$1') // convert "+ 1" to "+1"
.replace(/\*(\w+)\*/g, '$1') // convert *word* to word
.trim()
}

async function doit () {
// This relies on having the archmage repo cloned at ../archmange, and the packs built

// Find all the *.yaml files in the archmage/packs/dist directory
const yamlFiles = await readdir('../archmage/src/packs/src/srd-monsters/')

const monsters = []
for (const yamlFilename of yamlFiles) {
// Parse the yaml file
const rawData = await readFile(
`../archmage/src/packs/src/srd-monsters/${yamlFilename}`,
{
encoding: 'utf-8'
}
)
const parsed = yaml.parse(rawData)
debug(`Processing ${parsed.name}`)

let size = parsed.system.details.size.value
if (size === '2x') size = 'Double-strength'
if (size === '3x') size = 'Triple-strength'
if (size.toLowerCase() === 'normal') size = undefined

const monster = {
name: parsed.name,
level: parsed.system.attributes.level.value,
size: size,
tag: parsed.system.details.type.value,
role: parsed.system.details.role.value || undefined,
vuln: parsed.system.details.vulnerability.value || undefined,
ac: parsed.system.attributes.ac.value,
pd: parsed.system.attributes.pd.value,
md: parsed.system.attributes.md.value,
hp: parsed.system.attributes.hp.value,
initiative: parsed.system.attributes.init.mod,
attacks: [] as any[],
traits: [] as any[],
specials: [] as any[]
}

for (const item of parsed.items) {
if (item.type === 'action') {
let name = item.name as string
let tag = undefined
const m = name.match(/\[(.*)\](.*)/)
if (m) {
tag = m[1].trim()
name = m[2].trim()
}

const roll = stripFoundryMarkup(item.system.attack.value)
// .replace('[[d20', '')
// .replace(']]', '')
// .replace(/\+\s*(\d+)/, '+$1')
// .trim()

const extras = [] as any[]
for (const k of ['hit1', 'hit2', 'hit3', 'hit4', 'hit5']) {
const extraHit = item.system[k]
if (extraHit?.name) {
extras.push({
name: extraHit.name,
description: stripFoundryMarkup(extraHit.value)
})
}
}

monster.attacks.push({
name,
tag,
attack: roll,
hit: stripFoundryMarkup(item.system.hit.value),
extras
})
} else {
const thing = {
name: item.name,
description: stripFoundryMarkup(
item.system.description.value
)
}
const monsterKey = {
trait: 'traits',
nastierSpecial: 'specials'
}[item.type]
monster[monsterKey].push(thing)
}
}

monsters.push(monster)
}

// Write the results to a file
await writeFile('monsters.json', JSON.stringify(monsters, null, 2))
}

doit().then(console.log, console.error)
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"esbuild": "0.13.12",
"obsidian": "^0.12.17",
"tslib": "2.3.1",
"typescript": "4.4.4"
"typescript": "4.4.4",
"yaml": "^2.4.1"
}
}

0 comments on commit 910b72e

Please sign in to comment.