diff options
| author | Jan Tuomi <jan.tuomi@eficode.com> | 2019-07-02 23:39:32 +0300 |
|---|---|---|
| committer | Jan Tuomi <jan.tuomi@eficode.com> | 2019-07-02 23:39:32 +0300 |
| commit | a5cc8ad12ba5ba5450cbcbac289709f9835c4196 (patch) | |
| tree | a30496d95f567b2352780aa75cad88fea40da676 | |
| parent | a3f42d69d6463fd30fdf054882493b835a54d001 (diff) | |
Improve stuff
| -rwxr-xr-x | index.js | 66 |
1 files changed, 60 insertions, 6 deletions
@@ -21,7 +21,22 @@ const capitalize = (s) => { return s.charAt(0).toUpperCase() + s.slice(1) }; -const replaceSpecialValuesInActionText = (text) => { +const ordinalize = (i) => { + const j = i % 10, + k = i % 100; + if (j == 1 && k != 11) { + return i + "st"; + } + if (j == 2 && k != 12) { + return i + "nd"; + } + if (j == 3 && k != 13) { + return i + "rd"; + } + return i + "th"; +}; + +const replaceSpecialValues = (text) => { text = text.replace(/\{@hit (\d+?)\}/g, (match, p1) => printableNumber(p1)) text = text.replace(/\{@h\}(\d+?)/g, '$1'); text = text.replace(/\{@damage (.*?)\}/g, '$1'); @@ -41,7 +56,7 @@ const replaceSpecialValuesInActionText = (text) => { return text; }; -const xml = builder.create('root') +const xml = builder.create('root', { encoding: 'utf-8' }) .ele('compendium', { version: '5' }) const parseToXML = (obj) => { @@ -49,7 +64,7 @@ const parseToXML = (obj) => { monster.ele('name', obj.name) monster.ele('size', obj.size) - monster.ele('type', obj.type) + monster.ele('type', typeof obj.type === 'object' ? obj.type.type : obj.type) monster.ele('source', obj.source) monster.ele('alignment', obj.alignment && obj.alignment.length ? obj.alignment.join('') : null) monster.ele('ac', obj.ac && obj.ac.length ? obj.ac[0].ac : null) @@ -117,17 +132,56 @@ const parseToXML = (obj) => { e.ele('name', trait.name); trait.entries.forEach((entry) => { - const text = replaceSpecialValuesInActionText(entry); + const text = replaceSpecialValues(entry); e.ele('text', text); }); }); + obj.spellcasting && obj.spellcasting.forEach((spellcasting) => { + const e = monster.ele('trait'); + e.ele('name', spellcasting.name); + spellcasting.headerEntries && spellcasting.headerEntries.length && spellcasting.headerEntries.forEach((header) => { + e.ele('text', replaceSpecialValues(header)); + }); + + spellcasting.spells && Object.keys(spellcasting.spells).forEach((level) => { + const spell = spellcasting.spells[level]; + const slots = spell.slots; + const lst = spell.spells; + + const levelNum = parseInt(level, 10); + const ordinal = ordinalize(levelNum); + + let pre = levelNum !== 0 ? ordinal + ' level' : 'Cantrips'; + pre += slots ? ` (${slots} slot${slots > 0 ? 's' : ''})` : ''; + pre = pre.trim(); + const text = `${pre}: ${lst.join(', ')}`; + e.ele('text', replaceSpecialValues(text)); + }); + + if (spellcasting.will) { + const pre = 'At will' + const lst = spellcasting.will; + + const text = `${pre}: ${lst.join(', ')}`; + e.ele('text', replaceSpecialValues(text)); + } + + spellcasting.daily && Object.keys(spellcasting.daily).forEach((count) => { + const lst = spellcasting.daily[count]; + + let pre = `• ${count}/day`; + const text = `${pre}: ${lst.join(', ')}`; + e.ele('text', replaceSpecialValues(text)); + }); + }); + obj.legendary && obj.legendary.forEach((legendary) => { const e = monster.ele('legendary'); e.ele('name', legendary.name); legendary.entries.forEach((entry) => { - const text = replaceSpecialValuesInActionText(entry); + const text = replaceSpecialValues(entry); e.ele('text', text); }); }); @@ -137,7 +191,7 @@ const parseToXML = (obj) => { e.ele('name', action.name); action.entries.forEach((entry) => { - const text = replaceSpecialValuesInActionText(entry); + const text = replaceSpecialValues(entry); e.ele('text', text); }); }) |
