From 525e55a09202cf5420211e730e8e6bbfb315ee5f Mon Sep 17 00:00:00 2001 From: Nathan Date: Wed, 30 Oct 2024 18:48:20 +0000 Subject: [PATCH] Change io to global --- rooms.js | 30 +++++++++++++++--------------- server.js | 11 ++++++++--- 2 files changed, 23 insertions(+), 18 deletions(-) diff --git a/rooms.js b/rooms.js index 1154b65..623d364 100644 --- a/rooms.js +++ b/rooms.js @@ -10,12 +10,12 @@ const maxRooms = 3; const maxPlayersPerRoom = 1; const maxSpectatorsPerRoom = 0; -function requestRooms(socket, io, filter){ +function requestRooms(socket, filter){ console.log('+ requestRooms recieved'); console.log('- filter: '+filter); let response = getRooms(filter, dump = true); - io.to(socket.id).emit('returnRooms', response); + global.io.to(socket.id).emit('returnRooms', response); console.log(''); } @@ -35,16 +35,16 @@ function getRooms(filter = 'all', dump = false){ return response; } -function requestCreateRoom(socket, io, playerName){ +function requestCreateRoom(socket, playerName){ console.log('+ createRoom recieved'); console.log('- requested by: '+playerName); response = createRoom(roomId = false, dump = true); - io.to(socket.id).emit('returnCreateRoom', response); + global.io.to(socket.id).emit('returnCreateRoom', response); if(response.success){ let response = getRooms(filter = 'all', dump = true); - io.to(socket.id).emit('returnRooms', response); + global.io.to(socket.id).emit('returnRooms', response); } console.log(''); @@ -102,7 +102,7 @@ function createRoom(roomId = false, dump = true){ return response; } -function requestJoinRoom(socket, io, playerName, roomId){ +function requestJoinRoom(socket, playerName, roomId){ console.log('+ requestJoinRoom recieved'); @@ -140,12 +140,12 @@ function requestJoinRoom(socket, io, playerName, roomId){ ' connected on ' + roomName + ' (' + (people) + '/' + maxPlayersPerRoom + ')'); // Joined room (emit to the player that just joined) - io.to(socket.id).emit('responseRoom', response); + global.io.to(socket.id).emit('responseRoom', response); if (people >= maxPlayersPerRoom) { console.log('- starting game'); // startGame for room - startGame(roomId, io); + startGame(roomId); } } @@ -161,7 +161,7 @@ function isUserInRoom(playerName, roomId){ return false; } -function startGame(roomId, io){ +function startGame(roomId){ console.log('>> Room: ' + roomId + ': Requesting game...'); let people = roomData[roomId].players; @@ -187,21 +187,21 @@ function startGame(roomId, io){ // https://stackoverflow.com/a/25028953 //console.log(util.inspect(io.sockets.adapter.rooms.get(roomId), true, 4, true)) - let clients = io.sockets.adapter.rooms.get(roomId); + let clients = global.io.sockets.adapter.rooms.get(roomId); // This should return the deck data, etc. for each client // ideally only returning the items that the user can/should // see i.e. shouldn't give them the inDeck card list just a counter // shouldn't have opponent card data/their hand shouldn't be flipped - roomMod.roomGeneration().then(data => { + roomMod.roomGeneration(roomId).then(data => { response.success = true; response.message = data; // Each player then gets the roomGeneration stuff for (const clientId of clients) { - const clientSocket = io.sockets.sockets.get(clientId); + const clientSocket = global.io.sockets.sockets.get(clientId); console.log('>> responseStartGame: '+clientSocket.playerId); // Emit to client socket - io.to(clientSocket.id).emit('responseStartGame', response); + global.io.to(clientSocket.id).emit('responseStartGame', response); } }) @@ -209,9 +209,9 @@ function startGame(roomId, io){ response.message = err; // Each player then gets the error message for (const clientId of clients) { - const clientSocket = io.sockets.sockets.get(clientId); + const clientSocket = global.io.sockets.sockets.get(clientId); // Emit to client socket - io.to(clientSocket.id).emit('responseStartGame', err); + global.io.to(clientSocket.id).emit('responseStartGame', err); } }); diff --git a/server.js b/server.js index c19a614..7e875d3 100644 --- a/server.js +++ b/server.js @@ -7,6 +7,11 @@ const app = express(); const http = require('http').Server(app); const port = process.env.PORT || 3000; const io = require('socket.io')(http); +global.io = io; + +// To log the player sockets, so they can be easily referred to +// maybe jank, but can't see alternative currently +global.playerSocket = {}; // util is what nodejs uses for console.log, but has a depth of 2 set // so console.logs show [Array]/[Object] instead of useful info. @@ -28,15 +33,15 @@ function onConnection(socket){ // Rooms (joining, creating, etc) socket.on('requestRooms', function(filter) { - rooms.requestRooms(socket, io, filter); + rooms.requestRooms(socket, filter); }); socket.on('requestJoinRoom', function(playerName, roomId) { - rooms.requestJoinRoom(socket, io, playerName, roomId); + rooms.requestJoinRoom(socket, playerName, roomId); }); socket.on('requestCreateRoom', function(playerName) { - rooms.requestCreateRoom(socket, io, playerName); + rooms.requestCreateRoom(socket, playerName); }); // Game (actual things relating to the game)