From aa8c96990634944b8930eb7819ba57e211b5f5c0 Mon Sep 17 00:00:00 2001 From: hz Date: Mon, 9 Jun 2025 18:57:59 -0500 Subject: [PATCH] Adding a property to spawnable that denotes that the spawnable is consumed when an action is successfully performed --- Controllers/PlayerController.cs | 20 ++++++++++++ GameAuthApi.Tests/PlayerControllerTests.cs | 38 +++++++++++++++++++++- Models/Spawnable.cs | 3 ++ 3 files changed, 60 insertions(+), 1 deletion(-) diff --git a/Controllers/PlayerController.cs b/Controllers/PlayerController.cs index cf27e55..f58baf4 100644 --- a/Controllers/PlayerController.cs +++ b/Controllers/PlayerController.cs @@ -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."); diff --git a/GameAuthApi.Tests/PlayerControllerTests.cs b/GameAuthApi.Tests/PlayerControllerTests.cs index abfc656..0da2ed2 100644 --- a/GameAuthApi.Tests/PlayerControllerTests.cs +++ b/GameAuthApi.Tests/PlayerControllerTests.cs @@ -137,7 +137,8 @@ public class PlayerControllerTests { Name = "wood", CollectAction = "chop", - CollectResults = new List { new() { Result = "log", SuccessRate = 100 } } + CollectResults = new List { 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(result); Assert.Equal("log", ok.Value); } + + [Fact] + public async Task PerformAction_AddsItemToInventory() + { + var playerSvc = new Mock(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(cfg); + var loc = new Location { Id = "loc", Content = new List { "i" } }; + locSvc.Setup(l => l.GetByIdAsync("loc")).ReturnsAsync(loc); + locSvc.Setup(l => l.AddItemToLocationAsync("inv", It.IsAny())).Returns(Task.CompletedTask).Verifiable(); + locSvc.Setup(l => l.RemoveItemFromLocationAsync("loc", "i")).Returns(Task.CompletedTask).Verifiable(); + var itemSvc = new Mock(cfg); + itemSvc.Setup(i => i.GetByIdAsync("i")).ReturnsAsync(new Item { Id = "i", Name = "wood" }); + itemSvc.Setup(i => i.CreateAsync(It.Is(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(cfg); + spawnSvc.Setup(s => s.GetByNameAsync("wood")).ReturnsAsync(new Spawnable + { + Name = "wood", + CollectAction = "chop", + CollectResults = new List { 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(); + } } diff --git a/Models/Spawnable.cs b/Models/Spawnable.cs index 2e34d46..bbc68a5 100644 --- a/Models/Spawnable.cs +++ b/Models/Spawnable.cs @@ -18,4 +18,7 @@ public class Spawnable // Possible outcomes of collecting this item public List CollectResults { get; set; } = []; + + // If true, the spawnable item is removed after a successful action + public bool ConsumeOnSuccess { get; set; } = false; }