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-66] 특정 위치의 집 설치 기능 제작 #31

Merged
merged 7 commits into from
Jul 30, 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
11 changes: 6 additions & 5 deletions backend/app/Savor22b.Tests/Action/BuyCookingEquipmentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace Savor22b.Tests.Action;
using Libplanet.Crypto;
using Libplanet.State;
using Savor22b.Action;
using Savor22b.Constants;
using Savor22b.States;
using Xunit;

Expand Down Expand Up @@ -42,11 +43,11 @@ public void BuyCookingEquipmentExecute_AddsCookingEquipmentToKitchenStateList()
BlockIndex = 1,
});

var inventoryStateEncoded = state.GetState(_signer.PublicKey.ToAddress());
InventoryState inventoryState =
inventoryStateEncoded is Bencodex.Types.Dictionary bdict
? new InventoryState(bdict)
: throw new Exception();
var rootStateEncoded = state.GetState(_signer.PublicKey.ToAddress());
RootState rootState = rootStateEncoded is Bencodex.Types.Dictionary bdict
? new RootState(bdict)
: throw new Exception();
InventoryState inventoryState = rootState.InventoryState;

Assert.Equal(0, inventoryState.SeedStateList.Count);
Assert.Equal(0, inventoryState.RefrigeratorStateList.Count);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace Savor22b.Tests.Action;
using Libplanet.Crypto;
using Libplanet.State;
using Savor22b.Action;
using Savor22b.Constants;
using Savor22b.States;
using Xunit;

Expand Down
23 changes: 13 additions & 10 deletions backend/app/Savor22b.Tests/Action/GenerateFoodActionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,9 @@ public IEnumerable<object[]> IngredientsSamples()
yield return new object[] { recipeID, preset.Item1, preset.Item2 };
}

public InventoryState CreateInventoryStateFromExpectValues(List<int> expectUsedIngredientIDs, List<int> expectUsedFoodIDs)
public RootState CreateRootStateFromExpectValues(List<int> expectUsedIngredientIDs, List<int> expectUsedFoodIDs)
{
RootState rootState = new RootState();
InventoryState inventoryState = new InventoryState();

foreach (var ingredientID in expectUsedIngredientIDs)
Expand All @@ -101,24 +102,26 @@ public InventoryState CreateInventoryStateFromExpectValues(List<int> expectUsedI
inventoryState = inventoryState.AddRefrigeratorItem(RefrigeratorState.CreateFood(Guid.NewGuid(), foodID, "D", 1, 1, 1, 1));
}

return inventoryState;
rootState.SetInventoryState(inventoryState);

return rootState;
}

[Theory]
[ClassData(typeof(IngredientsSamplesData))]
public void GenerateFoodActionExecute_AddsFoodToRefrigeratorStateList(int expectRecipeID, List<int> expectUsedIngredients, List<int> expectUsedFoods)
{
IAccountStateDelta beforeState = new DummyState();
var beforeInventoryState = CreateInventoryStateFromExpectValues(expectUsedIngredients, expectUsedFoods);
beforeState = beforeState.SetState(_signer.PublicKey.ToAddress(), beforeInventoryState.Serialize());
var beforeRootState = CreateRootStateFromExpectValues(expectUsedIngredients, expectUsedFoods);
beforeState = beforeState.SetState(_signer.PublicKey.ToAddress(), beforeRootState.Serialize());

var random = new DummyRandom(1);

var newFoodGuid = Guid.NewGuid();
var action = new GenerateFoodAction(
expectRecipeID,
newFoodGuid,
(from stateList in beforeInventoryState.RefrigeratorStateList
(from stateList in beforeRootState.InventoryState.RefrigeratorStateList
select stateList.StateID).ToList());

var afterState = action.Execute(new DummyActionContext
Expand All @@ -130,11 +133,11 @@ public void GenerateFoodActionExecute_AddsFoodToRefrigeratorStateList(int expect
BlockIndex = 1,
});

var inventoryStateEncoded = afterState.GetState(_signer.PublicKey.ToAddress());
InventoryState afterInventoryState =
inventoryStateEncoded is Bencodex.Types.Dictionary bdict
? new InventoryState(bdict)
: throw new Exception();
var rootStateEncoded = afterState.GetState(_signer.PublicKey.ToAddress());
RootState rootState = rootStateEncoded is Bencodex.Types.Dictionary bdict
? new RootState(bdict)
: throw new Exception();
InventoryState afterInventoryState = rootState.InventoryState;

Assert.Equal(afterInventoryState.RefrigeratorStateList.Count, 1);
Assert.Equal(afterInventoryState.RefrigeratorStateList[0].RecipeID, expectRecipeID);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,25 @@ public void GenerateIngredientActionExecute_AddsIngredientToRefrigeratorStateLis
BlockIndex = 1,
});

