<commit_before>'use strict';

/**
 * Module dependencies.
 */
var mongoose = require('mongoose'),
	errorHandler = require('./errors'),
	Game = mongoose.model('Game'),
	_ = require('lodash');

function getGameAdapter(type) {
	var path = './games/games.' + type;
	return require(path);
}

/**
 * Create a Game
 */
exports.create = function(req, res) {
	var game = new Game(req.body);
	game.current_thrower = game.player1;

	// Verify that the current user is at least one of
	// the players in the game.
	console.log(req.user._id);
	console.log(req.body.player1);
	console.log(req.body.player2);
	if(parseInt(req.user._id) !== parseInt(req.body.player1) &&
		parseInt(req.user._id) !== parseInt(req.body.player2)) {
		return res.status(400).send({
			message: 'You must be one of the players in the created game.'
		});
	}

	var adapter = getGameAdapter(game.game_type.toLowerCase());
	game.scoreboard = adapter.createScoreboard();

	game.save(function(err) {
		if (err) {
			return res.status(400).send({
				message: errorHandler.getErrorMessage(err)
			});
		} else {
			// For the frontend, we need the populated values
			// for the display name as well now that we know
			// the ID of the winning player.
			if (game.winner !== null) {

				// todo Figure out how to properly get this when the game
				// just finishes
				game.winner = game.current_thrower;
			}
			res.jsonp(game);
		}
	});
};

/**
 * Show the current Game
 */
exports.read = function(req, res) {
	res.jsonp(req.game);
};

/**
 * Update a Game
 */
exports.update = function(req, res) {
	var game = req.game ;

	var adapter = getGameAdapter(game.game_type.toLowerCase());
	game = adapter.updateGameWithRound(req.body.round, game);

    game.current_thrower = (game.current_thrower.id === game.player1.id) ? game.player2 : game.player1;

	//game = _.extend(game , updated_game);

	game.save(function(err) {
		if (err) {
			return res.status(400).send({
				message: errorHandler.getErrorMessage(err)
			});
		} else {
			res.jsonp(game);
		}
	});
};

/**
 * Delete an Game
 */
exports.delete = function(req, res) {
	var game = req.game ;

	game.remove(function(err) {
		if (err) {
			return res.status(400).send({
				message: errorHandler.getErrorMessage(err)
			});
		} else {<commit_msg>Ensure game is not over before updating on server side.<commit_after>'use strict';

/**
 * Module dependencies.
 */
var mongoose = require('mongoose'),
	errorHandler = require('./errors'),
	Game = mongoose.model('Game'),
	_ = require('lodash');

function getGameAdapter(type) {
	var path = './games/games.' + type;
	return require(path);
}

/**
 * Create a Game
 */
exports.create = function(req, res) {
	var game = new Game(req.body);
	game.current_thrower = game.player1;

	// Verify that the current user is at least one of
	// the players in the game.
	console.log(req.user._id);
	console.log(req.body.player1);
	console.log(req.body.player2);
	if(parseInt(req.user._id) !== parseInt(req.body.player1) &&
		parseInt(req.user._id) !== parseInt(req.body.player2)) {
		return res.status(400).send({
			message: 'You must be one of the players in the created game.'
		});
	}

	var adapter = getGameAdapter(game.game_type.toLowerCase());
	game.scoreboard = adapter.createScoreboard();

	game.save(function(err) {
		if (err) {
			return res.status(400).send({
				message: errorHandler.getErrorMessage(err)
			});
		} else {
			// For the frontend, we need the populated values
			// for the display name as well now that we know
			// the ID of the winning player.
			if (game.winner !== null) {

				// todo Figure out how to properly get this when the game
				// just finishes
				game.winner = game.current_thrower;
			}
			res.jsonp(game);
		}
	});
};

/**
 * Show the current Game
 */
exports.read = function(req, res) {
	res.jsonp(req.game);
};

/**
 * Update a Game
 */
exports.update = function(req, res) {
	var game = req.game ;

	if(game.winner){
		res.jsonp(game);
		return;
	}

	var adapter = getGameAdapter(game.game_type.toLowerCase());
	game = adapter.updateGameWithRound(req.body.round, game);

    game.current_thrower = (game.current_thrower.id === game.player1.id) ? game.player2 : game.player1;

	//game = _.extend(game , updated_game);

	game.save(function(err) {
		if (err) {
			return res.status(400).send({
				message: errorHandler.getErrorMessage(err)
			});
		} else {
			res.jsonp(game);
		}
	});
};

/**
 * Delete an Game
 */
exports.delete = function(req, res) {
	var game = req.game ;

	game.remove(function(err) {
		if (err) {
			return res.status(400).send({
				message: errorHandler.getErrorMessage(err)
			});
		} else {
{'fraction_non_alphanumeric': 0.15696038103485602, 'fraction_numerical': 0.008659883091578264, 'mean_word_length': 3.4083969465648853, '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': '1776814', 'n_tokens_mistral': 1719, 'n_tokens_neox': 1582, 'n_words': 374}