Skip to content

Commit

Permalink
add api
Browse files Browse the repository at this point in the history
  • Loading branch information
ardittristan committed Nov 30, 2020
1 parent 16e1caa commit 921a864
Show file tree
Hide file tree
Showing 8 changed files with 170 additions and 21 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

<details>

<summary>API info</summary>

&nbsp;
`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.

</details>
50 changes: 50 additions & 0 deletions data/defaultApiOptions.js
Original file line number Diff line number Diff line change
@@ -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,
};
4 changes: 3 additions & 1 deletion lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -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?"
}
2 changes: 1 addition & 1 deletion module.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
45 changes: 45 additions & 0 deletions modules/api.js
Original file line number Diff line number Diff line change
@@ -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: `<p>${game.i18n.localize("npcGen.amountToGen").replace("%n", amount)}</p>`,
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();
});
});
}
});
}
46 changes: 36 additions & 10 deletions modules/applications/NPCGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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")) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
});
Expand All @@ -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);
}
});
Expand All @@ -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);
}
});
Expand All @@ -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);
}
});
Expand All @@ -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);
}
});
Expand All @@ -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);
}
});
Expand Down Expand Up @@ -488,8 +492,6 @@ export default class NPCGenerator extends FormApplication {
// Sub Class
this.generateSubclass(d);

console.log(this);

// First Name
this.generateFirstName();

Expand All @@ -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);

Expand Down Expand Up @@ -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);
}
}

/**
Expand Down Expand Up @@ -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);
}
}

/**
Expand Down Expand Up @@ -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;
}
22 changes: 13 additions & 9 deletions npc-gen.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 >------------ */

Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -109,4 +110,7 @@ Hooks.once("init", () => {

// register settings
registerSettings();

// register api
window.npcGen.generateNPC = generateNPC
});

0 comments on commit 921a864

Please sign in to comment.