5
5
angular . module ( 'pokerManager' ) .
6
6
controller ( 'PokerManagerCtrl' , PokerManagerController ) ;
7
7
8
- PokerManagerController . $inject = [ '$scope' , '$filter' , '$analytics' , 'toaster' , 'Utils' , 'Players' , 'Games' , 'playerModal' , 'communitiesSvc' ] ;
8
+ PokerManagerController . $inject = [ '$scope' , '$filter' , '$analytics' , 'toaster' , 'Utils' , 'Players' , 'Games' , 'playerModal' , 'communitiesSvc' , 'game' , 'Ref' , '$firebaseObject' ] ;
9
9
10
- function PokerManagerController ( $scope , $filter , $analytics , toaster , utils , Players , Games , playerModal , communitiesSvc ) {
10
+ function PokerManagerController ( $scope , $filter , $analytics , toaster , utils , Players , Games , playerModal , communitiesSvc , game , Ref , $firebaseObject ) {
11
11
'use strict' ;
12
12
13
13
var vm = this ;
@@ -23,7 +23,10 @@ angular.module( 'pokerManager' ).
23
23
vm . today = new Date ( ) ;
24
24
vm . serverMsg = [ ] ;
25
25
vm . players = [ ] ;
26
- vm . game = Games . create ( ) ;
26
+ // init game so not all methods fail before the game is loaded
27
+ vm . game = { } ;
28
+ // Binding the firebase instance to the scope. This assumes that the controller's name is `vm`
29
+ $firebaseObject ( Ref . child ( 'games/' + game . $id ) ) . $bindTo ( $scope , 'vm.game' ) ;
27
30
28
31
vm . init = init ;
29
32
vm . openPlayersControl = openPlayersControl ;
@@ -50,11 +53,17 @@ angular.module( 'pokerManager' ).
50
53
}
51
54
52
55
function saveGameToLocalStorage ( ) {
53
- utils . saveLocal ( 'game' , vm . game ) ;
56
+ var copy = angular . extend ( { } , vm . game ) ;
57
+ for ( var key in copy ) {
58
+ if ( copy . hasOwnProperty ( key ) && / ^ \$ / . test ( key ) ) {
59
+ delete copy [ key ] ;
60
+ }
61
+ }
62
+ utils . saveLocal ( 'game' , copy ) ;
54
63
}
55
64
56
65
function loadLocalStorageGame ( ) {
57
- var oldChipValue = vm . game . settings . chipValue ,
66
+ var oldChipValue = vm . game . chipValue ,
58
67
newChipValue ;
59
68
60
69
function playerEntity ( aPlayer ) {
@@ -66,8 +75,8 @@ angular.module( 'pokerManager' ).
66
75
return found && found [ 0 ] ;
67
76
}
68
77
69
- vm . game = utils . loadLocal ( 'game' ) ;
70
- newChipValue = parseInt ( vm . game . settings . chipValue , 10 ) ;
78
+ angular . extend ( vm . game , utils . loadLocal ( 'game' ) ) ;
79
+ newChipValue = parseInt ( vm . game . chipValue , 10 ) ;
71
80
// Players in game should be with same reference as players returned by the server
72
81
for ( var i = 0 ; i < vm . game . players . length ; ++ i ) {
73
82
var foundPlayer = playerEntity ( vm . game . players [ i ] ) ;
@@ -185,20 +194,22 @@ angular.module( 'pokerManager' ).
185
194
} , true ) ;
186
195
187
196
$scope . $watch ( function ( ) {
188
- return vm . game . settings . chipValue ;
197
+ return vm . game . chipValue ;
189
198
} , chipsValueChanged ) ;
190
199
191
200
function chipsValueChanged ( current , previous ) {
192
201
if ( ! current ) {
193
- current = vm . game . settings . chipValue = 1 ;
202
+ current = vm . game . chipValue = 1 ;
203
+ }
204
+ if ( vm . game . players && vm . game . players . length ) {
205
+ vm . game . players . forEach ( function updateChipsAndValue ( player , idx ) {
206
+ if ( player . currentChipCount ) {
207
+ player . currentChipCount = player . currentChipCount * current / ( previous || 1 ) ;
208
+ } else {
209
+ player . currentChipCount = ( player . buyin * current ) * current / ( previous || 1 ) ;
210
+ }
211
+ } ) ;
194
212
}
195
- vm . game . players . forEach ( function updateChipsAndValue ( player , idx ) {
196
- if ( player . currentChipCount ) {
197
- player . currentChipCount = player . currentChipCount * current / ( previous || 1 ) ;
198
- } else {
199
- player . currentChipCount = ( player . buyin * current ) * current / ( previous || 1 ) ;
200
- }
201
- } ) ;
202
213
}
203
214
}
204
215
} ) ( ) ;
0 commit comments