Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/examples/lessons/6_Maths_goShopping/js/lesson.js
blob: 5236876dbd4daa88ff4d5fd5131d500a054e1fc7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
$(document).ready(function(){
	var k = Karma({
		audio: [{'name':'correct','file':'correct.wav'},
			{'name':'incorrect','file':'incorrect.wav'}
		]});
	
	k.ready(function(){
		var i,j;
		var checkId;
		var correctCounter;
		var totalCounter;
		var TOTAL_THINGS = 19;
		var TOTAL_MONEYS = 5;
		var TOTAL_QUES = 10;
		var flag_correct;
		var flag_checked;
		var checked;
		var randThings = [];
		var randMoneys = [];
		var totalMoney;
		var totalPrice;
		
		var money = new Array(5,10,25,50,100);
		var things = new Array('roti','pomogranate','pen','pear','papaya','orange','mango','lays','ice',
								'grapes','food','doll','cap','cake','bread','book','balloon','bag','apple'
					);
		var priceThings = new Array(15,5,41,3,7,5,9,19,13,11,45,89,65,33,23,65,25,87,15);
		
		var scoreboard = $('#score_box').scoreboard({'layout':'horizontal', 
		       'winningScore': TOTAL_QUES});
		
		var genRandomThings = function(){
			randThings[0] = k.rand(0,TOTAL_THINGS-1); 
			for(i=1; i<TOTAL_THINGS; i++){
				do{
					flag = 0;
					randThings[i] = k.rand(0,TOTAL_THINGS-1);
					for(j=0; j<i; j++){
						if(randThings[i]===randThings[j]){
							flag++;
						}
					}
				}while(flag != 0 );  //end of do while loop	
			}
		
		};
		
		var genRandomMoneys = function(){
			randMoneys[0] = k.rand(0,TOTAL_MONEYS-1); 
			for(i=1; i<TOTAL_MONEYS; i++){
				do{
					flag = 0;
					randMoneys[i] = k.rand(0,TOTAL_MONEYS-1);
					for(j=0; j<i; j++){
						if(randMoneys[i]===randMoneys[j]){
							flag++;
						}
					}
				}while(flag != 0 );  //end of do while loop	
			}
		
		};
		
		
		
		
		var generatePositions = function(side,posId,topPos,leftPos){
			var side,posIdentify;
			var zindexVal;
			if(side === "left"){
				if(posId === 1){
					zindexVal = 1;
				}
				else if(posId === 3){
					zindexVal = 1;
				}
				else{
					zindexVal = 0;
				}
				$('#left-side').append('<div id="moneyPos'+posId+'" class="moneyPositions"></div>');
				$('#moneyPos'+posId).css({'top':topPos+'px','left':leftPos+'px','z-index':zindexVal});
			}
			else{
				$('#right-side').append('<div id="thingPos'+posId+'" class="thingsPositions"></div>');
				$('#thingPos'+posId).css({'top':topPos+'px','left':leftPos+'px'});
			}		
		};

		var checkAnswer = function(){
			 if(flag_checked != 0){
				 if(checkId == "moneyOk" && totalMoney >= totalPrice){
					 flag_checked = 0;
					 $('#howmuchText').html('<img src="assets/image/howMuchLeft.png" />');
				 }
				 else if(checkId == "moneyNeeded" && totalMoney < totalPrice){
					 flag_checked = 0;
					 $('#howmuchText').html('<img src="assets/image/howMuchNeeded.png" />');
				 }
				 else{  //try again ur score wont increase
					 flag_correct = 0;
				 }
			 }
			 if(flag_checked === 0){
				 $('#textDiv').html('<input type="text" class="textBox" maxlength="3"/>');
				 $('.textBox').val('');
				 foucs_blur();		$('.textBox').focus();
			}
		};
		
		var nextQuestions = function(){		
			$('#howmuchText').html('');			
			$('#textDiv').html('');
			
			totalMoney = 0;
			totalPrice  = 0;
			flag_correct = 1;
			flag_checked = 1;
			$('#left-side').html('').append('<img id="leftText" src="assets/image/leftText.png" />');
			$('#right-side').html('').append('<img id="rightText" src="assets/image/rightText.png" />');

			generatePositions('left',0,25,25);
			generatePositions('left',1,75,325);
			generatePositions('left',2,150,50);
			generatePositions('left',3,180,350);
			
			generatePositions('right',0,50,25);
			generatePositions('right',1,25,220);
			generatePositions('right',2,225,50);
			generatePositions('right',3,200,275);
			
			genRandomMoneys();
			var currentMoneys = k.rand(1,4);
			for(i = 0; i< currentMoneys ;i++){
				var randMoney = randMoneys[i];
				$('#moneyPos'+i).append('<img src = "assets/image/rs'+money[randMoney]+'.png" />');
				totalMoney += money[randMoney];
			}
			genRandomThings();
			var currentThings = k.rand(1,4);
			for(i = 0; i< currentThings ;i++){
				var randThing = randThings[i];
				$('#thingPos'+i).append('<img src = "assets/image/'+things[randThing]+'.png" />');
				totalPrice += priceThings[randThing];
			}

			$('.checkMoney').click(function(){
				checkId = $(this).attr('id');
				checkAnswer();
			});
		};
		

		
		function gameStart(){
			$('#content').removeClass('backOpaque');
			$('#gameOver').hide();
			totalCounter = 0;
			correctCounter = 0;
			nextQuestions();
			
		}
		
		var displayGameOver = function(){
			$('#content').addClass('backOpaque');
			$('#gameOver').show();
			if(correctCounter === totalCounter){		
				$('#gameOver').html('बधाई छ !!!  सबै उत्तर सहि भए !!! ');
			}
			else{
				$('#gameOver').html('<div id="gameOverInfo">किन गलत भयो पत्ता लगाउ र अर्को पटक सहि बनाउने कोशिश गर । <br /> You Got <span class="specialText">'+correctCounter+
				'</span> correct out of <span class="specialText">'+totalCounter+'</span>   questions .</div>');
			}	
		};
		
		var delayCorrectShow = function(){
			document.delayForm.delayval.value = 1;
			if(checked === 1){
				if(totalCounter === 10){
					displayGameOver();
				}
				else{
					nextQuestions();
				}
			}
			$('#checkDisplay').html('');
			 
			
		};	
		
		var checkMoney = function(){
			var flag_correctAns = 1;
			var textVal = $('.textBox').val();
			if( checkId === "moneyOk" && textVal == (totalMoney - totalPrice) ){ 
				flag_correctAns = 1;
			}
			else if(checkId === "moneyNeeded" && textVal == (totalPrice - totalMoney)){
				flag_correctAns = 1;
			}
			else{
				flag_correctAns = 0;
			}
				
			if(flag_correctAns === 1){
				checked = 1;
				k.audio.correct.play();
				if(flag_correct === 1){
					correctCounter++;
					scoreboard.scoreboard('inc');
				}
				totalCounter++;
				scoreboard.scoreboard('incTotal');			
				$('#checkDisplay').html('<img src="assets/image/correct.png" />');
				
			}
			else{
				flag_correct = 0;
				checked = 0;
				k.audio.incorrect.play();
				$('#checkDisplay').html('<img src="assets/image/incorrect.png" />');
				t=setTimeout(function(){delayCorrectShow();},1000);
			}
			t=setTimeout(function(){delayCorrectShow();},1000);
			
		};
		
		function foucs_blur(){
			$('input[type="text"]')
			.focus(function() {
			    $(this).addClass("focus");
			})
			.blur(function() {
			    $(this).removeClass("focus");
			})
			.keypress(function(event) {
				if(event.which === 13){
					checkMoney();
				}
				
			});
			
		}
		
		$('#linkStart').click(function(){
			gameStart();
		});
	
		$('#linkPlayAgain').click(function(){
			gameStart();		
		});
		$('#linkHelp').mouseover(function(){
			$('#help').slideDown(2000);
		})
		.mouseout(function(){
			$('#help').slideUp(2000);
			
		});
			}); //end of k.ready
		});	//end of document.read