From 35330e7850e7a8bb7c8d884b062ed495ffa38ce5 Mon Sep 17 00:00:00 2001 From: Nathan Date: Wed, 30 Oct 2024 22:08:40 +0000 Subject: [PATCH] Add playerId/Order in a jamjarred kinda way --- roomMod.js | 10 +++++++++- rooms.js | 51 +++++++++++++++++++++++++++++---------------------- 2 files changed, 38 insertions(+), 23 deletions(-) diff --git a/roomMod.js b/roomMod.js index f2bba43..be7b19f 100644 --- a/roomMod.js +++ b/roomMod.js @@ -48,20 +48,27 @@ function roomGeneration(roomId){ // Player data with Sockets let playerData = []; + let playerOrder = {}; // Need a better name 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); + // Which order the player is? Just using the array + playerOrder[clientSocket.playerId] = playerData.length; + playerData.push({ - 'playerId': clientSocket.playerId + 'playerDataId': playerData.length + ,'playerId': clientSocket.playerId ,'deck':{'playerId':i,'deckId':1} ,'socketId': clientSocket.id // TODO: ONLY FOR SERVERSIDE!!! }); i++; + } + // Add players for the room (seperate to playerData, currently what's used) // ?? itemData = setPlayers(playerData, itemData); @@ -83,6 +90,7 @@ function roomGeneration(roomId){ // on server-side so it's easily accessible roomData[roomId].itemData = itemData; roomData[roomId].playerData = playerData; + roomData[roomId].playerOrder = playerOrder; // Return the all the itemData to the client(s) // TODO: This will need to give different data for each, or at least diff --git a/rooms.js b/rooms.js index 10e51e5..7f3bfa5 100644 --- a/rooms.js +++ b/rooms.js @@ -157,7 +157,7 @@ function isUserInRoom(playerName, roomId){ return false; } -function startGame(roomId){ +async function startGame(roomId){ console.log('>> Room: ' + roomId + ': Requesting game...'); let people = global.roomData[roomId].players; @@ -189,27 +189,34 @@ function startGame(roomId){ // 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(roomId).then(data => { - response.success = true; - response.message = data; - // Each player then gets the roomGeneration stuff - for (const clientId of clients) { - const clientSocket = global.io.sockets.sockets.get(clientId); - console.log('>> responseStartGame: '+clientSocket.playerId); - // Emit to client socket - global.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 = global.io.sockets.sockets.get(clientId); - // Emit to client socket - global.io.to(clientSocket.id).emit('responseStartGame', err); - } - }); + + // Not sure how to catch errors for these await alls + // TODO: Look into error handling for await alls + const [itemData] = + await Promise.all([ + roomMod.roomGeneration(roomId), + ]); + + + // data is the 'itemData' not all the roomData + response.success = true; + response.message = itemData; + + // Each player then gets sent the roomGeneration stuff + for (const clientId of clients) { + + const clientSocket = global.io.sockets.sockets.get(clientId); + console.log('>> responseStartGame: '+clientSocket.playerId); + + // TODO: TESTING STUFF, REMOVE WHEN SORTED + let message = 'You are player: '+roomData[roomId].playerOrder[clientSocket.playerId]; + global.socketAlert(clientSocket.id, message, 'alert'); + + // Emit the itemData to client socket (TODO: only emit what each player should see/recieve) + global.io.to(clientSocket.id).emit('responseStartGame', response); + + } + }