From 692cfe41f9dbd2ec25f922b2364025619d5ae269 Mon Sep 17 00:00:00 2001 From: Lawin0129 <56766256+Lawin0129@users.noreply.github.com> Date: Thu, 26 Jan 2023 20:30:40 +0000 Subject: [PATCH] Add matchmaker. --- matchmaker/matchmaker.js | 69 ++++++++++++++++++++++++++++++++++++++++ xmpp/xmpp.js | 6 +++- 2 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 matchmaker/matchmaker.js diff --git a/matchmaker/matchmaker.js b/matchmaker/matchmaker.js new file mode 100644 index 0000000..77eeef4 --- /dev/null +++ b/matchmaker/matchmaker.js @@ -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" + })); + } +} \ No newline at end of file diff --git a/xmpp/xmpp.js b/xmpp/xmpp.js index f46810c..8f328e4 100644 --- a/xmpp/xmpp.js +++ b/xmpp/xmpp.js @@ -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 = "";