static void ExpandNode(this IBoard <IHex> board, StepCost directedStepCost, BoardStorage <int> costs, Queue queue, IHex here, int key, Hexside hexside ) { var neighbourCoords = here.Coords.GetNeighbour(hexside); board[neighbourCoords].IfHasValueDo(neighbour => { var cost = directedStepCost(here, hexside); if (cost > 0 && costs[neighbourCoords] < 0) { Trace($" Enqueue {neighbourCoords}: {cost,4}"); queue.Enqueue(key + cost, neighbour); } }); //if (neighbourHex != null) { // var cost = directedStepCost(here, hexside); // if (cost > 0 && costs[neighbourCoords] < 0) { // Trace($" Enqueue {neighbourCoords}: {cost,4}"); // queue.Enqueue(key + cost,neighbourHex); // } //} }
public void DeleteBoard_ProvideEmptyId_ErrorReturned() { var ctx = GetContext(); var boardStorage = new BoardStorage(ctx); var deleteResult = boardStorage.DeleteBoardAsync(new Board { Id = Guid.Empty }); Assert.False(deleteResult.Result.IsSuccess); Assert.NotEmpty(deleteResult.Result.ErrorReason); }
private void Action( IPriorityQueue <int, HexCoords> queue, BoardStorage <short?> store, TryDirectedCost tryDirectedStepCost, HexCoords here, int key, Hexside hexside ) { var neighbour = here.GetNeighbour(hexside); tryDirectedStepCost(here, hexside).IfHasValueDo(stepCost => { store[neighbour].ElseDo(() => Enqueue((short)(key + stepCost), neighbour, store)); }); }
public void UpdateBoard_ExceptionTrownByExternalCode_ExceptionHandled() { var ctxMocked = new Mock <ApiDbContext>(new DbContextOptionsBuilder <ApiDbContext>().UseInMemoryDatabase(Guid.NewGuid().ToString()).Options); ctxMocked.Setup(x => x.Find(It.IsAny <Type>())).Throws <Exception>(); var boardStorage = new BoardStorage(ctxMocked.Object); var updateResult = boardStorage.UpdateBoardTitleAsync(new Board { Id = Guid.NewGuid(), Title = "test" }); Assert.False(updateResult.Result.IsSuccess); Assert.Contains("Updating the board unexpectedly failed", updateResult.Result.ErrorReason); }
public void DeleteBoard_DeleteExistentBoard_BoardDeleted() { var boardToDelete = new Board { Title = "Title", Id = Guid.NewGuid() }; var ctx = GetContext(); ctx.Boards.Add(boardToDelete); ctx.SaveChanges(); var boardStorage = new BoardStorage(ctx); var deleteResult = boardStorage.DeleteBoardAsync(boardToDelete); Assert.NotNull(deleteResult); Assert.True(deleteResult.Result.IsSuccess); Assert.Null(ctx.Boards.Find(boardToDelete.Id)); }
private void ExpandNode(BoardStorage <short> store, Func <IHex, Hexside, IHex, int> directedStepCost, IPriorityQueue <int, IHex> queue, IHex here, int key, Hexside hexside ) { var neighbourCoords = here.Coords.GetNeighbour(hexside); var neighbourHex = Board[neighbourCoords]; if (neighbourHex != null) { var cost = directedStepCost(here, hexside, neighbourHex); if (cost > 0 && store[neighbourCoords] == -1) { FindPathDetailTrace(" Enqueue {0}: {1,4}", neighbourCoords, cost); queue.Enqueue(key + cost, neighbourHex); } } }
public void AddBoard_AddNewValidBoard_BoardSaved() { var title = "Test board"; var ctx = GetContext(); var boardStorage = new BoardStorage(ctx); var boardToAdd = new Board { Title = title }; var addResult = boardStorage.AddBoardAsync(boardToAdd); Assert.NotNull(addResult); Assert.True(addResult.Result.IsSuccess); var boardFromCtx = ctx.Boards.Find(addResult.Result.ResultEntity); Assert.NotNull(boardFromCtx); Assert.Equal(title, boardFromCtx.Title); }
public void UpdateBoard_UpdateExistentBoard_BoardUpdated() { var boardToUpdate = new Board { Title = "Title", Id = Guid.NewGuid() }; var ctx = GetContext(); ctx.Boards.Add(boardToUpdate); ctx.SaveChanges(); boardToUpdate.Title = "New Title"; var boardStorage = new BoardStorage(ctx); var updateRedult = boardStorage.UpdateBoardTitleAsync(boardToUpdate); Assert.NotNull(updateRedult); Assert.True(updateRedult.Result.IsSuccess); var boardFromDb = ctx.Boards.Find(updateRedult.Result.ResultEntity); Assert.NotNull(boardFromDb); Assert.Equal("New Title", boardFromDb.Title); }
internal void FillLandmark(IBoard <IHex> board) { if (board != null) { backingStore = new BoardStorage <short> .BlockedBoardStorage32x32(board.MapSizeHexes, c => - 1); var start = board[Coords]; var queue = DictionaryPriorityQueue <int, IHex> .NewQueue(); TraceFlags.FindPathDetail.Trace(true, "Find distances from {0}", start.Coords); queue.Enqueue(0, start); HexKeyValuePair <int, IHex> item; while (queue.TryDequeue(out item)) { var here = item.Value; var key = item.Key; if (backingStore[here.Coords] > 0) { continue; } TraceFlags.FindPathDetail.Trace("Dequeue Path at {0} w/ cost={1,4}.", here, key); backingStore[here.Coords] = (short)key; foreach (var there in here.GetAllNeighbours().Where(n => n != null && n.Hex.IsOnboard())) { var cost = board.DirectedStepCost(here, there.HexsideEntry); if (cost > 0 && backingStore[there.Hex.Coords] == -1) { TraceFlags.FindPathDetail.Trace(" Enqueue {0}: {1,4}", there.Hex.Coords, cost); queue.Enqueue(key + cost, there.Hex); } } } } }
private void FillLandmarkDetail(IPriorityQueue <int, IHex> queue, BoardStorage <short> store, Func <IHex, Hexside, IHex, int> directedStepCost ) { HexKeyValuePair <int, IHex> item; while (queue.TryDequeue(out item)) { var here = item.Value; var key = item.Key; if (store[here.Coords] > 0) { continue; } FindPathDetailTrace("Dequeue Path at {0} w/ cost={1,4}.", here, key); store[here.Coords] = (short)key; HexsideExtensions.HexsideList.ForEach(hexside => ExpandNode(store, directedStepCost, queue, here, key, hexside) ); } }
protected void Enqueue(short cost, HexCoords neighbour, BoardStorage <short?> store) { Tracing.FindPathDetail.Trace(" Enqueue {0}: {1,4}", neighbour, cost); store.SetItem(neighbour, cost); _queue.Enqueue(cost, neighbour); }
/// <summary>TODO</summary> public static int?DirectedCost <THex>(BoardStorage <Maybe <THex> > boardHexes, HexCoords hexCoords, Hexside hexside) where THex : IHex => boardHexes[hexCoords].Bind <int?>(hex => hex.EntryCost(hexside)).ToNullable();
/// <summary>TODO</summary> public static HexsideCosts EntryCosts <THex>(BoardStorage <Maybe <THex> > boardHexes, HexCoords hexCoords) where THex : IHex => new HexsideCosts(hexside => DirectedCost(boardHexes, hexCoords.GetNeighbour(hexside), hexside.Reversed));
/// <summary>TODO</summary> public static HexsideCosts ExitCosts <THex>(BoardStorage <Maybe <THex> > boardHexes, HexCoords hexCoords) where THex : IHex => new HexsideCosts(hexside => DirectedCost(boardHexes, hexCoords, hexside));