|
1 | 1 | var module = angular.module('scorekeep'); |
2 | 2 | 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) { |
4 | 4 | $scope.game = new GameService; |
5 | 5 | $scope.state = new StateService; // game state object |
6 | 6 | $scope.gamestate = []; // game state as Array |
| 7 | + $scope.moving = 0; |
7 | 8 |
|
8 | 9 | $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 | + }); |
16 | 20 | }); |
17 | 21 | } |
18 | | - $scope.playgame(); |
| 22 | + $scope.promise = $scope.playgame(); |
19 | 23 | $scope.interval = $interval(function(){ |
20 | | - $scope.playgame(); |
| 24 | + $scope.promise.then(function() { |
| 25 | + $scope.promise = $scope.playgame(); |
| 26 | + }) |
21 | 27 | }, 5000); |
22 | 28 |
|
23 | 29 | $scope.move = function(cellid){ |
24 | | - $scope.gamestate = $scope.state.state.split(''); |
25 | | - move = "" |
26 | | - if ( $scope.gamestate[cellid] != " " ) { |
| 30 | + if ( $scope.moving == 1 ) { |
27 | 31 | return; |
28 | 32 | } |
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 | + |
48 | 72 | }); |
49 | 73 | } |
50 | 74 |
|
|
0 commit comments