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

[Feature] Merge planting seed and random seed item #92

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
41 changes: 25 additions & 16 deletions backend/app/Savor22b.Tests/Action/PlantingSeedActionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ namespace Savor22b.Tests.Action;
using Savor22b.Action;
using Savor22b.States;
using Savor22b.Action.Exceptions;
using System.Collections.Immutable;

public class PlantingSeedActionTests : ActionTests
{
Expand All @@ -18,8 +17,10 @@ public PlantingSeedActionTests()
private InventoryState getInventoryState()
{
InventoryState inventoryState = new InventoryState();
var newSeed = new SeedState(Guid.NewGuid(), 1);
inventoryState = inventoryState.AddSeed(newSeed);

var itemState = new ItemState(Guid.NewGuid(), 1);
inventoryState = inventoryState.AddItem(itemState);

return inventoryState;
}

Expand Down Expand Up @@ -58,10 +59,12 @@ public void Execute_ValidAction()
SignerAddress(),
beforeRootState.Serialize()
);
var beforeSeedGuid = Guid.NewGuid();

PlantingSeedAction plantingSeedAction = new PlantingSeedAction(
beforeRootState.InventoryState.SeedStateList[0].StateID,
0
beforeSeedGuid,
0,
beforeRootState.InventoryState.ItemStateList[0].StateID
);

var random = new DummyRandom(1);
Expand All @@ -80,8 +83,9 @@ public void Execute_ValidAction()
? new RootState(bdict)
: throw new Exception();

Assert.Equal(rootState.VillageState!.HouseFieldStates[0]!.InstalledSeedGuid, beforeRootState.InventoryState.SeedStateList[0].StateID);
Assert.Equal(rootState.InventoryState.SeedStateList.Count, 0);
Assert.Equal(rootState.VillageState!.HouseFieldStates[0]!.InstalledSeedGuid, beforeSeedGuid);
Assert.Equal(rootState.InventoryState.SeedStateList.Count, 1);
Assert.Equal(rootState.InventoryState.ItemStateList.Count, 0);
}

[Fact]
Expand All @@ -98,10 +102,12 @@ public void Execute_InvalidVillageState()
SignerAddress(),
beforeRootState.Serialize()
);
var beforeSeedGuid = Guid.NewGuid();

PlantingSeedAction plantingSeedAction = new PlantingSeedAction(
beforeRootState.InventoryState.SeedStateList[0].StateID,
1
beforeSeedGuid,
1,
beforeRootState.InventoryState.ItemStateList[0].StateID
);

var random = new DummyRandom(1);
Expand Down Expand Up @@ -130,10 +136,12 @@ public void Execute_InvalidFieldIndex()
SignerAddress(),
beforeRootState.Serialize()
);
var beforeSeedGuid = Guid.NewGuid();

PlantingSeedAction plantingSeedAction = new PlantingSeedAction(
beforeRootState.InventoryState.SeedStateList[0].StateID,
VillageState.HouseFieldCount + 1
beforeSeedGuid,
VillageState.HouseFieldCount + 1,
beforeRootState.InventoryState.ItemStateList[0].StateID
);

var random = new DummyRandom(1);
Expand All @@ -152,7 +160,7 @@ public void Execute_InvalidFieldIndex()
}

[Fact]
public void Execute_InvalidSeedStateId()
public void Execute_InvalidItemStateId()
{
IAccountStateDelta beforeState = new DummyState();

Expand All @@ -162,15 +170,17 @@ public void Execute_InvalidSeedStateId()
SignerAddress(),
beforeRootState.Serialize()
);
var beforeSeedGuid = Guid.NewGuid();

PlantingSeedAction plantingSeedAction = new PlantingSeedAction(
Guid.NewGuid(),
0
beforeSeedGuid,
0,
Guid.NewGuid()
);

var random = new DummyRandom(1);

