コード例 #1
0
        public void PlayMove(string gameId, string move)
        {
            Guid gId;

            if (Guid.TryParse(gameId, out gId))
            {
                Game game = db.Games.Get()
                            .IncludeMultiple(x => x.BoardMoves)
                            .FirstOrDefault(x => x.Id == gId && x.GameState == GameState.Playing);

                if (game != null)
                {
                    if (IsPlayerTurn(game, WebSecurity.CurrentUserId))
                    {
                        string lastMove = (game.BoardMoves.Count == 0) ? null
                            : game.BoardMoves.OrderByDescending(x => x.DateTimePlayed).First().MoveContent;

                        IMoveExecutor executor = new DefaultMoveExecutor(
                            game.BoardContent,
                            move,
                            game.Turn, game.GameState,
                            game.WhiteKingPosition,
                            game.BlackKingPosition,
                            lastMove,
                            game.WhiteCanCastleKingSide,
                            game.WhiteCanCastleQueenSide,
                            game.BlackCanCastleKingSide,
                            game.BlackCanCastleQueenSide);

                        if (executor.IsNewMoveValid())
                        {
                            BoardModel model = executor.NewBoardModelState;
                            game.BoardContent            = model.GetBoardAsString();
                            game.Turn                    = model.Turn;
                            game.GameState               = model.GameState;
                            game.WhiteKingPosition       = model.WhiteKingPosition.ToString();
                            game.BlackKingPosition       = model.BlackKingPosition.ToString();
                            game.WhiteCanCastleKingSide  = model.WhiteCanCastleKingSide;
                            game.WhiteCanCastleQueenSide = model.WhiteCanCastleQueenSide;
                            game.BlackCanCastleKingSide  = model.BlackCanCastleKingSide;
                            game.BlackCanCastleQueenSide = model.BlackCanCastleQueenSide;

                            BoardMove newMove = new BoardMove
                            {
                                MoveContent    = move,
                                DateTimePlayed = DateTime.Now
                            };

                            game.BoardMoves.Add(newMove);
                            db.Games.Update(game);
                            db.Save();

                            Clients.Group(gameId).updateViewModel(game.BoardContent);
                        }
                        else
                        {
                            Clients.Caller.updateViewModel(game.BoardContent);
                        }
                    }
                }
            }
        }