<commit_before>/*  Copyright (c) 2012 Sven "FuzzYspo0N" Bergström, 2013 <NAME>

    originally written for: http://buildnewgames.com/real-time-multiplayer/
    
    substantially modified for collective behavior experiments 

    MIT Licensed.
*/

var 
    use_https       = true,
    gameport        = 8888,
    https           = require('https'),
    fs              = require('fs'),
    app             = require('express')(),
    _               = require('underscore');

try {
  var privateKey  = fs.readFileSync('/etc/apache2/ssl/private.key'),
      certificate = fs.readFileSync('/etc/apache2/ssl/ssl.crt'),
      options     = {key: privateKey, cert: certificate},
      server      = require('https').createServer(options,app).listen(gameport),
      io          = require('socket.io')(server);
} catch (err) {
  console.log("cannot find SSL certificates; falling back to http");
  var server      = app.listen(gameport),
      io          = require('socket.io')(server);
}

if (process.argv[2]) {
  var exp = process.argv[2];
  var gameServer = require('./sharedUtils/serverBase.js')(exp);  
} else {
  throw new("no experiment supplied");
}

var utils = require('./sharedUtils/sharedUtils.js');

var global_player_set = {};

// Log something so we know that server-side setup succeeded
console.log("info  - socket.io started");
console.log('\t :: Express :: Listening on port ' + gameport );

//  This handler will listen for requests on /*, any file from the
//  root of our server. See expressjs documentation for more info 
app.get( '/*' , function( req, res ) {
  // this is the current file they have requested
  var file = req.params[0]; 
  console.log('\t :: Express :: file requested: ' + file);    
  
  if(req.query.id && !valid_id(req.query.id)) {
    res.redirect('http://rxdhawkins.net:8888/forms/invalid.html');
  } else {
    if(req.query.id && req.query.id in global_player_set) {
      res.redirect('http://rxdhawkins.net:8888/forms/duplicate.html');
    } else {
      res.sendfile("./" + file); // give them what they want
    }
  }
}); 

// Socket.io will call this function when a client connects. We check
// to see if the client supplied a id. If so, we distinguish them by
// that, otherwise we assign them one at random
io.on('connection', function (client) {
  // Recover query string information and set condition
  var hs = client.handshake;    
  var query = require('url').parse(client.handshake.headers.referer, true).query;
  var id;
  if( !(query.id && query.id in global_player_set) ) {
    if(query.id) {
      global_player_set[query.id] = true;
      // use id from query string if exists
      id = query.id; 
    } else {
      // otherwise, create new one
      id = utils.UUID();
    }
    if(valid_id(id)) {
      console.log("user connecting...");
      initialize(query, client, id);
    }
  }
});

var valid_id = function(id) {
  return id.length == 41;
};

var initialize = function(query, client, id) {                        
  client.userid = id;
  client.emit('onconnected', { id: client.userid } );

  // Good to know when they connected
  console.log('\t socket.io:: player ' + client.userid + ' connected');

  //Pass off to game.server.js code
  gameServer.findGame(client);

  // Now we want set up some callbacks to handle messages that clients will send.
  // We'll just pass messages off to the server_onMessage function for now.
  client.on('message', function(m) {
    gameServer.onMessage(client, m);
  });
  <commit_msg>Redirect duplicate and invalid ids<commit_after>/*  Copyright (c) 2012 Sven "FuzzYspo0N" Bergström, 2013 Robert <NAME>

    originally written for: http://buildnewgames.com/real-time-multiplayer/
    
    substantially modified for collective behavior experiments 

    MIT Licensed.
*/

var 
    use_https       = true,
    gameport        = 8888,
    https           = require('https'),
    fs              = require('fs'),
    app             = require('express')(),
    _               = require('underscore');

try {
  var privateKey  = fs.readFileSync('/etc/apache2/ssl/private.key'),
      certificate = fs.readFileSync('/etc/apache2/ssl/ssl.crt'),
      options     = {key: privateKey, cert: certificate},
      server      = require('https').createServer(options,app).listen(gameport),
      io          = require('socket.io')(server);
} catch (err) {
  console.log("cannot find SSL certificates; falling back to http");
  var server      = app.listen(gameport),
      io          = require('socket.io')(server);
}

if (process.argv[2]) {
  var exp = process.argv[2];
  var gameServer = require('./sharedUtils/serverBase.js')(exp);  
} else {
  throw new("no experiment supplied");
}

var utils = require('./sharedUtils/sharedUtils.js');

var global_player_set = {};

// Log something so we know that server-side setup succeeded
console.log("info  - socket.io started");
console.log('\t :: Express :: Listening on port ' + gameport );

//  This handler will listen for requests on /*, any file from the
//  root of our server. See expressjs documentation for more info 
app.get( '/*' , function( req, res ) {
  // this is the current file they have requested
  var file = req.params[0]; 
  console.log('\t :: Express :: file requested: ' + file);    
  if(req.query.workerId && !valid_id(req.query.workerId)) {
    res.redirect('https://rxdhawkins.net:8888/' + exp + 'experiment/forms/invalid.html');
  } else {
    if(req.query.workerId && req.query.workerId in global_player_set) {
      res.redirect('https://rxdhawkins.net:8888' + exp + 'experiment/forms/duplicate.html');
    } else {
      res.sendfile("./" + file); // give them what they want
    }
  }
}); 

// Socket.io will call this function when a client connects. We check
// to see if the client supplied a id. If so, we distinguish them by
// that, otherwise we assign them one at random
io.on('connection', function (client) {
  // Recover query string information and set condition
  var hs = client.handshake;    
  var query = require('url').parse(client.handshake.headers.referer, true).query;
  var id;
  if( !(query.workerId && query.workerId in global_player_set) ) {
    if(query.workerId) {
      global_player_set[query.workerId] = true;
      // use id from query string if exists
      id = query.workerId; 
    } else {
      // otherwise, create new one
      id = utils.UUID();
    }
    if(valid_id(id)) {
      console.log("user connecting...");
      initialize(query, client, id);
    }
  }
});

var valid_id = function(id) {
  return (id.length == 12 || id.length == 13 ||
	  id.length == 14 || id.length == 15 || 
	  id.length == 41);
};

var initialize = function(query, client, id) {                        
  client.userid = id;
  client.emit('onconnected', { id: client.userid } );

  // Good to know when they connected
  console.log('\t socket.io:: player ' + client.userid + ' connected');

  //Pass off to game.server.js code
  gameServer.findGame(client);

  // Now we want set up some callbacks to handle messages that clients will send.
  // We'll just pass messages off to the server_onMessage function for now.
  client.on('message', function(m) {
    gameServer.onMessage(client, m);
  });
  
{'fraction_non_alphanumeric': 0.13936362373211061, 'fraction_numerical': 0.008892594136445742, 'mean_word_length': 2.8024300052826203, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, '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': '4779455', 'n_tokens_mistral': 2247, 'n_tokens_neox': 2185, 'n_words': 619}