<commit_before>const socketIo = require('socket.io');
const YellowSubsAction = require('../AI/yellowsub_ai_v2');
const Chess = require('../../client/src/components/AI/chess').Chess;

const getUniqeId = () => new Date().getTime();

const deepSubs = {
  img_url: 'http://yellowsubmarineswimschool.co.uk/wp-content/uploads/2016/12/Yellow-Submarine-Swim-School-Logo-transparent-windows-2.png',
  username: 'Yellow Submarine',
  message: '',
  id: -1,
  msgId: null,
  time: '',
};

const game = new Chess();

let numRounds = 0;

module.exports = (server) => {
  const io = socketIo(server);
  io.on('connect', (socket) => {
    // socket.on('join-room', (roomId) => {
    //   user.roomId = roomId;
    //   socket.join(user.roomId);
    // });

    socket.on('new-user', (username, roomId) => {
      const socketRooms = Object.keys(io.sockets.adapter.sids[socket.id]);
      const currentRoom = socketRooms[socketRooms.length - 1];
      if (socketRooms.length > 1) {
        socket.leave(currentRoom);
      }
      socket.join(roomId);
      deepSubs.msgId = getUniqeId();
      deepSubs.message = `${username} has joined the room!`;
      socket.broadcast.to(roomId).emit('receive-msg', deepSubs);
      deepSubs.message = `Hello ${username}! This is your room ID: ${roomId}`;
      io.to(socket.id).emit('receive-msg', deepSubs);
    });

    socket.on('AI', (boardState, isStart) => {
      const socketRooms = Object.keys(io.sockets.adapter.sids[socket.id]);
      const currentRoom = socketRooms[socketRooms.length - 1];
      if (isStart) {
        game.reset();
        numRounds = 0;
      }
      if (boardState) {
        game.load(boardState);
        socket.broadcast.to(currentRoom).emit('board-update', boardState, false);
      }

      // deepSubs move
      if (game.turn() === 'b') {
        const bestMove = YellowSubsAction(3, game, true, numRounds);
        game.move(bestMove);
        const aiBoard = game.fen();
        if (aiBoard) {
          numRounds += 1;
        }
        let status = 'normal';
        if (game.in_check()) {
          status = 'check';
        } else if (game.in_checkmate() || game.move() === []) {
          status = 'check_mate';
        }
        io.in(currentRoom).emit('board-update', aiBoard, status);
      }
    });

    socket.on('board-update', (newBoard, side) => {
      const socketRooms = Object.keys(io.sockets.adapter.sids[socket.id]);
      const currentRoom = socketRooms[socketRooms.length - 1];
      socket.broadcast.to(currentRoom).emit('board-update', newBoard, side);
    });

    socket.on('send-msg', (newMsg) => {
      const socketRooms = Object.keys(io.sockets.adapter.sids[socket.id]);
      const currentRoom = socketRooms[socketRooms.length - 1];
      io.to(currentRoom).emit('receive-msg', newMsg);<commit_msg>reducing searches<commit_after>const socketIo = require('socket.io');
const YellowSubsAction = require('../AI/yellowsub_ai_v2');
const Chess = require('../../client/src/components/AI/chess').Chess;

const getUniqeId = () => new Date().getTime();

const deepSubs = {
  img_url: 'http://yellowsubmarineswimschool.co.uk/wp-content/uploads/2016/12/Yellow-Submarine-Swim-School-Logo-transparent-windows-2.png',
  username: 'Yellow Submarine',
  message: '',
  id: -1,
  msgId: null,
  time: '',
};

const game = new Chess();

let numRounds = 0;

module.exports = (server) => {
  const io = socketIo(server);
  io.on('connect', (socket) => {
    // socket.on('join-room', (roomId) => {
    //   user.roomId = roomId;
    //   socket.join(user.roomId);
    // });

    socket.on('new-user', (username, roomId) => {
      const socketRooms = Object.keys(io.sockets.adapter.sids[socket.id]);
      const currentRoom = socketRooms[socketRooms.length - 1];
      if (socketRooms.length > 1) {
        socket.leave(currentRoom);
      }
      socket.join(roomId);
      deepSubs.msgId = getUniqeId();
      deepSubs.message = `${username} has joined the room!`;
      socket.broadcast.to(roomId).emit('receive-msg', deepSubs);
      deepSubs.message = `Hello ${username}! This is your room ID: ${roomId}`;
      io.to(socket.id).emit('receive-msg', deepSubs);
    });

    socket.on('AI', (boardState, isStart) => {
      const socketRooms = Object.keys(io.sockets.adapter.sids[socket.id]);
      const currentRoom = socketRooms[socketRooms.length - 1];
      if (isStart) {
        game.reset();
        numRounds = 0;
      }
      if (boardState) {
        game.load(boardState);
        socket.broadcast.to(currentRoom).emit('board-update', boardState, false);
      }

      // deepSubs move
      if (game.turn() === 'b') {
        const bestMove = YellowSubsAction(2, game, true, numRounds);
        game.move(bestMove);
        const aiBoard = game.fen();
        if (aiBoard) {
          numRounds += 1;
        }
        let status = 'normal';
        if (game.in_check()) {
          status = 'check';
        } else if (game.in_checkmate() || game.move() === []) {
          status = 'check_mate';
        }
        io.in(currentRoom).emit('board-update', aiBoard, status);
      }
    });

    socket.on('board-update', (newBoard, side) => {
      const socketRooms = Object.keys(io.sockets.adapter.sids[socket.id]);
      const currentRoom = socketRooms[socketRooms.length - 1];
      socket.broadcast.to(currentRoom).emit('board-update', newBoard, side);
    });

    socket.on('send-msg', (newMsg) => {
      const socketRooms = Object.keys(io.sockets.adapter.sids[socket.id]);
      const currentRoom = socketRooms[socketRooms.length - 1];
      io.to(currentRoom).emit('receive-msg', newMsg);
{'fraction_non_alphanumeric': 0.17060085836909872, 'fraction_numerical': 0.006437768240343348, 'mean_word_length': 2.784167794316644, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, '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': '6869898', 'n_tokens_mistral': 1912, 'n_tokens_neox': 1866, 'n_words': 322}