($q, $scope, $http, $interval, $routeParams, SessionService, UserService, GameService, GameCollection, RulesService, StateService, api)
| 1 | var module = angular.module('scorekeep'); |
| 2 | module.controller('GameController', Game); |
| 3 | function Game($q, $scope, $http, $interval, $routeParams, SessionService, UserService, GameService, GameCollection, RulesService, StateService, api) { |
| 4 | $scope.game = new GameService; |
| 5 | $scope.state = new StateService; // game state object |
| 6 | $scope.gamestate = []; // game state as Array |
| 7 | $scope.moving = 0; |
| 8 | $scope.user = UserService.get({ id: $routeParams.userid }); |
| 9 | $scope.winner = ''; |
| 10 | |
| 11 | $scope.playgame = function(){ |
| 12 | return $q(function(resolve, reject) { |
| 13 | GetGame = $scope.game.$get({ sessionid: $routeParams.sessionid, id: $routeParams.gameid }); |
| 14 | GetState = GetGame.then(function(){ |
| 15 | currentstate = $scope.game.states[$scope.game.states.length-1]; |
| 16 | return $scope.state.$get({ sessionid: $routeParams.sessionid, gameid: $routeParams.gameid, id: currentstate}); |
| 17 | }) |
| 18 | SetState = GetState.then(function(result){ |
| 19 | $scope.gamestate = $scope.state.state.split(''); |
| 20 | if ( $scope.gamestate[0] == 'A' ) { |
| 21 | $scope.winner = "X wins!"; |
| 22 | } else if ( $scope.gamestate[0] == 'B') { |
| 23 | $scope.winner = "O wins!"; |
| 24 | } |
| 25 | resolve(); |
| 26 | }); |
| 27 | }); |
| 28 | } |
| 29 | $scope.promise = $scope.playgame(); |
| 30 | $scope.interval = $interval(function(){ |
| 31 | $scope.promise.then(function() { |
| 32 | $scope.promise = $scope.playgame(); |
| 33 | }) |
| 34 | }, 5000); |
| 35 | |
| 36 | $scope.move = function(cellid){ |
| 37 | if ( $scope.moving == 1 || $scope.winner != '' ) { |
| 38 | return; |
| 39 | } |
| 40 | $scope.moving = 1; |
| 41 | $scope.promise.then(function(){ |
| 42 | $scope.promise = $q(function(resolve,reject){ |
| 43 | console.log("MOVE on cell " + cellid); |
| 44 | $scope.gamestate = $scope.state.state.split(''); |
| 45 | move = "" |
| 46 | // move is invalid |
| 47 | if ( $scope.gamestate[cellid] != " " ) { |
| 48 | return; |
| 49 | } |
| 50 | // temporarily update game board and determine move |
| 51 | if ($scope.gamestate[0] == "X") { |
| 52 | $scope.gamestate[cellid] = "X"; |
| 53 | $scope.gamestate[0] = "O"; |
| 54 | move = "X" + cellid; |
| 55 | } else { |
| 56 | $scope.gamestate[cellid] = "O"; |
| 57 | $scope.gamestate[0] = "X"; |
| 58 | move = "O" + cellid; |
| 59 | } |
| 60 | // send move |
nothing calls this directly
no outgoing calls
no test coverage detected