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. + +
diff --git a/data/defaultApiOptions.js b/data/defaultApiOptions.js new file mode 100644 index 0000000..94041e5 --- /dev/null +++ b/data/defaultApiOptions.js @@ -0,0 +1,50 @@ +export default { + "ClassBarbarian": true, + "ClassBard": true, + "ClassCleric": true, + "ClassDruid": true, + "ClassFighter": true, + "ClassMonk": true, + "ClassPaladin": true, + "ClassRanger": true, + "ClassRogue": true, + "ClassSorcerer": true, + "ClassWarlock": true, + "ClassWizard": true, + "GenderFemale": true, + "GenderMale": true, + "GenderNon-Binary": true, + "OrientationAsexual": true, + "OrientationBisexual": true, + "OrientationHeterosexual": true, + "OrientationHomosexual": true, + "ProfessionEntertainer": true, + "ProfessionLearned": true, + "ProfessionLesser Nobility": true, + "ProfessionMartial": true, + "ProfessionProfessional": true, + "ProfessionUnderclass": true, + "ProfessionWorking Class": true, + "RaceDragonborn": true, + "RaceDwarf": true, + "RaceElf": true, + "RaceGnome": true, + "RaceHalf-Elf": true, + "RaceHalf-Orc": true, + "RaceHalfling": true, + "RaceHuman": true, + "RaceTiefling": true, + "RelationshipStatusDivorced": true, + "RelationshipStatusIn a relationship": true, + "RelationshipStatusMarried": true, + "RelationshipStatusMarried and having an affair": true, + "RelationshipStatusRecently broken up": true, + "RelationshipStatusRecently divorced": true, + "RelationshipStatusRecently widowed": true, + "RelationshipStatusSeeing someone who is married": true, + "RelationshipStatusSingle": true, + "RelationshipStatusWidowed": true, + "TraitBad Traits": true, + "TraitGood Traits": true, + "level": 1, +}; diff --git a/lang/en.json b/lang/en.json index 6c1ff0f..d896aa0 100644 --- a/lang/en.json +++ b/lang/en.json @@ -54,5 +54,7 @@ "npcGen.forceFirstName": "Force first names from this race to be used", "npcGen.forceLastName": "Force last names from this race to be used", "npcGen.exampleDoubleClick": "Double click for an example", - "npcGen.icon": "Icon" + "npcGen.icon": "Icon", + "npcGen.areYouSure": "Are you sure?", + "npcGen.amountToGen": "Are you sure you want to create %n npcs?" } diff --git a/module.json b/module.json index c5c63ea..20fce18 100644 --- a/module.json +++ b/module.json @@ -2,7 +2,7 @@ "name": "npcgen", "title": "Not Enough NPCs: A 5e NPC Generator", "description": "Allows you to generate basic npc characters in foundry.", - "version": "0.2.0", + "version": "0.2.1", "author": "ardittristan#0001", "esmodules": ["npc-gen.js"], "socket": true, diff --git a/modules/api.js b/modules/api.js new file mode 100644 index 0000000..10cb901 --- /dev/null +++ b/modules/api.js @@ -0,0 +1,45 @@ +import { classesJSON, languagesJSON, namesJSON, personalityTraitsJSON, plotHooksJSON, professionsJSON, racesJSON, sexJSON, listJSON } from "../npc-gen.js"; +import NPCGenerator from "./applications/NPCGenerator.js"; +import defaultOptions from "../data/defaultApiOptions.js"; + +/** + * @param {number} [amount=1] + * @param {defaultOptions} [options={}] + */ +export default function generateNPC(amount = 1, options = {}) { + let confirmed = false; + Dialog.confirm({ + title: game.i18n.localize("npcGen.areYouSure"), + content: `

${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 });