<commit_before>/**
 * Emerging Citizens
 * Developed by Engagement Lab, 2016
 * ==============
 * PlayerLogin submission socket handler.
 *
 * @class sockets/handlers
 * @static
 * @author <NAME>
 *
 * ==========
 */


var appRoot = require('app-root-path'),
    logger = require('winston'),
    colors = require('colors'),

    TemplateLoader = require(appRoot + '/lib/TemplateLoader'),
    Session = require(appRoot + '/lib/SessionManager');
    Common = require(appRoot + '/lib/games/Common');

var PlayerLogin = function (nsp, socket, emitter) {

  var Templates = new TemplateLoader();

  var currentSpace;
  var currentSocket;

  var playerGameId;

  currentSpace = nsp;
  currentSocket = socket;

  // Expose handler methods for events
  this.handler = {

    room: function(package) {
      console.log (package, "package");
      if(package.gameId === null) {
        console.warn('gameId missing for socket ID "' + currentSocket.id + '"!');
        return;
      }

      // If '-group' specified as room affix, remove for game id
      if(package.gameId.indexOf('-group') !== -1)
          playerGameId = package.gameId.replace('-group', '');
      else {
        playerGameId = package.gameId;
  
        if(!Session.Get(playerGameId)) {
          currentSocket.emit('game:notfound');
          return;
        }
      }
  
      currentSocket.join(package.gameId, function(err) {

        if(err)
          throw err;
      });

      if(package.msgData == 'moderator' && Session.Get(playerGameId)) {

        Session.GroupView(package.gameId, currentSocket.id);
        Session.Get(playerGameId).ModeratorJoin(currentSpace);
      }


        
      logger.info(currentSocket.id + ' connected to room.');

    },
    
    'login:submit': function(package) {

      var player = {socket_id: currentSocket.id, username: package.msgData.username, uid: package.msgData.uid};

      // Mark player as ready inside game session
      Session.Get(package.gameId).PlayerReady(player, currentSpace);

      logger.info(player.username  + ' logged in.');
      
      // Advance player to waiting screen
      Templates.Load('partials/player/waiting', undefined, function(html) {
        var data = {
                    id: currentSocket.id,
                    html: html,
                    gameType: Session.Get(package.gameId).GetGameType()
                   };

        currentSocket.emit('player:loggedin', data);
      });

      // Send player's id (debugging)
      // currentSocket.emit('player:id', );
      
    },
    
    'login:active': function(package) {

      if(!Session.Get(package.gameId))
        return;

      logger.info('login:active', 'Checking if player "' + package.uid + '" is active.');

      // console.log(Session.Get(package.gameId));

      // See if this player is still marked as active inside game session
      if(Session.Get(package.gameId).PlayerIsActive(package.uid)) {

        var player = {socket_id: currentSocket.id, username: package.username, uid: package.uid};

        // Set client to reconnected state
        currentSocket.emit('player:reconnected', true);

        // Mark player as ready inside game session
        Session.Get(package.gameId).PlayerReady(player, currentSocket);

      }
      else {
        logger.info('login:active', 'Player "' + package.uid + '" not active.');
      }
      
    },


    disconnect: function(package) {

      var session = Session.Get(playerGameId);

      if(!session)
        return;

      var isGroup = (currentSocket.id === session.groupModerator);

      if(isGroup)
        logger.info(playerGameId + " group view disconnecting. Bu-bye.");

      else {

        var player = session.GetPlayerById(currentSocket.id);

        if(player)
          logger.info("Player '" + player.username + "' disconnecting. Nooooo!");

      }

      if(playerGameId && session) {

        if(currentSocket.id === session.groupModerator) {
          logger.debug('is group moderator');
          session.End(currentSpace, true);
        }

      }
    }
  
  };

  logger.info("New PlayerLogin for socket: ".green + currentSocket.id);

};
module.exports = PlayerLogin;<commit_msg>moderator disconnect delete session<commit_after>/**
 * Emerging Citizens
 * Developed by Engagement Lab, 2016
 * ==============
 * PlayerLogin submission socket handler.
 *
 * @class sockets/handlers
 * @static
 * @author <NAME>
 *
 * ==========
 */


