@@ -75,6 +75,10 @@ async function buildMonsterFromFile(file: File): Promise<Map<string, Monster>> {
7575 } ;
7676 importedMonsters . set ( importedMonster . name , importedMonster ) ;
7777 } catch ( e ) {
78+ console . log (
79+ "🚀 ~ file: DnDAppFilesImport.ts ~ line 78 ~ e" ,
80+ e
81+ ) ;
7882 continue ;
7983 }
8084 }
@@ -93,7 +97,7 @@ function getTraits(
9397 monster : Element ,
9498 arg1 : "trait" | "action" | "legendary" | "reaction"
9599) : Trait [ ] {
96- if ( ! monster . getElementsByTagName ( arg1 ) ) return [ ] ;
100+ if ( ! monster . getElementsByTagName ( arg1 ) ?. length ) return [ ] ;
97101 const traits = monster . getElementsByTagName ( arg1 ) ;
98102 const traitList = [ ] ;
99103 for ( let trait of Array . from ( traits ) ) {
@@ -105,8 +109,7 @@ function getTraits(
105109 const text = [ ] ;
106110 const traitTexts = trait . getElementsByTagName ( "text" ) ;
107111 for ( let index in traitTexts ) {
108- if ( ! traitTexts [ index ] )
109- text . push ( traitTexts [ index ] . textContent ) ;
112+ if ( ! traitTexts [ index ] ) text . push ( traitTexts [ index ] . textContent ) ;
110113 }
111114 traitList . push ( {
112115 name : name [ 0 ] . textContent ,
@@ -117,7 +120,7 @@ function getTraits(
117120}
118121
119122function getSpells ( monster : Element ) : Spell [ ] {
120- if ( ! monster . getElementsByTagName ( "trait" ) ) return [ ] ;
123+ if ( ! monster . getElementsByTagName ( "trait" ) ?. length ) return [ ] ;
121124 const traits = Array . from ( monster . getElementsByTagName ( "trait" ) ) ;
122125 const spellcasting = traits . find ( ( x ) =>
123126 x . getElementsByTagName ( "name" ) [ 0 ] ?. textContent . includes ( "Spellcasting" )
@@ -129,7 +132,7 @@ function getSpells(monster: Element): Spell[] {
129132}
130133
131134function getSkillSaves ( monster : Element ) : { [ key : string ] : number } [ ] {
132- if ( ! monster . getElementsByTagName ( "skill" ) ) return [ ] ;
135+ if ( ! monster . getElementsByTagName ( "skill" ) ?. length ) return [ ] ;
133136 let saves = monster
134137 . getElementsByTagName ( "skill" ) [ 0 ]
135138 . textContent . split ( ", " ) ;
@@ -166,7 +169,7 @@ function getSaves(monster: Element): {
166169 wisdom ?: number ;
167170 charisma ?: number ;
168171} [ ] {
169- if ( ! monster . getElementsByTagName ( "save" ) ) return [ ] ;
172+ if ( ! monster . getElementsByTagName ( "save" ) ?. length ) return [ ] ;
170173 let saves = monster . getElementsByTagName ( "save" ) [ 0 ] . textContent . split ( ", " ) ;
171174 let ret : {
172175 strength ?: number ;
@@ -184,10 +187,9 @@ function getSaves(monster: Element): {
184187}
185188
186189function getHP ( monster : Element , arg1 : "hp" | "hit_dice" ) : string {
187- if ( ! monster . getElementsByTagName ( "hp" ) ) return "" ;
188- let [ , hp , hit_dice ] = monster
189- . getElementsByTagName ( "hp" ) [ 0 ]
190- . textContent . match ( / ( \d + ) \( ( [ \s \S ] + ) \) / ) ;
190+ if ( ! monster . getElementsByTagName ( "hp" ) ?. length ) return "" ;
191+ const monsterHP = monster . getElementsByTagName ( "hp" ) [ 0 ] . textContent ;
192+ let [ , hp , hit_dice ] = monsterHP . match ( / ( \d + ) \( ( [ \s \S ] + ) \) / ) ?? [ , "" , "" ] ;
191193 return { hp : hp , hit_dice : hit_dice } [ arg1 ] ;
192194}
193195const SIZES : { [ key : string ] : string } = {
@@ -216,22 +218,29 @@ function getAC(monster: Element): number {
216218}
217219function getSource ( monster : Element ) : string {
218220 let source = "Unknown" ;
219- const description = monster . getElementsByTagName ( "description" ) ;
220- if ( description && description . length ) {
221+ if ( monster . getElementsByTagName ( "source" ) ?. length ) {
222+ source = monster . getElementsByTagName ( "source" ) [ 0 ] . textContent ;
223+ } else if (
224+ monster . getElementsByTagName ( "trait" ) ?. length &&
225+ Array . from ( monster . getElementsByTagName ( "trait" ) ) . find (
226+ ( t ) => t . getElementsByTagName ( "name" ) ?. [ 0 ] . textContent == "Source"
227+ )
228+ ) {
229+ let trait = Array . from ( monster . getElementsByTagName ( "trait" ) ) . find (
230+ ( t ) => t . getElementsByTagName ( "name" ) ?. [ 0 ] ?. textContent == "Source"
231+ ) ;
232+ source = trait
233+ ?. getElementsByTagName ( "text" ) ?. [ 0 ]
234+ ?. textContent ?. replace ( / p . \d + / , "" )
235+ . trim ( ) ;
236+ } else if ( monster . getElementsByTagName ( "description" ) ?. length ) {
237+ const description = monster . getElementsByTagName ( "description" ) ;
221238 const searchString = "Source: " ;
222239 const sourcePos = description [ 0 ] . textContent . lastIndexOf ( searchString ) ;
223240 const sources = description [ 0 ] . textContent
224241 . substr ( sourcePos + searchString . length )
225242 . split ( / , ? / ) ;
226243 source = sources [ 0 ] ;
227- } else {
228- const types = monster . getElementsByTagName ( "type" ) ;
229- if ( types && types . length ) {
230- let type = types [ 0 ] . textContent . split ( / , ? / ) ;
231- source = titleCase (
232- type . length > 1 ? type [ type . length - 1 ] : source
233- ) ;
234- }
235244 }
236245 return source ;
237246}
0 commit comments