Skip to content

Commit 2a81090

Browse files
committed
[feature firebase-migration]:
Creating a new game
1 parent 34fbf25 commit 2a81090

File tree

11 files changed

+124
-82
lines changed

11 files changed

+124
-82
lines changed

app/index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
<!-- build:js scripts/libs.min.js -->
5959
<script src="lib/jquery-1.10.2.min.js"></script>
6060
<script src="lib/firebase.js"></script>
61-
<script src="lib/angular/angular.min.js"></script>
61+
<script src="lib/angular/angular.js"></script>
6262
<script src="lib/angular/angular-route.min.js"></script>
6363
<script src="lib/angular/angular-resource.min.js"></script>
6464
<script src="lib/angular/angular-animate.min.js"></script>

app/partials/tmpls/on-going-game-tmpl.html

+13-13
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
<div class="current-game">
2-
<h3>Current Game: played at &quot;{{ game.location }}&quot; (# of players: {{ game.players.length }}).</h3>
2+
<h3>Current Game: played at &quot;{{ vm.game.location }}&quot; (# of players: {{ vm.game.players.length }}).</h3>
33

44
<accordion class="settings-menu">
55
<accordion-group>
66
<accordion-heading>
7-
<i class="glyphicon glyphicon-tasks"></i> Game Preferences: Chip value - {{ game.settings.chipValue }}, Default buyin - {{ game.settings.defaultBuyin }}, Max buyin - {{ game.settings.maxBuyin }}
7+
<i class="glyphicon glyphicon-tasks"></i> Game Preferences: Chip value - {{ vm.game.chipValue }}, Default buyin - {{ vm.game.defaultBuyin }}, Max buyin - {{ vm.game.maxBuyin }}
88
</accordion-heading>
99
<form action="" class="form-inline row">
1010
<div class="form-group col-md-4">
1111
<label for="chipValue">Chip value:</label>
12-
<input class="form-control" type="range" min="1" step="1" max="400" id="chipValue" ng-model="game.settings.chipValue" />
12+
<input class="form-control" type="range" min="1" step="1" max="400" id="chipValue" ng-model="vm.game.chipValue" />
1313
</div>
1414
<div class="form-group col-md-4">
1515
<label for="defaultBuyin">Default buyin:</label>
16-
<input class="form-control" type="number" min="50" step="50" id="defaultBuyin" ng-model="game.settings.defaultBuyin" />
16+
<input class="form-control" type="number" min="50" step="50" id="defaultBuyin" ng-model="vm.game.defaultBuyin" />
1717
</div>
1818
<div class="form-group col-md-4">
1919
<label for="maxBuyin">Max buyin:</label>
20-
<input class="form-control" type="number" min="50" step="50" id="maxBuyin" ng-model="game.settings.maxBuyin" />
20+
<input class="form-control" type="number" min="50" step="50" id="maxBuyin" ng-model="vm.game.maxBuyin" />
2121
</div>
2222
</form>
2323
</accordion-group>
@@ -26,12 +26,12 @@ <h3>Current Game: played at &quot;{{ game.location }}&quot; (# of players: {{ ga
2626
<form class="form-inline" role="form">
2727
<div class="form-group input-group-sm">
2828
<label for="gameLocation">Playing at</label>
29-
<input id="gameLocation" type="text" ng-model="game.location" placeholder="Game Location" class="form-control" />
29+
<input id="gameLocation" type="text" ng-model="vm.game.location" placeholder="Game Location" class="form-control" />
3030
</div>
3131
<div class="form-group">
3232
<label for="gameDate">on</label>
3333
<div class="input-group input-group-sm">
34-
<input id="gameDate" class="form-control" type="text" datepicker-popup="yyyy-MM-dd" readonly="readonly" ng-model="game.date" datepicker-options="vm.dateOptions" is-open="game.dateOpen" show-weeks="false" />
34+
<input id="gameDate" class="form-control" type="text" datepicker-popup="yyyy-MM-dd" readonly="readonly" ng-model="vm.game.date" datepicker-options="vm.dateOptions" is-open="vm.game.dateOpen" show-weeks="false" />
3535
<span class="input-group-btn">
3636
<button type="button" class="btn btn-default" ng-click="vm.toggleGameDate($event)"><i class="glyphicon glyphicon-calendar"></i></button>
3737
</span>
@@ -40,10 +40,10 @@ <h3>Current Game: played at &quot;{{ game.location }}&quot; (# of players: {{ ga
4040
<div class="form-group">
4141
<label># of hands:</label>
4242
<div class="btn-group">
43-
<button class="btn btn-sm btn-success" ng-click="game.numberOfHands = game.numberOfHands + 1"><i class="glyphicon glyphicon-plus"></i>
43+
<button class="btn btn-sm btn-success" ng-click="vm.game.numberOfHands = vm.game.numberOfHands + 1"><i class="glyphicon glyphicon-plus"></i>
4444
</button>
45-
<button class="btn btn-sm btn-default" style="width: 80px;">{{ game.numberOfHands }}</button>
46-
<button class="btn btn-sm btn-danger" ng-click="game.numberOfHands = game.numberOfHands - 1" ng-disabled="game.numberOfHands <= 0"><i class="glyphicon glyphicon-minus"></i>
45+
<button class="btn btn-sm btn-default" style="width: 80px;">{{ vm.game.numberOfHands }}</button>
46+
<button class="btn btn-sm btn-danger" ng-click="vm.game.numberOfHands = vm.game.numberOfHands - 1" ng-disabled="vm.game.numberOfHands <= 0"><i class="glyphicon glyphicon-minus"></i>
4747
</button>
4848
</div>
4949
</div>
@@ -54,19 +54,19 @@ <h3>Current Game: played at &quot;{{ game.location }}&quot; (# of players: {{ ga
5454
<button class="btn btn-default" ng-click="vm.startGame()">Start game</button>
5555

5656
<div class="row game-container">
57-
<div class="col-md-4 col-xs-6 fader" ng-repeat="player in game.players | orderBy:'name'">
57+
<div class="col-md-4 col-xs-6 fader" ng-repeat="player in vm.game.players | orderBy:'name'">
5858
<player-card player="player"/>
5959
</div>
6060
</div>
6161
<ul class="nav nav-pills" role="tablist">
6262
<li role="presentation"><a href="">Total Buyin <span class="badge">{{ vm.totalBuyin() }}</span></a></li>
6363
<li role="presentation"><a href="">Total Chips <span class="badge">{{ vm.totalChips() }}</span></a></li>
64-
<li role="presentation"><a href="">Total Buyout <span class="badge">{{ vm.totalChips() / game.settings.chipValue }}</span></a></li>
64+
<li role="presentation"><a href="">Total Buyout <span class="badge">{{ vm.totalChips() / vm.game.chipValue }}</span></a></li>
6565
<li role="presentation"><a href="">Total Hosting <span class="badge">{{ vm.totalHosting() }}</span></a></li>
6666
</ul>
6767
<div class="row bg-info end-game-wrap">
6868
<div class="col-md-12">
69-
<button ng-click="vm.saveGame()" class="btn btn-block btn-primary" ng-disabled="(game.players.length <= 0) || vm.isGameInProgress()" analytics-on analytics-event="Save Game" analytics-category="Actions" analytics-label="{{ game.location }}"><i class="glyphicon glyphicon-download-alt"></i>&nbsp;End Game</button>
69+
<button ng-click="vm.saveGame()" class="btn btn-block btn-primary" ng-disabled="(vm.game.players.length <= 0) || vm.isGameInProgress()" analytics-on analytics-event="Save Game" analytics-category="Actions" analytics-label="{{ vm.game.location }}"><i class="glyphicon glyphicon-download-alt"></i>&nbsp;End Game</button>
7070
</div>
7171
</div>
7272

app/scripts/app.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616
$routeProvider.when('/game/:gameId', {
1717
templateUrl: 'partials/partial1.html',
1818
controller: 'PokerManagerCtrl',
19-
controllerAs: 'vm'
19+
controllerAs: 'vm',
20+
resolve: {
21+
game: gameRouteResolver
22+
}
2023
});
2124
$routeProvider.when('/login', {
2225
templateUrl: 'partials/login.html',
@@ -40,4 +43,9 @@
4043
});
4144
$routeProvider.otherwise({redirectTo: '/stats'});
4245
}
46+
47+
gameRouteResolver.$inject = ['$route', '$firebaseObject', 'Ref'];
48+
function gameRouteResolver($route, $firebaseObject, Ref) {
49+
return $firebaseObject(Ref.child('game/' + $route.current.params.gameId));
50+
}
4351
}());

app/scripts/communities/communities.ctrl.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
.module( 'pokerManager' )
44
.controller( 'CommunitiesCtrl', CommunitiesController );
55

6-
CommunitiesController.$inject = [ 'communitiesSvc', 'userService', 'Players', 'playerModal', 'Ref' ];
7-
function CommunitiesController( communitiesSvc, userService, Players, playerModal, Ref ) {
6+
CommunitiesController.$inject = [ 'communitiesSvc', 'userService', 'Players', 'playerModal', 'Games', 'Ref', '$location' ];
7+
function CommunitiesController( communitiesSvc, userService, Players, playerModal, Games, Ref, $location ) {
88
var vm = this,
99
collapseState = {};
1010

@@ -18,6 +18,7 @@
1818
vm.communitiesDropdownToggle = communitiesDropdownToggle;
1919
vm.userUid = userService.getUser() && userService.getUser().uid;
2020
vm.addMember = addMember;
21+
vm.createGame = createGame;
2122

2223
vm.communities.$loaded().then( function () {
2324
vm.communities.forEach( function ( community ) {
@@ -78,6 +79,13 @@
7879
} );
7980
}
8081

82+
function createGame(community) {
83+
return Games.newGame(community.$id)
84+
.then(function (game) {
85+
$location.url('/game/' + (game.$id || '0'));
86+
});
87+
}
88+
8189
function isCollapsed( communityId ) {
8290
return collapseState[communityId];
8391
}

app/scripts/communities/communities.view.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ <h4 class="panel-title">
4545
</div>
4646
<div class="row">
4747
<div class="col-xs-12">
48-
<a href="#/game/0" class="btn btn-success">Start a new game</a>
48+
<button class="btn btn-success" ng-click="vm.createGame( community )">Start a new game</button>
4949
</div>
5050
</div>
5151
</div>

app/scripts/manager/game-ctrl.js

+20-19
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,9 @@ angular.module( 'pokerManager' ).
3838
vm.isGameInProgress = isGameInProgress;
3939
vm.saveGame = saveGame;
4040

41-
vm.initGame();
42-
4341
function initGame() {
44-
45-
angular.element.extend( $scope.game, new Game() );
46-
delete $scope.game.id;
42+
vm.game.players.splice(0, vm.game.players.length);
43+
vm.game.name = '';
4744

4845
// vm.game = new Game();
4946

@@ -58,14 +55,14 @@ angular.module( 'pokerManager' ).
5855

5956
function clearGame() {
6057
// Reset is-playing state
61-
$scope.game.players.forEach( function( player ) {
58+
vm.game.players.forEach( function( player ) {
6259
player.isPlaying = false;
6360
player.balance += ( player.buyout - player.buyin );
6461
} );
65-
$scope.game.players.splice( 0, $scope.game.players.length );
62+
vm.game.players.splice( 0, vm.game.players.length );
6663

6764
// Reset game
68-
$scope.game = Game.create();
65+
vm.game = Game.create();
6966
}
7067

7168
function buyin( player, rationalBuyin ) {
@@ -75,7 +72,7 @@ angular.module( 'pokerManager' ).
7572
player.buyin = 0;
7673
player.currentChipCount = 0;
7774
player.paidHosting = false;
78-
$scope.game.players.push( player );
75+
vm.game.players.push( player );
7976
}
8077
player.buyin += calculatedBuyin;
8178
player.balance -= player.buyin;
@@ -88,7 +85,7 @@ angular.module( 'pokerManager' ).
8885
}
8986

9087
function startGame() {
91-
$scope.game.players.forEach( function ( player ) {
88+
vm.game.players.forEach( function ( player ) {
9289
vm.buyin( player, 1 );
9390
} );
9491
}
@@ -103,10 +100,10 @@ angular.module( 'pokerManager' ).
103100

104101
function cancelAddPlayer( player ) {
105102
// Remove from current game
106-
var index = $scope.game.players.indexOf( player );
103+
var index = vm.game.players.indexOf( player );
107104

108105
if ( index > -1 ) {
109-
$scope.game.players.splice( index, 1 );
106+
vm.game.players.splice( index, 1 );
110107

111108
// Reset fields
112109
if ( player ) {
@@ -158,17 +155,21 @@ angular.module( 'pokerManager' ).
158155
}
159156

160157
function totalBuyin() {
161-
return utils.totalsCalc( $scope.game.players, 'buyin' );
158+
return utils.totalsCalc( vm.game.players, 'buyin' );
162159
}
163160

164161
function totalChips() {
165-
return utils.totalsCalc( $scope.game.players, 'currentChipCount' );
162+
return utils.totalsCalc( vm.game.players, 'currentChipCount' );
166163
}
167164

168165
function totalHosting() {
166+
if (!vm.game.players) {
167+
return 0;
168+
}
169+
169170
var sum = 0;
170-
for( var i = 0; i < $scope.game.players.length; ++i ) {
171-
sum += $scope.game.players[ i ].paidHosting ? 10 : 0;
171+
for( var i = 0; i < vm.game.players.length; ++i ) {
172+
sum += vm.game.players[ i ].paidHosting ? 10 : 0;
172173
}
173174
return sum;
174175
}
@@ -177,17 +178,17 @@ angular.module( 'pokerManager' ).
177178
$event.preventDefault();
178179
$event.stopPropagation();
179180

180-
$scope.game.dateOpen = !$scope.game.dateOpen;
181+
vm.game.dateOpen = !vm.game.dateOpen;
181182
}
182183

183184
function isGameInProgress() {
184-
return $scope.game.players.some( function ( player ) {
185+
return vm.players && vm.game.players.some( function ( player ) {
185186
return player.isPlaying;
186187
} );
187188
}
188189

189190
function saveGame() {
190-
Game.save( $scope.game ).$promise.then( function gameSaveSuccess( data ) {
191+
Game.save( vm.game ).$promise.then( function gameSaveSuccess( data ) {
191192
if ( angular.isFunction( $scope.saveSuccessCallback ) ) {
192193
$scope.saveSuccessCallback( data );
193194
}

app/scripts/manager/game-drtv.js

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ angular.module( 'pokerManager' ).
1717
},
1818
controller: 'GameCtrl',
1919
controllerAs: 'vm',
20+
bindToController: true,
2021
templateUrl: 'partials/tmpls/on-going-game-tmpl.html',
2122
link: {
2223
pre: function ( scope, element, attrs ) {

app/scripts/manager/manager-ctrl.js

+27-16
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
angular.module( 'pokerManager' ).
66
controller( 'PokerManagerCtrl', PokerManagerController );
77

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' ];
99

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 ) {
1111
'use strict';
1212

1313
var vm = this;
@@ -23,7 +23,10 @@ angular.module( 'pokerManager' ).
2323
vm.today = new Date();
2424
vm.serverMsg = [];
2525
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');
2730

2831
vm.init = init;
2932
vm.openPlayersControl = openPlayersControl;
@@ -50,11 +53,17 @@ angular.module( 'pokerManager' ).
5053
}
5154

5255
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 );
5463
}
5564

5665
function loadLocalStorageGame() {
57-
var oldChipValue = vm.game.settings.chipValue,
66+
var oldChipValue = vm.game.chipValue,
5867
newChipValue;
5968

6069
function playerEntity( aPlayer ) {
@@ -66,8 +75,8 @@ angular.module( 'pokerManager' ).
6675
return found && found[0];
6776
}
6877

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 );
7180
// Players in game should be with same reference as players returned by the server
7281
for ( var i = 0; i < vm.game.players.length; ++i ) {
7382
var foundPlayer = playerEntity( vm.game.players[ i ] );
@@ -185,20 +194,22 @@ angular.module( 'pokerManager' ).
185194
}, true );
186195

187196
$scope.$watch( function () {
188-
return vm.game.settings.chipValue;
197+
return vm.game.chipValue;
189198
}, chipsValueChanged );
190199

191200
function chipsValueChanged( current, previous ) {
192201
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+
});
194212
}
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-
} );
202213
}
203214
}
204215
})();

app/scripts/services.js

+12
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,32 @@ angular.module( 'pokerManager.services', [ 'ngResource' ] ).
77

88
var utils = {
99
totalsCalc: function ( anArray, fieldNameToSum ) {
10+
if (!anArray) {
11+
return 0;
12+
}
13+
1014
var sum = 0;
1115
for( var i = 0; i < anArray.length; ++i ) {
1216
sum += parseInt( anArray[ i ][ fieldNameToSum ] );
1317
}
1418
return sum;
1519
},
1620
avgsCalc: function ( anArray, fieldNameToSum ) {
21+
if (!anArray) {
22+
return 0;
23+
}
24+
1725
var sum = 0;
1826
for( var i = 0; i < anArray.length; ++i ) {
1927
sum += parseInt( anArray[ i ][ fieldNameToSum ] );
2028
}
2129
return ( sum / anArray.length );
2230
},
2331
maxCalc: function ( anArray, fieldNameToSum ) {
32+
if (!anArray) {
33+
return 0;
34+
}
35+
2436
var max = 0;
2537
for( var i = 0; i < anArray.length; ++i ) {
2638
max = Math.max( anArray[ i ][ fieldNameToSum ], max );

0 commit comments

Comments
 (0)