Skip to content

Commit

Permalink
V9 Ready
Browse files Browse the repository at this point in the history
  • Loading branch information
cswendrowski committed Dec 29, 2021
1 parent fffe203 commit 97d9657
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 41 deletions.
5 changes: 5 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/jsLibraryMappings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions .idea/npc-chatter.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions module.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
"name": "npc-chatter",
"title": "NPC Chatter",
"description": "Your NPCs have things to say",
"semanticVersion": "2.1.1",
"version": "2.1.1",
"minimumCoreVersion": "0.6.2",
"compatibleCoreVersion": "0.8.8",
"semanticVersion": "2.2.0",
"version": "2.2.0",
"minimumCoreVersion": "9",
"compatibleCoreVersion": "9",
"author": "Cody Swendrowski <[email protected]>",
"scripts": [
"./npcchatter.js"
Expand All @@ -23,5 +23,5 @@
],
"url": "https://github.com/cswendrowski/FoundryVtt-Npc-Chatter",
"manifest": "https://raw.githubusercontent.com/cswendrowski/FoundryVtt-Npc-Chatter/master/module.json",
"download": "https://github.com/cswendrowski/FoundryVtt-Npc-Chatter/releases/download/v2.1.1/npcchatter.zip"
"download": "https://github.com/cswendrowski/FoundryVtt-Npc-Chatter/releases/download/v2.2.0/npcchatter.zip"
}
90 changes: 54 additions & 36 deletions npcchatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,51 @@ class NpcChatter {
static timer;

getChatterTables() {
const chatterFolder = game.folders.contents.filter(x => x.type == "RollTable" && x.name.toLowerCase() == "npc chatter")[0];
const tables = game.tables.contents.filter(x => x.name.toLowerCase().endsWith("chatter") || x.data.folder == chatterFolder._id);
const chatterFolder = game.folders.contents.find(x => x.type == "RollTable" && x.name.toLowerCase() == "npc chatter");
if ( !chatterFolder ) {
ui.notifications.warn("Could not find the 'NPC Chatter' Folder");
return [];
}
const tables = game.tables.contents.filter(x => x.name.toLowerCase().endsWith("chatter") || x.data.folder == chatterFolder.id);
return tables;
}

randomGlobalChatterEvery(milliseconds, options={}) {
NpcChatter.timer = window.setInterval(() => { game.npcChatter.globalChatter(options); }, milliseconds);
}

static _getChatterScene() {
const sceneType = game.settings.get("npc-chatter", "scenetype");
switch (sceneType) {
case "active": return game.scenes.find(x => x.active);
case "viewed": return game.scenes.find(x => x.id === game.user.viewedScene);
}
}

async globalChatter(options={}) {
const tables = this.getChatterTables();

const userCharacterActorIds = game.users.contents.filter(x => x.character).map(x => x.character.id);
const activeScene = game.scenes.filter(x => x.active)[0];
const npcTokens = activeScene.data.tokens.filter(x => !userCharacterActorIds.includes(x.actorId));
const scene = NpcChatter._getChatterScene();
const npcTokens = scene.data.tokens.filter(x => !userCharacterActorIds.includes(x.actorId));

const eligableTables = tables.filter(x => npcTokens.filter(t => x.name.toLowerCase().includes(t.name.toLowerCase().replace("chatter", "").trim()) > 0));
const eligibleTables = tables.filter(x => npcTokens.filter(t => x.name.toLowerCase().includes(t.name.toLowerCase().replace("chatter", "").trim()) > 0));
if ( eligibleTables.length === 0 ) {
ui.notifications.warn("You have no NPC Chatter tables setup for these tokens");
return;
}

const tableIndex = Math.floor((Math.random() * eligableTables.length) + 0);
const table = eligableTables[tableIndex];
const tableIndex = Math.floor((Math.random() * eligibleTables.length) + 0);
const table = eligibleTables[tableIndex];

const eligableTokens = npcTokens.filter(x => x.name.toLowerCase().includes(table.name.toLowerCase().replace("chatter", "").trim()));

const tokenIndex = Math.floor((Math.random() * eligableTokens.length) + 0);
const token = eligableTokens[tokenIndex];

if (token == undefined) return;


let roll = await table.roll();
const result = roll.results[0].data.text;
game.socket.emit("module.npc-chatter", {
Expand All @@ -41,14 +59,20 @@ class NpcChatter {
}

async tokenChatter(token, options={}) {
if ( !token ) {
ui.notifications.error("No Token passed in");
return;
}
const tables = this.getChatterTables();

const eligableTables = tables.filter(x => token.name.toLowerCase().includes(x.name.toLowerCase().replace("chatter", "").trim()));

if (eligableTables.length == 0) return;
const eligibleTables = tables.filter(x => token.name.toLowerCase().includes(x.name.toLowerCase().replace("chatter", "").trim()));
if ( eligibleTables.length === 0 ) {
ui.notifications.warn("You have no NPC Chatter tables setup for this token");
return;
}

const tableIndex = Math.floor((Math.random() * eligableTables.length) + 0);
const table = eligableTables[tableIndex];
const tableIndex = Math.floor((Math.random() * eligibleTables.length) + 0);
const table = eligibleTables[tableIndex];
let roll = await table.roll();
const result = roll.results[0].data.text;
game.socket.emit("module.npc-chatter", {
Expand All @@ -60,29 +84,10 @@ class NpcChatter {
}

async selectedChatter(options={}) {
const tables = this.getChatterTables();

const npcTokens = canvas.tokens.controlled;

const eligableTables = tables.filter(x => npcTokens.filter(t => x.name.toLowerCase().includes(t.name.toLowerCase().replace("chatter", "").trim()) > 0));

if (eligableTables.length == 0) return;

const tableIndex = Math.floor((Math.random() * eligableTables.length) + 0);
const table = eligableTables[tableIndex];

const eligableTokens = npcTokens.filter(x => x.name.toLowerCase().includes(table.name.toLowerCase().replace("chatter", "").trim()));

const tokenIndex = Math.floor((Math.random() * eligableTokens.length) + 0);
const token = eligableTokens[tokenIndex];
let roll = await table.roll();
const result = roll.results[0].data.text;
game.socket.emit("module.npc-chatter", {
tokenId: token.id,
msg: result
});
const emote = Object.keys(options).length ? {emote: options} : false;
await canvas.hud.bubbles.say(token, result, emote);
const tokenIndex = Math.floor((Math.random() * npcTokens.length) + 0);
const token = npcTokens[tokenIndex];
return this.tokenChatter(token, options);
}

async turnOffGlobalTimerChatter() {
Expand All @@ -91,9 +96,22 @@ class NpcChatter {
}
}

Hooks.once('ready', async function() {
Hooks.once('init', async () => {
game.settings.register("npc-chatter", "scenetype", {
name: "Should Tokens Chatter on the active scene, or the viewed scene?",
type: String,
config: true,
scope: "world",
default: "viewed",
choices: {
active: "Active Scene",
viewed: "Viewed Scene"
}
});
});

Hooks.once('ready', async () => {
game.npcChatter = new NpcChatter();
console.log("Npc Chatter is now ready");

game.socket.on("module.npc-chatter", async (toShow) => {
//console.log("Got token " + toShow.tokenId + " with text " + toShow.msg);
Expand Down

0 comments on commit 97d9657

Please sign in to comment.