Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dependent effect via socket #4878

Draft
wants to merge 4 commits into
base: 4.2.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions dnd5e.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {default as registry} from "./module/registry.mjs";
import * as utils from "./module/utils.mjs";
import {ModuleArt} from "./module/module-art.mjs";
import registerModuleData from "./module/module-registration.mjs";
import Sockets5e from "./module/sockets/sockets.mjs";
import Tooltips5e from "./module/tooltips.mjs";

/* -------------------------------------------- */
Expand Down Expand Up @@ -89,6 +90,9 @@ Hooks.once("init", function() {
// Configure bastions
game.dnd5e.bastion = new documents.Bastion();

// Configure sockets
game.dnd5e.sockets = new Sockets5e();

// Configure tooltips
game.dnd5e.tooltips = new Tooltips5e();

Expand Down
7 changes: 7 additions & 0 deletions module/sockets/_module.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import AddDependent from "./add-dependent.mjs";
import GrantEffect from "./grant-effect.mjs";

export default [
AddDependent,
GrantEffect
];
59 changes: 59 additions & 0 deletions module/sockets/add-dependent.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import SocketEvent from "./socket-event.mjs";

/**
* @typedef {import("./socket-event.mjs").EventData} AddDependentData
* @property {string} effect The uuid of the effect to add dependents to.
* @property {string[]} dependents The uuids of the effects to add as dependents.
*/

export default class AddDependentData extends SocketEvent {
/** @inheritDoc */
static eventName = "addDependent";

/* -------------------------------------------- */

/**
* As a user allowed to do so, perform the operation.
* @param {AddDependentData} data The event data.
*/
static async finalize(data) {
const uuids = [data.effect, ...data.dependents];
const effects = await Promise.all(uuids.map(uuid => fromUuid(uuid)));
if ( effects.some(effect => effect?.documentName !== "ActiveEffect") ) return;
const effect = effects.shift();
effect.addDependent(effects);
}

/* -------------------------------------------- */

/**
* Initiate the socket event.
* @param {ActiveEffect5e} effect The effect to add dependents to.
* @param {...ActiveEffect5e} dependents The effects to add as dependents.
* @returns {AddDependentData|void} The event data.
*/
static initiate(effect, ...dependents) {
if ( [effect, ...dependents].some(effect => effect?.documentName !== "ActiveEffect") ) {
throw new Error("You must supply both an effect and dependent effect instances for this operation.");
}

// Get a valid user to perform the operation.
// TODO: improve in v13
const userId = effect.isOwner ? game.user.id : game.users.find(user => {
return user.active && effect.testUserPermission(user, "OWNER");
})?.id;

if ( !userId ) {
// TODO: add to i18n
ui.notifications.error("DND5E.SOCKETS.AddDependent.Warning.MissingUser", { localize: true });
return;
}

return {
event: AddDependentData.eventName,
userIds: [userId],
effect: effect.uuid,
dependents: dependents.map(effect => effect.uuid)
};
}
}
57 changes: 57 additions & 0 deletions module/sockets/grant-effect.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import SocketEvent from "./socket-event.mjs";

/**
* @typedef {import("./socket-event.mjs").EventData} GrantEffectData
* @property {string} actor The uuid of the actor to receive the effect.
* @property {object} effectData Data used to create the effect.
*/

export default class GrantEffectEvent extends SocketEvent {
/** @inheritDoc */
static eventName = "grantEffect";

/* -------------------------------------------- */

/**
* As a user allowed to do so, perform the operation.
* @param {GrantEffectData} data The event data.
*/
static async finalize(data) {
const actor = await fromUuid(data.actor);
if ( actor?.documentName !== "Actor" ) return;
getDocumentClass("ActiveEffect").create(data.effectData, { parent: actor });
}

/* -------------------------------------------- */

/**
* Initiate the socket event.
* @param {Actor5e} actor The actor to receive the effect.
* @param {ActiveEffect5e} effect The effect to be duplicated onto the actor.
* @returns {GrantEffectData|void} The event data.
*/
static initiate(actor, effect) {
if ( (actor?.documentName !== "Actor") || (effect?.documentName !== "ActiveEffect") ) {
throw new Error("You must supply both an actor and effect instance for this operation.");
}

// Get a valid user to perform the operation.
// TODO: improve in v13
const userId = actor.isOwner ? game.user.id : game.users.find(user => {
return user.active && actor.testUserPermission(user, "OWNER");
})?.id;

if ( !userId ) {
// TODO: add to i18n
ui.notifications.error("DND5E.SOCKETS.GrantEffect.Warning.MissingUser", { localize: true });
return;
}

return {
event: GrantEffectEvent.eventName,
userIds: [userId],
actor: actor.uuid,
effectData: effect.toObject()
};
}
}
35 changes: 35 additions & 0 deletions module/sockets/socket-event.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* @typedef {object} EventData
* @property {string} event The unique event data name.
* @property {string[]} userIds The ids of users that should perform the operation.
*/

export default class SocketEvent {
/**
* The socket event name, which must be unique and is used to call the event.
* @type {string}
*/
static eventName;

/* -------------------------------------------- */

/**
* As a user allowed to do so, perform the operation.
* @param {EventData} data The event data.
* @abstract
*/
static async finalize(data) {
throw new Error("The 'finalize' method of a socket event must be subclassed.");
}

/* -------------------------------------------- */

/**
* Initiate the socket event. Subclasses can and should change the signature of this method.
* @returns {EventData}
* @abstract
*/
static initiate() {
throw new Error("The 'initiate' method of a socket event must be subclassed.");
}
}
60 changes: 60 additions & 0 deletions module/sockets/sockets.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import configs from "./_module.mjs";

export default class Sockets5e {
constructor() {
game.socket.on("system.dnd5e", this.#handleSocket.bind(this));
for (const config of configs) this.#register(config);
}

/* -------------------------------------------- */

/**
* Store event configurations.
* @type {Map<string, SocketEvent>}
*/
#events = new Map();

/* -------------------------------------------- */

/**
* Register a socket event config.
* @param {SocketEvent} config
*/
#register(config) {
this.#events.set(config.eventName, config);
}

/* -------------------------------------------- */

/**
* Handle the socket event for the correct user(s).
* @param {object} data Socket data.
* @param {boolean} [data._emit] Internally used parameter for whether this was initiated or emitted.
* @returns {*}
*/
#handleSocket({ _emit, ...data }) {
if ( !data.userIds.includes(game.user.id) ) {
if (!_emit) game.socket.emit("system.dnd5e", { ...data, _emit: true });
return;
}

const config = this.#events.get(data.event);
if ( config ) return config.finalize(data);

throw new Error(`'${data.event}' is not a valid socket event action!`);
}

/* -------------------------------------------- */

/**
* Initiate a given event, which may or may not be emitted.
* @param {string} event The event name.
* @param {any[]} args Function parameters for the handler.
*/
initiate(event, ...args) {
const config = this.#events.get(event);
if ( !config ) return;
const data = config.initiate(...args);
this.#handleSocket(data);
}
}