-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
376 lines (315 loc) · 9.35 KB
/
main.js
File metadata and controls
376 lines (315 loc) · 9.35 KB
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
const config = require('./config');
const fs = require('fs');
let custom_css = '';
jcmp.events.AddRemoteCallable('chat_submit_message', (player, message, channel) => {
message = message.trim();
if (message.startsWith('/'))
{
jcmp.events.Call('chat_command', player, message, channel);
return;
}
if (typeof channel == 'undefined' || channel == null || !config.default_channels.includes(channel))
{
console.log(`${player.name} sent a message on an invalid channel: ${channel}`);
return;
}
const returns = jcmp.events.Call('chat_message', player, message, channel);
if (returns.some(r => r === false))
{
return;
}
returns.forEach((r) =>
{
if (typeof r == 'string')
{
message = r;
}
});
if (message.length > 0)
{
LogMessage(message, channel, player);
console.log(`[${GetDate()} ${GetTime()}] [chat2] ${player.name}: ${message}`);
if (channel == "Local")
{
const pos = player.position;
jcmp.players.forEach((p) =>
{
if (dist(pos, p.position) < config.local_distance)
{
jcmp.events.CallRemote('chat_message', p, JSON.stringify(FormatMessage(message, player, null, {channel: channel})));
}
});
}
else
{
jcmp.events.CallRemote('chat_message', null, JSON.stringify(FormatMessage(message, player, null, {channel: channel})));
}
}
});
/**
* Formats a message.
*
* @param {string} msg - Message sent by the player.
* @param {Player} player - Player who sent the message.
* @param {RGB} color - Color of the message, only used by system messages.
* @param {object} args - Additional arguments for the message, such as channel or timeout.
* @return {object} - Returns formatted message.
*/
function FormatMessage(msg, player, color, args)
{
if (player != null && typeof player != 'undefined')
{
return FormatPlayerMessage(msg, player, args.channel);
}
else
{
color = (color == undefined) ? new RGB(255,255,255) : color;
return FormatSystemMessage(msg, color, args);
}
}
/**
* Formats a message sent by the server.
*
* @param {string} msg - Message sent by the player.
* @param {RGB} c - Color of the message.
* @param {object} args - Additional arguments for the message, such as channel or timeout.
* @return {object} - Returns formatted message.
*/
// Can't put msg in the HTML in case players are using <i> or [#ffffff] things
function FormatSystemMessage(msg, c, args)
{
if (typeof args == 'undefined')
{
return (
{
html: FormatChatMessage(`<span class="message-body" style="color:rgb(${c.r},${c.g},${c.b});" id="m_">${msg}</span>`),
});
}
else
{
let obj = {};
// If we don't use any vulnerable player names, just make it the HTML
if (args.use_name == undefined)
{
obj.html = FormatChatMessage(`<span class="message-body" style="color:rgb(${c.r},${c.g},${c.b});" id="m_">${msg}</span>`);
}
else
{
obj.html = FormatChatMessage(`<span class="message-body" style="color:rgb(${c.r},${c.g},${c.b});" id="m_"></span>`);
obj.msg = msg;
}
if (typeof args.timeout != 'undefined')
{
obj.timeout = args.timeout;
}
if (typeof args.channel != 'undefined')
{
obj.channel = args.channel;
}
if (typeof args.style != 'undefined')
{
obj.style = args.style;
}
return obj;
}
}
/**
* Formats a message sent by a player.
*
* @param {string} msg - Message sent by the player.
* @param {Player} player - Player who sent the message.
* @param {string} channel - Channel that the player sent the message on.
* @return {object} - Returns formatted message.
*/
function FormatPlayerMessage(msg, player, channel)
{
let html = '';
const obj = {
html: html,
name: player.name,
msg: msg,
channel: channel,
pid: player.networkId
}
let isAdmin = false;
let htmlTags = "";
if (player.tags != undefined) {
for (let t in player.tags) {
let tag = player.tags[t];
htmlTags += `<span id="tag" style="background-color:${tag.color};">${tag.name}</span>`;
isAdmin = (tag.name.toUpperCase() === "ADMIN");
}
}
const color = (config.using_freeroam) ? player.freeroam.colour : player.color;
html = `${htmlTags}<span class="player-name" style="color: ${color};" id="n_${player.networkId}"></span>[#FFFFFF]: <span class="message-body" id="m_"></span>`;
if (msg.indexOf(`@everyone`) > -1 && isAdmin)
{
obj.everyone = true;
}
html = FormatChatMessage(html);
obj.html = html;
return obj;
}
/**
* Formats hex tags [#ffffff] into HTML.
*
* @param {string} msg - Message to format for hex tags.
* @return {string} - Returns formatted HTML string.
*/
// Credit to Jan Christophersen for the hex tag formatter from original chat package
function FormatChatMessage(msg) // Formats messages with [#FFFFFF] tags
{
let i = 0;
let pos = msg.indexOf('[#');
while (pos !== -1)
{
const start = pos;
const end = pos + 8;
if (msg.charAt(end) !== ']')
{
pos = msg.indexOf('[#', pos+1);
continue;
}
const color = msg.substring(start + 1, end);
let buf = msg.substr(0, start);
if (i == 0)
{
buf += `<font style="color: ${color}">`;
}
else
{
buf += `</font><font style="color: ${color}">`;
}
buf += msg.substr(end + 1, msg.length);
msg = buf;
pos = msg.indexOf('[#', end);
i++;
}
msg = msg + "</font>";
return msg;
}
const chat =
{
/**
* Sends a message to a player.
*
* @param {Player} target - Player to send the message to.
* @param {string} message - Message to send to the player.
* @param {RGB} color - Color of the message in RGB format. Optional.
* @param {object} args - Additional arguments, such as timeout or channel.
*/
send(target, message, color, args)
{
const msg = JSON.stringify(FormatMessage(message, null, color, args));
jcmp.events.CallRemote('chat_message', target, msg);
},
/**
* Broadcasts a message to all players.
*
* @param {string} message - Message to be sent.
* @param {RGB} color - Color of the message in RGB format. Optional.
* @param {object} args - Additional arguments, such as timeout, channel, style, or use_name.
*/
broadcast(message, color, args)
{
const msg = JSON.stringify(FormatMessage(message, null, color, args));
jcmp.events.CallRemote('chat_message', null, msg);
},
/**
* Adds custom CSS to chat.
*
* @param {string} css
*/
addCustomCSS(css)
{
custom_css = css;
jcmp.events.CallRemote('chat/AddCustomCSS', null, custom_css);
}
}
jcmp.events.Add('get_chat', () =>
{
return chat;
});
jcmp.events.AddRemoteCallable('chat_ready', (player) =>
{
jcmp.events.CallRemote('chat/InitConfig', player, JSON.stringify(config));
jcmp.events.CallRemote('chat/AddCustomCSS', player, custom_css);
const name = player.name;
jcmp.events.CallRemote('chat2/store_name', player, name);
jcmp.events.CallRemote('chat2/AddPlayer', null, player.networkId, name);
let data = [];
jcmp.players.forEach(function(p)
{
if (p.networkId != player.networkId)
{
data.push({id: p.networkId, name: p.name});
}
});
jcmp.events.CallRemote('chat2/InitPlayers', player, JSON.stringify(data));
})
/**
* Logs a chat message sent by a player to the /logs folder in the chat package.
*
* @param {string} msg - Message that the player sent.
* @param {string} channel - Channel that the message was sent on.
* @param {Player} player - Player who sent the message.
*/
function LogMessage(msg, channel, player)
{
let time = GetTime();
let date = GetDate();
let log_message = `\r\n${time} [${channel}] [${player.client.steamId}] ${player.name}: ${msg}`;
fs.appendFile(`${__dirname}/logs/${date}.log`, log_message, function (err)
{
if (err)
{
throw err;
}
});
}
jcmp.events.Add('PlayerDestroyed', (player) =>
{
jcmp.events.CallRemote('chat2/RemovePlayer', null, player.name);
})
/**
* Gets a nicely formatted time string.
*
* @return {string}
*/
function GetTime()
{
const date = new Date();
let hour = date.getHours();
hour = (hour < 10 ? "0" : "") + hour;
let min = date.getMinutes();
min = (min < 10 ? "0" : "") + min;
let sec = date.getSeconds();
sec = (sec < 10 ? "0" : "") + sec;
return `[${hour}:${min}:${sec}]`;
}
/**
* Gets a nicely formatted date string for log filenames.
*
* @return {string}
*/
function GetDate()
{
const date = new Date();
let year = date.getFullYear();
let month = date.getMonth() + 1;
month = (month < 10 ? "0" : "") + month;
let day = date.getDate();
day = (day < 10 ? "0" : "") + day;
return `${year}-${month}-${day}`;
}
/**
* Returns the distance between two Vector3s.
*
* @param {Vector3f} a
* @param {Vector3f} b
* @return {number}
*/
function dist(a, b)
{
return b.sub(a).length;
}