Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions Back-End/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ const io = socketIo(server, {
class GameQueue {
constructor(gameName) {
this.gameName = gameName;
this.queue = []; // Array to maintain the order of players
this.playerMap = new Map(); // Map for quick access to player data
this.queueTimer = null; // Timer for resetting the queue due to inactivity
this.queue = [];
this.playerMap = new Map();
this.queueTimer = null;
}

addPlayer(player) {
Expand All @@ -52,6 +52,7 @@ class GameQueue {
const player = this.playerMap.get(playerId);
this.queue = this.queue.filter(p => p.id !== playerId);
this.playerMap.delete(playerId);

console.log(`${player.name} has been removed from the ${this.gameName} queue.`);
if (this.queue.length > 0) {
this.startQueueResetTimer();
Expand Down Expand Up @@ -89,15 +90,14 @@ class GameQueue {
clearTimeout(this.queueTimer);
this.queueTimer = setTimeout(() => {
this.reset();
}, 60000); // Reset the queue after 60 seconds of inactivity
}, 60000);
}

stopQueueResetTimer() {
clearTimeout(this.queueTimer);
}
}

// Initialize queues for each game
let queues = {
valorant: new GameQueue('valorant'),
league: new GameQueue('league'),
Expand All @@ -119,13 +119,27 @@ io.on("connection", (socket) => {
}
});

socket.on("disconnectPlayer", () => {
socket.on("disconnectPlayerQueue", () => {
Object.keys(queues).forEach(gameName => {
if (queues[gameName].removePlayer(socket.id)) {
queues[gameName].matchPlayers();
}
});
});

// socket.on("playerDisconnection", () => {
// console.log(`Player ${socket.id} has left the game.`);

// // Retrieve the game or queue the player is part of
// const gameDetails = findGameByPlayerId(socket.id); // Implement this function based on your application logic

// if (gameDetails) {
// // Determine and declare the winner
// const winnerId = determineWinner(gameDetails, socket.id); // Adjust implementation as needed
// io.to(winnerId).emit("gameOver", { winner: true });

// }
// });
});


Expand Down
9 changes: 7 additions & 2 deletions Front-End/src/Components/Multiplayer/Loadingpage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ const Loadingpage = () => {

useEffect(() => {
if (timeLeft <= 0) {
socket.emit('disconnectPlayer');
socket.emit('disconnectPlayerQueue');
const Toast = Swal.mixin({
toast: true,
position: 'top-end',
Expand Down Expand Up @@ -94,12 +94,17 @@ const Loadingpage = () => {
};

const handleLeaveQueueClick = () => {
socket.emit('disconnectPlayer');
socket.emit('disconnectPlayerQueue');
dispatch(multiplayerActions.setLoading(true));
dispatch(multiplayerActions.setOpponent(''));
navigate('/multiplayer/gameSelect');
};

window.addEventListener("beforeunload", (event) => {
event.preventDefault();
socket.emit('disconnectPlayerQueue')
});

return (
<div>
{loading ? (
Expand Down
4 changes: 2 additions & 2 deletions Front-End/src/Components/SocketContext.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ export const SocketProvider = ({ children }) => {
const [socket, setSocket] = useState(null);

useEffect(() => {
const newSocket = io(process.env.REACT_APP_SOCKET_URL);
// const newSocket = io('http://localhost:3001');
// const newSocket = io(process.env.REACT_APP_SOCKET_URL);
const newSocket = io('http://localhost:3001');
setSocket(newSocket);

return () => newSocket.close();
Expand Down
4 changes: 2 additions & 2 deletions Front-End/src/api.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Server Testt
const root = process.env.REACT_APP_SOCKET_URL
// const root = process.env.REACT_APP_SOCKET_URL

// Development
// const root = 'http://localhost:3001';
const root = 'http://localhost:3001';

const API = {
BugSubmit: `${root}/form/bug`,
Expand Down