Can't enable rematch for socket.io game

Viewed 13

Github Link to project

I am adding features to a multiplayer snake Javascript game that I found on Youtube. The app uses socket.io to connect the players. I'm able to get the game to restart using a rematch button that appears after the game, but it appears that both players are not in the match. The games start separately from each other in each browser.

Using this rematch function, I get the room name and size, and ~I THINK~ restart the game with the same players. But clearly I'm missing something...

  function handleRematch() {
//current room
roomName = clientRooms[client.id];
console.log(roomName);
const room = io.sockets.adapter.rooms.get(roomName);
numClients = room.size;
console.log(numClients);
console.log(client.number);

if (numClients === 2) {
  state[roomName] = initGame();
  console.log(state[roomName]);
  startGameInterval(roomName);
  console.log('here');
}}

I also commented out the line in this function that nulls the game.. my thinking is that the state should be null AFTER the player presses the leave room button or else the rematch won't work.

function startGameInterval(roomName) {
  const intervalId = setInterval(() => {
    const winner = gameLoop(state[roomName]);
    console.log(winner);
    if (!winner) {
      emitGameState(roomName, state[roomName]);
    } else {
      emitGameOver(roomName, winner);
      //reset state of room because game has ended
      // state[roomName] = null;
      clearInterval(intervalId);
      console.log('game over');
    }
  }, 1000 / FRAME_RATE);
}

Can any brave coders help me out with this? I've been tearing my hair out for at least a week.

0 Answers
Related