-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathxmpp.js
More file actions
423 lines (361 loc) · 14.3 KB
/
xmpp.js
File metadata and controls
423 lines (361 loc) · 14.3 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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
const WebSocket = require('ws');
const { randomUUID } = require('crypto');
const clients = new Map(); // has to be a Map instead of {} due to non-string keys
const partys = new Map();
const wss = new WebSocket.Server({ port: 81 }); // initiate a new server that listens on port 81
const db = require('./db');
// Define message handlers in an object
const messageHandlers = {
'auth': handleAuth,
'partyinvite': handlePartyInvite,
'retrieveactiveplayers': handleActivePlayers,
'killConnection': handleKillConnection,
'disconnectall': handleDisconnectAll,
'sendfriendrequest': handleSendFriendRequest,
'acceptfriendrequest': handleAcceptFriendRequest,
'declinefriendrequest': handleDeclineFriendRequest,
'retrievefriends': handleRetrieveFriends,
'updatestatus': handleUpdateStatus,
'updatecosmeticavatar': handleUpdateCosmeticAvatar,
'vbucksreward': handleVbucksReward,
'setlevelandxp': handleSetLevelAndXP,
'removefriend': handleRemoveFriend,
'beginmatchmaking': BeginMatchmaking
};
// set up event handlers and do other things upon a client connecting to the server
wss.on('connection', (ws) => {
// create an id to track the client
const id = randomUUID();
clients.set(ws, { id });
// Handle incoming messages from clients
ws.on('message', async (data) => {
try {
const message = JSON.parse(data);
// Check if message type has a handler, then call the handler function
if (message.type && messageHandlers[message.type]) {
messageHandlers[message.type](ws, message);
} else {
console.log('Unhandled message type:', message.type);
}
} catch (e) {
console.log(`${e} -- ${data}`);
}
});
// stop tracking the client upon that client closing the connection
ws.on('close', () => {
clients.delete(ws);
});
// send the id back to the newly connected client
ws.send(`You have been assigned id ${id}`);
});
// function for sending a message to every connected client
function serverBroadcast(message) {
wss.clients.forEach((client) => {
if (client.readyState === WebSocket.OPEN) {
client.send(message);
}
});
}
// function to get all online players
function getOnlinePlayers() {
const onlinePlayers = [];
clients.forEach((clientData, client) => {
if (clientData.accountId) {
onlinePlayers.push({
id: clientData.id,
accountId: clientData.accountId,
displayName: clientData.displayName,
status: clientData.status,
avatar: clientData.avatar
});
}
});
return onlinePlayers;
}
// function to get client by account ID
function getClientByAccountId(accountId) {
for (const [client, clientData] of clients.entries()) {
if (clientData.accountId === accountId) {
return { id: clientData.id, ws: client };
}
}
return null;
}
// Message handler functions
function handleAuth(ws, message) {
if (message.accountId) {
const existingClient = getClientByAccountId(message.accountId);
if (existingClient) {
existingClient.ws.close(3000, "Login Expired Or Logged In Elsewhere"); // Close the older connection
}
clients.set(ws, { accountId: message.accountId, displayName: message.displayname, status: "Lobby - 1 / 4", avatar: { templateId: "AthenaCharacter:CID_001_Athena_Commando_F_Default", bInMatchmaking: false } });
//ws.send(`You have been authenticated with accountId ${message.accountId}`);
}
}
function findPartyByAccountId(accountId) {
for (const [partyUUID, party] of partys) {
if (party.leader.accountId === accountId || party.members.some(member => member.accountId === accountId)) {
return { partyUUID, party };
}
}
return null;
}
function sendPartyInvite(targetClient, partyUUID, from) {
const inviteMessage = {
type: 'alert',
partyUUID: partyUUID,
from: from.UsernameFrom,
header: 'Party Invitation',
text: `${from.UsernameFrom} has invited you to join their party.`
};
targetClient.send(JSON.stringify(inviteMessage));
}
function handlePartyInvite(ws, message) {
const { accountIdFrom, accountId, UsernameFrom, CosmeticInformationForSender } = message;
const fromClient = getClientByAccountId(accountIdFrom);
const toClient = getClientByAccountId(accountId);
if (!fromClient || !toClient) {
ws.send(JSON.stringify({ type: 'alert', header: 'Error', text: 'Invalid account ID(s).' }));
return;
}
const fromParty = findPartyByAccountId(accountIdFrom);
if (fromParty) {
if (fromParty.party.members.length < 4) {
// Send party invite to target
sendPartyInvite(toClient.ws, fromParty.partyUUID, { accountIdFrom, UsernameFrom });
} else {
const partyFullMessage = {
type: 'alert',
header: 'Party Full',
text: `Your party is full. Cannot invite more members.`,
bShowPlayerIcon: false
};
ws.send(JSON.stringify(partyFullMessage));
}
} else {
// Create a new party with sender as the leader and send invite to target
const partyUUID = randomUUID();
const newParty = {
leader: { accountId: accountIdFrom, username: UsernameFrom },
members: [
{
accountId: accountIdFrom,
username: UsernameFrom,
cosmeticInformation: CosmeticInformationForSender
}
]
};
partys.set(partyUUID, newParty);
sendPartyInvite(toClient.ws, partyUUID, { accountIdFrom, UsernameFrom });
}
}
function handleActivePlayers(ws) {
const senderId = clients.get(ws).id;
const onlinePlayers = getOnlinePlayers();
const responseMessage = {
type: 'activeplayers',
senderId: senderId,
players: onlinePlayers,
};
ws.send(JSON.stringify(responseMessage));
}
function handleKillConnection(message) {
const existingClient = getClientByAccountId(message.accountId);
if(existingClient) {
clients.delete(existingClient);
existingClient.ws.close(3000, "Server has requested that you disconnect."); // Close the older connection
}
}
function handleDisconnectAll() {
clients.forEach((clientData, client) => {
if (clientData.accountId) {
client.close(3000, "Server has requested that you disconnect.");
}
});
}
async function handleSendFriendRequest(ws, message) {
const { AccountIdFrom, AccountIdTo, UsernameFrom } = message;
// Check if the recipient is online
const targetClient = getClientByAccountId(AccountIdTo);
if (targetClient) {
const alertMessage = {
type: 'alert',
header: 'Friend Request',
text: `<Username>${UsernameFrom}</> sent you a Friend Request`,
bShowPlayerIcon: true
};
targetClient.ws.send(JSON.stringify(alertMessage));
}
const SentSuccessAlertMessage = {
type: 'alert',
header: 'Successfully Sent',
text: `Successfully sent Friend Request!`,
bShowPlayerIcon: false
};
ws.send(JSON.stringify(SentSuccessAlertMessage));
// Add friend request to the database
await db.addFriendRequest(AccountIdFrom, AccountIdTo, UsernameFrom);
}
async function handleAcceptFriendRequest(ws, message) {
const { AccountIdFrom, AccountIdTo } = message;
try {
// Get sender's username from database
const senderData = await db.getUserDataById(AccountIdFrom);
const senderUsername = senderData ? senderData.username : 'Unknown User';
const SentToData = await db.getUserDataById(AccountIdTo);
const SentToDataUsername = SentToData ? SentToData.username : 'Unknown User';
// Check if the sender is online
const senderClient = getClientByAccountId(AccountIdFrom);
if (senderClient) {
const alertMessage = {
type: 'alert',
header: 'Friend Request Accepted',
text: `Your Friend Request to <Username>${SentToDataUsername}</> has been accepted`,
bShowPlayerIcon: false
};
senderClient.ws.send(JSON.stringify(alertMessage));
}
const SentToAlertMessage = {
type: 'alert',
header: 'Friend Request Accepted',
text: `Successfully accepted <Username>${senderUsername}</>'s Friend Request!`,
bShowPlayerIcon: false
};
ws.send(JSON.stringify(SentToAlertMessage));
// Add friend relationship to the database
await db.addFriend(AccountIdFrom, AccountIdTo);
} catch (err) {
console.error('Error accepting friend request:', err);
}
}
async function handleDeclineFriendRequest(ws, message) {
const { AccountIdFrom, AccountIdTo } = message;
try {
// Get sender's username from database
const senderData = await db.getUserDataById(AccountIdFrom);
const senderUsername = senderData ? senderData.username : 'Unknown User';
const SentToData = await db.getUserDataById(AccountIdTo);
const SentToDataUsername = SentToData ? SentToData.username : 'Unknown User';
// Check if the sender is online
const senderClient = getClientByAccountId(AccountIdFrom);
if (senderClient) {
const alertMessage = {
type: 'alert',
header: 'Friend Request Declined',
text: `Your Friend Request to <Username>${SentToDataUsername}</> has been declined.`,
bShowPlayerIcon: false
};
senderClient.ws.send(JSON.stringify(alertMessage));
}
const SentToAlertMessage = {
type: 'alert',
header: 'Friend Request Declined',
text: `Successfully declined <Username>${senderUsername}</>'s Friend Request.`,
bShowPlayerIcon: false
};
ws.send(JSON.stringify(SentToAlertMessage));
await db.declineFriendRequest(AccountIdFrom, AccountIdTo);
} catch (err) {
console.error('Error accepting friend request:', err);
}
}
async function handleRetrieveFriends(ws) {
const accountId = clients.get(ws).accountId; // Assuming you store accountId in the client data
const onlinePlayers = getOnlinePlayers(); // Assuming getOnlinePlayers() is defined and returns online players array
const friendsData = await db.getFriends(accountId, onlinePlayers);
const responseMessage = {
type: 'friendslist',
onlineFriends: friendsData.onlineFriends,
offlineFriends: friendsData.offlineFriends,
numOnlineFriends: friendsData.numOnlineFriends,
numOfflineFriends: friendsData.numOfflineFriends
};
ws.send(JSON.stringify(responseMessage));
}
function handleUpdateStatus(ws, message) {
const clientData = clients.get(ws);
if (clientData) {
clientData.status = message.status;
clients.set(ws, clientData);
}
}
function handleUpdateCosmeticAvatar(ws, message) {
const clientData = clients.get(ws);
if (clientData) {
clientData.avatar = {
templateId: message.templateId
};
clients.set(ws, clientData);
}
}
function handleVbucksReward(ws, message) {
const clientData = clients.get(ws);
if (clientData) {
db.getUserDataById(clientData.accountId)
.then((userData) => {
if (userData) {
const newAmount = parseInt(userData.vbucks) + parseInt(message.reward);
db.updateUserDataById(clientData.accountId, { vbucks: newAmount.toString() });
}
})
.catch((err) => {});
}
}
function handleSetLevelAndXP(ws, message) {
const clientData = clients.get(ws);
if (clientData) {
db.updateProfileAthenaById(clientData.accountId, {statistics: { Level: parseInt(message.level), XP: parseInt(message.xp)}});
}
}
async function handleRemoveFriend(ws, message) {
const { AccountIdFrom, AccountIdTo } = message;
try {
const senderData = await db.getUserDataById(AccountIdFrom);
const senderUsername = senderData ? senderData.username : 'Unknown User';
const SentToAlertMessage = {
type: 'alert',
header: 'Friend Removed',
text: `Successfully removed ${senderUsername} from your friends list.`,
bShowPlayerIcon: false
};
ws.send(JSON.stringify(SentToAlertMessage));
await db.removeFriend(AccountIdFrom, AccountIdTo);
} catch (err) {
console.error('Error accepting friend request:', err);
}
}
async function BeginMatchmaking(ws, message) {
// matchmaking checks for the player
const clientData = clients.get(ws);
if (clientData) {
clientData.bInMatchmaking = true;
clients.set(ws, clientData);
// if the client cancels matchmaking then clientData.bInMatchmaking will be false, we'll use that to ensure we should continue running this code.
const playerUserData = await db.getUserDataById(clientData.accountId);
if(playerUserData) {
if(clientData.bInMatchmaking == false) return;
if(playerUserData.mcpbandata.length > 0) {
const currentDate = new Date();
const hasActiveBan = playerUserData.mcpbandata.some(item => {
if (item.unbanDate) {
const [day, hour, minute, month, year] = item.unbanDate.split('-').map(Number);
const unbanDate = new Date(year, month - 1, day, hour, minute);
return unbanDate > currentDate;
}
return false;
});
if(hasActiveBan) {
const BanModalMessage = {
type: 'banmodal',
ban: null, // we'll have to put the ban object from there MCP ban data
};
ws.send(JSON.stringify(BanModalMessage));
return;
}
}
}
else {
}
}
}
console.log('The XMPP server is running and waiting for connections');