Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@ianjsikes
Created February 17, 2019 21:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ianjsikes/1aedfd113243d9cfe69c3b445f1cb562 to your computer and use it in GitHub Desktop.
Save ianjsikes/1aedfd113243d9cfe69c3b445f1cb562 to your computer and use it in GitHub Desktop.
A node script to convert Improved Initiative-style JSON data to Homebrewery-formatted Markdown stat blocks for DnD 5e creatures
/**
Copyright 2019 Ian J Sikes
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the Software without restriction, including without
limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions
of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
const path = require("path");
const fs = require("fs");
// Read and validate arguments
if (process.argv.length !== 3) {
console.error("Please supply one argument (path to input json file).");
process.exit(1);
}
const inputPath = process.argv[2];
if (!fs.existsSync(inputPath)) {
console.error("No file found at path: " + inputPath);
process.exit(1);
}
// Load input file
const inputData = JSON.parse(fs.readFileSync(inputPath, "utf8"));
// Get all creatures in input file, and load them into an array of creature objects, sorted alphabetically
const creaturesArr = [];
for (const key in inputData) {
if (!key.startsWith("ImprovedInitiative.Creatures.")) {
continue;
}
let obj = JSON.parse(inputData[key]);
creaturesArr.push(obj);
}
creaturesArr.sort((a, b) => (a.Name < b.Name ? -1 : 1));
// fs.writeFileSync(
// getDebugPath(inputPath),
// JSON.stringify(creaturesArr, null, 2)
// );
const mdContent = creaturesArr
.map(creature => objToStatBlock(creature))
.join("\n");
fs.writeFileSync(getOutputPath(inputPath), mdContent);
console.log(`✨ Done! File written to ${getOutputPath(inputPath)}`);
// *********************
// * UTILITY FUNCTIONS *
// *********************
/**
* Main parsing function. Converts an Improved Initiative creature object to
* a Markdown string formatted for Homebrewery/GM Binder.
*/
function objToStatBlock(obj) {
// NAME AND TYPE
let str = `___\n> ## ${obj.Name}`;
if (obj.Type) str += `\n> *${obj.Type}*`;
str += `\n>___`;
// AC, HP, SPEED
str += `\n> - **Armor Class** ${obj.AC.Value}`;
if (obj.AC.Notes) str += ` ${obj.AC.Notes}`;
str += `\n> - **Hit Points** ${obj.HP.Value}`;
if (obj.HP.Notes) str += ` ${obj.HP.Notes}`;
str += `\n> - **Speed** ${obj.Speed.length ? obj.Speed.join(", ") : "0 ft."}`;
str += `\n>___`;
// ABILITY SCORES
str += `\n>|STR|DEX|CON|INT|WIS|CHA|`;
str += `\n>|:---:|:---:|:---:|:---:|:---:|:---:|`;
str += `\n>|`;
for (const ability in obj.Abilities) {
const stat = obj.Abilities[ability];
const modifier = statToModifier(stat);
str += `${stat} (${modifier})|`;
}
str += `\n>___`;
// SAVES, SKILLS, SENSES, DAMAGE MODIFIERS, LANGUAGES, CR
if (obj.Saves && obj.Saves.length) {
const savesStr = obj.Saves.map(
save => `${save.Name} ${numToMod(save.Modifier)}`
).join(", ");
str += `\n> - **Saving Throws** ${savesStr}`;
}
if (obj.Skills && obj.Skills.length) {
const skillsStr = obj.Skills.map(
skill => `${skill.Name} ${numToMod(skill.Modifier)}`
).join(", ");
str += `\n> - **Skills** ${skillsStr}`;
}
str += arrayPropToStr(obj, "DamageVulnerabilities", "Damage Vulnerabilities");
str += arrayPropToStr(obj, "DamageResistances", "Damage Resistances");
str += arrayPropToStr(obj, "DamageImmunities", "Damage Immunities");
str += arrayPropToStr(obj, "ConditionImmunities", "Condition Immunities");
str += arrayPropToStr(obj, "Senses", "Senses");
str += arrayPropToStr(obj, "Languages", "Languages");
if (obj.Challenge) {
str += `\n> - **Challenge** ${obj.Challenge} ${crToXp(obj.Challenge)}`;
}
str += `\n>___`;
// TRAITS
if (obj.Traits.length) {
str += obj.Traits.map(trait => {
let name = trait.Name.endsWith(".") ? trait.Name : `${trait.Name}.`;
let content = trait.Content.replace(/\n\.\n/gim, "\n>\n> ").replace(
/\n[^> ]/gim,
"\n> "
);
return `\n> ***${name}*** ${content}`;
}).join("\n>");
}
// ACTIONS
if (obj.Actions.length) {
str += `\n> ### Actions`;
str += obj.Actions.map(action => {
let name = action.Name.endsWith(".") ? action.Name : `${action.Name}.`;
let content = action.Content.replace(/\n\.\n/gim, "\n>\n> ").replace(
/\n[^> ]/gim,
"\n> "
);
return `\n> ***${name}*** ${content}`;
}).join("\n>");
}
str += "\n";
return str;
}
function getOutputPath(inputPath) {
return inputPath.replace(".json", ".md");
}
function getDebugPath(inputPath) {
return inputPath.replace(".json", ".debug.json");
}
function numToMod(num) {
return num >= 0 ? `+${num}` : `${num}`;
}
function statToModifier(stat) {
const modifier = Math.floor((stat - 10) / 2);
return numToMod(modifier);
}
function arrayPropToStr(obj, prop, name) {
if (obj[prop] && obj[prop].length) {
const str = `${obj[prop].join("; ")}`;
return `\n> - **${name}** ${str}`;
}
return "";
}
function crToXp(cr) {
const crToXpDict = {
"0": "10",
"1/8": "25",
"1/4": "50",
"1/2": "100",
"1": "200",
"2": "450",
"3": "700",
"4": "1,100",
"5": "1,800",
"6": "2,300",
"7": "2,900",
"8": "3,900",
"9": "5,000",
"10": "5,900",
"11": "7,200",
"12": "8,400",
"13": "10,000",
"14": "11,500",
"15": "13,000",
"16": "15,000",
"17": "18,000",
"18": "20,000",
"19": "22,000",
"20": "25,000",
"21": "33,000",
"22": "41,000",
"23": "50,000",
"24": "62,000",
"25": "75,000",
"26": "90,000",
"27": "105,000",
"28": "120,000",
"29": "135,000",
"30": "155,000"
};
if (cr in crToXpDict) return `(${crToXpDict[cr]} XP)`;
else return ``;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment