Add matchmaker.

This commit is contained in:
Lawin0129
2023-01-26 20:30:40 +00:00
parent 63e0c7d179
commit 692cfe41f9
2 changed files with 74 additions and 1 deletions
+69
View File
@@ -0,0 +1,69 @@
const crypto = require("crypto");
module.exports = (ws) => {
// create hashes
const ticketId = crypto.createHash('md5').update(`1${Date.now()}`).digest('hex');
const matchId = crypto.createHash('md5').update(`2${Date.now()}`).digest('hex');
const sessionId = crypto.createHash('md5').update(`3${Date.now()}`).digest('hex');
// you can use setTimeout to send the websocket messages at certain times
setTimeout(Connecting, 200/* Milliseconds */);
setTimeout(Waiting, 1000); // 0.8 Seconds after Connecting
setTimeout(Queued, 2000); // 1 Second after Waiting
setTimeout(SessionAssignment, 6000); // 4 Seconds after Queued
setTimeout(Join, 8000); // 2 Seconds after SessionAssignment
function Connecting() {
ws.send(JSON.stringify({
"payload": {
"state": "Connecting"
},
"name": "StatusUpdate"
}));
}
function Waiting() {
ws.send(JSON.stringify({
"payload": {
"totalPlayers": 1,
"connectedPlayers": 1,
"state": "Waiting"
},
"name": "StatusUpdate"
}));
}
function Queued() {
ws.send(JSON.stringify({
"payload": {
"ticketId": ticketId,
"queuedPlayers": 0,
"estimatedWaitSec": 0,
"status": {},
"state": "Queued"
},
"name": "StatusUpdate"
}));
}
function SessionAssignment() {
ws.send(JSON.stringify({
"payload": {
"matchId": matchId,
"state": "SessionAssignment"
},
"name": "StatusUpdate"
}));
}
function Join() {
ws.send(JSON.stringify({
"payload": {
"matchId": matchId,
"sessionId": sessionId,
"joinDelaySec": 1
},
"name": "Play"
}));
}
}
+5 -1
View File
@@ -12,7 +12,8 @@ const User = require("../model/user.js");
const Friends = require("../model/friends.js");
const port = 80;
const wss = new WebSocket({ server: app.listen(port, () => log.xmpp(`XMPP started listening on port ${port}`)) });
const wss = new WebSocket({ server: app.listen(port, () => log.xmpp(`XMPP and Matchmaker started listening on port ${port}`)) });
const matchmaker = require("../matchmaker/matchmaker.js");
global.Clients = [];
@@ -44,6 +45,9 @@ app.get("/clients", (req, res) => {
})
wss.on('connection', async (ws) => {
// Start matchmaker if it's not connecting for xmpp.
if (ws.protocol.toLowerCase() != "xmpp") return matchmaker(ws);
var accountId = "";
var displayName = "";
var token = "";