コード例 #1
0
        public ActionResult Join([Bind(Include = "GameID")] RefreshDTO dto)
        {
            var  name        = User.Identity.Name;
            int  id          = dto.GameID;
            Room currentRoom = _roomsManager.GetRoomById(id);

            if (currentRoom == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.NotFound));
            }
            currentRoom.MarkUserAsActive(new User()
            {
                Name = name
            });
            var currentGame = currentRoom.Game;

            lock (currentGame)
            {
                GameVM vm = GameVM.From(currentGame, currentRoom.Owner.Name);
                _roomsManager.AddUserToRoom(new User()
                {
                    Name = name
                }, id);
                currentRoom.TryStartGame();
                return(Refresh(dto));
            }
        }
コード例 #2
0
        public ActionResult Refresh([Bind(Include = "GameID")] RefreshDTO dto)
        {
            var  name        = User.Identity.Name;
            int  id          = dto.GameID;
            Room currentRoom = _roomsManager.GetRoomById(id);

            if (currentRoom == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.NotFound));
            }
            currentRoom.MarkUserAsActive(new User()
            {
                Name = name
            });
            var currentGame = currentRoom.Game;

            lock (currentGame)
            {
                GameVM vm = GameVM.From(currentGame, currentRoom.Owner.Name);
                if (currentGame.CurrentPlayer != null)
                {
                    vm.IsPlayerTurn = currentGame.CurrentPlayer.Name == name;
                }
                JsonResult result = new JsonResult {
                    Data = JsonConvert.SerializeObject(vm)
                };
                return(result);
            }
        }
コード例 #3
0
        public ActionResult Index(int id = 0)
        {
            var  name        = User.Identity.Name;
            Room currentRoom = _roomsManager.GetRoomById(id);

            if (currentRoom == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.NotFound));
            }
            currentRoom.MarkUserAsActive(new User()
            {
                Name = name
            });
            var currentGame = currentRoom.Game;

            if (name == currentRoom.Owner.Name)
            {
                _roomsManager.AddUserToRoom(new User()
                {
                    Name = name
                }, id);
            }
            lock (currentGame)
            {
                GameVM vm = GameVM.From(currentGame, currentRoom.Owner.Name);
                if (currentGame.CurrentPlayer != null)
                {
                    vm.IsPlayerTurn = currentGame.CurrentPlayer.Name == name;
                }
                _roomsManager.SaveChanges(currentGame);
                return(View(vm));
            }
        }
コード例 #4
0
        public static GameVM MakeTurnAndUpdateGame(this Game currentGame, MoveDTO moveCoords, string name, string ownerName)
        {
            GameVM vm;
            Player enemyPlayer = currentGame.CurrentPlayer == currentGame.Player1
                    ? currentGame.Player2
                    : currentGame.Player1;

            if (currentGame.CurrentPlayer.Turn(currentGame.Board, enemyPlayer,
                                               currentGame.Board[moveCoords.fromX, moveCoords.fromY], new Position(moveCoords.toX, moveCoords.toY)))
            {
                currentGame.CurrentPlayer = currentGame.CurrentPlayer == currentGame.Player1 ? currentGame.Player2 : currentGame.Player1;
            }
            vm = GameVM.From(currentGame, ownerName);
            if (currentGame.CurrentPlayer != null)
            {
                vm.IsPlayerTurn = currentGame.CurrentPlayer.Name == name;
            }
            currentGame.SetGameState();
            vm.GameState = (int)currentGame.GameState;
            return(vm);
        }