Adding a property to spawnable that denotes that the spawnable is consumed when an action is successfully performed

This commit is contained in:
hz
2025-06-09 18:57:59 -05:00
parent 8ac2b5865f
commit aa8c969906
3 changed files with 60 additions and 1 deletions
+20
View File
@@ -328,7 +328,27 @@ public class PlayerController : ControllerBase
{
cumulative += outcome.SuccessRate;
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("Nothing happens.");
+37 -1
View File
@@ -137,7 +137,8 @@ public class PlayerControllerTests
{
Name = "wood",
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)
@@ -150,4 +151,39 @@ public class PlayerControllerTests
var ok = Assert.IsType<OkObjectResult>(result);
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();
}
}
+3
View File
@@ -18,4 +18,7 @@ public class Spawnable
// Possible outcomes of collecting this item
public List<CollectOutcome> CollectResults { get; set; } = [];
// If true, the spawnable item is removed after a successful action
public bool ConsumeOnSuccess { get; set; } = false;
}