Change io to global

develop^2
Nathan Steel 1 year ago
parent 8df0969836
commit 525e55a092

@ -10,12 +10,12 @@ const maxRooms = 3;
const maxPlayersPerRoom = 1; const maxPlayersPerRoom = 1;
const maxSpectatorsPerRoom = 0; const maxSpectatorsPerRoom = 0;
function requestRooms(socket, io, filter){ function requestRooms(socket, filter){
console.log('+ requestRooms recieved'); console.log('+ requestRooms recieved');
console.log('- filter: '+filter); console.log('- filter: '+filter);
let response = getRooms(filter, dump = true); let response = getRooms(filter, dump = true);
io.to(socket.id).emit('returnRooms', response); global.io.to(socket.id).emit('returnRooms', response);
console.log(''); console.log('');
} }
@ -35,16 +35,16 @@ function getRooms(filter = 'all', dump = false){
return response; return response;
} }
function requestCreateRoom(socket, io, playerName){ function requestCreateRoom(socket, playerName){
console.log('+ createRoom recieved'); console.log('+ createRoom recieved');
console.log('- requested by: '+playerName); console.log('- requested by: '+playerName);
response = createRoom(roomId = false, dump = true); response = createRoom(roomId = false, dump = true);
io.to(socket.id).emit('returnCreateRoom', response); global.io.to(socket.id).emit('returnCreateRoom', response);
if(response.success){ if(response.success){
let response = getRooms(filter = 'all', dump = true); let response = getRooms(filter = 'all', dump = true);
io.to(socket.id).emit('returnRooms', response); global.io.to(socket.id).emit('returnRooms', response);
} }
console.log(''); console.log('');
@ -102,7 +102,7 @@ function createRoom(roomId = false, dump = true){
return response; return response;
} }
function requestJoinRoom(socket, io, playerName, roomId){ function requestJoinRoom(socket, playerName, roomId){
console.log('+ requestJoinRoom recieved'); console.log('+ requestJoinRoom recieved');
@ -140,12 +140,12 @@ function requestJoinRoom(socket, io, playerName, roomId){
' connected on ' + roomName + ' (' + (people) + '/' + maxPlayersPerRoom + ')'); ' connected on ' + roomName + ' (' + (people) + '/' + maxPlayersPerRoom + ')');
// Joined room (emit to the player that just joined) // 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) { if (people >= maxPlayersPerRoom) {
console.log('- starting game'); console.log('- starting game');
// startGame for room // startGame for room
startGame(roomId, io); startGame(roomId);
} }
} }
@ -161,7 +161,7 @@ function isUserInRoom(playerName, roomId){
return false; return false;
} }
function startGame(roomId, io){ function startGame(roomId){
console.log('>> Room: ' + roomId + ': Requesting game...'); console.log('>> Room: ' + roomId + ': Requesting game...');
let people = roomData[roomId].players; let people = roomData[roomId].players;
@ -187,21 +187,21 @@ function startGame(roomId, io){
// https://stackoverflow.com/a/25028953 // https://stackoverflow.com/a/25028953
//console.log(util.inspect(io.sockets.adapter.rooms.get(roomId), true, 4, true)) //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 // This should return the deck data, etc. for each client
// ideally only returning the items that the user can/should // ideally only returning the items that the user can/should
// see i.e. shouldn't give them the inDeck card list just a counter // 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 // 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.success = true;
response.message = data; response.message = data;
// Each player then gets the roomGeneration stuff // Each player then gets the roomGeneration stuff
for (const clientId of clients) { 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); console.log('>> responseStartGame: '+clientSocket.playerId);
// Emit to client socket // 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; response.message = err;
// Each player then gets the error message // Each player then gets the error message
for (const clientId of clients) { for (const clientId of clients) {
const clientSocket = io.sockets.sockets.get(clientId); const clientSocket = global.io.sockets.sockets.get(clientId);
// Emit to client socket // Emit to client socket
io.to(clientSocket.id).emit('responseStartGame', err); global.io.to(clientSocket.id).emit('responseStartGame', err);
} }
}); });

@ -7,6 +7,11 @@ const app = express();
const http = require('http').Server(app); const http = require('http').Server(app);
const port = process.env.PORT || 3000; const port = process.env.PORT || 3000;
const io = require('socket.io')(http); 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 // 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. // so console.logs show [Array]/[Object] instead of useful info.
@ -28,15 +33,15 @@ function onConnection(socket){
// Rooms (joining, creating, etc) // Rooms (joining, creating, etc)
socket.on('requestRooms', function(filter) { socket.on('requestRooms', function(filter) {
rooms.requestRooms(socket, io, filter); rooms.requestRooms(socket, filter);
}); });
socket.on('requestJoinRoom', function(playerName, roomId) { socket.on('requestJoinRoom', function(playerName, roomId) {
rooms.requestJoinRoom(socket, io, playerName, roomId); rooms.requestJoinRoom(socket, playerName, roomId);
}); });
socket.on('requestCreateRoom', function(playerName) { socket.on('requestCreateRoom', function(playerName) {
rooms.requestCreateRoom(socket, io, playerName); rooms.requestCreateRoom(socket, playerName);
}); });
// Game (actual things relating to the game) // Game (actual things relating to the game)

Loading…
Cancel
Save