0

I am working on a multiplayer game and I have a room array with the following layout (I added comments for better understanding):

Room_Array
[
    [
        "Room_0",   // room name
        10,         // max people
        "Random",   // room type
        [           // now arrays of players follow
            [
               1,     // ShortID
               123,   // position X
               234,   // position Y
               10     // angle
            ],
            [
               2,
               123,
               234,
               10
            ],
            [
               3,
               123,
               234,
               10
            ],
        ]
    ]
    // here other rooms are created with the same layout as Room_0 when the max people is reached
]

How would I go around to remove the whole array of the player with ShortID = 2? in case he disconnects?

So the desired result would be:

Room_Array
[
    [
        "Room_0",   // room name
        10,         // max people
        "Random",   // room type
        [           // now arrays of players follow
            [
               1,     // ShortID
               123,   // position X
               234,   // position Y
               10     // angle
            ],
            [
               3,
               123,
               234,
               10
            ],
        ]
    ]
]

I tried the following code and in the console log it showed me the elements of the array I need to splice, so 2, 123, 234, 10. The commented splice resulted in error unidentified element 1.

for (var i = 0; i < Room_Array.length; i++)
{
    if (Room_Array[i][0] === PlayerObject[socket.id].RoomName)
    {
        for (var j = 0; j < Room_Array[i][3].length; j++)
        {
            if (Room_Array[i][3][j][0] === PlayerObject[socket.id].ShortID)
            {
                console.log("Array to splice: " + Room_Array[i][3][j]);
                //Room_Array.splice([i][3][j], 1); // error unidentified 1

            }
        }


    break;
    }
}
2
  • 1
    Is there a reason you've gone for this array structure rather than something like a json object? It would make this a lot simpler for you. Commented Mar 28, 2020 at 13:10
  • @PaulRyan I usually use json to store data, the arrays are needed as a lot of operations are done on them very fast. I'm not 100% of the performance difference and the project is very complex right now to change anything in that regard. Commented Mar 28, 2020 at 15:36

3 Answers 3

1

Here is a working solution that mutates the initial array.

const Room_Array = [
    [
        "Room_0",   // room name
        10,         // max people
        "Random",   // room type
        [           // now arrays of players follow
            [
               1,     // ShortID
               123,   // position X
               234,   // position Y
               10     // angle
            ],
            [
               2,     // ShortID
               123,   // position X
               234,   // position Y
               10     // angle
            ],
            [
               3,
               123,
               234,
               10
            ],
        ]
    ]
];
    
function removeUser (array, id) {
  array.forEach(room => {
    const [roomName, maxPeople, roomType, players] = room;
    const index = players.findIndex(([shortId]) => shortId === id);
    if(index > -1) {
      players.splice(index, 1);
    }
  });
}

removeUser(Room_Array, 2);

console.log(Room_Array);
    

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

4 Comments

Thanks a lot for the help! It works great with one room, however once there are more it cuts the same index from all the subarrays. I've fixed it but I'm afraid it's a bad patch. If you'd like to see you can check this fiddle: jsfiddle.net/jefawk/f8epo0ny TLDR: added if (index > 0) { players.splice(index, 1); }
It's not a bad patch, pretty good solution as a condition for "the player was not found"
if(index > -1) is correct, i have no idea why i hated on 0
I didn't notice you wrote if (index > 0) the correct condition is if (index > -1) since 0 is a valid value for an array index
0

Using foreach modifying the existing array

let Room_Array=[["Room_0", 10, "Random",[[ 1,123,234,10],[2,123,234,10],[3,123,234, 10]],]];
function remove(id){
Room_Array.forEach( room => room[3].splice(room[3].findIndex( rm=>rm[0]==id),1))
};remove(2);
console.log(Room_Array);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Using map returning a new array

let Room_Array=[["Room_0", 10, "Random",[[ 1,123,234,10],[2,123,234,10],[3,123,234, 10]],]];
function remove(id){
return Room_Array.map( room => {room[3].splice(room[3].findIndex( rm=>rm[0]==id),1);return room;})
}
console.log(remove(2));
.as-console-wrapper { max-height: 100% !important; top: 0; }

1 Comment

Thanks for the help! Just as Guerric P if there are more rooms the element index disappears from all the subarrays. I took his code and checked the index against -1 and then removed it, probably could have been done on what you posted too. Thanks a lot again though :) !
0

If you want to go down the JSON route instead, which I highly recommend, here's a working example:

const Room_Array = 
[
    {
        roomName: "Room_0",
        maxPeope: 10,
        roomType: "Random",
        players: [
            {
               shortID: 1,
               xPosition: 123,
               yPosition: 234, 
               angle: 10
            },
            {
               shortID: 2,
               xPosition: 123,
               yPosition: 234, 
               angle: 10
            },
            {
               shortID: 3,
               xPosition: 123,
               yPosition: 234, 
               angle: 10
            },
        ]
    }
];

function removeUser(id)
{
  Room_Array.forEach((room) => 
  {
    room.players = room.players.filter(player => player.shortID !== id);
  });
  console.log(Room_Array);
}

removeUser(1);

Comments

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.