Exemple #1
0
        public IActionResult AntichessRvsNStart()
        {
            IActionResult result;

            do
            {
                Piece[][] board = BoardExtensions.GenerateEmptyBoard()
                                  .AddWhiteRook()
                                  .AddBlackKnight();
                result = StartNewSession(board, "Antichess");
            } while (result == null);
            return(result);
        }
        public IActionResult KRNvsKWithSeparatedKingsStart()
        {
            IActionResult result;

            do
            {
                Piece[][] board = BoardExtensions.GenerateEmptyBoard()
                                  .AddSeparatedKings()
                                  .AddWhiteRook()
                                  .AddWhiteKnight();
                result = StartNewSession(board, "Atomic");
            } while (result == null);
            return(result);
        }
Exemple #3
0
        public async Task PrivateBoardShowsResult()
        {
            user = Auth.CurrentUser = await GetUser("[email protected]", null);

            BoardExtensions.Board = await BoardExtensions.BoardLookup(services, "benefact");

            var rpc    = new BoardsInterface(services);
            var result = await rpc.Get(null);

            Assert.AreEqual("benefact", result.UrlName);
            Assert.AreEqual(null, result.UserPrivilege);
            Assert.AreEqual(Privilege.None, result.DefaultPrivilege);
            Assert.IsNotNull(result.Description);
        }
Exemple #4
0
        public async Task ArchiveCardNotOwnerFails()
        {
            var cardId = (await services.DoWithDB(db => db.Cards.FirstOrDefaultAsync())).Id;

            user = Auth.CurrentUser = await GetUser("[email protected]", Privilege.Read);

            BoardExtensions.Board = await BoardExtensions.BoardLookup(services, "benefact");

            var rpc   = new CardsInterface(services);
            var error = await Assert.ThrowsExceptionAsync <HTTPError>(
                () => rpc.Archive(new CardArchiveRequest()
            {
                CardId = cardId
            }));
        }
Exemple #5
0
        public async Task CreateInviteReturnsNew()
        {
            user = Auth.CurrentUser = await GetUser("[email protected]", Privilege.Admin);

            BoardExtensions.Board = await BoardExtensions.BoardLookup(services, "benefact");

            var rpc         = new BoardsInterface(services);
            var firstResult = await rpc.Invite(new CreateInviteRequest()
            {
                Privilege = (Privilege)7
            });

            Assert.IsNotNull(firstResult);
            var secondResult = await rpc.Invite(new CreateInviteRequest()
            {
                Privilege = (Privilege)3
            });

            Assert.AreNotEqual(firstResult, secondResult);
        }
        public async Task <ActionResult> Get(int?id, string boardId)
        {
            BoardExtensions.Board = await BoardExtensions.BoardLookup(Services, boardId);

            Auth.ThrowIfUnauthorized(privilege: Privilege.Read);
            if (id == null)
            {
                throw new HTTPError("Invalid file id", 400);
            }
            var attachment = (await Services.DoWithDB(db =>
                                                      db.Attachments.Include(a => a.Storage).BoardFilter(id.Value).FirstOrDefaultAsync()
                                                      ));

            if (attachment == null)
            {
                throw new HTTPError("File not found", 404);
            }

            return(new FileContentResult(attachment.Storage.Data, new MediaTypeHeaderValue(attachment.ContentType)));
        }
Exemple #7
0
        public async Task ArchiveCardOwnerSucceeds()
        {
            var cardId = (await services.DoWithDB(db => db.Cards.FirstOrDefaultAsync())).Id;

            user = Auth.CurrentUser = await GetUser("*****@*****.**", Privilege.Read);

            BoardExtensions.Board = await BoardExtensions.BoardLookup(services, "benefact");

            var rpc = new CardsInterface(services);
            await rpc.Archive(new CardArchiveRequest()
            {
                CardId = cardId
            });

            var archive = (await services.DoWithDB(db => db.Cards
                                                   .Where(c => c.Id == cardId)
                                                   .FirstOrDefaultAsync())).Archived;

            Assert.IsTrue(archive);
        }
Exemple #8
0
        public async Task PrivateBoardJoinSucceeds()
        {
            user = Auth.CurrentUser = await GetUser("*****@*****.**", Privilege.Admin);

            BoardExtensions.Board = await BoardExtensions.BoardLookup(services, "benefact");

            var rpc         = new BoardsInterface(services);
            var firstResult = await rpc.Invite(new CreateInviteRequest()
            {
                Privilege = (Privilege)7
            });

            Assert.IsNotNull(firstResult);
            user = Auth.CurrentUser = await GetUser("[email protected]", null);

            var joinResult = await rpc.Join(new JoinRequest()
            {
                Key = firstResult
            });

            Assert.AreEqual(7, (int)joinResult.Privilege);
        }
