0

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);
    });

1 Answer 1

1

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 use 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.

Sign up to request clarification or add additional context in comments.

5 Comments

Is it possible to get the amount of players that have successfuly entered a username? If I can get that I am able to run the rest of my logic. As I need to see how many people have connected before a new game will start
You just add everyone to one game, if you want to reuse this code. Or you just increment a counter out of scope. Or you just map the games arrays to their lengths, and then reduce it. games.map(game => game.length).reduce((cumulative, gameLen) => gameLen + cumulative), or something like that.
If possible would you mind giving an example of a counter? Very new to utilizing this sort of data
While a counter is probably not the best idea, it is probably the simplest. You just say 'var userCounter = 0' in the same scope as 'games'. Then, inside your onnewuser socket function, you just type usercounter++. Just make sure to decrement it when a user disconnects...
Cool thank you will have to see if I can figure it out

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.