From a3a073ce8cda6c0e00d6fc0bfe04d73c67106224 Mon Sep 17 00:00:00 2001 From: Alexander Morse Date: Wed, 17 Apr 2019 18:29:56 -0400 Subject: [PATCH 1/2] Add channelSend event --- src/Channel.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/Channel.js b/src/Channel.js index b0b7ed8c..9acd6979 100644 --- a/src/Channel.js +++ b/src/Channel.js @@ -48,6 +48,7 @@ class Channel { * @param {Player} sender * @param {string} message * @fires GameEntity#channelReceive + * @fires GameEntity#channelSend */ send(state, sender, message) { @@ -88,6 +89,15 @@ class Channel { // strip color tags const rawMessage = message.replace(/\<\/?\w+?\>/gm, ''); + // Emit channel events + + /** + * @event GameEntity#channelSend + * @param {Channel} channel + * @param {string} rawMessage + */ + sender.emit('channelSend', this, rawMessage); + for (const target of targets) { /** * Docs limit this to be for GameEntity (Area/Room/Item) but also applies From e8b473db7b570efc3eed69ede75216786578871f Mon Sep 17 00:00:00 2001 From: Alexander Morse Date: Wed, 17 Apr 2019 18:51:59 -0400 Subject: [PATCH 2/2] Add eventOnly option to Channel config --- src/Channel.js | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/src/Channel.js b/src/Channel.js index 9acd6979..76d065df 100644 --- a/src/Channel.js +++ b/src/Channel.js @@ -12,6 +12,8 @@ const PartyAudience = require('./PartyAudience'); * @property {PlayerRoles} minRequiredRole If set only players with the given role or greater can use the channel * @property {string} description * @property {{sender: function, target: function}} [formatter] + * @property {boolean} eventOnly If true, only channel events will be fired in response to a message, without + * explicitly sending the message to players */ class Channel { /** @@ -22,6 +24,7 @@ class Channel { * @param {PlayerRoles} [config.minRequiredRole] * @param {string} [config.color] * @param {{sender: function, target: function}} [config.formatter] + * @param {boolean} [config.eventOnly] */ constructor(config) { if (!config.name) { @@ -41,6 +44,7 @@ class Channel { sender: this.formatToSender.bind(this), target: this.formatToReceipient.bind(this), }; + this.eventOnly = config.eventOnly || false; } /** @@ -68,24 +72,28 @@ class Channel { throw new NoPartyError(); } + if (this.audience instanceof PrivateAudience && !targets.length) { + throw new NoRecipientError(); + } + // Allow audience to change message e.g., strip target name. message = this.audience.alterMessage(message); - // Private channels also send the target player to the formatter - if (this.audience instanceof PrivateAudience) { - if (!targets.length) { - throw new NoRecipientError(); + // Send messages with Broadcast unless the channel is eventOnly. + if (!this.eventOnly) { + // Private channels also send the target player to the formatter + if (this.audience instanceof PrivateAudience) { + Broadcast.sayAt(sender, this.formatter.sender(sender, targets[0], message, this.colorify.bind(this))); + } else { + Broadcast.sayAt(sender, this.formatter.sender(sender, null, message, this.colorify.bind(this))); } - Broadcast.sayAt(sender, this.formatter.sender(sender, targets[0], message, this.colorify.bind(this))); - } else { - Broadcast.sayAt(sender, this.formatter.sender(sender, null, message, this.colorify.bind(this))); + + // send to audience targets + Broadcast.sayAtFormatted(this.audience, message, (target, message) => { + return this.formatter.target(sender, target, message, this.colorify.bind(this)); + }); } - // send to audience targets - Broadcast.sayAtFormatted(this.audience, message, (target, message) => { - return this.formatter.target(sender, target, message, this.colorify.bind(this)); - }); - // strip color tags const rawMessage = message.replace(/\<\/?\w+?\>/gm, '');