Exemple #9
0
        public async Task CardStateUpdates()
        {
            user = Auth.CurrentUser = await GetUser("*****@*****.**", Privilege.Admin);

            BoardExtensions.Board = await BoardExtensions.BoardLookup(services, "benefact");

            Auth.CurrentRole = user.Roles.FirstOrDefault(r => r.BoardId == BoardExtensions.Board?.Id);
            var query = new CardQuery()
            {
                Groups = new Dictionary <string, List <CardQueryTerm> >()
                {
                    { "Done", new List <CardQueryTerm>()
                      {
                          new CardQueryTerm()
                          {
                              State = CardState.InProgress
                          }
                      } }
                }
            };
            var boards   = new BoardsInterface(services);
            var response = await boards.Get(query);

            var cards      = response.Cards.First().Value;
            var doneColumn = response.Columns.First(c => c.State == CardState.Complete);

            Assert.IsTrue(cards.Any());
            var rpc = new CardsInterface(services);
            await rpc.Update(new CardData()
            {
                ColumnId = doneColumn.Id,
                Id       = cards.First().Id,
            });

            var response2 = await boards.Get(query);

            Assert.IsTrue(response2.Cards.Values.First().Count < cards.Count);
        }
        public async Task <AttachmentData> Add(string boardId)
        {
            BoardExtensions.Board = await BoardExtensions.BoardLookup(Services, boardId);

            Auth.ThrowIfUnauthorized(privilege: Privilege.Contribute);
            var file = Request.Form.Files.FirstOrDefault();

            if (file == null)
            {
                throw new HTTPError("Post contains no files", 401);
            }
            if (!Request.Form.TryGetValue("CardId", out var cardIdString) || !int.TryParse(cardIdString, out var cardId))
            {
                throw new HTTPError("Invalid CardId", 401);
            }
            var stream = new MemoryStream();
            await file.CopyToAsync(stream);

            var attachment = new AttachmentData()
            {
                BoardId = BoardExtensions.Board.Id,
                Storage = new StorageEntry()
                {
                    Data = stream.ToArray(),
                },
                Name        = file.FileName,
                ContentType = file.ContentType,
                CardId      = cardId,
                UserId      = Auth.CurrentUser.Id,
            };

            return(await Services.DoWithDB(async db =>
            {
                await db.Attachments.AddAsync(attachment);
                return attachment;
            }));
        }