var inventoryStateEncoded = state.GetState(_signer.PublicKey.ToAddress());
InventoryState inventoryState =
inventoryStateEncoded is Bencodex.Types.Dictionary bdict
? new InventoryState(bdict)
: throw new Exception();
var rootStateEncoded = state.GetState(_signer.PublicKey.ToAddress());
RootState rootState = rootStateEncoded is Bencodex.Types.Dictionary bdict
? new RootState(bdict)
: throw new Exception();
InventoryState inventoryState = rootState.InventoryState;

Assert.Equal(inventoryState.SeedStateList.Count, 0);
Assert.Equal(inventoryState.RefrigeratorStateList.Count, 1);
}

private (IAccountStateDelta, Guid) AddSeedState(IAccountStateDelta state)
{
RootState rootState = new RootState();
InventoryState inventoryState = new InventoryState();
var newSeed = new SeedState(Guid.NewGuid(), 1);
inventoryState = inventoryState.AddSeed(newSeed);

return (state.SetState(_signer.PublicKey.ToAddress(), inventoryState.Serialize()), newSeed.StateID);
rootState.SetInventoryState(inventoryState);

return (state.SetState(_signer.PublicKey.ToAddress(), rootState.Serialize()), newSeed.StateID);
}
}
66 changes: 66 additions & 0 deletions backend/app/Savor22b.Tests/Action/PlaceUserHouseActionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
namespace Savor22b.Tests.Action;

using System;
using Libplanet.Crypto;
using Libplanet.State;
using Xunit;
using Savor22b.Action;
using Savor22b.States;
using Libplanet;
using Savor22b.Constants;

public class PlaceUserHouseActionTests
upa-r-upa marked this conversation as resolved.
Show resolved Hide resolved
{
private PrivateKey _signer = new PrivateKey();

public PlaceUserHouseActionTests()
{
}

[Theory]
[InlineData(1, 0, 0)]
[InlineData(1, 1, 1)]
[InlineData(2, 0, 0)]
[InlineData(2, 1, 1)]
[InlineData(2, 2, 6)]
public void PlaceUserHouseActionExecute_UpdateUserHouseStateToExistsUserHouseState(
upa-r-upa marked this conversation as resolved.
Show resolved Hide resolved
int villageId,
int targetX,
int targetY
)
{
IAccountStateDelta state = new DummyState();
RootState rootState = new RootState();

var random = new DummyRandom(1);

var action = new PlaceUserHouseAction(villageId, targetX, targetY);

state = action.Execute(new DummyActionContext
{
PreviousStates = state,
Signer = _signer.PublicKey.ToAddress(),
Random = random,
Rehearsal = false,
BlockIndex = 1,
});

var rootStateEncoded = state.GetState(_signer.PublicKey.ToAddress());
rootState = rootStateEncoded is Bencodex.Types.Dictionary bdict
? new RootState(bdict)
: throw new Exception();

Assert.Equal(villageId, rootState.VillageState!.HouseState.VillageID);
Assert.Equal(targetX, rootState.VillageState.HouseState.PositionX);
Assert.Equal(targetY, rootState.VillageState.HouseState.PositionY);

var globalUserHouseStateEncoded = state.GetState(Addresses.UserHouseDataAddress);

GlobalUserHouseState globalUserHouseState = globalUserHouseStateEncoded is Bencodex.Types.Dictionary stateEncoded
? new GlobalUserHouseState(stateEncoded)
: throw new Exception();


Assert.Equal(_signer.PublicKey.ToAddress(), globalUserHouseState.UserHouse[$"{villageId},{targetX},{targetY}"]);
}
}
40 changes: 24 additions & 16 deletions backend/app/Savor22b.Tests/Action/UseRandomSeedItemActionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,18 @@ public UseRandomSeedItemActionTests()
public void UseRandomSeedItemActionExecute_AddsSeedToSeedStateList()
{
var seedStateID = Guid.NewGuid();
IAccountStateDelta state = new DummyState();
var random = new DummyRandom(1);

IAccountStateDelta state = new DummyState();
RootState beforeRootState = new RootState();
InventoryState beforeInventoryState = new InventoryState();

var itemState = new ItemState(Guid.NewGuid(), 1);
beforeInventoryState = beforeInventoryState.AddItem(itemState);
state = state.SetState(SignerAddress(), beforeInventoryState.Serialize());

beforeRootState.SetInventoryState(beforeInventoryState);

state = state.SetState(SignerAddress(), beforeRootState.Serialize());

var action = new UseRandomSeedItemAction(seedStateID, itemState.StateID);

Expand All @@ -37,11 +42,11 @@ public void UseRandomSeedItemActionExecute_AddsSeedToSeedStateList()
BlockIndex = 1,
});

