示例#1
0
        public ActionResult CreateGame(CreateGameVM game)
        {
            if (ModelState.IsValid)
            {
                Game newGame = new Game();
                newGame.Id = Guid.NewGuid();
                newGame.HostId = WebSecurity.CurrentUserId;
                newGame.GameState = GameState.Waiting;
                newGame.DateCreated = DateTime.Now;
                newGame.Turn = Turn.White;
                newGame.Description = game.Description;
                newGame.WhiteCanCastleKingSide = true;
                newGame.WhiteCanCastleQueenSide = true;
                newGame.BlackCanCastleKingSide = true;
                newGame.BlackCanCastleQueenSide = true;
                newGame.WhiteKingPosition = BoardConstants.INITIAL_WHITE_KING_POSITION;
                newGame.BlackKingPosition = BoardConstants.INITIAL_BLACK_KING_POSITION;
                newGame.BoardContent = BoardConstants.INITIAL_BOARD;
                if (game.ColorSelect == ColorSelect.White)
                {
                    newGame.WhitePlayerId = WebSecurity.CurrentUserId;
                }
                else if (game.ColorSelect == ColorSelect.Black)
                {
                    newGame.BlackPlayerId = WebSecurity.CurrentUserId;
                }
                else
                {
                    int r = rand.Next(0, 2);
                    if (r == 0)
                    {
                        newGame.WhitePlayerId = WebSecurity.CurrentUserId;
                    }
                    else
                    {
                        newGame.BlackPlayerId = WebSecurity.CurrentUserId;
                    }
                }

                db.Games.Insert(newGame);
                db.Save();

                gameLobbyHubContext.Clients.All.addGame(newGame.Id.ToString(), newGame.HostId,
                    newGame.Description);

                return RedirectToAction("Game", new { id = newGame.Id });
            }
            else
            {
                return View("GameLobby");
            }
        }
示例#2
0
        private bool IsPlayerTurn(Game game, int userId)
        {
            if ((game.Turn == Turn.White && userId == game.WhitePlayerId) ||
                (game.Turn == Turn.Black && userId == game.BlackPlayerId))
            {
                return true;
            }

            return false;
        }