-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathChannel.ts
264 lines (237 loc) · 7.74 KB
/
Channel.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
import { Broadcast, Broadcastable } from './Broadcast';
import { ChannelAudience } from './ChannelAudience';
import { PlayerOrNpc } from './GameEntity';
import { IGameState } from './GameState';
import { PartyAudience } from './PartyAudience';
import { Player } from './Player';
import { PlayerRoles } from './PlayerRoles';
import { PrivateAudience } from './PrivateAudience';
import { WorldAudience } from './WorldAudience';
export interface IChannelLoader extends Array<Channel> {}
export interface IChannelConfig {
/** @property {string} name Name of the channel */
name: string;
/** @property {ChannelAudience} audience */
audience: ChannelAudience;
/** @property {string} [description] */
description: string;
/** @property {PlayerRoles} [minRequiredRole] */
minRequiredRole: PlayerRoles;
/** @property {string} [color] */
color?: string;
/** @property {{sender: function, target: function}} [formatter] */
formatter: { sender: Function; target: Function };
bundle?: string;
aliases?: string[];
eventOnly?: boolean;
}
/**
* @property {ChannelAudience} audience People who receive messages from this channel
* @property {string} name Actual name of the channel the user will type
* @property {string} color Default color. This is purely a helper if you're using default format methods
* @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
*/
export class Channel {
/** @property {ChannelAudience} audience People who receive messages from this channel */
audience: ChannelAudience;
/** @property {string} name Actual name of the channel the user will type */
name: string;
/** @property {string} color Default color. This is purely a helper if you're using default format methods */
color: string | null;
/** @property {PlayerRoles} minRequiredRole If set only players with the given role or greater can use the channel */
minRequiredRole: PlayerRoles | null;
/** @property {string} description */
description: string;
/** @property {{sender: function, target: function}} [formatter] */
formatter: { sender: Function; target: Function };
bundle: string | null;
aliases: string[] | null;
eventOnly: boolean;
/**
* @param {object} config
* @param {string} config.name Name of the channel
* @param {ChannelAudience} config.audience
* @param {string} [config.description]
* @param {PlayerRoles} [config.minRequiredRole]
* @param {string} [config.color]
* @param {{sender: function, target: function}} [config.formatter]
* @param {boolean} [config.eventOnly]
*/
constructor(config: IChannelConfig) {
if (!config.name) {
throw new Error('Channels must have a name to be usable.');
}
if (!config.audience) {
throw new Error(`Channel ${config.name} is missing a valid audience.`);
}
this.name = config.name;
this.minRequiredRole =
typeof config.minRequiredRole !== 'undefined'
? config.minRequiredRole
: null;
this.description = config.description;
this.bundle = config.bundle || null; // for debugging purposes, which bundle it came from
this.audience = config.audience || new WorldAudience();
this.color = config.color || null;
this.aliases = config.aliases || null;
this.formatter = config.formatter || {
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: IGameState, sender: Player, message: string) {
// If they don't include a message, explain how to use the channel.
if (!message.length) {
throw new NoMessageError();
}
if (!this.audience) {
throw new Error(
`Channel [${this.name} has invalid audience [${this.audience}]`
);
}
this.audience.configure({ state, sender, message });
const targets = this.audience.getBroadcastTargets();
if (this.audience instanceof PartyAudience && !sender.party) {
throw new NoPartyError();
}
// Allow audience to change message e.g., strip target name.
message = this.audience.alterMessage(message);
// 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))
);
}
if (!message.length) {
throw new NoMessageError();
}
const target = targets[0];
Broadcast.sayAt(
sender,
this.formatter.sender(sender, target, 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 as Broadcastable, 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);
for (const target of targets) {
/**
* Docs limit this to be for GameEntity (Area/Room/Item) but also applies
* to NPC and Player
*
* @event GameEntity#channelReceive
* @param {Channel} channel
* @param {Character} sender
* @param {string} rawMessage
*/
target.emit('channelReceive', this, sender, rawMessage);
}
}
describeSelf(sender: Player) {
Broadcast.sayAt(sender, `\r\nChannel: ${this.name}`);
Broadcast.sayAt(sender, 'Syntax: ' + this.getUsage());
if (this.description) {
Broadcast.sayAt(sender, this.description);
}
}
getUsage() {
if (this.audience instanceof PrivateAudience) {
return `${this.name} <target> [message]`;
}
return `${this.name} [message]`;
}
/**
* How to render the message the player just sent to the channel
* E.g., you may want "chat" to say "You chat, 'message here'"
* @param {Player} sender
* @param {Player} target
* @param {string} message
* @param {Function} colorify
* @return {string}
*/
formatToSender(
sender: PlayerOrNpc,
target: PlayerOrNpc,
message: string,
colorify: Function
) {
return colorify(`[${this.name}] ${sender.name}: ${message}`);
}
/**
* How to render the message to everyone else
* E.g., you may want "chat" to say "Playername chats, 'message here'"
* @param {Player} sender
* @param {Player} target
* @param {string} message
* @param {Function} colorify
* @return {string}
*/
formatToReceipient(
sender: PlayerOrNpc,
target: PlayerOrNpc,
message: string,
colorify: Function
) {
return this.formatToSender(sender, target, message, colorify);
}
colorify(message: string) {
if (!this.color) {
return message;
}
const colors = Array.isArray(this.color) ? this.color : [this.color];
const open = (colors as string[]).map((color: string) => `<${color}>`).join('');
const close = colors
.reverse()
.map((color) => `</${color}>`)
.join('');
return open + message + close;
}
}
export class NoPartyError extends Error {}
export class NoRecipientError extends Error {}
export class NoMessageError extends Error {}