Skip to content

Commit

Permalink
feat: Add block reduction rogic
Browse files Browse the repository at this point in the history
  • Loading branch information
Atralupus committed Aug 20, 2023
1 parent a4fe29f commit d0d9a8f
Show file tree
Hide file tree
Showing 7 changed files with 282 additions and 34 deletions.
146 changes: 141 additions & 5 deletions backend/app/Savor22b.Tests/Action/CreateFoodActionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ namespace Savor22b.Tests.Action;
using System;
using System.Collections.Immutable;
using Libplanet.State;
using Savor22b.Util;
using Savor22b.Action;
using Savor22b.Action.Exceptions;
using Savor22b.Model;
Expand Down Expand Up @@ -64,6 +65,23 @@ var equipmentCategoryId in recipeCandidate.RequiredKitchenEquipmentCategoryList
return recipe!;
}

private KitchenEquipment getRandomNotRequiredKitchenEquipment(Recipe recipe)
{
foreach (var kitchenEquipment in CsvDataHelper.GetKitchenEquipmentCSVData())
{
if (
!recipe.RequiredKitchenEquipmentCategoryList.Contains(
kitchenEquipment.KitchenEquipmentCategoryID
)
)
{
return kitchenEquipment;
}
}

throw new Exception("");
}

private List<RefrigeratorState> generateMaterials(
ImmutableList<int> IngredientIDList,
ImmutableList<int> FoodIDList
Expand All @@ -86,7 +104,10 @@ ImmutableList<int> FoodIDList
return RefrigeratorItemList;
}

