Skip to content

Commit

Permalink
Adding group inspiration feature
Browse files Browse the repository at this point in the history
  • Loading branch information
mysurvive committed Jul 24, 2024
1 parent facea65 commit f91dba9
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 14 deletions.
79 changes: 65 additions & 14 deletions src/module/so-inspired.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { updatePlayerList } from "./socket";
import {
addSharedInspiration,
removeSharedInspiration,
updatePlayerList,
} from "./socket";

Hooks.on("renderChatMessage", () => {});
Hooks.on("init", () => {
Expand Down Expand Up @@ -33,6 +37,22 @@ Hooks.on("init", () => {
default: { colorpicker1: "#401f25", colorpicker2: "#741b2b" },
});

game.settings.register("so-inspired", "useSharedInspiration", {
name: "Use Shared Inspiration",
hint: "Changes from the default per-user inspiration count to a shared pool of inspiration that any player can use.",
scope: "world",
config: true,
type: Boolean,
default: false,
});

game.settings.register("so-inspired", "sharedInspiration", {
scope: "world",
config: false,
type: Number,
default: 0,
});

game.settings.register("so-inspired", "inspirationName", {
name: "Inspiration Name",
hint: 'Set the global name for "Inspiration"',
Expand Down Expand Up @@ -234,7 +254,12 @@ Hooks.on("tidy5e-sheet.renderActorSheet", (app, element) => {
(u) => u.character?.uuid === app.actor.uuid
);
if (actorOwner) {
currentInspiration = actorOwner.getFlag("so-inspired", "inspirationCount");
currentInspiration = game.settings.get(
"so-inspired",
"useSharedInspiration"
)
? game.settings.get("so-inspired", "sharedInspiration")
: actorOwner.getFlag("so-inspired", "inspirationCount");

let newInspirationArea = `
<div
Expand Down Expand Up @@ -295,14 +320,18 @@ async function updatePlayerListInspo() {
for (const player of playerList) {
const user = await fromUuid(`User.${$(player).attr("data-user-id")}`);
if (!user.isGM) {
const inspoCount = user.getFlag("so-inspired", "inspirationCount");
const inspoCount = game.settings.get(
"so-inspired",
"useSharedInspiration"
)
? game.settings.get("so-inspired", "sharedInspiration")
: actorOwner.getFlag("so-inspired", "inspirationCount");

Check failure on line 328 in src/module/so-inspired.js

View workflow job for this annotation

GitHub Actions / lint

'actorOwner' is not defined
$(player)

Check failure on line 329 in src/module/so-inspired.js

View workflow job for this annotation

GitHub Actions / lint

'actorOwner' is not defined
.children()
.last()
.append(`<span class="inspiration-count">${inspoCount}</span>`);
}
}
updatePlayerList();
}

function createInspoFlag() {
Expand All @@ -320,10 +349,12 @@ function renderNewInspoSheet(_sheet, html) {
if (actorOwner) {
let currentInspiration;
let maxInspiration;
currentInspiration = actorOwner.getFlag(
currentInspiration = game.settings.get(
"so-inspired",
"inspirationCount"
);
"useSharedInspiration"
)
? game.settings.get("so-inspired", "sharedInspiration")
: actorOwner.getFlag("so-inspired", "inspirationCount");
maxInspiration = game.settings.get("so-inspired", "maxInspiration");

const inspirationArea = $(html).find(".inspiration");
Expand Down Expand Up @@ -374,21 +405,33 @@ async function addInspiration(user, _sheet) {
return;
}
const maxInspo = game.settings.get("so-inspired", "maxInspiration");
const currentInspo = user.getFlag("so-inspired", "inspirationCount");
const currentInspo = game.settings.get("so-inspired", "useSharedInspiration")
? game.settings.get("so-inspired", "sharedInspiration")
: actorOwner.getFlag("so-inspired", "inspirationCount");

Check failure on line 410 in src/module/so-inspired.js

View workflow job for this annotation

GitHub Actions / lint

'actorOwner' is not defined

Check failure on line 411 in src/module/so-inspired.js

View workflow job for this annotation

GitHub Actions / lint

'actorOwner' is not defined
if (currentInspo < maxInspo) {
await user.setFlag("so-inspired", "inspirationCount", currentInspo + 1);
game.settings.get("so-inspired", "useSharedInspiration")
? await addSharedInspiration()
: await user.setFlag("so-inspired", "inspirationCount", currentInspo + 1);
ChatMessage.create({
user: user,
flavor:
(_sheet ? _sheet.actor.name : user.name) +
(game.settings.get("so-inspired", "useSharedInspiration")
? "The group "
: _sheet
? _sheet.actor.name
: user.name) +
` has gained ${game.settings.get("so-inspired", "inspirationName")}!`,
});
} else {
ChatMessage.create({
user: user,
flavor:
(_sheet ? _sheet.actor.name : user.name) +
(game.settings.get("so-inspired", "useSharedInspiration")
? "The group "
: _sheet
? _sheet.actor.name
: user.name) +
` was granted ${game.settings.get(
"so-inspired",
"inspirationName"
Expand All @@ -399,6 +442,7 @@ async function addInspiration(user, _sheet) {
});
}
if (_sheet) _sheet.render(true);
updatePlayerList();
}

async function removeInspiration(user, _sheet) {
Expand All @@ -407,17 +451,24 @@ async function removeInspiration(user, _sheet) {
return;
}
const minInspo = 0;
const currentInspo = user.getFlag("so-inspired", "inspirationCount");
const currentInspo = game.settings.get("so-inspired", "useSharedInspiration")
? game.settings.get("so-inspired", "sharedInspiration")
: actorOwner.getFlag("so-inspired", "inspirationCount");

Check failure on line 456 in src/module/so-inspired.js

View workflow job for this annotation

GitHub Actions / lint

'actorOwner' is not defined
if (currentInspo > minInspo) {

Check failure on line 457 in src/module/so-inspired.js

View workflow job for this annotation

GitHub Actions / lint

'actorOwner' is not defined
await user.setFlag("so-inspired", "inspirationCount", currentInspo - 1);
game.settings.get("so-inspired", "useSharedInspiration")
? await removeSharedInspiration()
: await user.setFlag("so-inspired", "inspirationCount", currentInspo - 1);
ChatMessage.create({
user: user,
flavor:
_sheet.actor.name +
(game.settings.get("so-inspired", "useSharedInspiration")
? "The group "
: _sheet.actor.name) +
` has used ${game.settings.get("so-inspired", "inspirationName")}!`,
});
}
_sheet.render(true);
updatePlayerList();
}

function updateSheetForInspo(user) {
Expand Down
34 changes: 34 additions & 0 deletions src/module/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,46 @@ let socket;
Hooks.once("socketlib.ready", () => {
socket = socketlib.registerModule("so-inspired");
socket.register("updatePlayerList", _socketUpdatePlayerList);
socket.register("addSharedInspiration", _socketAddSharedInspiration);
socket.register("removeSharedInspiration", _socketRemoveSharedInspiration);
});

export async function updatePlayerList() {
return await socket.executeForOthers(_socketUpdatePlayerList);
}

export async function addSharedInspiration() {
return await socket.executeAsGM(_socketAddSharedInspiration);
}

export async function removeSharedInspiration() {
return await socket.executeAsGM(_socketRemoveSharedInspiration);
}

async function _socketUpdatePlayerList() {
ui.players.render();
}

async function _socketAddSharedInspiration() {
const currentInspiration = await game.settings.get(
"so-inspired",
"sharedInspiration"
);
await game.settings.set(
"so-inspired",
"sharedInspiration",
currentInspiration + 1
);
}

async function _socketRemoveSharedInspiration() {
const currentInspiration = await game.settings.get(
"so-inspired",
"sharedInspiration"
);
await game.settings.set(
"so-inspired",
"sharedInspiration",
currentInspiration - 1
);
}

0 comments on commit f91dba9

Please sign in to comment.