var inventoryStateEncoded = state.GetState(SignerAddress());
InventoryState inventoryState =
inventoryStateEncoded is Bencodex.Types.Dictionary bdict
? new InventoryState(bdict)
: throw new Exception();
var rootStateEncoded = state.GetState(SignerAddress());
RootState rootState = rootStateEncoded is Bencodex.Types.Dictionary bdict
? new RootState(bdict)
: throw new Exception();
InventoryState inventoryState = rootState.InventoryState;

Assert.Equal(inventoryState.SeedStateList.Count, 1);
Assert.Equal(inventoryState.ItemStateList.Count, 0);
Expand All @@ -57,8 +62,9 @@ inventoryStateEncoded is Bencodex.Types.Dictionary bdict
public void UseRandomSeedItemActionExecute_AddsSeedStateToExistsSeedsList(int existsSeedsListLength)
{
IAccountStateDelta state = new DummyState();
InventoryState beforeInventoryState = new InventoryState();
var itemState = new ItemState(Guid.NewGuid(), 1);
RootState rootState = new RootState();
InventoryState beforeInventoryState = new InventoryState();

for (int i = 0; i < existsSeedsListLength; i++)
{
Expand All @@ -67,7 +73,9 @@ public void UseRandomSeedItemActionExecute_AddsSeedStateToExistsSeedsList(int ex
}

beforeInventoryState = beforeInventoryState.AddItem(itemState);
state = state.SetState(SignerAddress(), beforeInventoryState.Serialize());
rootState.SetInventoryState(beforeInventoryState);

state = state.SetState(SignerAddress(), rootState.Serialize());

var random = new DummyRandom(1);

Expand All @@ -82,13 +90,13 @@ public void UseRandomSeedItemActionExecute_AddsSeedStateToExistsSeedsList(int ex
BlockIndex = 1,
});

var afterInventoryStateEncoded = state.GetState(SignerAddress());
InventoryState afterInventoryState =
afterInventoryStateEncoded is Bencodex.Types.Dictionary bdict
? new InventoryState(bdict)
: throw new Exception();
var afterRootStateEncoded = state.GetState(SignerAddress());

RootState afterRootState = afterRootStateEncoded is Bencodex.Types.Dictionary bdict
? new RootState(bdict)
: throw new Exception();

Assert.Equal(existsSeedsListLength + 1, afterInventoryState.SeedStateList.Count);
Assert.Equal(afterInventoryState.ItemStateList.Count, 0);
Assert.Equal(existsSeedsListLength + 1, afterRootState.InventoryState.SeedStateList.Count);
Assert.Equal(afterRootState.InventoryState.ItemStateList.Count, 0);
}
}
14 changes: 8 additions & 6 deletions backend/app/Savor22b/Action/BuyCookingEquipmentAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace Savor22b.Action;
using Libplanet.Assets;
using Libplanet.Headless.Extensions;
using Libplanet.State;
using Savor22b.Constants;
using Savor22b.Helpers;
using Savor22b.Model;
using Savor22b.States;
Expand Down Expand Up @@ -84,10 +85,11 @@ public override IAccountStateDelta Execute(IActionContext ctx)
IAccountStateDelta states = ctx.PreviousStates;
Address Recipient = Addresses.ShopVaultAddress;

InventoryState inventoryState =
states.GetState(ctx.Signer) is Bencodex.Types.Dictionary stateEncoded
? new InventoryState(stateEncoded)
: new InventoryState();
RootState rootState = states.GetState(ctx.Signer) is Bencodex.Types.Dictionary rootStateEncoded
? new RootState(rootStateEncoded)
: new RootState();

