diff --git a/CHANGELOG.md b/CHANGELOG.md
index 4e62c12..654136e 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,5 +1,9 @@
# Patch Notes
+## Version 0.2.1
+
+* Add api for generating npcs.
+
## Version 0.2.0
* Add token icon capability to generator.
diff --git a/README.md b/README.md
index ce94d93..92cd688 100644
--- a/README.md
+++ b/README.md
@@ -32,3 +32,21 @@ Check the [Changelog](https://github.com/ardittristan/VTTNPCGen/blob/master/CHAN
## Credits
A lot of the generator data comes from [Cellule/dndGenerator](https://github.com/Cellule/dndGenerator)
+
+
+
+#### Api usage
+
+Though not recommended there is an api that can be used to generate npcs.
+
+API info
+
+
+`npcGen.generateNPC(amount, options);`
+
+* `amount` is amount of npcs to generate
+* `options` is an object with all the options for the generator, how this object looks can be found in the console when clicking generate in the npc generator. If no options are provided everything default is enabled.
+
+
${game.i18n.localize("npcGen.amountToGen").replace("%n", amount)}
`, + yes: () => { + confirmed = true; + }, + defaultYes: false, + }).then(() => { + if (!confirmed) return; + + if (Object.keys(options).length === 0) { + options = defaultOptions; + } + + for (let i = 1; i < amount + 1; i++) { + let generator = new NPCGenerator({ + classesJSON: classesJSON, + languagesJSON: languagesJSON, + namesJSON: namesJSON, + personalityTraitsJSON: personalityTraitsJSON, + plotHooksJSON: plotHooksJSON, + professionsJSON: professionsJSON, + racesJSON: racesJSON, + sexJSON: sexJSON, + listJSON: listJSON, + }); + + generator.generateNPC(options).then(() => { + generator._apiSave().then(() => { + generator.close(); + }); + }); + } + }); +} diff --git a/modules/applications/NPCGenerator.js b/modules/applications/NPCGenerator.js index 2537a83..ceb9060 100644 --- a/modules/applications/NPCGenerator.js +++ b/modules/applications/NPCGenerator.js @@ -22,6 +22,9 @@ export default class NPCGenerator extends FormApplication { this.done = false; + this.generateNPC = this.generateNPC.bind(this); + this._apiSave = this._apiSave.bind(this); + /* -------------< Combine with user JSON if enabled >--------------- */ if (game.settings.get("npcgen", "onlyClassesJSON")) { @@ -276,6 +279,7 @@ export default class NPCGenerator extends FormApplication { * 'key':entry.metadata.package+'.'+entry.metadata.name */ async _updateObject(_e, d) { + console.info("%cINFO | Entries in npc generator:", "color: #fff; background-color: #444; padding: 2px 4px; border-radius: 2px;", removeGenFromObj(d)); if (!this.done) { this.updateFormValues(d); this.generateNPC(d); @@ -348,7 +352,7 @@ export default class NPCGenerator extends FormApplication { /** @type {String[]} */ let gendersOut = []; genders.forEach((gender) => { - for (let i = 0; i < this.getProbValue(d, "Gender", gender); i++) { + for (let i = 0; i < (this.getProbValue(d, "Gender", gender) || 1); i++) { gendersOut.push(gender); } }); @@ -364,7 +368,7 @@ export default class NPCGenerator extends FormApplication { /** @type {String[]} */ let professionsOut = []; professions.forEach((profession) => { - for (let i = 0; i < this.getProbValue(d, "Profession", profession); i++) { + for (let i = 0; i < (this.getProbValue(d, "Profession", profession) || 1); i++) { professionsOut.push(profession); } }); @@ -373,7 +377,7 @@ export default class NPCGenerator extends FormApplication { /** @type {String[]} */ let relationshipStatusOut = []; relationshipStatus.forEach((relationshipStatus) => { - for (let i = 0; i < this.getProbValue(d, "RelationshipStatus", relationshipStatus); i++) { + for (let i = 0; i < (this.getProbValue(d, "RelationshipStatus", relationshipStatus) || 1); i++) { relationshipStatusOut.push(relationshipStatus); } }); @@ -382,7 +386,7 @@ export default class NPCGenerator extends FormApplication { /** @type {String[]} */ let orientationsOut = []; orientations.forEach((orientation) => { - for (let i = 0; i < this.getProbValue(d, "Orientation", orientation); i++) { + for (let i = 0; i < (this.getProbValue(d, "Orientation", orientation) || 1); i++) { orientationsOut.push(orientation); } }); @@ -391,7 +395,7 @@ export default class NPCGenerator extends FormApplication { /** @type {String[]} */ let racesOut = []; races.forEach((race) => { - for (let i = 0; i < this.getProbValue(d, "Race", race); i++) { + for (let i = 0; i < (this.getProbValue(d, "Race", race) || 1); i++) { racesOut.push(race); } }); @@ -400,7 +404,7 @@ export default class NPCGenerator extends FormApplication { /** @type {String[]} */ let classesOut = []; classes.forEach((klass) => { - for (let i = 0; i < this.getProbValue(d, "Class", klass); i++) { + for (let i = 0; i < (this.getProbValue(d, "Class", klass) || 1); i++) { classesOut.push(klass); } }); @@ -488,8 +492,6 @@ export default class NPCGenerator extends FormApplication { // Sub Class this.generateSubclass(d); - console.log(this); - // First Name this.generateFirstName(); @@ -504,8 +506,9 @@ export default class NPCGenerator extends FormApplication { /** * saves npc to actor * @param {Object} d + * @param {boolean} [isApi=false] */ - async saveNPC(d) { + async saveNPC(d, isApi = false) { // set abilities let abilities = this.getAbilities(d); @@ -534,7 +537,9 @@ export default class NPCGenerator extends FormApplication { let actor = await CONFIG.Actor.entityClass.create(actorOptions); - actor.sheet.render(true); + if (!isApi) { + actor.sheet.render(true); + } } /** @@ -1215,6 +1220,15 @@ export default class NPCGenerator extends FormApplication { ...super._getHeaderButtons(), ]; } + + async _apiSave() { + let data = this; + Object.keys(data).forEach((key) => { + if (Array.isArray(data[key]) && key.startsWith("gen")) data[key] = data[key].join(", "); + }); + + await this.saveNPC(data, true); + } } /** @@ -1265,3 +1279,15 @@ async function asyncForEach(arr, callback) { await callback(arr[i], i, arr); } } + +/** + * @param {Object} obj + */ +function removeGenFromObj(obj) { + Object.keys(obj).forEach((key) => { + if (key.startsWith("gen")) { + delete obj[key]; + } + }); + return obj; +} diff --git a/npc-gen.js b/npc-gen.js index 4b6ef74..8818bad 100644 --- a/npc-gen.js +++ b/npc-gen.js @@ -2,6 +2,7 @@ import * as acemodule from "./lib/ace/ace.js"; import { registerJSONEditorSettings, registerSettings } from "./modules/settings.js"; import { registerHelpers } from "./modules/handlebars.js"; import NPCGenerator from "./modules/applications/NPCGenerator.js"; +import generateNPC from "./modules/api.js" /* -------------< Ace multi module compat >------------ */ @@ -46,15 +47,15 @@ function setAceModules(stringArray) { /* -------------< End Ace multi module compat >------------ */ -let classesJSON = {}; -let personalityTraitsJSON = {}; -let plotHooksJSON = {}; -let professionsJSON = {}; -let racesJSON = {}; -let sexJSON = {}; -let listJSON = {}; -let languagesJSON = []; -let namesJSON = {}; +export let classesJSON = {}; +export let personalityTraitsJSON = {}; +export let plotHooksJSON = {}; +export let professionsJSON = {}; +export let racesJSON = {}; +export let sexJSON = {}; +export let listJSON = {}; +export let languagesJSON = []; +export let namesJSON = {}; initJSON(); async function initJSON() { @@ -109,4 +110,7 @@ Hooks.once("init", () => { // register settings registerSettings(); + + // register api + window.npcGen.generateNPC = generateNPC });