var appRoot = require('app-root-path'),
    logger = require('winston'),
    colors = require('colors'),

    TemplateLoader = require(appRoot + '/lib/TemplateLoader'),
    Session = require(appRoot + '/lib/SessionManager');
    Common = require(appRoot + '/lib/games/Common');

var PlayerLogin = function (nsp, socket, emitter) {

  var Templates = new TemplateLoader();

  var currentSpace;
  var currentSocket;

  var playerGameId;

  currentSpace = nsp;
  currentSocket = socket;

  // Expose handler methods for events
  this.handler = {

    room: function(package) {
      console.log (package, "package");
      if(package.gameId === null) {
        console.warn('gameId missing for socket ID "' + currentSocket.id + '"!');
        return;
      }

      // If '-group' specified as room affix, remove for game id
      if(package.gameId.indexOf('-group') !== -1)
          playerGameId = package.gameId.replace('-group', '');
      else {
        playerGameId = package.gameId;
  
        if(!Session.Get(playerGameId)) {
          currentSocket.emit('game:notfound');
          return;
        }
      }
  
      currentSocket.join(package.gameId, function(err) {

        if(err)
          throw err;
      });

      if(package.msgData == 'moderator' && Session.Get(playerGameId)) {

        Session.GroupView(package.gameId, currentSocket.id);
        Session.Get(playerGameId).ModeratorJoin(currentSpace);
      }


        
      logger.info(currentSocket.id + ' connected to room.');

    },
    
    'login:submit': function(package) {

      var player = {socket_id: currentSocket.id, username: package.msgData.username, uid: package.msgData.uid};

      // Mark player as ready inside game session
      Session.Get(package.gameId).PlayerReady(player, currentSpace);

      logger.info(player.username  + ' logged in.');
      
      // Advance player to waiting screen
      Templates.Load('partials/player/waiting', undefined, function(html) {
        var data = {
                    id: currentSocket.id,
                    html: html,
                    gameType: Session.Get(package.gameId).GetGameType()
                   };

        currentSocket.emit('player:loggedin', data);
      });

      // Send player's id (debugging)
      // currentSocket.emit('player:id', );
      
    },
    
    'login:active': function(package) {

      if(!Session.Get(package.gameId))
        return;

      logger.info('login:active', 'Checking if player "' + package.uid + '" is active.');

      // console.log(Session.Get(package.gameId));

      // See if this player is still marked as active inside game session
      if(Session.Get(package.gameId).PlayerIsActive(package.uid)) {

        var player = {socket_id: currentSocket.id, username: package.username, uid: package.uid};

        // Set client to reconnected state
        currentSocket.emit('player:reconnected', true);

        // Mark player as ready inside game session
        Session.Get(package.gameId).PlayerReady(player, currentSocket);

      }
      else {
        logger.info('login:active', 'Player "' + package.uid + '" not active.');
      }
      
    },


    disconnect: function(package) {

      var session = Session.Get(playerGameId);

      if(!session)
        return;

      var isGroup = (currentSocket.id === session.groupModerator);

      if(isGroup) {
        logger.info(playerGameId + " group view disconnecting. Bu-bye.");

      } else {

        var player = session.GetPlayerById(currentSocket.id);

        if(player)
          logger.info("Player '" + player.username + "' disconnecting. Nooooo!");

      }

      if(playerGameId && session) {

        if(currentSocket.id === session.groupModerator) {
          logger.debug('is group moderator');
          session.Delete(currentSpace, true);
        }

      }
    }
  
  };

  logger.info("New PlayerLogin for socket: ".green + currentSocket.id);

};
module.exports = PlayerLogin;
{'fraction_non_alphanumeric': 0.1367052367887391, 'fraction_numerical': 0.001192890373374687, 'mean_word_length': 2.427636958299264, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4489233', 'n_tokens_mistral': 2455, 'n_tokens_neox': 2334, 'n_words': 504}