|
| 1 | +var module = angular.module('scorekeep'); |
| 2 | +module.controller('GameController', Game); |
| 3 | +function Game($scope, $http, $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 | + |
| 8 | + $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 | + console.log(result); |
| 16 | + $scope.gamestate = $scope.state.state.split(''); |
| 17 | + console.log("state string: '" + $scope.state.state + "'"); |
| 18 | + console.log("state array: " + $scope.gamestate); |
| 19 | + }); |
| 20 | + } |
| 21 | + $scope.playgame(); |
| 22 | + |
| 23 | + $scope.move = function(cellid){ |
| 24 | + console.log("MOVE on cell " + cellid); |
| 25 | + $scope.gamestate = $scope.state.state.split(''); |
| 26 | + console.log("state id: " + $scope.state.id); |
| 27 | + console.log("state string: '" + $scope.state.state + "'"); |
| 28 | + move = "" |
| 29 | + if ( $scope.gamestate[cellid] != " " ) { |
| 30 | + return; |
| 31 | + } |
| 32 | + if ($scope.gamestate[0] == "X") { |
| 33 | + $scope.gamestate[cellid] = "X"; |
| 34 | + $scope.gamestate[0] = "O"; |
| 35 | + move = "X" + cellid; |
| 36 | + console.log(move); |
| 37 | + } else { |
| 38 | + $scope.gamestate[cellid] = "O"; |
| 39 | + $scope.gamestate[0] = "X"; |
| 40 | + move = "O" + cellid; |
| 41 | + console.log(move); |
| 42 | + } |
| 43 | + console.log("new state string: '" + $scope.gamestate.join('') + "'"); |
| 44 | + PostMove = $http.post(api + 'move/' + $routeParams.sessionid + "/" + $routeParams.gameid + "/" + $routeParams.userid, move); |
| 45 | + GetGame = PostMove.then(function(){ |
| 46 | + return $scope.game.$get({ sessionid: $routeParams.sessionid, id: $routeParams.gameid }); |
| 47 | + }) |
| 48 | + GetState = GetGame.then(function(GetGameResult){ |
| 49 | + stateid = $scope.game.states[$scope.game.states.length-1]; |
| 50 | + return $scope.state.$get({ sessionid: $routeParams.sessionid, gameid: $routeParams.gameid, id: stateid}); |
| 51 | + }); |
| 52 | + GetState.then(function(){ |
| 53 | + $scope.gamestate = $scope.state.state.split(''); |
| 54 | + }); |
| 55 | + } |
| 56 | +} |
0 commit comments