Assert.Throws<NotFoundDataException>(() =>
Assert.Throws<NotHaveRequiredException>(() =>
{
plantingSeedAction.Execute(new DummyActionContext
{
Expand All @@ -182,5 +192,4 @@ public void Execute_InvalidSeedStateId()
});
});
}

}
115 changes: 0 additions & 115 deletions backend/app/Savor22b.Tests/Action/UseRandomSeedItemActionTests.cs

This file was deleted.

51 changes: 41 additions & 10 deletions backend/app/Savor22b/Action/PlantingSeedAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,30 @@ public class PlantingSeedAction : SVRAction
{
public Guid SeedGuid;
public int FieldIndex;
public Guid ItemStateIdToUse;

public PlantingSeedAction() { }

public PlantingSeedAction(Guid seedGuid, int fieldIndex)
public PlantingSeedAction(Guid seedGuid, int fieldIndex, Guid itemStateIdToUse)
{
SeedGuid = seedGuid;
FieldIndex = fieldIndex;
ItemStateIdToUse = itemStateIdToUse;
}

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

protected override void LoadPlainValueInternal(IImmutableDictionary<string, IValue> plainValue)
{
SeedGuid = plainValue[nameof(SeedGuid)].ToGuid();
FieldIndex = plainValue[nameof(FieldIndex)].ToInteger();
ItemStateIdToUse = plainValue[nameof(ItemStateIdToUse)].ToGuid();
}

public void checkAndRaisePlantingAble(RootState rootState)
Expand All @@ -54,13 +58,36 @@ public void checkAndRaisePlantingAble(RootState rootState)
{
throw new FieldAlreadyOccupiedException("Field is already occupied");
}
}

private Seed generateRandomSeed(IRandom random, int villageId)
{
int randomIndex;
var seeds = CsvDataHelper.GetSeedCSVData();
var village = CsvDataHelper.GetVillageCharacteristicByVillageId(villageId)!;

do
{
randomIndex = random.Next(0, seeds.Count);
} while (!village.AvailableSeedIdList.Contains(seeds[randomIndex].Id));

var randomSeedCsvData = seeds[randomIndex];

return randomSeedCsvData;
}

SeedState? seedState = rootState.InventoryState.GetSeedState(SeedGuid);
private InventoryState FindAndRemoveItem(InventoryState state)
{
var item = state.ItemStateList.Find(state => state.StateID == ItemStateIdToUse);

if (seedState is null)
if (item is null)
{
throw new NotFoundDataException("Seed not found");
throw new NotHaveRequiredException($"You don't have `{ItemStateIdToUse}` item");
}

state = state.RemoveItem(ItemStateIdToUse);

return state;
}

public override IAccountStateDelta Execute(IActionContext ctx)
Expand All @@ -76,18 +103,22 @@ is Bencodex.Types.Dictionary rootStateEncoded
checkAndRaisePlantingAble(rootState);

InventoryState inventoryState = rootState.InventoryState;
SeedState seedState = rootState.InventoryState.GetSeedState(SeedGuid)!;
inventoryState = FindAndRemoveItem(inventoryState);

inventoryState = inventoryState.RemoveSeed(SeedGuid);
rootState.SetInventoryState(inventoryState);
Seed seedCsvData = generateRandomSeed(
ctx.Random,
rootState.VillageState!.HouseState.VillageID
);
var seedState = new SeedState(SeedGuid, seedCsvData.Id);
inventoryState = inventoryState.AddSeed(seedState);

Seed seed = CsvDataHelper.GetSeedById(seedState.SeedID)!;
rootState.SetInventoryState(inventoryState);

HouseFieldState houseFieldState = new HouseFieldState(
SeedGuid,
seedState.SeedID,
seedCsvData.Id,
ctx.BlockIndex,
seed.RequiredBlock
seedCsvData.RequiredBlock
);

rootState.VillageState!.UpdateHouseFieldState(FieldIndex, houseFieldState);
Expand Down
Loading
Loading