private Square[] RepairMissingSquares(Game game, Square[] squares)
        {
            if (!squares.Any())
            {
                var missingSquares = new Board().Squares.SelectMany(s => s).ToArray();

                foreach (var square in missingSquares)
                {
                    square.Game = game;
                    square.ChessPiece = square.ChessPiece;
                    UnitOfWork.Add(square);
                }

                UnitOfWork.Commit();
                squares = missingSquares;
            }
            return squares;
        }
        private void RespondToChallenge(long challengeId, bool accepted)
        {
            var challenge = UnitOfWork.Find<Challenge>(challengeId);

            if (accepted)
            {
                var board = new Board();
                var squares = board.Squares.SelectMany(s => s);

                var game = new Game
                {
                    DarkPlayer = challenge.DarkPlayer,
                    LightPlayer = challenge.LightPlayer,
                    Name = challenge.Title,
                    Challenge = challenge,
                };

                foreach (var square in squares)
                {
                    square.Game = game;
                    square.ChessPiece = (ChessPiece) square.ChessPiece; //TODO: Why did I do this?
                    UnitOfWork.Add(square);
                }

                UnitOfWork.Add(game);
            }

            challenge.Accepted = accepted;
            UnitOfWork.Commit();
        }
        private GameModel GetGameModel(Game game)
        {
            var squares = game.Squares.ToArray();

            squares = RepairMissingSquares(game, squares);

            var moves = game.Moves.ToList();
            var board = new Board(squares);

            var boardModel = AutoMapper.Mapper.Map<BoardViewModel>(board);
            var darkPlayerModel = AutoMapper.Mapper.Map<PlayerViewModel>(game.DarkPlayer);
            var lightPlayerModel = AutoMapper.Mapper.Map<PlayerViewModel>(game.LightPlayer);
            var winnerPlayer = (PlayerViewModel)null;

            if (game.WinnerPlayer != null)
                winnerPlayer = AutoMapper.Mapper.Map<PlayerViewModel>(game.WinnerPlayer);

            var gameModel = new GameModel()
            {
                Board = boardModel,
                Name = game.Name,
                DarkScore = game.DarkScore,
                LightScore = game.LightScore,
                Id = game.Id,
                MoveCount = game.MoveCount,
                PlayerDark = darkPlayerModel,
                PlayerLight = lightPlayerModel,
                Moves = moves,
                IsCurrentPlayersMove = IsPlayersMove(game, CurrentUser),
                Complete = game.Complete,
                Winner = winnerPlayer,
            };

            return gameModel;
        }
Example #4
0
 public GameManager(Game game)
 {
     Game = game;
     var squares = game.Squares.Select(s => s).ToArray();
     _board = new Board(squares);
 }