diff --git a/public/board.js b/public/board.js index e3c0d4f..cd51410 100644 --- a/public/board.js +++ b/public/board.js @@ -1272,12 +1272,6 @@ let board = new Board; // Everything beyond initialisation shouldn't be required so no need to wait // At least I think, for now. May need more stuff in future -// Request the deck(s) from server/db -// When the page loads (for now) start the game -requestStartGame(); -// This will trigger an emit to and on from the server will trigger loadBoard(); -// public/main.js // socket.on('responseStartGame', function (data) { - function loadBoard(data) { console.log(data); diff --git a/public/index.html b/public/index.html index e34efda..83a167a 100644 --- a/public/index.html +++ b/public/index.html @@ -115,12 +115,6 @@ -
-
- - -
- diff --git a/public/main.js b/public/main.js index 413e56d..dbb0606 100644 --- a/public/main.js +++ b/public/main.js @@ -140,7 +140,6 @@ socket.on('responseJoinRoom', function (name) { if (name != 'error') { room = name; console.log('<< Room Response: ' + name); - alert('Commented out draw board, for UI stuff'); //board.drawBoard(); } else { socket.disconnect(); @@ -164,11 +163,8 @@ socket.on('responseGetCards', function (data) { responseGetCards(data); }); -// Testing getting cards from server/DB -function requestStartGame(){ - console.log('+ requestStartGame'); - socket.emit('requestStartGame'); -} +// When game starts, loadBoard to add data for clients +// and display the starting board function responseStartGame(data){ if(!data.success){ alert(data.message); @@ -186,25 +182,3 @@ socket.on('responseStartGame', function (data) { loadBoard(data.message); }); -// More experimenty -function requestRoomGeneration(){ - console.log('+ requestRoomGeneration'); - socket.emit('requestRoomGeneration'); -} -function responseRoomGeneration(data){ - if(!data.success){ - alert(data.message); - return false; - } - return data; -} -socket.on('responseRoomGeneration', function (data) { - console.log('<< responseRoomGeneration'); - responseStartGame(data); - if(data.success !== true){ - alert('Err with responseRoomGeneration. '+data.message); - } - // Pass only the data to loadBoard - loadBoard(data.message); -}); - diff --git a/server.js b/server.js index 70889cb..c29979d 100644 --- a/server.js +++ b/server.js @@ -41,52 +41,11 @@ for (let roomId = 1; roomId <= numRoomsToPreGen; roomId++) { createRoom(roomId); } -// For testing to see console logs -roomMod.roomGeneration(2); -//cardGen.requestDeck(); - -function requestStartGame(socket){ - response = {success: false, message: 'Failed requestStartGame() server.js'}; - cardGen.requestDeck().then(data => { - response.success = true; - response.message = data; - io.to(socket.id).emit('responseStartGame', response); - - }) - .catch(err => { - response.message = err; - io.to(socket.id).emit('responseStartGame', err); - }); -} -function requestRoomGeneration(socket){ - response = {success: false, message: 'Failed requestRoomGeneration() server.js'}; - roomMod.roomGeneration().then(data => { - response.success = true; - response.message = data; - io.to(socket.id).emit('responseRoomGeneration', response); - }) - .catch(err => { - response.message = err; - io.to(socket.id).emit('responseRoomGeneration', err); - }); - -} - function onConnection(socket){ console.log('+ User connected'); console.log(''); - // New testing fella (working afaik) - // TODO: request specific deckId/playerId (and multiples, i.e. get 6 decks at same - // time, based on deckId/playerId combo. Maybe pass as array [deckId, playerId],[deck - socket.on('requestStartGame', function() { - requestStartGame(socket); - }); - socket.on('requestRoomGeneration', function() { - requestRoomGeneration(socket); - }); - socket.on('requestRooms', function(filter) { requestRooms(socket, filter); }); @@ -175,6 +134,7 @@ function createRoom(roomId = false, dump = true){ room['timeout'] = {}; room['timeout']['s'] = 10; room['people'] = 0; + room['playerIds'] = {}; //room['deck'] = []; //room['turn'] = 0; @@ -210,39 +170,160 @@ function createRoom(roomId = false, dump = true){ // TODO: break into requestJoinRoom, and JoinRoom? // Maybe not needed here? As won't be done via backend? +// TODO: Need a 'leave room'/disconnect function requestJoinRoom(socket, playerName, roomId){ console.log('+ requestJoinRoom recieved'); - socket.playerName = playerName; - for (let i = 1; i <= numRooms; i++) { - let name = 'Room_' + i; - let people = roomData[i]['people']; + let room = roomData[roomId]; + + // Add socket for playerName so that players in room get + // responses, etc. from the room (something like that) + + // Add player to socket object, so it can be referred to + // as each socket is individual to a player, but rooms need to + // emit to multiple players. These .player can be used in loops - console.log('- people: '+people); - console.log('- maxPlayersPerRoom: '+maxPlayersPerRoom); + // https://socket.io/docs/v4/rooms/ + // https://stackoverflow.com/a/18096649 + socket.playerId = playerName; - if (true || people < maxPlayersPerRoom) { + if(room === undefined){ + io.to(socket.id).emit('responseRoom', 'error'); + console.log('>> Room does not exist'); + } + + let roomName = 'Room_' + roomId; + let people = room['people']; + // people = io.sockets.adapter.rooms[roomId].length; // This gets the sockets in the room (i.e. the players) + + //console.log(util.inspect(io.sockets.adapter.rooms, true, 4, true)) + //console.log('- people(socket: '+io.sockets.adapter.rooms[player]+')'); + //console.log('- maxPlayersPerRoom: '+maxPlayersPerRoom); + + if(isUserInRoom(playerName, roomId)){ + console.log('Already in room'); + return false; + } + + if (people < maxPlayersPerRoom) { - people = roomData[i]['people'] += 1; - socket.join(name); - console.log('>> User ' + socket.playerName + - ' connected on ' + name + ' (' + (people) + '/' + maxPlayersPerRoom + ')'); + // Update people in room count + people = room['people'] += 1; - io.to(socket.id).emit('responseJoinRoom', name); + // Add playerId to room (playerName for now while Ids don't exist TODO) + room['playerIds'][playerName] = playerName; + + // https://socket.io/docs/v4/rooms/ + // https://stackoverflow.com/a/25028953 + socket.join(roomId); + + // 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 numClients = clients ? clients.size : 0; + for (const clientId of clients) { + //this is the socket of each client in the room. + const clientSocket = io.sockets.sockets.get(clientId); + console.log(clientSocket.playerId); // The playerId set beggining of func. + } + */ - if (people >= maxPlayersPerRoom) { - console.log('- starting game'); - //startGame(name); - } + console.log('>> User ' + playerName + + ' connected on ' + roomName + ' (' + (people) + '/' + maxPlayersPerRoom + ')'); - return; + // Joined room (emit to the player that just joined) + io.to(socket.id).emit('responseJoinRoom', roomName); + if (people >= maxPlayersPerRoom) { + console.log('- starting game'); + // startGame for room + startGame(roomId); } + + } + +} + +// Will need to be different to playerName in future (in case dupes) +// would use playerId TODO +function isUserInRoom(playerName, roomId){ + if(playerName in roomData[roomId]['playerIds']){ + return true; + } + return false; +} + +function startGame(roomId){ + + console.log('>> Room: ' + roomId + ': Requesting game...'); + let people = roomData[roomId].players; + /* + try { + //people = io.sockets.adapter.rooms.get(roomId).size; + } catch (e) { + console.log('>> Room: ' + roomId + ': No people here...'); + return; + } + */ + + // For now, if there's 2 people only. Will need changing for + // 3v1, 5v1, 2v2, etc... + + let response = {success: false, message: 'Failed requestStartGame() server.js'}; + if(people < maxPlayersPerRoom){ + console.log('Too few people'); + } + + console.log('>> Room: ' + roomId + ': Starting'); + + + // 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); + + /* + for (const clientId of clients) { + //this is the socket of each client in the room. + const clientSocket = io.sockets.sockets.get(clientId); + console.log(clientSocket.playerId); // The playerId set in requestJoin + + // Just so I know how to access stuff in future. + console.log(roomData[roomId].playerIds[clientSocket.playerId] + 'is in the game'); } + */ + + // 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 => { + 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); + console.log('>> responseStartGame: '+clientSocket.playerId); + // Emit to client socket + io.to(clientSocket.id).emit('responseStartGame', response); + } + + }) + .catch(err => { + response.message = err; + // Each player then gets the error message + for (const clientId of clients) { + const clientSocket = io.sockets.sockets.get(clientId); + // Emit to client socket + io.to(clientSocket.id).emit('responseStartGame', err); + } + }); + +} - io.to(socket.id).emit('responseRoom', 'error'); - console.log('>> Rooms exceeded'); +// Then do functions like this? +function shuffleDeck(roomId, playerId){ }