Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SVR-121] 종자의 마을별 제한 기능 추가 #82

Merged
merged 4 commits into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 33 additions & 20 deletions backend/app/Savor22b.Tests/Action/UseRandomSeedItemActionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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
Expand All @@ -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();
Expand All @@ -74,21 +82,26 @@ 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());

var random = new DummyRandom(1);

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());

Expand Down
30 changes: 19 additions & 11 deletions backend/app/Savor22b/Action/UseRandomSeedItemAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,43 +7,45 @@ namespace Savor22b.Action;
using Libplanet.State;
using Savor22b.Action.Exceptions;
using Savor22b.States;

using Savor22b.Action.Util;

[ActionType(nameof(UseRandomSeedItemAction))]
public class UseRandomSeedItemAction : SVRAction
{
public Guid SeedStateID;
public Guid ItemStateIdToUse;

public UseRandomSeedItemAction()
{
}
public UseRandomSeedItemAction() { }

public UseRandomSeedItemAction(Guid seedStateID, Guid itemStateIdToUse)
{
SeedStateID = seedStateID;
ItemStateIdToUse = itemStateIdToUse;
}


protected override IImmutableDictionary<string, IValue> PlainValueInternal =>
new Dictionary<string, IValue>()
{
[nameof(SeedStateID)] = SeedStateID.Serialize(),
[nameof(ItemStateIdToUse)] = ItemStateIdToUse.Serialize()
}.ToImmutableDictionary();

protected override void LoadPlainValueInternal(
IImmutableDictionary<string, IValue> plainValue)
protected override void LoadPlainValueInternal(IImmutableDictionary<string, IValue> 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);
Expand All @@ -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);

Expand Down
32 changes: 32 additions & 0 deletions backend/app/Savor22b/Helpers/CsvDataHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public static class CsvDataHelper
private static ImmutableList<KitchenEquipmentCategory> kitchenEquipmentCategoryList;
private static ImmutableList<Item> itemList;
private static ImmutableList<Village> villageList;
private static ImmutableList<VillageCharacteristic> villageCharacteristicList;

public static void Initialize(string csvDataPath)
{
Expand Down Expand Up @@ -308,4 +309,35 @@ public static ImmutableList<Village> GetVillageCSVData()
}

#endregion village


#region villageCharacteristic

private static void initVillageCharacteristic()
{
if (villageCharacteristicList == null)
{
CsvParser<VillageCharacteristic> csvParser = new CsvParser<VillageCharacteristic>();
var csvPath = GetCSVDataPath(CsvDataFileNames.VillageCharacteristics);
villageCharacteristicList = csvParser.ParseCsv(csvPath).ToImmutableList();
}
}

public static ImmutableList<VillageCharacteristic> GetVillageCharacteristicCSVData()
{
initVillageCharacteristic();

return villageCharacteristicList;
}

public static VillageCharacteristic? GetVillageCharacteristicByVillageId(int villageId)
{
initVillageCharacteristic();

return villageCharacteristicList.Find(
characteristic => characteristic.VillageId == villageId
);
}

#endregion villageCharacteristic
}
5 changes: 3 additions & 2 deletions backend/app/Savor22b/Model/VillageCharacteristic.cs
Original file line number Diff line number Diff line change
@@ -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<int> AvailableSeedIdList { get; set; }
}
2 changes: 1 addition & 1 deletion resources/savor22b/tabledata/village_characteristics.csv
Original file line number Diff line number Diff line change
@@ -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
Loading