Games = new Mongo.Collection('games');

var Schemas = {};

Schemas.Games = new SimpleSchema({
	player1: {
		type: String,
		label: 'User ID of player1'
	},
	player2: {
		type: String,
		label: 'User ID of player2'
	},
	gameDate: {
		type: Date,
		label: 'Date of the game'
	},
	scorePlayer1: {
		type: Number,
		label: 'Player1 score',
		min: 0
	},
	scorePlayer2: {
		type: Number,
		label: 'player2 score',
		min: 0
	}
});

Games.attachSchema(Schemas.Games);

Games.helpers({
	player1Data: function() {
		return Meteor.users.findOne(this.player1);
	},
	player2Data: function() {
		return Meteor.users.findOne(this.player2);
	},
	scoreGap: function() {
		if (this.scorePlayer1 > this.scorePlayer2) {
			return this.scorePlayer1 - this.scorePlayer2;
		} else {
			return this.scorePlayer2 - this.scorePlayer1;
		}
	},
	over10: function() {
		if (this.scorePlayer1 > 10 || this.scorePlayer2 > 10) {
			return true;
		} else {
			return false;
		}
	},
	winner: function() {
		if (this.scorePlayer1 > this.scorePlayer2) {
			return player1;
		} else {
			return player2;
		}
	}
});




Meteor.methods({
	addAGame: function(gameData) {
		var player1Infos = Meteor.users.findOne({ _id: gameData.player1 });
		var player2Infos = Meteor.users.findOne({ _id: gameData.player2 });
		var lastPoints1 = player1Infos.profile.points.pop();
		var lastPoints2 = player2Infos.profile.points.pop();
		var pointsToAdd = 0;
		var pointsToLoose = 0;
		var pointBase = 40;
		var winner = function() {
			if (gameData.scorePlayer1 > gameData.scorePlayer2) {
				return gameData.player1;
			} else {
				return gameData.player2;
			}
		};
		if (lastPoints1 - lastPoints2 < 400 && lastPoints2 - lastPoints1 > -400) {
			if (winner() === gameData.player1) {
				pointsToAdd = Math.round(lastPoints1 + pointBase * (1 - 1 / (1 + Math.pow(10, (lastPoints1 - lastPoints2) / 400))));
				pointsToLoose = Math.round(lastPoints2 - pointBase * (1 - 1 / (1 + Math.pow(10, (lastPoints2 - lastPoints1) / 400))));
				Meteor.users.update({ _id: gameData.player1 }, {
					$push: {
						'profile.points': pointsToAdd
					}
				});
				Meteor.users.update({ _id: gameData.player2 }, {
					$push: {
						'profile.points': pointsToLoose
					}
				});
			} else if (winner() === gameData.player2) {
				pointsToAdd = Math.round(lastPoints2 + pointBase * (1 - 1 / (1 + Math.pow(10, (lastPoints2 - lastPoints1) / 400))));
				pointsToLoose = Math.round(lastPoints1 - pointBase * (1 - 1 / (1 + Math.pow(10, (lastPoints1 - lastPoints2) / 400))));
				Meteor.users.update({ _id: gameData.player2 }, {
					$push: {
						'profile.points': pointsToAdd
					}
				});
				Meteor.users.update({ _id: gameData.player1 }, {
					$push: {
						'profile.points': pointsToLoose
					}
				});
			}
		}
		return Games.insert(gameData);
	}
});

@@ -410,16 +410,84 @@
 %09min: 0%0A
+%09%7D,%0A%09practiceGame: %7B%0A%09%09type: Boolean,%0A%09%09label: 'Is a practice game'%0A
 %09%7D%0A%7D);%0A%0A
@@ -2811,18 +2811,127 @@
 %0A%09%09%09%7D%0A%09%09
-%7D%0A
+%09gameData.practiceGame = false;%0A%09%09%09return Games.insert(gameData);%0A%09%09%7D else %7B%0A%09%09%09gameData.practiceGame = true;%0A%09
 %09%09return
@@ -2955,15 +2955,19 @@
 eData);%0A
+%09%09%7D%0A
 %09%7D%0A%7D);%0A

Add practiceGame boolean