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 eventOnly option, channelSend event to Channel class #76

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
42 changes: 30 additions & 12 deletions src/Channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
/**
Expand All @@ -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) {
Expand All @@ -41,13 +44,15 @@ class Channel {
sender: this.formatToSender.bind(this),
target: this.formatToReceipient.bind(this),
};
this.eventOnly = config.eventOnly || false;
}

/**
* @param {GameState} state
* @param {Player} sender
* @param {string} message
* @fires GameEntity#channelReceive
* @fires GameEntity#channelSend
*/
send(state, sender, message) {

Expand All @@ -67,27 +72,40 @@ 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, '');

// Emit channel events

/**
* @event GameEntity#channelSend
* @param {Channel} channel
* @param {string} rawMessage
*/
sender.emit('channelSend', this, rawMessage);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR has been open for a bit and I recently merged it into my local fork and tried using it. One thing that stood out is that it may be helpful to actually emit this on the audience if possible... Mainly, I would like to handle sending to different audience members in different ways depending on whether they use telnet or webclient.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, on second thought, it is still possible to do with the way this is set up. The one thing it may make a bit harder is having NPCs use the channels but that can be done with a behavior.


for (const target of targets) {
/**
* Docs limit this to be for GameEntity (Area/Room/Item) but also applies
Expand Down