/// <inheritdoc />
        public ChessGameDetails ConvertToChessGameDetails(DbChessGame source)
        {
            if (source == null)
            {
                return(null);
            }

            var history = source.History?.OrderBy(x => x.CreatedAt).Select(CovertToChessMove).ToList() ?? new List <BaseMove>();

            var representation = new ChessRepresentationInitializer().Create();
            var chessMechanism = new ChessMechanism();

            foreach (var move in history)
            {
                representation = chessMechanism.ApplyMove(representation, move);
            }

            return(new ChessGameDetails
            {
                ChallengeDate = source.ChallengeDate,
                Id = source.Id,
                InitiatedBy = ConvertUser(source.InitiatedBy),
                LastMoveDate = source.LastMoveDate,
                Name = source.Name,
                Opponent = ConvertUser(source.Opponent),
                BlackPlayer = ConvertUser(source.BlackPlayer),
                WhitePlayer = ConvertUser(source.WhitePlayer),
                Representation = representation
            });
        }
Exemple #2
0
        private IEnumerable <PlayerPoints> GetPlayersPoints(DbChessGame game)
        {
            if (game.BlackPlayer.Id == game.WhitePlayer.Id)
            {
                return(Enumerable.Empty <PlayerPoints>());
            }

            var whitePlayer = new PlayerPoints()
            {
                Username         = game.WhitePlayer.UserName,
                IsBot            = game.WhitePlayer.Bot,
                AveragePlyPoints = GetPoints(ChessPlayer.White, game.Status) / GetNumberOfMoves(ChessPlayer.White, game.History)
            };

            var blackPlayer = new PlayerPoints()
            {
                Username         = game.BlackPlayer.UserName,
                IsBot            = game.BlackPlayer.Bot,
                AveragePlyPoints = GetPoints(ChessPlayer.Black, game.Status) / GetNumberOfMoves(ChessPlayer.Black, game.History)
            };

            return(new[]
            {
                whitePlayer,
                blackPlayer
            });
        }
        /// <summary>
        /// Gets a dictionary of player usernames based on their playing colour in the game.
        /// </summary>
        /// <param name="game">The chess game coming from database.</param>
        /// <returns>Dictionary of game players.</returns>
        public static Dictionary <ChessPlayer, string> GetPlayerNames(this DbChessGame game)
        {
            var dictionary = new Dictionary <ChessPlayer, string>(2);

            dictionary.Add(ChessPlayer.White, game.WhitePlayer.UserName);
            dictionary.Add(ChessPlayer.Black, game.BlackPlayer.UserName);

            return(dictionary);
        }
        /// <inheritdoc />
        public ChallengeRequestResult Add(string participantPlayerName, ChallengeRequest challengeRequest)
        {
            var initiatedBy = _dbContext.Users.SingleOrDefault(x => x.UserName == participantPlayerName);

            if (initiatedBy == null)
            {
                return(new ChallengeRequestResult
                {
                    RequestResult = ChallengeRequestResultStatuses.InitiatedByUserNull,
                    NewlyCreatedGame = null
                });
            }

            var opponent = _dbContext.Users.SingleOrDefault(x => x.UserName == challengeRequest.Opponent);

            if (opponent == null)
            {
                return(new ChallengeRequestResult
                {
                    RequestResult = ChallengeRequestResultStatuses.OpponentNull,
                    NewlyCreatedGame = null
                });
            }

            var now = DateTime.UtcNow;

            // Randomize sides
            var players = new[] { initiatedBy, opponent }.OrderBy(x => Guid.NewGuid()).ToArray();
            var white = players[0];
            var black = players[1];

            var newGame = new DbChessGame()
            {
                ChallengeDate = now,
                InitiatedBy   = initiatedBy,
                Opponent      = opponent,
                WhitePlayer   = white,
                BlackPlayer   = black,
                Name          = $"{initiatedBy.UserName} vs {opponent.UserName}",
                LastMoveDate  = now,
                Status        = GameState.InProgress
            };

            var newEntity = _dbContext.Add(newGame).Entity;

            _dbContext.SaveChanges();

            return(new ChallengeRequestResult
            {
                RequestResult = ChallengeRequestResultStatuses.Ok,
                NewlyCreatedGame = _chessGameConverter.ConvertToChessGameDetails(newEntity)
            });
        }
        /// <inheritdoc />
        public ChessGame ConvertToChessGame(DbChessGame source)
        {
            if (source == null)
            {
                return(null);
            }

            return(new ChessGame
            {
                ChallengeDate = source.ChallengeDate,
                Id = source.Id,
                InitiatedBy = ConvertUser(source.InitiatedBy),
                LastMoveDate = source.LastMoveDate,
                Name = source.Name,
                Opponent = ConvertUser(source.Opponent),
                BlackPlayer = ConvertUser(source.BlackPlayer),
                WhitePlayer = ConvertUser(source.WhitePlayer),
                Outcome = source.Status
            });
        }