Skip to content

Commit 71fe8ad

Browse files
committed
q state refresh and moves, prevent q'ing multiple moves
1 parent c5b9999 commit 71fe8ad

File tree

1 file changed

+56
-32
lines changed

1 file changed

+56
-32
lines changed

public/app/gameController.js

Lines changed: 56 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,74 @@
11
var module = angular.module('scorekeep');
22
module.controller('GameController', Game);
3-
function Game($scope, $http, $interval, $routeParams, SessionService, UserService, GameService, GameCollection, RulesService, StateService, api) {
3+
function Game($q, $scope, $http, $interval, $routeParams, SessionService, UserService, GameService, GameCollection, RulesService, StateService, api) {
44
$scope.game = new GameService;
55
$scope.state = new StateService; // game state object
66
$scope.gamestate = []; // game state as Array
7+
$scope.moving = 0;
78

89
$scope.playgame = function(){
9-
GetGame = $scope.game.$get({ sessionid: $routeParams.sessionid, id: $routeParams.gameid });
10-
GetState = GetGame.then(function(){
11-
currentstate = $scope.game.states[$scope.game.states.length-1];
12-
return $scope.state.$get({ sessionid: $routeParams.sessionid, gameid: $routeParams.gameid, id: currentstate});
13-
})
14-
GetState.then(function(result){
15-
$scope.gamestate = $scope.state.state.split('');
10+
return $q(function(resolve, reject) {
11+
GetGame = $scope.game.$get({ sessionid: $routeParams.sessionid, id: $routeParams.gameid });
12+
GetState = GetGame.then(function(){
13+
currentstate = $scope.game.states[$scope.game.states.length-1];
14+
return $scope.state.$get({ sessionid: $routeParams.sessionid, gameid: $routeParams.gameid, id: currentstate});
15+
})
16+
SetState = GetState.then(function(result){
17+
$scope.gamestate = $scope.state.state.split('');
18+
resolve();
19+
});
1620
});
1721
}
18-
$scope.playgame();
22+
$scope.promise = $scope.playgame();
1923
$scope.interval = $interval(function(){
20-
$scope.playgame();
24+
$scope.promise.then(function() {
25+
$scope.promise = $scope.playgame();
26+
})
2127
}, 5000);
2228

2329
$scope.move = function(cellid){
24-
$scope.gamestate = $scope.state.state.split('');
25-
move = ""
26-
if ( $scope.gamestate[cellid] != " " ) {
30+
if ( $scope.moving == 1 ) {
2731
return;
2832
}
29-
if ($scope.gamestate[0] == "X") {
30-
$scope.gamestate[cellid] = "X";
31-
$scope.gamestate[0] = "O";
32-
move = "X" + cellid;
33-
} else {
34-
$scope.gamestate[cellid] = "O";
35-
$scope.gamestate[0] = "X";
36-
move = "O" + cellid;
37-
}
38-
PostMove = $http.post(api + 'move/' + $routeParams.sessionid + "/" + $routeParams.gameid + "/" + $routeParams.userid, move);
39-
GetGame = PostMove.then(function(){
40-
return $scope.game.$get({ sessionid: $routeParams.sessionid, id: $routeParams.gameid });
41-
})
42-
GetState = GetGame.then(function(GetGameResult){
43-
stateid = $scope.game.states[$scope.game.states.length-1];
44-
return $scope.state.$get({ sessionid: $routeParams.sessionid, gameid: $routeParams.gameid, id: stateid});
45-
});
46-
GetState.then(function(){
47-
$scope.gamestate = $scope.state.state.split('');
33+
$scope.moving = 1;
34+
$scope.promise.then(function(){
35+
$scope.promise = $q(function(resolve,reject){
36+
console.log("MOVE on cell " + cellid);
37+
$scope.gamestate = $scope.state.state.split('');
38+
move = ""
39+
// move is invalid
40+
if ( $scope.gamestate[cellid] != " " ) {
41+
return;
42+
}
43+
// temporarily update game board and determine move
44+
if ($scope.gamestate[0] == "X") {
45+
$scope.gamestate[cellid] = "X";
46+
$scope.gamestate[0] = "O";
47+
move = "X" + cellid;
48+
} else {
49+
$scope.gamestate[cellid] = "O";
50+
$scope.gamestate[0] = "X";
51+
move = "O" + cellid;
52+
}
53+
// send move
54+
PostMove = $http.post(api + 'move/' + $routeParams.sessionid + "/" + $routeParams.gameid + "/" + $routeParams.userid, move);
55+
// get new game state
56+
GetGame = PostMove.then(function(){
57+
return $scope.game.$get({ sessionid: $routeParams.sessionid, id: $routeParams.gameid });
58+
})
59+
GetState = GetGame.then(function(GetGameResult){
60+
stateid = $scope.game.states[$scope.game.states.length-1];
61+
return $scope.state.$get({ sessionid: $routeParams.sessionid, gameid: $routeParams.gameid, id: stateid});
62+
});
63+
// update game board
64+
GetState.then(function(){
65+
$scope.gamestate = $scope.state.state.split('');
66+
$scope.moving = 0;
67+
resolve();
68+
});
69+
70+
});
71+
4872
});
4973
}
5074

0 commit comments

Comments
 (0)