예제 #1
0
        public void CompleteMatch(ref ChessMatchModel chessMatch)
        {
            var statId = _statsHandler.AddStat(chessMatch);

            chessMatch.IdOfStat = statId;
            PrettyConsole.Log(LogSeverity.Info, "Complete ChessMatch",
                              $"Completed Chess Match With Id: {chessMatch.Id}");
        }
예제 #2
0
 public void UpdateMatch(ChessMatchModel chessMatch)
 {
     if (chessMatch == null)
     {
         return;
     }
     if (chessMatch.Winner != 1)
     {
         CompleteMatch(ref chessMatch);
     }
     _database.Update <ChessMatchModel>($"{chessMatch.Id}", chessMatch);
 }
        public string AddStat(ChessMatchModel chessMatch)
        {
            var id = $"{Guid.NewGuid()}";

            return(_database.Create <ChessMatchStatsModel>(ref id, new ChessMatchStatsModel
            {
                Id = id,
                GuildId = chessMatch.GuildId,
                Participants = new[] { chessMatch.ChallengerId, chessMatch.ChallengeeId },
                CreatedBy = chessMatch.ChallengerId,
                Winner = chessMatch.Winner,
                EndBy = chessMatch.EndBy,
                EndDate = DateTime.Now,
                MoveCount = (ulong)chessMatch.HistoryList.Count
            }).Id);
        }
