Add playerId/Order in a jamjarred kinda way

develop^2
Nathan Steel 1 year ago
parent ea7c9b8596
commit 35330e7850

@ -48,20 +48,27 @@ function roomGeneration(roomId){
// Player data with Sockets // Player data with Sockets
let playerData = []; let playerData = [];
let playerOrder = {}; // Need a better name
let i = 1; let i = 1;
let clients = global.io.sockets.adapter.rooms.get(roomId); let clients = global.io.sockets.adapter.rooms.get(roomId);
for (const clientId of clients) { for (const clientId of clients) {
const clientSocket = global.io.sockets.sockets.get(clientId); const clientSocket = global.io.sockets.sockets.get(clientId);
// Which order the player is? Just using the array
playerOrder[clientSocket.playerId] = playerData.length;
playerData.push({ playerData.push({
'playerId': clientSocket.playerId 'playerDataId': playerData.length
,'playerId': clientSocket.playerId
,'deck':{'playerId':i,'deckId':1} ,'deck':{'playerId':i,'deckId':1}
,'socketId': clientSocket.id // TODO: ONLY FOR SERVERSIDE!!! ,'socketId': clientSocket.id // TODO: ONLY FOR SERVERSIDE!!!
}); });
i++; i++;
} }
// Add players for the room (seperate to playerData, currently what's used) // Add players for the room (seperate to playerData, currently what's used)
// ?? // ??
itemData = setPlayers(playerData, itemData); itemData = setPlayers(playerData, itemData);
@ -83,6 +90,7 @@ function roomGeneration(roomId){
// on server-side so it's easily accessible // on server-side so it's easily accessible
roomData[roomId].itemData = itemData; roomData[roomId].itemData = itemData;
roomData[roomId].playerData = playerData; roomData[roomId].playerData = playerData;
roomData[roomId].playerOrder = playerOrder;
// Return the all the itemData to the client(s) // Return the all the itemData to the client(s)
// TODO: This will need to give different data for each, or at least // TODO: This will need to give different data for each, or at least

@ -157,7 +157,7 @@ function isUserInRoom(playerName, roomId){
return false; return false;
} }
function startGame(roomId){ async function startGame(roomId){
console.log('>> Room: ' + roomId + ': Requesting game...'); console.log('>> Room: ' + roomId + ': Requesting game...');
let people = global.roomData[roomId].players; let people = global.roomData[roomId].players;
@ -189,27 +189,34 @@ function startGame(roomId){
// 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(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);
}
}) // Not sure how to catch errors for these await alls
.catch(err => { // TODO: Look into error handling for await alls
response.message = err; const [itemData] =
// Each player then gets the error message await Promise.all([
for (const clientId of clients) { roomMod.roomGeneration(roomId),
const clientSocket = global.io.sockets.sockets.get(clientId); ]);
// Emit to client socket
global.io.to(clientSocket.id).emit('responseStartGame', err);
} // 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);
}
} }

Loading…
Cancel
Save