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

Merge 2023hackathon #111

Merged
merged 4 commits into from
Jan 7, 2024
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
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "frontend/Saver-22b-godot/addons/GodotGraphQL"]
path = frontend/Saver-22b-godot/addons/GodotGraphQL
url = https://github.com/Dracks/GodotGraphQL.git
16 changes: 16 additions & 0 deletions backend/app/Libplanet.Headless/Extensions/BencodexExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,22 @@ public static long ToLong(this IValue serialized) =>

#endregion long

#region bool

public static IValue Serialize(this bool boolean) =>
(Text)boolean.ToString();

public static IValue Serialize(this bool? boolean) =>
Serialize(Serialize, boolean);

public static bool ToBoolean(this IValue serialized) =>
bool.Parse(((Text)serialized).Value);

public static bool? ToNullableBoolean(this IValue serialized) =>
Deserialize(ToBoolean, serialized);

#endregion bool

#region Text

public static IValue Serialize(this string text) =>
Expand Down
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()
});
});
}

}
160 changes: 160 additions & 0 deletions backend/app/Savor22b.Tests/Action/UseLifeStoneActionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
namespace Savor22b.Tests.Action;

using System;
using System.Collections.Immutable;
using Libplanet;
using Libplanet.State;
using Savor22b.Action;
using Savor22b.Action.Exceptions;
using Savor22b.States;
using Xunit;

public class UseLifeStoneActionTests : ActionTests
{
private static readonly int LifeStoneItemId = 2;

[Fact]
public void UseLifeStoneAction_Success()
{
var stateDelta = CreatePresetStateDelta();

var food = DeriveRootStateFromAccountStateDelta(stateDelta)
.InventoryState
.RefrigeratorStateList[0];

var action = new UseLifeStoneAction(food.StateID);

stateDelta = action.Execute(
new DummyActionContext
{
PreviousStates = stateDelta,
Signer = SignerAddress(),
Random = random,
Rehearsal = false,
BlockIndex = 1,
}
);

food.IsSuperFood = true;

var newInventoryState = DeriveRootStateFromAccountStateDelta(stateDelta).InventoryState;

Assert.Equal(food, newInventoryState.RefrigeratorStateList[0]);
Assert.Empty(newInventoryState.ItemStateList);
}

[Fact]
public void UseLifeStoneAction_Fail_NoLifeStone()
{
var stateDelta = CreatePresetStateDelta(hasLifeStone: false);

var food = DeriveRootStateFromAccountStateDelta(stateDelta)
.InventoryState
.RefrigeratorStateList[0];

var action = new UseLifeStoneAction(food.StateID);

Assert.Throws<NotHaveRequiredException>(() =>
{
action.Execute(
new DummyActionContext
{
PreviousStates = stateDelta,
Signer = SignerAddress(),
Random = random,
Rehearsal = false,
BlockIndex = 1,
}
);
});
}

[Fact]
public void UseLifeStoneAction_Fail_AlreadyIsSuperFood()
{
var stateDelta = CreatePresetStateDelta();

var rootState = DeriveRootStateFromAccountStateDelta(stateDelta);
var inventoryState = rootState.InventoryState;

var food = rootState.InventoryState.RefrigeratorStateList[0];
food.IsSuperFood = true;

rootState.SetInventoryState(inventoryState);

var action = new UseLifeStoneAction(food.StateID);

Assert.Throws<AlreadyIsSuperFoodException>(() =>
{
action.Execute(
new DummyActionContext
{
PreviousStates = stateDelta.SetState(SignerAddress(), rootState.Serialize()),
Signer = SignerAddress(),
Random = random,
Rehearsal = false,
BlockIndex = 1,
}
);
}
);
}

[Fact]
public void UseLifeStoneAction_AssertPresetStateDelta()
{
var stateDelta = CreatePresetStateDelta();
var inventoryState = DeriveRootStateFromAccountStateDelta(stateDelta).InventoryState;
Assert.Single(inventoryState.ItemStateList);
Assert.Equal(LifeStoneItemId, inventoryState.ItemStateList[0].ItemID);
Assert.Single(inventoryState.RefrigeratorStateList);
Assert.False(inventoryState.RefrigeratorStateList[0].IsSuperFood);

stateDelta = CreatePresetStateDelta(hasLifeStone: false);
inventoryState = DeriveRootStateFromAccountStateDelta(stateDelta).InventoryState;
Assert.Empty(inventoryState.ItemStateList);
}

private IAccountStateDelta CreatePresetStateDelta(bool hasLifeStone = true)
{
IAccountStateDelta state = new DummyState();
Address signerAddress = SignerAddress();

var rootStateEncoded = state.GetState(signerAddress);
RootState rootState = rootStateEncoded is Bencodex.Types.Dictionary bdict
? new RootState(bdict)
: new RootState();

InventoryState inventoryState = rootState.InventoryState;

if (hasLifeStone)
{
inventoryState = inventoryState.AddItem(new ItemState(Guid.NewGuid(), LifeStoneItemId));
}

var food = RefrigeratorState.CreateFood(
Guid.NewGuid(),
1,
"D",
1,
1,
1,
1,
1,
ImmutableList<Guid>.Empty
);
inventoryState = inventoryState.AddRefrigeratorItem(food);

rootState.SetInventoryState(inventoryState);

return state.SetState(signerAddress, rootState.Serialize());
}

private RootState DeriveRootStateFromAccountStateDelta(IAccountStateDelta stateDelta) {
var rootStateEncoded = stateDelta.GetState(SignerAddress());
RootState rootState = rootStateEncoded is Bencodex.Types.Dictionary bdict
? new RootState(bdict)
: throw new Exception();
return rootState;
}
}
Loading
Loading