diff --git a/backend/app/Savor22b.Tests/Action/UseRandomSeedItemActionTests.cs b/backend/app/Savor22b.Tests/Action/UseRandomSeedItemActionTests.cs index fc8318f7..87817c67 100644 --- a/backend/app/Savor22b.Tests/Action/UseRandomSeedItemActionTests.cs +++ b/backend/app/Savor22b.Tests/Action/UseRandomSeedItemActionTests.cs @@ -10,15 +10,14 @@ namespace Savor22b.Tests.Action; public class UseRandomSeedItemActionTests : ActionTests { - public UseRandomSeedItemActionTests() - { - } + public UseRandomSeedItemActionTests() { } [Fact] public void UseRandomSeedItemActionExecute_AddsSeedToSeedStateList() { var seedStateID = Guid.NewGuid(); var random = new DummyRandom(1); + var villageId = 1; IAccountStateDelta state = new DummyState(); RootState beforeRootState = new RootState(); @@ -28,19 +27,24 @@ public void UseRandomSeedItemActionExecute_AddsSeedToSeedStateList() beforeInventoryState = beforeInventoryState.AddItem(itemState); beforeRootState.SetInventoryState(beforeInventoryState); + beforeRootState.SetVillageState( + new VillageState(new HouseState(villageId, 0, 0, new KitchenState())) + ); state = state.SetState(SignerAddress(), beforeRootState.Serialize()); var action = new UseRandomSeedItemAction(seedStateID, itemState.StateID); - state = action.Execute(new DummyActionContext - { - PreviousStates = state, - Signer = SignerAddress(), - Random = random, - Rehearsal = false, - BlockIndex = 1, - }); + state = action.Execute( + new DummyActionContext + { + PreviousStates = state, + Signer = SignerAddress(), + Random = random, + Rehearsal = false, + BlockIndex = 1, + } + ); var rootStateEncoded = state.GetState(SignerAddress()); RootState rootState = rootStateEncoded is Bencodex.Types.Dictionary bdict @@ -59,8 +63,12 @@ public void UseRandomSeedItemActionExecute_AddsSeedToSeedStateList() [InlineData(100)] [InlineData(1000)] [InlineData(10000)] - public void UseRandomSeedItemActionExecute_AddsSeedStateToExistsSeedsList(int existsSeedsListLength) + public void UseRandomSeedItemActionExecute_AddsSeedStateToExistsSeedsList( + int existsSeedsListLength + ) { + int villageId = 1; + IAccountStateDelta state = new DummyState(); var itemState = new ItemState(Guid.NewGuid(), 1); RootState rootState = new RootState(); @@ -74,6 +82,9 @@ public void UseRandomSeedItemActionExecute_AddsSeedStateToExistsSeedsList(int ex beforeInventoryState = beforeInventoryState.AddItem(itemState); rootState.SetInventoryState(beforeInventoryState); + rootState.SetVillageState( + new VillageState(new HouseState(villageId, 0, 0, new KitchenState())) + ); state = state.SetState(SignerAddress(), rootState.Serialize()); @@ -81,14 +92,16 @@ public void UseRandomSeedItemActionExecute_AddsSeedStateToExistsSeedsList(int ex var action = new UseRandomSeedItemAction(Guid.NewGuid(), itemState.StateID); - state = action.Execute(new DummyActionContext - { - PreviousStates = state, - Signer = SignerAddress(), - Random = random, - Rehearsal = false, - BlockIndex = 1, - }); + state = action.Execute( + new DummyActionContext + { + PreviousStates = state, + Signer = SignerAddress(), + Random = random, + Rehearsal = false, + BlockIndex = 1, + } + ); var afterRootStateEncoded = state.GetState(SignerAddress()); diff --git a/backend/app/Savor22b/Action/UseRandomSeedItemAction.cs b/backend/app/Savor22b/Action/UseRandomSeedItemAction.cs index 91ef69e6..a3116beb 100644 --- a/backend/app/Savor22b/Action/UseRandomSeedItemAction.cs +++ b/backend/app/Savor22b/Action/UseRandomSeedItemAction.cs @@ -7,7 +7,7 @@ namespace Savor22b.Action; using Libplanet.State; using Savor22b.Action.Exceptions; using Savor22b.States; - +using Savor22b.Action.Util; [ActionType(nameof(UseRandomSeedItemAction))] public class UseRandomSeedItemAction : SVRAction @@ -15,9 +15,7 @@ public class UseRandomSeedItemAction : SVRAction public Guid SeedStateID; public Guid ItemStateIdToUse; - public UseRandomSeedItemAction() - { - } + public UseRandomSeedItemAction() { } public UseRandomSeedItemAction(Guid seedStateID, Guid itemStateIdToUse) { @@ -25,7 +23,6 @@ public UseRandomSeedItemAction(Guid seedStateID, Guid itemStateIdToUse) ItemStateIdToUse = itemStateIdToUse; } - protected override IImmutableDictionary PlainValueInternal => new Dictionary() { @@ -33,17 +30,22 @@ public UseRandomSeedItemAction(Guid seedStateID, Guid itemStateIdToUse) [nameof(ItemStateIdToUse)] = ItemStateIdToUse.Serialize() }.ToImmutableDictionary(); - protected override void LoadPlainValueInternal( - IImmutableDictionary plainValue) + protected override void LoadPlainValueInternal(IImmutableDictionary plainValue) { SeedStateID = plainValue[nameof(SeedStateID)].ToGuid(); ItemStateIdToUse = plainValue[nameof(ItemStateIdToUse)].ToGuid(); } - private SeedState generateRandomSeed(IRandom random) + private SeedState generateRandomSeed(IRandom random, int villageId) { + int randomIndex; var seeds = CsvDataHelper.GetSeedCSVData(); - int randomIndex = random.Next(0, seeds.Count); + var village = CsvDataHelper.GetVillageCharacteristicByVillageId(villageId)!; + + do + { + randomIndex = random.Next(0, seeds.Count); + } while (!village.AvailableSeedIdList.Contains(seeds[randomIndex].Id)); var randomSeedData = seeds[randomIndex]; var randomSeed = new SeedState(SeedStateID, randomSeedData.Id); @@ -69,13 +71,19 @@ public override IAccountStateDelta Execute(IActionContext ctx) { IAccountStateDelta states = ctx.PreviousStates; - RootState rootState = states.GetState(ctx.Signer) is Bencodex.Types.Dictionary rootStateEncoded + RootState rootState = states.GetState(ctx.Signer) + is Bencodex.Types.Dictionary rootStateEncoded ? new RootState(rootStateEncoded) : new RootState(); + Validation.EnsureVillageStateExists(rootState); + InventoryState inventoryState = rootState.InventoryState; - SeedState seedState = generateRandomSeed(ctx.Random); + SeedState seedState = generateRandomSeed( + ctx.Random, + rootState.VillageState!.HouseState.VillageID + ); inventoryState = FindAndRemoveItem(inventoryState); inventoryState = inventoryState.AddSeed(seedState); diff --git a/backend/app/Savor22b/Helpers/CsvDataHelper.cs b/backend/app/Savor22b/Helpers/CsvDataHelper.cs index c24ba61a..d2e29868 100644 --- a/backend/app/Savor22b/Helpers/CsvDataHelper.cs +++ b/backend/app/Savor22b/Helpers/CsvDataHelper.cs @@ -17,6 +17,7 @@ public static class CsvDataHelper private static ImmutableList kitchenEquipmentCategoryList; private static ImmutableList itemList; private static ImmutableList villageList; + private static ImmutableList villageCharacteristicList; public static void Initialize(string csvDataPath) { @@ -308,4 +309,35 @@ public static ImmutableList GetVillageCSVData() } #endregion village + + + #region villageCharacteristic + + private static void initVillageCharacteristic() + { + if (villageCharacteristicList == null) + { + CsvParser csvParser = new CsvParser(); + var csvPath = GetCSVDataPath(CsvDataFileNames.VillageCharacteristics); + villageCharacteristicList = csvParser.ParseCsv(csvPath).ToImmutableList(); + } + } + + public static ImmutableList GetVillageCharacteristicCSVData() + { + initVillageCharacteristic(); + + return villageCharacteristicList; + } + + public static VillageCharacteristic? GetVillageCharacteristicByVillageId(int villageId) + { + initVillageCharacteristic(); + + return villageCharacteristicList.Find( + characteristic => characteristic.VillageId == villageId + ); + } + + #endregion villageCharacteristic } diff --git a/backend/app/Savor22b/Model/VillageCharacteristic.cs b/backend/app/Savor22b/Model/VillageCharacteristic.cs index 41a74888..6f1d3ac2 100644 --- a/backend/app/Savor22b/Model/VillageCharacteristic.cs +++ b/backend/app/Savor22b/Model/VillageCharacteristic.cs @@ -1,9 +1,10 @@ +using System.Collections.Immutable; + namespace Savor22b.Model; public class VillageCharacteristic { - // ID,VillageID,AvailableSeedID public int Id { get; set; } public int VillageId { get; set; } - public string AvailableSeedId { get; set; } + public ImmutableList AvailableSeedIdList { get; set; } } diff --git a/resources/savor22b/tabledata/village_characteristics.csv b/resources/savor22b/tabledata/village_characteristics.csv index fe78a234..7fbdef58 100644 --- a/resources/savor22b/tabledata/village_characteristics.csv +++ b/resources/savor22b/tabledata/village_characteristics.csv @@ -1,3 +1,3 @@ -ID,VillageID,AvailableSeedID +ID,VillageID,AvailableSeedIDList 1,1,1;2;3;4;5;6;7;9;10 2,2,1;2;3;4;8;9;10;11;12;13