<commit_before>import socketio from 'socket.io';

import server from './express-server.js';
import RoomManager from './room-manager.js';

// Socket.IO

const io = socketio(server);
const roomManager = new RoomManager();

// A wrapper around RoomManager.getRoom() to run the given callback if the
// specified room exists, otherwise responding with an error message if the room
// does not exist
function getRoom(callback) {
  return (options, fn) => {
    options.room = roomManager.getRoom(options.roomCode);
    if (options.room) {
      callback(options, fn);
    } else {
      console.log(`room ${options.roomCode} not found`);
      fn({ status: 'roomNotFound' });
    }
  };
}

io.on('connection', (socket) => {

  console.log(`connected: ${socket.id}`);

  // Room events

  socket.on('open-room', ({ player }, fn) => {
    console.log(`open room by player ${player.name}`);
    const room = roomManager.openRoom();
    const localPlayer = room.addPlayer({ player, socket });
    fn({
      status: 'waitingForPlayers',
      roomCode: room.code,
      game: room.game,
      localPlayer
    });
  });

  socket.on('join-room', getRoom(({ room, playerId }, fn) => {
    console.log(`join room by ${playerId}`);
    roomManager.markRoomAsActive(room);
    const localPlayer = room.connectPlayer({ playerId, socket });
    let status;
    if (localPlayer) {
      if (room.players.length === 1) {
        status = 'waitingForPlayers';
      } else if (room.game.pendingNewGame && localPlayer === room.game.requestingPlayer) {
        status = 'requestingNewGame';
      } else if (room.game.pendingNewGame && localPlayer !== room.game.requestingPlayer) {
        status = 'newGameRequested';
      } else {
        status = 'returningPlayer';
      }
      // If this join-room call represents a player reconnecting to the game
      // (where they were previously disconnected), inform the other player that
      // they have reconnected
      const otherPlayer = room.game.getOtherPlayer(localPlayer);
      if (otherPlayer && otherPlayer.socket) {
        delete localPlayer.lastDisconnectReason;
        otherPlayer.socket.emit('player-reconnected', {
          // If the game is still pending, make sure to stay in a pending state,
          // otherwise we can clear the status message
          status: room.game.pendingNewGame ? null : 'playerReconnected',
          localPlayer: otherPlayer
        });
      }
    } else if (room.players.length === 2) {
      // If both players are currently connected, all future connections
      // represent spectators
      status = 'watchingGame';
    } else {
      status = 'newPlayer';
    }
    fn({
      status,
      game: room.game,
      localPlayer
    });
  }));

  socket.on('close-room', getRoom(({ room }, fn) => {
    roomManager.closeRoom(room);
    fn({ status: 'closedRoom' });
  }));

  socket.on('decline-new-game', getRoom(({ playerId, room }, fn) => {
    console.log(`leave room by ${playerId}`);
    const localPlayer = room.getPlayerById(playerId);
    localPlayer.lastDisconnectReason = 'newGameDeclined';<commit_msg>Update log message to match event name<commit_after>import socketio from 'socket.io';

import server from './express-server.js';
import RoomManager from './room-manager.js';

// Socket.IO

const io = socketio(server);
const roomManager = new RoomManager();

// A wrapper around RoomManager.getRoom() to run the given callback if the
// specified room exists, otherwise responding with an error message if the room
// does not exist
function getRoom(callback) {
  return (options, fn) => {
    options.room = roomManager.getRoom(options.roomCode);
    if (options.room) {
      callback(options, fn);
    } else {
      console.log(`room ${options.roomCode} not found`);
      fn({ status: 'roomNotFound' });
    }
  };
}

io.on('connection', (socket) => {

  console.log(`connected: ${socket.id}`);

  // Room events

  socket.on('open-room', ({ player }, fn) => {
    console.log(`open room by player ${player.name}`);
    const room = roomManager.openRoom();
    const localPlayer = room.addPlayer({ player, socket });
    fn({
      status: 'waitingForPlayers',
      roomCode: room.code,
      game: room.game,
      localPlayer
    });
  });

  socket.on('join-room', getRoom(({ room, playerId }, fn) => {
    console.log(`join room by ${playerId}`);
    roomManager.markRoomAsActive(room);
    const localPlayer = room.connectPlayer({ playerId, socket });
    let status;
    if (localPlayer) {
      if (room.players.length === 1) {
        status = 'waitingForPlayers';
      } else if (room.game.pendingNewGame && localPlayer === room.game.requestingPlayer) {
        status = 'requestingNewGame';
      } else if (room.game.pendingNewGame && localPlayer !== room.game.requestingPlayer) {
        status = 'newGameRequested';
      } else {
        status = 'returningPlayer';
      }
      // If this join-room call represents a player reconnecting to the game
      // (where they were previously disconnected), inform the other player that
      // they have reconnected
      const otherPlayer = room.game.getOtherPlayer(localPlayer);
      if (otherPlayer && otherPlayer.socket) {
        delete localPlayer.lastDisconnectReason;
        otherPlayer.socket.emit('player-reconnected', {
          // If the game is still pending, make sure to stay in a pending state,
          // otherwise we can clear the status message
          status: room.game.pendingNewGame ? null : 'playerReconnected',
          localPlayer: otherPlayer
        });
      }
    } else if (room.players.length === 2) {
      // If both players are currently connected, all future connections
      // represent spectators
      status = 'watchingGame';
    } else {
      status = 'newPlayer';
    }
    fn({
      status,
      game: room.game,
      localPlayer
    });
  }));

  socket.on('close-room', getRoom(({ room }, fn) => {
    roomManager.closeRoom(room);
    fn({ status: 'closedRoom' });
  }));

  socket.on('decline-new-game', getRoom(({ playerId, room }, fn) => {
    console.log(`decline new game by ${playerId}`);
    const localPlayer = room.getPlayerById(playerId);
    localPlayer.lastDisconnectReason = 'newGameDeclined';
{'fraction_non_alphanumeric': 0.1341678703257904, 'fraction_numerical': 0.0006419515326592842, 'mean_word_length': 2.9096612296110416, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '723803', 'n_tokens_mistral': 1789, 'n_tokens_neox': 1778, 'n_words': 492}