Adding a property to spawnable that denotes that the spawnable is consumed when an action is successfully performed
This commit is contained in:
@@ -328,8 +328,28 @@ public class PlayerController : ControllerBase
|
|||||||
{
|
{
|
||||||
cumulative += outcome.SuccessRate;
|
cumulative += outcome.SuccessRate;
|
||||||
if (roll < cumulative)
|
if (roll < cumulative)
|
||||||
|
{
|
||||||
|
if (!string.IsNullOrEmpty(player.InventoryLocationId))
|
||||||
|
{
|
||||||
|
var newItem = new Item
|
||||||
|
{
|
||||||
|
Name = outcome.Result,
|
||||||
|
LocationId = player.InventoryLocationId
|
||||||
|
};
|
||||||
|
|
||||||
|
await _itemService.CreateAsync(newItem);
|
||||||
|
await _locationService.AddItemToLocationAsync(player.InventoryLocationId, newItem.Id);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (spawnable.ConsumeOnSuccess)
|
||||||
|
{
|
||||||
|
await _locationService.RemoveItemFromLocationAsync(location.Id, itemId);
|
||||||
|
await _itemService.DeleteAsync(itemId);
|
||||||
|
}
|
||||||
|
|
||||||
return Ok(outcome.Result);
|
return Ok(outcome.Result);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return Ok("Nothing happens.");
|
return Ok("Nothing happens.");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -137,7 +137,8 @@ public class PlayerControllerTests
|
|||||||
{
|
{
|
||||||
Name = "wood",
|
Name = "wood",
|
||||||
CollectAction = "chop",
|
CollectAction = "chop",
|
||||||
CollectResults = new List<CollectOutcome> { new() { Result = "log", SuccessRate = 100 } }
|
CollectResults = new List<CollectOutcome> { new() { Result = "log", SuccessRate = 100 } },
|
||||||
|
ConsumeOnSuccess = false
|
||||||
});
|
});
|
||||||
|
|
||||||
var controller = new PlayerController(playerSvc.Object, locSvc.Object, spawnSvc.Object, itemSvc.Object)
|
var controller = new PlayerController(playerSvc.Object, locSvc.Object, spawnSvc.Object, itemSvc.Object)
|
||||||
@@ -150,4 +151,39 @@ public class PlayerControllerTests
|
|||||||
var ok = Assert.IsType<OkObjectResult>(result);
|
var ok = Assert.IsType<OkObjectResult>(result);
|
||||||
Assert.Equal("log", ok.Value);
|
Assert.Equal("log", ok.Value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task PerformAction_AddsItemToInventory()
|
||||||
|
{
|
||||||
|
var playerSvc = new Mock<PlayerService>(cfg);
|
||||||
|
var player = new Player { Id = "p", UserId = "u", LocationId = "loc", InventoryLocationId = "inv" };
|
||||||
|
playerSvc.Setup(p => p.GetByUserIdAsync("u")).ReturnsAsync(player);
|
||||||
|
var locSvc = new Mock<LocationService>(cfg);
|
||||||
|
var loc = new Location { Id = "loc", Content = new List<string> { "i" } };
|
||||||
|
locSvc.Setup(l => l.GetByIdAsync("loc")).ReturnsAsync(loc);
|
||||||
|
locSvc.Setup(l => l.AddItemToLocationAsync("inv", It.IsAny<string>())).Returns(Task.CompletedTask).Verifiable();
|
||||||
|
locSvc.Setup(l => l.RemoveItemFromLocationAsync("loc", "i")).Returns(Task.CompletedTask).Verifiable();
|
||||||
|
var itemSvc = new Mock<ItemService>(cfg);
|
||||||
|
itemSvc.Setup(i => i.GetByIdAsync("i")).ReturnsAsync(new Item { Id = "i", Name = "wood" });
|
||||||
|
itemSvc.Setup(i => i.CreateAsync(It.Is<Item>(it => it.Name == "log" && it.LocationId == "inv"))).Returns(Task.CompletedTask).Verifiable();
|
||||||
|
itemSvc.Setup(i => i.DeleteAsync("i")).Returns(Task.CompletedTask).Verifiable();
|
||||||
|
var spawnSvc = new Mock<SpawnableService>(cfg);
|
||||||
|
spawnSvc.Setup(s => s.GetByNameAsync("wood")).ReturnsAsync(new Spawnable
|
||||||
|
{
|
||||||
|
Name = "wood",
|
||||||
|
CollectAction = "chop",
|
||||||
|
CollectResults = new List<CollectOutcome> { new() { Result = "log", SuccessRate = 100 } },
|
||||||
|
ConsumeOnSuccess = true
|
||||||
|
});
|
||||||
|
|
||||||
|
var controller = new PlayerController(playerSvc.Object, locSvc.Object, spawnSvc.Object, itemSvc.Object)
|
||||||
|
{
|
||||||
|
ControllerContext = ContextWithUser("u")
|
||||||
|
};
|
||||||
|
|
||||||
|
await controller.PerformAction(new PerformActionRequest { Action = "chop" });
|
||||||
|
|
||||||
|
locSvc.Verify();
|
||||||
|
itemSvc.Verify();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,4 +18,7 @@ public class Spawnable
|
|||||||
|
|
||||||
// Possible outcomes of collecting this item
|
// Possible outcomes of collecting this item
|
||||||
public List<CollectOutcome> CollectResults { get; set; } = [];
|
public List<CollectOutcome> CollectResults { get; set; } = [];
|
||||||
|
|
||||||
|
// If true, the spawnable item is removed after a successful action
|
||||||
|
public bool ConsumeOnSuccess { get; set; } = false;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user