예제 #4
0
        private async Task <ImageLinkModel> DrawBoard(ChessMatchModel match)
        {
            await Task.Run(async() =>
            {
                var board = Image.Load(_assetService.GetImagePath("Chess", "board.png"));

                var httpClient      = new HttpClient();
                var turnIndicator   = Image.Load(_assetService.GetImagePath("Chess", "turn_indicator.png"));
                var whiteAvatarData = Image.Load(await httpClient.GetByteArrayAsync(match.WhiteAvatarUrl));
                var blackAvatarData = Image.Load(await httpClient.GetByteArrayAsync(match.BlackAvatarUrl));
                httpClient.Dispose();

                var moves = match.HistoryList.Select(x => x.Move).ToList();

                var game = moves.Count != 0 ? new ChessGame(moves, true) : new ChessGame();

                var boardPieces = game.GetBoard();

                var lastMove     = match.HistoryList.OrderByDescending(x => x.MoveDate).FirstOrDefault();
                var rankToRowMap = new Dictionary <int, int>
                {
                    { 1, 7 },
                    { 2, 6 },
                    { 3, 5 },
                    { 4, 4 },
                    { 5, 3 },
                    { 6, 2 },
                    { 7, 1 },
                    { 8, 0 }
                };
                var turnIndicatorPoint = game.WhoseTurn != Player.Black ? new Point(538, 367) : new Point(40, 15);
                board.Mutate(processor =>
                {
                    #region Mutating the board

                    for (var x = 0; x < boardPieces.Length; ++x)
                    {
                        for (var y = 0; y < boardPieces[x].Length; ++y)
                        {
                            if (lastMove != null && (lastMove.Move.OriginalPosition.File == (File)x &&
                                                     rankToRowMap[lastMove.Move.OriginalPosition.Rank] == y ||
                                                     lastMove.Move.NewPosition.File == (File)x &&
                                                     rankToRowMap[lastMove.Move.NewPosition.Rank] == y))
                            {
                                DrawImage(processor, "yellow_square", x, y);
                            }
                            var piece = boardPieces[y][x];
                            if (piece == null)
                            {
                                continue;
                            }
                            if (piece.GetFenCharacter().ToString().ToUpper() == "K" && game.IsInCheck(piece.Owner))
                            {
                                DrawImage(processor, "red_square", x, y);
                            }
                            var str = "white";
                            if (new[] { 'r', 'n', 'b', 'q', 'k', 'p' }.Contains(piece.GetFenCharacter()))
                            {
                                str = "black";
                            }
                            DrawImage(processor, string.Format("{0}_{1}", str, piece.GetFenCharacter()), x, y);
                        }
                    }

                    var blackPawnCount = game.PiecesOnBoard.Count(x =>
                                                                  x.GetFenCharacter() == 'p' && x.Owner == Player.Black && !x.IsPromotionResult);
                    var whitePawnCount = game.PiecesOnBoard.Count(x =>
                                                                  x.GetFenCharacter() == 'P' && x.Owner == Player.White && !x.IsPromotionResult);
                    var blackRookCount = game.PiecesOnBoard.Count(x =>
                                                                  x.GetFenCharacter() == 'r' && x.Owner == Player.Black && !x.IsPromotionResult);
                    var whiteRookCount = game.PiecesOnBoard.Count(x =>
                                                                  x.GetFenCharacter() == 'R' && x.Owner == Player.White && !x.IsPromotionResult);
                    var blackKnightCount = game.PiecesOnBoard.Count(x =>
                                                                    x.GetFenCharacter() == 'n' && x.Owner == Player.Black && !x.IsPromotionResult);
                    var whiteKnightCount = game.PiecesOnBoard.Count(x =>
                                                                    x.GetFenCharacter() == 'N' && x.Owner == Player.White && !x.IsPromotionResult);
                    var blackBishopCount = game.PiecesOnBoard.Count(x =>
                                                                    x.GetFenCharacter() == 'b' && x.Owner == Player.Black && !x.IsPromotionResult);
                    var whiteBishopCount = game.PiecesOnBoard.Count(x =>
                                                                    x.GetFenCharacter() == 'B' && x.Owner == Player.White && !x.IsPromotionResult);
                    var blackQueenCount = game.PiecesOnBoard.Count(x =>
                                                                   x.GetFenCharacter() == 'q' && x.Owner == Player.Black && !x.IsPromotionResult);
                    var whiteQueenCount = game.PiecesOnBoard.Count(x =>
                                                                   x.GetFenCharacter() == 'Q' && x.Owner == Player.White && !x.IsPromotionResult);
                    var row = 1;

                    var blackPawn = Image.Load(_assetService.GetImagePath("Chess", "black_p.png"));

                    for (var index = 8; index > blackPawnCount; --index)
                    {
                        if (index % 2 == 0)
                        {
                            processor.DrawImage(blackPawn, new Size(30, 30), new Point(533, 16 + row * 30),
                                                new GraphicsOptions());
                        }
                        else
                        {
                            processor.DrawImage(blackPawn, new Size(30, 30), new Point(566, 16 + row * 30),
                                                new GraphicsOptions());
                            ++row;
                        }
                    }

                    row = 8;

                    var image2 = Image.Load(_assetService.GetImagePath("Chess", "white_P.png"));
                    for (var index = 8; index > whitePawnCount; --index)
                    {
                        if (index % 2 == 0)
                        {
                            processor.DrawImage(image2, new Size(30, 30), new Point(20, 125 + row * 30),
                                                new GraphicsOptions());
                        }
                        else
                        {
                            processor.DrawImage(image2, new Size(30, 30), new Point(53, 125 + row * 30),
                                                new GraphicsOptions());
                            --row;
                        }
                    }

                    row = 5;

                    var image3 = Image.Load(_assetService.GetImagePath("Chess", "black_r.png"));
                    for (var index = 2; index > blackRookCount; --index)
                    {
                        if (index % 2 == 0)
                        {
                            processor.DrawImage(image3, new Size(30, 30), new Point(533, 16 + row * 30),
                                                new GraphicsOptions());
                        }
                        else
                        {
                            processor.DrawImage(image3, new Size(30, 30), new Point(566, 16 + row * 30),
                                                new GraphicsOptions());
                            ++row;
                        }
                    }

                    row = 4;

                    var whiteRook = Image.Load(_assetService.GetImagePath("Chess", "white_R.png"));
                    for (var index = 2; index > whiteRookCount; --index)
                    {
                        if (index % 2 == 0)
                        {
                            processor.DrawImage(whiteRook, new Size(30, 30), new Point(20, 125 + row * 30),
                                                new GraphicsOptions());
                        }
                        else
                        {
                            processor.DrawImage(whiteRook, new Size(30, 30), new Point(53, 125 + row * 30),
                                                new GraphicsOptions());
                            --row;
                        }
                    }

                    row = 6;

                    var blackKnight = Image.Load(_assetService.GetImagePath("Chess", "black_n.png"));
                    for (var index = 2; index > blackKnightCount; --index)
                    {
                        if (index % 2 == 0)
                        {
                            processor.DrawImage(blackKnight, new Size(30, 30), new Point(533, 16 + row * 30),
                                                new GraphicsOptions());
                        }
                        else
                        {
                            processor.DrawImage(blackKnight, new Size(30, 30), new Point(566, 16 + row * 30),
                                                new GraphicsOptions());
                            ++row;
                        }
                    }

                    row = 3;

                    var whiteKnight = Image.Load(_assetService.GetImagePath("Chess", "white_N.png"));
                    for (var i = 2; i > whiteKnightCount; --i)
                    {
                        if (i % 2 == 0)
                        {
                            processor.DrawImage(whiteKnight, new Size(30, 30), new Point(20, 125 + row * 30),
                                                new GraphicsOptions());
                        }
                        else
                        {
                            processor.DrawImage(whiteKnight, new Size(30, 30), new Point(53, 125 + row * 30),
                                                new GraphicsOptions());
                            --row;
                        }
                    }

                    row = 7;

                    var blackBishop = Image.Load(_assetService.GetImagePath("Chess", "black_b.png"));
                    for (var index = 2; index > blackBishopCount; --index)
                    {
                        if (index % 2 == 0)
                        {
                            processor.DrawImage(blackBishop, new Size(30, 30), new Point(533, 16 + row * 30),
                                                new GraphicsOptions());
                        }
                        else
                        {
                            processor.DrawImage(blackBishop, new Size(30, 30), new Point(566, 16 + row * 30),
                                                new GraphicsOptions());
                            ++row;
                        }
                    }

                    row = 2;

                    var whiteBishop = Image.Load(_assetService.GetImagePath("Chess", "white_B.png"));
                    for (var index = 2; index > whiteBishopCount; --index)
                    {
                        if (index % 2 == 0)
                        {
                            processor.DrawImage(whiteBishop, new Size(30, 30), new Point(20, 125 + row * 30),
                                                new GraphicsOptions());
                        }
                        else
                        {
                            processor.DrawImage(whiteBishop, new Size(30, 30), new Point(53, 125 + row * 30),
                                                new GraphicsOptions());
                            --row;
                        }
                    }

                    row = 8;

                    var blackQueen = Image.Load(_assetService.GetImagePath("Chess", "black_q.png"));
                    if (blackQueenCount == 0)
                    {
                        processor.DrawImage(blackQueen, new Size(30, 30), new Point(533, 16 + row * 30),
                                            new GraphicsOptions());
                    }
                    row = 1;

                    var whiteQueen = Image.Load(_assetService.GetImagePath("Chess", "white_Q.png"));
                    if (whiteQueenCount == 0)
                    {
                        processor.DrawImage(whiteQueen, new Size(30, 30), new Point(20, 125 + row * 30),
                                            new GraphicsOptions());
                    }

                    processor.DrawImage(turnIndicator, new Size(56, 56), turnIndicatorPoint, new GraphicsOptions());
                    processor.DrawImage(whiteAvatarData, new Size(50, 50), new Point(541, 370), new GraphicsOptions());
                    processor.DrawImage(blackAvatarData, new Size(50, 50), new Point(43, 18), new GraphicsOptions());

                    var table = Image.Load(_assetService.GetImagePath("Chess", "table.png"));
                    processor.DrawImage(table, new Size(108, 148), new Point(596, 18), new GraphicsOptions());

                    if (!SystemFonts.TryFind("Arial", out var font))
                    {
                        PrettyConsole.Log(LogSeverity.Critical, "Image drawing", "Cannot find font 'Arial' install Arial as a font and try again");
                        Console.ReadLine();
                        Environment.Exit(0);
                    }

                    processor.DrawText("Last 5 moves", font.CreateFont(12, FontStyle.Bold), Rgba32.Black, new PointF(616, 19));
                    processor.DrawText("White", font.CreateFont(12, FontStyle.Bold), Rgba32.Black, new PointF(606, 42));
                    processor.DrawText("Black", font.CreateFont(12, FontStyle.Bold), Rgba32.Black, new PointF(660, 42));
                    var blackMoves = match.HistoryList.OrderByDescending(x => x.MoveDate).Where(x => x.Player == Player.Black).ToList();
                    var whiteMoves = match.HistoryList.OrderByDescending(x => x.MoveDate).Where(x => x.Player == Player.White).ToList();

                    for (var i = 0; i < blackMoves.Count && i < 5; i++)
                    {
                        var safeguard = i;
                        var player    = blackMoves[i].Player.ToString().ToLower();
                        var file      = blackMoves[i].Move.NewPosition.File;
                        var rank      = blackMoves[i].Move.NewPosition.Rank;
                        var fenChar   = blackMoves[i].MovedfenChar;
                        var image     = Image.Load(_assetService.GetImagePath("Chess", $"{player}_{fenChar}.png"));
                        if (blackMoves.Count != whiteMoves.Count)
                        {
                            safeguard++;
                        }
                        if (whiteMoves.Count > 5 && safeguard == 5)
                        {
                            continue;
                        }
                        processor.DrawImage(image, new Size(12, 12), new Point(660, 65 + safeguard * 21), new GraphicsOptions());

                        processor.DrawText($"{file.ToString() + rank}", font.CreateFont(12, FontStyle.Bold), Rgba32.Black, new PointF(672, 63 + safeguard * 21));
                    }
                    for (var i = 0; i < whiteMoves.Count && i < 5; i++)
                    {
                        var player  = whiteMoves[i].Player.ToString().ToLower();
                        var file    = whiteMoves[i].Move.NewPosition.File;
                        var rank    = whiteMoves[i].Move.NewPosition.Rank;
                        var fenChar = whiteMoves[i].MovedfenChar;
                        var image   = Image.Load(_assetService.GetImagePath("Chess", $"{player}_{fenChar}.png"));
                        processor.DrawImage(image, new Size(12, 12), new Point(606, 65 + i * 21), new GraphicsOptions());

                        processor.DrawText($"{file.ToString() + rank}", font.CreateFont(12, FontStyle.Bold), Rgba32.Black, new PointF(618, 63 + i * 21));
                    }

                    #endregion
                });
                board.Save($"{Directory.GetCurrentDirectory()}\\Chessboards\\board{match.Id}-{match.HistoryList.Count}.png");
            });

            var nextPlayer = WhoseTurn(new ChessMatchStatusModel
            {
                Match = new ChessMatchModel
                {
                    HistoryList  = match.HistoryList,
                    ChallengeeId = match.ChallengeeId,
                    ChallengerId = match.ChallengerId
                }
            });

            return(new ImageLinkModel
            {
                ImageLink = $"attachment://board{match.Id}-{match.HistoryList.Count}.png",
                NextPlayer = nextPlayer
            });
        }