<commit_before>var express = require('express');
var app = express();
var server = require('http').Server(app);
var io = require('socket.io')(server);
var session = require('express-session')({
  secret: 'my-secret',
  resave: true,
  saveUninitialized: true
});
var sharedSession = require('express-socket.io-session');
var Game = require('./server/game.js');
var spec = require('./server/deckSpec');
var game, readyCount = 0;

app.set('port', (process.env.PORT || 3000));

app.use(express.static(__dirname + '/client'));

// Attach session
app.use(session);

// Share session with io sockets
io.use(sharedSession(session));

// object holds player username, socket id, and password
var players = {};

io.on('connection', function(socket) {
  // Accept a login event with user's data
  socket.on('login', function(userdata) {
    socket.handshake.session.userdata = userdata;
    socket.handshake.session.save();

    if (players[userdata.username]){
      // check for password match
      if (userdata.password === players[userdata.username].password) {
        // Move on with valid login stuff...
        console.log("Welcome back!");
        players[userdata.username].socket = socket.id;
      } else {
        // Invalid login handling...
        console.log('Wrong password!');
      }
    } else {
      // If player is logging in for the first time, register
      // their info on the players object
      players[ userdata.username ] = { 
        password: <PASSWORD>,
        color: userdata.color, 
        numMeeps: 7, 
        socket: socket.id,
        ready: false
      };
      console.log(players);
    }
    io.emit('numReady', {ready: readyCount, total: Object.keys(players).length});
  });

  socket.on('logout', function(userdata) {
    if (socket.handshake.session.userdata) {
      delete socket.handshake.session.userdata;
      // Save the data to the session store
      socket.handshake.session.save();
        io.emit('numReady', {ready: readyCount, total: Object.keys(players).length});

    }
  });

  socket.on('endTurn', function(data) { // data will be the tile they placed
   // verify that player ending turn, is actually the current player
    var name = game.players[ game.currentPlayer ];

    if (players[name].socket === socket.id) {
      var gameState = game.update(data); // return gamestate

      // makes 'nextPlayer' the socket id; client needs to check this
      // to decide if it is their turn;
      gameState.nextPlayer = players[ gameState.nextPlayer ].socket;

      // emit next turn to all connected sockets
      io.emit('nextTurn', gameState);

    } else {
      console.log("Not your turn!");
    }
  });

  // indicates that all parties are ready to play
  // potentially refactor to need 'ready' from all connected players
  socket.on('playerReady', function(data){

    // on 'playersRead' emission the client needs to send their username;
    var username = data;

    // so if somehow the wrong username gets sent it won't crash the thing
    if (players[username] === undefined || players[username].ready){
      return;
    }

    // mark each player as ready as they emit this. 
    // when all players are ready emit an 'all players ready'

    players[ username ].ready = true;
    readyCount++;

    var allReady = true;
    for(var player in players){
      allReady = allReady && players[player].ready;
    }

    if (allReady) {
      // Makes sure there are between 2 to 5 players logged in
      // if there are, emit that, create the game, emit the first turn stuff;
      if (Object.keys(players).length >= 2 && Object.keys(players).length <= 5) {
        io.emit('allReady', {});
        game = new Game(13, spec, players);
        var gameState = game.initialState();
        gameState.nextPlayer = players[ gameState.nextPlayer ].socket;
        io.emit('nextTurn', gameState);
      } else {
        console.log('Invalid number of players: ', Object.keys(players).length);
      }
    } else {
      // if everyone isn't ready emit the num that are
      io.emit('numReady', {ready: readyCount, total: Object.keys(players).length});
    }
  });

  // send num of meeps and meep color to client side
  socket.on('meepDataReq', function(data) {
    // client side sends current username
    if (data.numMeeps) {
      players[data.username].numMeeps = data.numMeeps;
      socket.emit('meepDataRes', { numMeeps: data.numMeeps });
    } else {
      socket.emit('meepDataRes', { numMeeps: players[data.username].numMeeps });
    }
    // var meepColor = players[data.username].color;
  });
});

server.listen(app.get('port'), function() {
  console.log('Node app is running on port', app.get('port'));
});

module.exports = app;<commit_msg>make comments relevant to current code state reject logins for more than 5 users make sure 'numReady' is emitting when it needs to<commit_after>var express = require('express');
var app = express();
var server = require('http').Server(app);
var io = require('socket.io')(server);
var session = require('express-session')({
  secret: 'my-secret',
  resave: true,
  saveUninitialized: true
});
var sharedSession = require('express-socket.io-session');
var Game = require('./server/game.js');
var spec = require('./server/deckSpec');
var game, readyCount = 0;