private (RootState, List<Guid>, List<int>) createPreset(Recipe recipe)
private (RootState, List<Guid>, List<int>) createPreset(
Recipe recipe,
bool hasBlockReduction = false
)
{
int spaceNumber = 1;
List<Guid> kitchenEquipmentsToUse = new List<Guid>();
Expand All @@ -113,9 +134,28 @@ ImmutableList<int> FoodIDList
var kitchenEquipments = CsvDataHelper.GetAllKitchenEquipmentByCategoryId(
equipmentCategoryId
);
KitchenEquipment targetKitchenEquipment = null;

if (hasBlockReduction)
{
var higherKitchenEquipment = kitchenEquipments.Find(
k => k.BlockTimeReductionPercent > 1
);
if (higherKitchenEquipment == null)
{
throw new Exception("");
}

targetKitchenEquipment = higherKitchenEquipment;
}
else
{
targetKitchenEquipment = kitchenEquipments[0];
}

var kitchenEquipmentState = new KitchenEquipmentState(
Guid.NewGuid(),
kitchenEquipments[0].ID,
targetKitchenEquipment.ID,
equipmentCategoryId
);
inventoryState = inventoryState.AddKitchenEquipmentItem(kitchenEquipmentState);
Expand Down Expand Up @@ -239,7 +279,7 @@ select stateList.StateID
foreach (var spaceNumber in spaceNumbersToUse)
{
Assert.NotNull(
beforeRootState.VillageState!.HouseState.KitchenState.GetApplianceSpaceStateByNumber(
rootState.VillageState!.HouseState.KitchenState.GetApplianceSpaceStateByNumber(
spaceNumber
)
);
Expand All @@ -264,6 +304,92 @@ select stateList.StateID
}
}

[Fact]
public void Execute_Success_WithBlockReduction()
{
var recipe = getRandomRecipeWithEquipmentCategory("main");
var blockIndex = 1;

IAccountStateDelta beforeState = new DummyState();
var (beforeRootState, kitchenEquipmentStateIdsToUse, spaceNumbersToUse) = createPreset(
recipe,
true
);
var edibleStateIdsToUse = (
from stateList in beforeRootState.InventoryState.RefrigeratorStateList
select stateList.StateID
).ToList();

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

var newFoodGuid = Guid.NewGuid();
var action = new CreateFoodAction(
recipe.ID,
newFoodGuid,
edibleStateIdsToUse,
kitchenEquipmentStateIdsToUse,
spaceNumbersToUse
);

var afterState = action.Execute(
new DummyActionContext
{
PreviousStates = beforeState,
Signer = SignerAddress(),
Random = random,
Rehearsal = false,
BlockIndex = blockIndex,
}
);

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

var sumReductionPercent = 0;
foreach (var kitchenEquipmentState in afterInventoryState.KitchenEquipmentStateList)
{
var kitchenEquipment = CsvDataHelper.GetKitchenEquipmentByID(
kitchenEquipmentState.KitchenEquipmentID
);
sumReductionPercent = sumReductionPercent + kitchenEquipment!.BlockTimeReductionPercent;
}
var avgReductionPercent =
sumReductionPercent / afterInventoryState.KitchenEquipmentStateList.Count;
var expectedDurationBlock = MathUtil.ReduceByPercentage(
recipe.RequiredBlock,
avgReductionPercent
);

Assert.Equal(
expectedDurationBlock,
afterInventoryState.GetRefrigeratorItem(newFoodGuid).AvailableBlockIndex - blockIndex
);

foreach (var spaceNumber in spaceNumbersToUse)
{
Assert.NotNull(
afterRootState.VillageState!.HouseState.KitchenState.GetApplianceSpaceStateByNumber(
spaceNumber
)
);

Assert.True(
afterRootState.VillageState!.HouseState.KitchenState
.GetApplianceSpaceStateByNumber(spaceNumber)
.IsInUse(blockIndex)
);
Assert.Equal(
expectedDurationBlock,
afterRootState.VillageState!.HouseState.KitchenState
.GetApplianceSpaceStateByNumber(spaceNumber)
.CookingDurationBlock
);
}
}

[Fact]
public void Execute_Failure_NotFoundKitchenEquipmentState()
{
Expand Down Expand Up @@ -404,6 +530,7 @@ public void Execute_Failure_NotHaveRequiredKitchenEquipmentState()
{
var blockIndex = 1;
Recipe recipe = getRandomRecipeWithEquipmentCategory("sub");
KitchenEquipment notRequiredKitchenEquipment = getRandomNotRequiredKitchenEquipment(recipe);

IAccountStateDelta beforeState = new DummyState();
var (beforeRootState, kitchenEquipmentStateIdsToUse, spaceNumbersToUse) = createPreset(
Expand All @@ -420,7 +547,11 @@ select stateList.StateID
);
beforeRootState.SetInventoryState(
beforeRootState.InventoryState.AddKitchenEquipmentItem(
new KitchenEquipmentState(kitchenEquipmentStateIdsToUse[0], -1, -1)
new KitchenEquipmentState(
kitchenEquipmentStateIdsToUse[0],
notRequiredKitchenEquipment.ID,
notRequiredKitchenEquipment.KitchenEquipmentCategoryID
)
)
);
beforeState = beforeState.SetState(SignerAddress(), beforeRootState.Serialize());
Expand Down Expand Up @@ -454,14 +585,19 @@ public void Execute_Failure_NotHaveRequiredInstalledKitchenEquipmentState()
{
var blockIndex = 1;
Recipe recipe = getRandomRecipeWithEquipmentCategory("main");
KitchenEquipment notRequiredKitchenEquipment = getRandomNotRequiredKitchenEquipment(recipe);

IAccountStateDelta beforeState = new DummyState();
var (beforeRootState, kitchenEquipmentStateIdsToUse, spaceNumbersToUse) = createPreset(
recipe
);
foreach (var spaceNumber in spaceNumbersToUse)
{
var illusionKitchenEquipment = new KitchenEquipmentState(Guid.NewGuid(), -1, -1);
var illusionKitchenEquipment = new KitchenEquipmentState(
Guid.NewGuid(),
notRequiredKitchenEquipment.ID,
notRequiredKitchenEquipment.KitchenEquipmentCategoryID
);
beforeRootState.SetInventoryState(
beforeRootState.InventoryState.AddKitchenEquipmentItem(illusionKitchenEquipment)
);
Expand Down
Loading

0 comments on commit d0d9a8f

Please sign in to comment.