Exemplo n.º 1
0
 public ChessModel GetChessModel(string ticket)
 {
     lock (mutex)
     {
         var options = GetOptions();
         var ret     = new ChessModel
         {
             State                  = GetState(),
             AllUsers               = GetAllUsers(),
             IsBoardFull            = userTickets.Count == 2 || IsComputerGame() && userTickets.Count == 1,
             IsComputerGame         = IsComputerGame(),
             CanPlayAgainstComputer = CanPlayAgainstComputer(),
             ChessEngineNames       = GetChessEngineNames()
         };
         var ctx = GetContext(ticket);
         if (ctx != null)
         {
             ret.CurrentUser = GetCurrentUser(ctx);
         }
         if (chessboard != null)
         {
             ret.Board = new BoardModel();
             // board options and status
             ret.Board.WhitePlayer       = chessboard.WhitePlayer;
             ret.Board.BlackPlayer       = chessboard.BlackPlayer;
             ret.Board.GameOver          = chessboard.GameOver;
             ret.Board.GameStarted       = chessboard.GameStarted;
             ret.Board.GameOption        = ConvertGameOption(chessboard.GameOption);
             ret.Board.NextGameRequested = chessboard.NextGameRequested;
             ret.Board.CurrentColor      = ConvertFigureColor(chessboard.CurrentColor);
             // last stroke and last moved figure
             ret.Board.LastStroke = ConvertFigure(chessboard.LastStroke);
             ret.Board.LastMoves  = new List <MoveModel>();
             foreach (var move in chessboard.LastMoves)
             {
                 ret.Board.LastMoves.Add(ConvertLastMoved(move));
             }
             // reasons for game over and the winner
             ret.Board.TimeOut    = chessboard.TimeOut;
             ret.Board.KingStrike = chessboard.KingStrike;
             ret.Board.GiveUp     = chessboard.GiveUp;
             ret.Board.CheckMate  = chessboard.CheckMate;
             ret.Board.StaleMate  = chessboard.StaleMate;
             if (chessboard.GameOption != GameOption.FastChess)
             {
                 ret.Board.Check = chessboard.Check;
             }
             if (chessboard.Winner.HasValue)
             {
                 ret.Board.Winner = chessboard.Winner.Value == FigureColor.White ? ret.Board.WhitePlayer : ret.Board.BlackPlayer;
             }
             // figures and all moves for the current player
             ret.Board.Figures = new List <FigureModel>();
             foreach (var figure in chessboard.GetAllFigures())
             {
                 var fm = ConvertFigure(figure);
                 if (!chessboard.GameOver &&
                     ret.CurrentUser != null &&
                     ret.CurrentUser.Name == chessboard.CurrentPlayer &&
                     figure.Color == chessboard.CurrentColor)
                 {
                     foreach (var move in chessboard.GetAllMoves(figure, ret.Board.Check))
                     {
                         fm.Moves.Add(new MoveModel {
                             Row = move.Item1, Column = move.Item2
                         });
                     }
                 }
                 ret.Board.Figures.Add(fm);
             }
         }
         return(ret);
     }
 }