InventoryState inventoryState = rootState.InventoryState;

var cookingEquipmentList = GetCookingEquipmentCSVData();
var desiredEquipment = FindCookingEquipment(cookingEquipmentList);
Expand All @@ -99,8 +101,8 @@ public override IAccountStateDelta Execute(IActionContext ctx)
desiredEquipment.Price,
allowNegativeBalance: false
);
inventoryState = inventoryState.AddCookingEquipmentItem(cookingEquipmentState);
rootState.SetInventoryState(inventoryState.AddCookingEquipmentItem(cookingEquipmentState));

return states.SetState(ctx.Signer, inventoryState.Serialize());
return states.SetState(ctx.Signer, rootState.Serialize());
}
}
1 change: 1 addition & 0 deletions backend/app/Savor22b/Action/BuyRandomSeedItemAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace Savor22b.Action;
using Libplanet.Assets;
using Libplanet.Headless.Extensions;
using Libplanet.State;
using Savor22b.Constants;
using Savor22b.Helpers;
using Savor22b.Model;
using Savor22b.States;
Expand Down
13 changes: 8 additions & 5 deletions backend/app/Savor22b/Action/GenerateFoodAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
public Guid FoodStateID;
public List<Guid> RefrigeratorStateIDs;

public GenerateFoodAction()

Check warning on line 21 in backend/app/Savor22b/Action/GenerateFoodAction.cs

View workflow job for this annotation

GitHub Actions / build-and-tests

Non-nullable field 'RefrigeratorStateIDs' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.
{
}

Expand Down Expand Up @@ -224,15 +224,18 @@

IAccountStateDelta states = ctx.PreviousStates;

InventoryState inventoryState =
states.GetState(ctx.Signer) is Bencodex.Types.Dictionary stateEncoded
? new InventoryState(stateEncoded)
: new InventoryState();
RootState rootState = states.GetState(ctx.Signer) is Bencodex.Types.Dictionary rootStateEncoded
? new RootState(rootStateEncoded)
: new RootState();

InventoryState inventoryState = rootState.InventoryState;

inventoryState = CheckAndRemoveForRecipe(inventoryState);
RefrigeratorState food = GenerateFood(ctx.Random);
inventoryState = inventoryState.AddRefrigeratorItem(food);

return states.SetState(ctx.Signer, inventoryState.Serialize());
rootState.SetInventoryState(inventoryState);

return states.SetState(ctx.Signer, rootState.Serialize());
}
}
17 changes: 11 additions & 6 deletions backend/app/Savor22b/Action/GenerateIngredientAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ public GenerateIngredientAction(Guid seedStateID, Guid refrigeratorStateID)
}

protected override IImmutableDictionary<string, IValue> PlainValueInternal =>
new Dictionary<string, IValue>(){
new Dictionary<string, IValue>()
{
[nameof(SeedStateID)] = SeedStateID.Serialize(),
[nameof(RefrigeratorStateID)] = RefrigeratorStateID.Serialize()
}.ToImmutableDictionary();
Expand Down Expand Up @@ -111,10 +112,12 @@ public override IAccountStateDelta Execute(IActionContext ctx)

IAccountStateDelta states = ctx.PreviousStates;

InventoryState inventoryState =
states.GetState(ctx.Signer) is Bencodex.Types.Dictionary stateEncoded
? new InventoryState(stateEncoded)
: new InventoryState();

RootState rootState = states.GetState(ctx.Signer) is Bencodex.Types.Dictionary rootStateEncoded
? new RootState(rootStateEncoded)
: new RootState();

InventoryState inventoryState = rootState.InventoryState;

var seed = inventoryState.SeedStateList.Find(seed => seed.StateID == SeedStateID);

Expand All @@ -129,6 +132,8 @@ public override IAccountStateDelta Execute(IActionContext ctx)
inventoryState = inventoryState.AddRefrigeratorItem(ingredient);
inventoryState = inventoryState.RemoveSeed(SeedStateID);

return states.SetState(ctx.Signer, inventoryState.Serialize());
rootState.SetInventoryState(inventoryState);

return states.SetState(ctx.Signer, rootState.Serialize());
}
}
Loading
Loading