Exemple #11
0
        public void GetAllLinesOfBoard_ValidSmallBoardInformation_ShouldReturnAllLines()
        {
            // arrange
            var board = new SmallBoardInformation[3][];

            for (var x = 0; x < 3; x++)
            {
                board[x] = new SmallBoardInformation[3];
                for (var y = 0; y < 3; y++)
                {
                    board[x][y] = new SmallBoardInformation
                    {
                        Value = TileValue.Empty,
                    };
                }
            }

            // act
            IEnumerable <IEnumerable <TileInformation> > result =
                BoardExtensions.GetAllLinesOfBoard(board);

            // assert
            result.Should().HaveCount(8);
        }
        private static List <Move> GetIndividualKingBeats(Cell king, CellState[,] boardArray, Team teamPlaying, List <Move> beats)
        {
            var clonedBeats   = Clone.CloneJson(beats);
            var possibleGoTos = new List <Cell>();

            // top right
            for (var count = 1; king.X + count <= 7 && king.Y - count >= 0; count++)
            {
                if ((boardArray[king.X + count, king.Y - count] == CellState.BlackKing && teamPlaying == Team.White) ||
                    (boardArray[king.X + count, king.Y - count] == CellState.BlackPiece && teamPlaying == Team.White) ||
                    (boardArray[king.X + count, king.Y - count] == CellState.WhiteKing && teamPlaying == Team.Black) ||
                    (boardArray[king.X + count, king.Y - count] == CellState.WhitePiece && teamPlaying == Team.Black))
                {
                    count++;
                    for (; king.X + count <= 7 && king.Y - count >= 0; count++)
                    {
                        if (boardArray[king.X + count, king.Y - count] != CellState.Empty)
                        {
                            break;
                        }
                        if (boardArray[king.X + count, king.Y - count] == CellState.Empty)
                        {
                            possibleGoTos.Add(new Cell
                            {
                                X = king.X + count,
                                Y = king.Y - count,
                            });
                        }
                    }

                    break;
                }
                else if (boardArray[king.X + count, king.Y - count] != CellState.Empty)
                {
                    break;
                }
            }
            // top left
            for (var count = 1; king.X - count >= 0 && king.Y - count >= 0; count++)
            {
                if ((boardArray[king.X - count, king.Y - count] == CellState.BlackKing && teamPlaying == Team.White) ||
                    (boardArray[king.X - count, king.Y - count] == CellState.BlackPiece && teamPlaying == Team.White) ||
                    (boardArray[king.X - count, king.Y - count] == CellState.WhiteKing && teamPlaying == Team.Black) ||
                    (boardArray[king.X - count, king.Y - count] == CellState.WhitePiece && teamPlaying == Team.Black))
                {
                    count++;
                    for (; king.X - count >= 0 && king.Y - count >= 0; count++)
                    {
                        if (boardArray[king.X - count, king.Y - count] != CellState.Empty)
                        {
                            break;
                        }
                        if (boardArray[king.X - count, king.Y - count] == CellState.Empty)
                        {
                            possibleGoTos.Add(new Cell
                            {
                                X = king.X - count,
                                Y = king.Y - count,
                            });
                        }
                    }

                    break;
                }
                else if (boardArray[king.X - count, king.Y - count] != CellState.Empty)
                {
                    break;
                }
            }
            // bottom left
            for (var count = 1; king.X - count >= 0 && king.Y + count <= 7; count++)
            {
                if ((boardArray[king.X - count, king.Y + count] == CellState.BlackKing && teamPlaying == Team.White) ||
                    (boardArray[king.X - count, king.Y + count] == CellState.BlackPiece && teamPlaying == Team.White) ||
                    (boardArray[king.X - count, king.Y + count] == CellState.WhiteKing && teamPlaying == Team.Black) ||
                    (boardArray[king.X - count, king.Y + count] == CellState.WhitePiece && teamPlaying == Team.Black))
                {
                    count++;
                    for (; king.X - count >= 0 && king.Y + count <= 7; count++)
                    {
                        if (boardArray[king.X - count, king.Y + count] != CellState.Empty)
                        {
                            break;
                        }
                        if (boardArray[king.X - count, king.Y + count] == CellState.Empty)
                        {
                            possibleGoTos.Add(new Cell
                            {
                                X = king.X - count,
                                Y = king.Y + count,
                            });
                        }
                    }

                    break;
                }
                else if (boardArray[king.X - count, king.Y + count] != CellState.Empty)
                {
                    break;
                }
            }
            // bottom right
            for (var count = 1; king.X + count <= 7 && king.Y + count <= 7; count++)
            {
                if ((boardArray[king.X + count, king.Y + count] == CellState.BlackKing && teamPlaying == Team.White) ||
                    (boardArray[king.X + count, king.Y + count] == CellState.BlackPiece && teamPlaying == Team.White) ||
                    (boardArray[king.X + count, king.Y + count] == CellState.WhiteKing && teamPlaying == Team.Black) ||
                    (boardArray[king.X + count, king.Y + count] == CellState.WhitePiece && teamPlaying == Team.Black))
                {
                    count++;
                    for (; king.X + count <= 7 && king.Y + count <= 7; count++)
                    {
                        if (boardArray[king.X + count, king.Y + count] != CellState.Empty)
                        {
                            break;
                        }
                        if (boardArray[king.X + count, king.Y + count] == CellState.Empty)
                        {
                            possibleGoTos.Add(new Cell
                            {
                                X = king.X + count,
                                Y = king.Y + count,
                            });
                        }
                    }

                    break;
                }
                else if (boardArray[king.X + count, king.Y + count] != CellState.Empty)
                {
                    break;
                }
            }

            if (possibleGoTos.Count == 0)
            {
                return(new List <Move>());
            }

            List <List <Move> > possibleBeats = new List <List <Move> >();

            possibleGoTos.ForEach((possibleGoTo) =>
            {
                var move = new Move
                {
                    StartingPoint = king,
                    EndingPoint   = possibleGoTo,
                };

                var newBoardArray = BoardExtensions.UpdateFromMoves(boardArray, new List <Move>()
                {
                    move
                });

                var beatsList = new List <Move>();
                beatsList.Add(move);
                var newBeats = GetIndividualKingBeats(possibleGoTo, newBoardArray, teamPlaying, beatsList);
                if (newBeats.Count > 1)
                {
                    beatsList = newBeats;
                }
                possibleBeats.Add(beatsList);
            });

            if (possibleBeats.Count > 0)
            {
                clonedBeats.AddRange(possibleBeats.OrderByDescending(x => x.Count()).First());
            }
            return(clonedBeats);
        }
Exemple #13
0
 /// <summary>
 ///     Gets the index of the ingredient.
 /// </summary>
 /// <param name="cellIdx">Index of the cell.</param>
 /// <param name="source">The source.</param>
 /// <returns></returns>
 /// <exception cref="System.ArgumentOutOfRangeException">source;null</exception>
 public int GetCellIndex(int cellIdx, TravelSource source)
 {
     return(BoardExtensions.GetCellIndex(cellIdx, ColumnsCount, RowsCount, source));
 }