<commit_before>var fs = require('fs');
var User = require('../models/User');
var request = require('request');
var _ = require('underscore');

var STARRYBOUND_PLAYERS_DIR = process.env.STARRYBOUND_PLAYERS_DIR;
var SERVER_STATUS_ENDPOINT = process.env.SERVER_STATUS_ENDPOINT;

var url = '/claim';

/**
 * GET /claim
 * Upgrade player page
 */

exports.claimForm = function(req, res){
  res.render('claim');
}

/**
 * POST /claim
 * Upgrades Starrybound players by placing them in a new group.
 * @todo: check that character name has not already been claimed? or just check that they are in the 'guest' group before allowing a claim.
 */

exports.claimPlayer = function ( req, res ) {
  req.assert('player', 'Character name cannot be blank').notEmpty();
  var errors = req.validationErrors();
  if (errors) {
    req.flash('errors', errors);
    return res.redirect( url );
  }
  var player = req.body.player;
  var player_json = STARRYBOUND_PLAYERS_DIR + player.toLowerCase() + '.json';
  fs.exists( player_json, function (exists) {
    if ( exists ) {
      request( SERVER_STATUS_ENDPOINT, function (error, response, body) {
        if (!error && response.statusCode == 200) {
          data = JSON.parse( body.toLowerCase );
          if ( _.contains( data.playersonline, player ) ) {
            req.flash('info', { msg: 'You need to logout before we can promote you.' });
            return res.redirect( url );
          } else {
            fs.readFile( player_json, 'utf8', function (err, contents) {
              if (err) { console.log('Error: ' + err); return; }
              player = JSON.parse(contents);
              player.groupName = "player";
              fs.writeFile( player_json, JSON.stringify(player, null, 4) , function(err) {
                if(err) { console.log(err); return; } 
                else {
                  // Save the character name to the users profile.
                  User.findById(req.user.id, function(err, user) {
                    if (err) return next(err);
                    user.profile.player = req.body.player;
                    user.save(function(err) {
                      if (err) return next(err);
                      req.flash('info', { msg: 'Character claimed! You\'re all set! Login and have fun!' });
                      res.redirect('/account');
                    });
                  });
                }
              }); 
            });
          }
        }
      });<commit_msg>whitespace fix<commit_after>var fs = require('fs');
var User = require('../models/User');
var request = require('request');
var _ = require('underscore');

var STARRYBOUND_PLAYERS_DIR = process.env.STARRYBOUND_PLAYERS_DIR;
var SERVER_STATUS_ENDPOINT = process.env.SERVER_STATUS_ENDPOINT;

var url = '/claim';

/**
 * GET /claim
 * Upgrade player page
 */

exports.claimForm = function(req, res){
  res.render('claim');
}

/**
 * POST /claim
 * Upgrades Starrybound players by placing them in a new group.
 * @todo: check that character name has not already been claimed? or just check that they are in the 'guest' group before allowing a claim.
 */

exports.claimPlayer = function ( req, res ) {
  req.assert('player', 'Character name cannot be blank').notEmpty();
  var errors = req.validationErrors();
  if (errors) {
    req.flash('errors', errors);
    return res.redirect( url );
  }
  var player = req.body.player;
  var player_json = STARRYBOUND_PLAYERS_DIR + player.toLowerCase() + '.json';
  fs.exists( player_json, function (exists) {
    if ( exists ) {
      request( SERVER_STATUS_ENDPOINT, function (error, response, body) {
        if (!error && response.statusCode == 200) {
          data = JSON.parse( body.toLowerCase );
          if ( _.contains( data.playersonline, player ) ) {
            req.flash('info', { msg: 'You need to logout before we can promote you.' });
            return res.redirect( url );
          } else {
            fs.readFile( player_json, 'utf8', function (err, contents) {
              if (err) { console.log('Error: ' + err); return; }
              player = JSON.parse(contents);
              player.groupName = "player";
              fs.writeFile( player_json, JSON.stringify(player, null, 4) , function(err) {
                if(err) { console.log(err); return; } 
                else {
                  // Save the character name to the users profile.
                  User.findById(req.user.id, function(err, user) {
                    if (err) return next(err);
                    user.profile.player = req.body.player;
                    user.save(function(err) {
                      if (err) return next(err);
                      req.flash('info', { msg: 'Character claimed! You\'re all set! Login and have fun!' });
                      res.redirect('/account');
                    });
                  });
                }
              });
            });
          }
        }
      });
{'fraction_non_alphanumeric': 0.13027597402597402, 'fraction_numerical': 0.002029220779220779, 'mean_word_length': 2.0710280373831775, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5790410', 'n_tokens_mistral': 1409, 'n_tokens_neox': 1369, 'n_words': 383}