From 16e307c32cf7d580f6ee18c3d52b4acbd6277687 Mon Sep 17 00:00:00 2001 From: Nathan Date: Wed, 30 Oct 2024 20:06:42 +0000 Subject: [PATCH] Add 'playerData' with socketId to roomMod --- components.js | 4 ++++ public/main.js | 5 +++++ roomMod.js | 41 ++++++++++++++++++++++++++++++----------- rooms.js | 2 +- 4 files changed, 40 insertions(+), 12 deletions(-) diff --git a/components.js b/components.js index c23847d..c1617b7 100644 --- a/components.js +++ b/components.js @@ -7,6 +7,10 @@ const component = { //item : [], //itemCount : 0, + roomId : null, + turn : 0, + playerTurn : 0, + // Card Stuff cardData : {}, cardFace : {}, diff --git a/public/main.js b/public/main.js index dbb0606..026e162 100644 --- a/public/main.js +++ b/public/main.js @@ -182,3 +182,8 @@ socket.on('responseStartGame', function (data) { loadBoard(data.message); }); +socket.on('alert', function (data) { + console.log('<< alert'); + alert(data.message); +}); + diff --git a/roomMod.js b/roomMod.js index a305e51..c126695 100644 --- a/roomMod.js +++ b/roomMod.js @@ -36,21 +36,37 @@ function setTeams(){ } -function roomGeneration(playerCount, teamCount = null, playerTeams = null){ +function roomGeneration(roomId){ return new Promise((resolve, reject) => { (async () => { + // Player's sockets + console.log('--- Room for generation ---'); + console.log(io.sockets.adapter.rooms.get(roomId)); + let itemData = startItemCount(); - // This will be passed by the joinRoom for each player, then built - // on startGame) - // Need to make it work if they're the same deck too - let playerData = [ - {'playerId': 1, 'deck':{'playerId':1,'deckId':1}}, - {'playerId': 2, 'deck':{'playerId':2,'deckId':1}}, - ]; + // Player data with Sockets + let playerData = []; + let i = 1; + let clients = global.io.sockets.adapter.rooms.get(roomId); + for (const clientId of clients) { + const clientSocket = global.io.sockets.sockets.get(clientId); + + playerData.push({ + 'playerId': clientSocket.playerId + ,'deck':{'playerId':i,'deckId':1} + ,'socketId': clientSocket.id // TODO: ONLY FOR SERVERSIDE!!! + }); + + i++; + } - // Add players for the room + // Test alert so that different data/emits can be sent per-player + global.io.to(playerData[0].socketId).emit('alert', {'message': 'roomMod.js test'}); + + + // Add players for the room (seperate to playerData, currently what's used) itemData = setPlayers(playerData, itemData); // TODO: Get their selected decks @@ -61,8 +77,11 @@ function roomGeneration(playerCount, teamCount = null, playerTeams = null){ // Generate the decks, and card within the deck cardLists [itemData] = await Promise.all([ cardGen.requestDeck(itemData) ]); - //console.log('deckData'); - //console.log(deckData); + // Some room stuff, likely change this + itemData.roomId = roomId; + itemData.turn = 0; // The turn count of the match + itemData.playersTurn = 0; // This means it's playerData[0] turn + return resolve(itemData); })() }); diff --git a/rooms.js b/rooms.js index 623d364..3233d76 100644 --- a/rooms.js +++ b/rooms.js @@ -7,7 +7,7 @@ let roomData = {}; // Variables for server overall let numRooms = 0; const maxRooms = 3; -const maxPlayersPerRoom = 1; +const maxPlayersPerRoom = 2; const maxSpectatorsPerRoom = 0; function requestRooms(socket, filter){