@@ -9,20 +9,45 @@ Template.home.helpers({
9
9
} ,
10
10
player1Score : function ( ) {
11
11
var game = Games . findOne ( { current : true } ) ;
12
- console . log ( game ) ;
13
- if ( ! game )
12
+ if ( ! game ) {
14
13
return ;
14
+ }
15
15
var player = game . players [ 0 ] ;
16
- console . log ( player ) ;
17
16
var bubbles = Bubbles . find ( { userId : player , gameId : game . _id } ) . fetch ( ) ;
18
17
return bubbles . length ;
19
18
} ,
20
19
player2Score : function ( ) {
21
20
var game = Games . findOne ( { current : true } ) ;
22
- if ( ! game )
21
+ if ( ! game ) {
23
22
return ;
23
+ }
24
24
var player = game . players [ 1 ] ;
25
- return Bubbles . find ( { userId : player , gameId : game . _id } ) . fetch ( ) . length ;
25
+ var bubbles = Bubbles . find ( { userId : player , gameId : game . _id } ) . fetch ( ) ;
26
+ return bubbles . length ;
27
+ } ,
28
+ endGame : function ( ) {
29
+ var game = Games . findOne ( { current : true } ) ;
30
+ if ( ! game ) {
31
+ return ;
32
+ }
33
+ var player1 = Bubbles . find ( { userId : game . players [ 0 ] , gameId : game . _id } ) . fetch ( ) . length ;
34
+ var player2 = Bubbles . find ( { userId : game . players [ 1 ] , gameId : game . _id } ) . fetch ( ) . length ;
35
+ var totalPressed = player1 + player2 ;
36
+ var winner ;
37
+ if ( player1 > player2 ) {
38
+ winner = "Player 1 Wins!!!" ;
39
+ }
40
+ else if ( player2 > player1 ) {
41
+ winner = "Player 2 Wins!!!" ;
42
+ }
43
+ else {
44
+ winner = "Tie Game!!!!" ;
45
+ }
46
+ if ( totalPressed !== 64 ) {
47
+ return ;
48
+ }
49
+ bootbox . alert ( winner ) ;
50
+ Meteor . call ( 'finishGame' , game . _id )
26
51
}
27
52
} ) ;
28
53
@@ -33,19 +58,18 @@ Template.home.events({
33
58
"click #finishGame" : function ( ) {
34
59
var game = Games . findOne ( { current : true } ) ;
35
60
Meteor . call ( 'finishGame' , game . _id ) ;
36
- }
61
+ }
37
62
} ) ;
38
63
39
64
Template . grid . helpers ( {
40
65
buttons : function ( ) {
41
- console . log ( 'Inside the grid -> button helper' ) ;
42
66
var user = Meteor . user ( ) ;
43
67
if ( ! user ) {
44
- return ;
68
+ return ;
45
69
}
46
70
var game = Games . findOne ( { players : { $in : [ user . _id ] } , current : true } ) ;
47
71
if ( ! game ) {
48
- return ;
72
+ return ;
49
73
}
50
74
return Bubbles . find ( { gameId : game . _id } ) ;
51
75
}
@@ -57,5 +81,5 @@ Template.grid.events({
57
81
var _id = clickedElement . attr ( 'button_id' ) ;
58
82
Meteor . call ( 'hideButton' , _id , Meteor . userId ( ) ) ;
59
83
}
60
- } )
84
+ } ) ;
61
85
0 commit comments