factor_za factor_za - 5 months ago 32
Javascript Question

Socket.io and multi dimensional arrays?

I am currently maing a simple game where a user first connects enters a user name and then it is stored away.

So the question is I have one multi dimensional array. But no when a second player connects how will it know? Do I need to create two of them? And if so how does it know where to push it?

As I will need to then find the .length so I can see there are two users.

var socketStorage = {};
// ===============================================
// GAME LOGIC
// ===============================================
socketStorage[socket.id] = {};

socketStorage[socket.id] = {
username : "username",
socket : socket,
currQuestion : {
answered : false,
answer : false
},
allQuestionsAnswers : []
};

socket.on("setusername", function(msg){
socketStorage[socket.id].username = msg.username;
console.log(socketStorage[socket.id].username);
});

Answer

There are no multi dimensional arrays in your example. You also haven't explained in the game--does everyone play together at the same time, or are there different games which can each contain their own players, isolated from each other?

Here's an example way to handle this: I haven't used sockets in a while, so I'm using your code and my rough memory of how it's handled--correct any mistakes as necessary.

var games = []
var socketStorage = {};
// ===============================================
// GAME LOGIC
// ===============================================
// socketStorage[socket.id] = {}; // <- this line is unnecessary.

socketStorage[socket.id] = {
    username : "username",     
    socket : socket,      
    currQuestion : {   
        answered : false,
        answer : false 
    },
    allQuestionsAnswers : [] 
}; 

socket.on("setusername", function(msg){
  socketStorage[socket.id].username = msg.username;
  console.log(socketStorage[socket.id].username);
});

socket.on("newgame", function(msg){
  newGameId = games.length;
  games.push([]);
  games[gameId].push(socketStorage[socket.id]);
  socket.emit("newgamecreated", { gameId: newGameId });
});

socket.on("joingame", function(msg){
  var gameId = msg.gameNumber;
  games[gameId].push(socketStorage[socket.id]);
});

socket.on("getgames", function(msg){
  gamesToUsers = {};
  games.forEach(function(game, index) {
    gamesToUsers[index] = game.map(function(socket) {
      return socket.username
    });
  });
  socket.emit("currentgames", gamesToUsers);
});

This is incomplete, and incredibly basic, and likely has several errors, but should give you enough to understand the concept of how to go about this. Whenever someone does something in a game, you'll just want to user a loop to socket.emit the new information to everyone who is in the same game. so,

socket.on("somethinghappenedingame", function(msg){
  game = msg.gameNumber;
  update = msg.update;
  games[game].forEach(function(socketObj){
    socketObj.socket.emit("gameUpdate", update);
  });
});

Hope that helps.