Files
2025-04-24 19:03:40 +02:00

104 lines
2.4 KiB
JavaScript

const functions = require("./functions.js");
module.exports = async (ws) => {
const ticketId = functions.MakeID().replace(/-/gi, "");
const matchId = functions.MakeID().replace(/-/gi, "");
const sessionId = functions.MakeID().replace(/-/gi, "");
ws.ticketId = ticketId;
ws.matchId = matchId;
ws.sessionId = sessionId;
if (global.matchmakingFunctions && typeof global.matchmakingFunctions.registerConnection === 'function') {
global.matchmakingFunctions.registerConnection(ws);
} else {
console.log("[Matchmaker] Warning: global.matchmakingFunctions.registerConnection not available");
}
ws.on('close', () => {
if (global.matchmakingFunctions && typeof global.matchmakingFunctions.unregisterConnection === 'function') {
global.matchmakingFunctions.unregisterConnection(ws);
}
});
Connecting();
await functions.sleep(800);
Waiting();
await functions.sleep(1000);
Queued();
function Connecting() {
if (ws.readyState === ws.OPEN) {
ws.send(
JSON.stringify({
payload: {
state: "Connecting",
},
name: "StatusUpdate",
})
);
}
}
function Waiting() {
if (ws.readyState === ws.OPEN) {
ws.send(
JSON.stringify({
payload: {
totalPlayers: 1,
connectedPlayers: 1,
state: "Waiting",
},
name: "StatusUpdate",
})
);
}
}
function Queued() {
if (ws.readyState === ws.OPEN) {
ws.send(
JSON.stringify({
payload: {
ticketId: ticketId,
queuedPlayers: 1,
estimatedWaitSec: 60,
status: {},
state: "Queued",
},
name: "StatusUpdate",
})
);
}
let queueUpdateCount = 0;
const queueUpdateInterval = setInterval(() => {
if (ws.readyState !== ws.OPEN) {
clearInterval(queueUpdateInterval);
return;
}
queueUpdateCount++;
// lmfao
ws.send(
JSON.stringify({
payload: {
ticketId: ticketId,
queuedPlayers: Math.max(1, 20 - queueUpdateCount),
estimatedWaitSec: Math.max(5, 60 - queueUpdateCount * 5),
status: {},
state: "Queued",
},
name: "StatusUpdate",
})
);
if (queueUpdateCount >= 20) {
clearInterval(queueUpdateInterval);
}
}, 3000);
}
};