app.set('port', (process.env.PORT || 3000));
app.use(express.static(__dirname + '/client'));
app.use(session);
io.use(sharedSession(session));

// object holds player username, socket id, and password
var players = {};

io.on('connection', function(socket) {
  // Accept a login event with user's data
  socket.on('login', function(userdata) {
    socket.handshake.session.userdata = userdata;
    socket.handshake.session.save();

    // if player already exists in players obj we want to log them back in and not
    // create a new user or overwrite their existing stuff other than socket id
    // currently useless as the client does not support revival of the lost game state
    if (players[userdata.username]){
      if (userdata.password === players[userdata.username].password) {
        players[userdata.username].socket = socket.id;
      } else {
        // TODO: Invalid login handling...
        console.log('Wrong password!');
      }
    } else {
      // If player is logging in for the first time, register
      // their info on the players object
      // only do this if the lobby isn't full
      if (Object.keys(players).length < 5) {
        players[ userdata.username ] = { 
          password: <PASSWORD>,
          color: userdata.color, 
          numMeeps: 7, 
          socket: socket.id,
          ready: false
        };
      } else {
        // TODO: alert that the game is full
        console.log('game full');
      }
      console.log(players);
    }
    // emit numready whenever there is a new connection
    // used to display number of users connected and ready to play on the home page
    io.emit('numReady', {ready: readyCount, total: Object.keys(players).length});
  });

  // currently unused on the client side
  socket.on('logout', function(userdata) {
    if (socket.handshake.session.userdata) {
      delete socket.handshake.session.userdata;
      socket.handshake.session.save();
      io.emit('numReady', {ready: readyCount, total: Object.keys(players).length});
    }
  });

  // data will be the tile they placed
  socket.on('endTurn', function(data) { 
   // verify that player ending turn, is actually the current player
   // there might be a better way to do this using the session
    var name = game.players[ game.currentPlayer ];

    if (players[ name ].socket === socket.id) {
      // this updates the server side game object with all of the info from the
      // player move. will be useful for implementing client reconnections
      var gameState = game.update(data);

      // makes 'nextPlayer' the socket id; client needs to check this
      // to decide if it is their turn;
      gameState.nextPlayer = players[ gameState.nextPlayer ].socket;

      // emit next turn to all connected sockets
      io.emit('nextTurn', gameState);
    }
      // TODO: (maybe) alert the client that they are trying to end their turn
      // when it isn't their turn
  });

  // indicates that parties are ready to play
  socket.on('playerReady', function(data){

    // on 'playersReady' emission the client needs to send their username;
    // maybe refactor to also require the socket.id so that user A can't ready 
    // user B through the console
    var username = data;

    // prevent nonexistent username from doing stuff
    // and it won't do stuff if the player is already 'ready'
    if (players[ username ] === undefined || players[username].ready){
      return;
    }

    players[ username ].ready = true;
    readyCount++;
    var allReady = true;
    
    for(var player in players){
      allReady = allReady && players[player].ready;
    }

    if (allReady) {
      // Makes sure there are between 2 to 5 players logged in
      // if there are, emit that, create the game, emit the first turn stuff;
      if (Object.keys(players).length >= 2 && Object.keys(players).length <= 5) {
        io.emit('allReady', {});
        game = new Game(13, spec, players);
        var gameState = game.initialState();
        gameState.nextPlayer = players[ gameState.nextPlayer ].socket;
        io.emit('nextTurn', gameState);
      } else {
        // TODO: alert that there are not enough people to play the game
        //   also be too many if the untested code to block too many logins fails
        console.log('Invalid number of players: ', Object.keys(players).length);
      }
    }
    // emit numReady every time someone readys up 
    io.emit('numReady', {ready: readyCount, total: Object.keys(players).length});
  });

  // send num of meeps and meep color to client side
  socket.on('meepDataReq', function(data) {
    // client side sends current username
    if (data.numMeeps) {
      players[ data.username ].numMeeps = data.numMeeps;
      socket.emit('meepDataRes', { numMeeps: data.numMeeps });
    } else {
      socket.emit('meepDataRes', { numMeeps: players[ data.username ].numMeeps });
    }
    // var meepColor = players[data.username].color;
  });
});

server.listen(app.get('port'), function() {
  console.log('Node app is running on port', app.get('port'));
});

module.exports = app;
{'fraction_non_alphanumeric': 0.12622950819672132, 'fraction_numerical': 0.002507232401157184, 'mean_word_length': 2.8582589285714284, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 7, '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': '1572750', 'n_tokens_mistral': 2993, 'n_tokens_neox': 2922, 'n_words': 977}