msenkpiel
12/6/2011 - 3:49 PM

Soccer Betting Calculation

Soccer Betting Calculation

var app = {
	calculate:function(resultA, resultB, betA, betB)
	{
		console.log('Result: ' + resultA + ':' + resultB + ' Bet: ' + betA + ':' + betB);
		
		if(this.testStrike(resultA, resultB, betA, betB))
		{
			console.log('3 Points');
			return;
		}
		else
		{
			if(this.testWinner(resultA, resultB, betA, betB))
			{
				if(this.testDifference(resultA, resultB, betA, betB))
				{
					console.log('2 Points');
					return;
				}
				else
				{
					console.log('1 Points');
					return;
				}
			}
			else
			{
				console.log('0 Points');
				return;
			}
		}
	},
	testStrike:function(resultA, resultB, betA, betB)
	{
		var a = false;
		var b = false;
	
		if(resultA == betA)
		{
			a = true;
		}
		
		if(resultB == betB)
		{
			b = true;
		}
		
		if(a && b)
		{
			return true;
		}
		else
		{
			return false
		}
	},
	testWinner:function(resultA, resultB, betA, betB)
	{		
		if(this.getWinnerTeam(resultA, resultB) == this.getWinnerTeam(betA, betB))
		{
			console.log('bet winner is true');
			return true;
		}
		else
		{
			console.log('bet winner is false');
			return false;
		}
	},
	getWinnerTeam:function(teamA, teamB)
	{
		var winner = (teamA / teamB);
		if(isNaN(winner))
		{
			return 'evenOfPoints';
		}
		else
		{
			if(winner > 1)
			{
				 return 'A';
			}
			else
			{
				return 'B';
			}
		}
	},
	testDifference:function(resultA, resultB, betA, betB)
	{
		var resultDiff = 0;
		var betDiff = 0;
		
		if(this.getWinnerTeam(resultA, resultB) == 'A')
		{
			resultDiff = (resultA-resultB);
			betDiff = (betA-betB);
		}
		else
		{
			resultDiff = (resultB-resultA);
			betDiff = (betB-betA);
		}
		
		console.log('resultDiff:', resultDiff, 'betDiff:', betDiff);
		
		if(betDiff == resultDiff)
		{
			return true;
		}
		else
		{
			return false;	
		}
	}
}