Skip to content
This repository was archived by the owner on Feb 7, 2025. It is now read-only.

Commit

Permalink
Merge branch 'main' into feat/level-up
Browse files Browse the repository at this point in the history
  • Loading branch information
Atralupus authored Mar 11, 2024
2 parents 4c68a3a + 512e686 commit af22e1e
Show file tree
Hide file tree
Showing 13 changed files with 168 additions and 22 deletions.
1 change: 1 addition & 0 deletions backend/app/Savor22b.Tests/Action/CancelFoodActionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public class CancelFoodActionTests : ActionTests

RootState beforeRootState = new RootState(
beforeInventoryState,
new DungeonState(),
new VillageState(new HouseState(1, 1, 1, beforeKitchenState))
);

Expand Down
1 change: 1 addition & 0 deletions backend/app/Savor22b.Tests/Action/CreateFoodActionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ ImmutableList<int> FoodIDList

RootState rootState = new RootState(
inventoryState,
new DungeonState(),
new VillageState(new HouseState(1, 1, 1, resultKitchenState))
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public void Execute_Success_Normal()
IAccountStateDelta beforeState = new DummyState();
RootState beforeRootState = new RootState(
new InventoryState(),
new DungeonState(),
new VillageState(
new HouseState(
1, 1, 1, new KitchenState()
Expand Down Expand Up @@ -70,6 +71,7 @@ public void Execute_Success_AfterRemoveWeed()
IAccountStateDelta beforeState = new DummyState();
RootState beforeRootState = new RootState(
new InventoryState(),
new DungeonState(),
new VillageState(
new HouseState(
1, 1, 1, new KitchenState()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public void Execute_Success_NormalCase(int spaceNumber)
ImmutableList<KitchenEquipmentState>.Empty.Add(new KitchenEquipmentState(Guid.NewGuid(), 1, 1)),
ImmutableList<ItemState>.Empty
),
new DungeonState(),
new VillageState(
new HouseState(
1, 1, 1, new KitchenState()
Expand Down Expand Up @@ -99,6 +100,7 @@ public void Execute_Failure_NotHaveKitchenEquipment()
ImmutableList<KitchenEquipmentState>.Empty,
ImmutableList<ItemState>.Empty
),
new DungeonState(),
new VillageState(
new HouseState(
1, 1, 1, new KitchenState()
Expand Down Expand Up @@ -138,6 +140,7 @@ public void Execute_Failure_NotPlacedHouse()
ImmutableList<KitchenEquipmentState>.Empty,
ImmutableList<ItemState>.Empty
),
new DungeonState(),
null
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ private IAccountStateDelta createStateForRelocationHouse()
prevGlobalUserHouseState.UserHouse.Add("1,0,0", SignerAddress());

VillageState villageState = new VillageState(new HouseState(1, 0, 0, new KitchenState()));
RootState prevRootState = new RootState(new InventoryState(), villageState);
RootState prevRootState = new RootState(new InventoryState(), new DungeonState(), villageState);

state = state.MintAsset(
SignerAddress(),
Expand Down Expand Up @@ -141,6 +141,7 @@ private IAccountStateDelta createStateForInProgressReplaceHouse()
VillageState villageState = new VillageState(new HouseState(1, 0, 0, new KitchenState()));
RootState prevRootState = new RootState(
new InventoryState(),
new DungeonState(),
villageState,
new RelocationState(1, 90, 1, 0, 0)
);
Expand Down
3 changes: 2 additions & 1 deletion backend/app/Savor22b.Tests/Action/PlantingSeedActionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ public void Execute_InvalidVillageState()

InventoryState inventoryState = getInventoryState();
RootState beforeRootState = new RootState(
inventoryState
inventoryState,
new DungeonState()
);

beforeState = beforeState.SetState(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public void Execute_Success_Normal()
IAccountStateDelta beforeState = new DummyState();
RootState beforeRootState = new RootState(
new InventoryState(),
new DungeonState(),
new VillageState(
new HouseState(
1, 1, 1, new KitchenState()
Expand Down
1 change: 1 addition & 0 deletions backend/app/Savor22b.Tests/Action/RemoveWeedActionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public RootState createRootStatePreset()
{
RootState beforeRootState = new RootState(
new InventoryState(),
new DungeonState(),
new VillageState(
new HouseState(
1, 1, 1, new KitchenState()
Expand Down
24 changes: 24 additions & 0 deletions backend/app/Savor22b/GraphTypes/Types/DungeonStateType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
namespace Savor22b.GraphTypes.Types;

using GraphQL.Types;
using Libplanet.Blockchain;
using Libplanet.Net;
using Libplanet.Store;
using Savor22b.States;

public class DungeonStateType : ObjectGraphType<DungeonState>
{
public DungeonStateType(
BlockChain blockChain,
BlockRenderer blockRenderer,
IStore store,
Swarm? swarm = null
)
{
Field<NonNullGraphType<IntGraphType>>(
name: "DungeonKeyCount",
description: "The number of dungeon keys the user has.",
resolve: context => context.Source.GetDungeonKeyCount(blockChain.Count)
);
}
}
5 changes: 5 additions & 0 deletions backend/app/Savor22b/GraphTypes/Types/UserStateType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,10 @@ public UserStateType()
description: "The village state of the user.",
resolve: context => context.Source.VillageState
);
Field<NonNullGraphType<DungeonStateType>>(
name: "dungeonState",
description: "The dungeon state of the user.",
resolve: context => context.Source.DungeonState
);
}
}
33 changes: 33 additions & 0 deletions backend/app/Savor22b/States/DungeonKeyHistory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
namespace Savor22b.States;

using Bencodex.Types;
using Libplanet.Headless.Extensions;

public class DungeonKeyHistory : State
{
public long BlockIndex { get; private set; }
public int DungeonId { get; private set; }

public DungeonKeyHistory(long blockIndex, int dungeonId)
{
BlockIndex = blockIndex;
DungeonId = dungeonId;
}

public DungeonKeyHistory(Dictionary encoded)
{
BlockIndex = encoded[nameof(BlockIndex)].ToLong();
DungeonId = encoded[nameof(DungeonId)].ToInteger();
}

public IValue Serialize()
{
var pairs = new[]
{
new KeyValuePair<IKey, IValue>((Text)nameof(BlockIndex), BlockIndex.Serialize()),
new KeyValuePair<IKey, IValue>((Text)nameof(DungeonId), DungeonId.Serialize()),
};

return new Dictionary(pairs);
}
}
73 changes: 73 additions & 0 deletions backend/app/Savor22b/States/DungeonState.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
namespace Savor22b.States;

using System.Collections.Immutable;
using Bencodex.Types;

public class DungeonState : State
{
public static readonly int MaxDungeonKey = 5;
public static readonly int DungeonKeyChargeIntervalBlock = 12;

public ImmutableList<DungeonKeyHistory> DungeonKeyHistories { get; private set; }

public DungeonState()
{
DungeonKeyHistories = ImmutableList<DungeonKeyHistory>.Empty;
}

public DungeonState(ImmutableList<DungeonKeyHistory> dungeonKeyHistories)
{
DungeonKeyHistories = dungeonKeyHistories;
}

public DungeonState(Dictionary encoded)
{
if (encoded.TryGetValue((Text)nameof(DungeonKeyHistories), out var dungeonKeyHistories))
{
DungeonKeyHistories = ((List)dungeonKeyHistories)
.Select(element => new DungeonKeyHistory((Dictionary)element))
.ToImmutableList();
}
else
{
DungeonKeyHistories = ImmutableList<DungeonKeyHistory>.Empty;
}
}

public IValue Serialize()
{
return new Dictionary(new[]
{
new KeyValuePair<IKey, IValue>(
(Text)nameof(DungeonKeyHistories),
new List(DungeonKeyHistories.Select(element => element.Serialize()))),
});
}

public int GetDungeonKeyCount(long blockIndex)
{
return MaxDungeonKey - GetCurrentDungeonKeyHistories(blockIndex).Count;
}

public ImmutableList<DungeonKeyHistory> GetCurrentDungeonKeyHistories(long blockIndex)
{
var lowerBoundIndex = blockIndex - (MaxDungeonKey * DungeonKeyChargeIntervalBlock);
var result = new List<DungeonKeyHistory>();

for (int i = DungeonKeyHistories.Count - 1; i >= 0; i--)
{
var history = DungeonKeyHistories[i];

if (history.BlockIndex > lowerBoundIndex && history.BlockIndex <= blockIndex)
{
result.Add(history);
}
else if (history.BlockIndex <= lowerBoundIndex)
{
break;
}
}

return result.ToImmutableList();
}
}
40 changes: 20 additions & 20 deletions backend/app/Savor22b/States/RootState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace Savor22b.States;
public class RootState : State
{
public InventoryState InventoryState { get; private set; }
public DungeonState DungeonState { get; private set; }

public RelocationState? RelocationState { get; private set; }

Expand All @@ -13,31 +14,15 @@ public class RootState : State
public RootState()
{
InventoryState = new InventoryState();
DungeonState = new DungeonState();
VillageState = null;
RelocationState = null;
}

public RootState(InventoryState inventoryState)
public RootState(InventoryState inventoryState, DungeonState dungeonState, VillageState? villageState = null, RelocationState? relocationState = null)
{
InventoryState = inventoryState;
VillageState = null;
RelocationState = null;
}

public RootState(InventoryState inventoryState, VillageState villageState)
{
InventoryState = inventoryState;
VillageState = villageState;
RelocationState = null;
}

public RootState(
InventoryState inventoryState,
VillageState villageState,
RelocationState relocationState
)
{
InventoryState = inventoryState;
InventoryState = inventoryState ?? new InventoryState();
DungeonState = dungeonState ?? new DungeonState();
VillageState = villageState;
RelocationState = relocationState;
}
Expand All @@ -55,6 +40,17 @@ public RootState(Bencodex.Types.Dictionary encoded)
InventoryState = new InventoryState();
}

if (encoded.ContainsKey((Text)nameof(DungeonState)))
{
DungeonState = new DungeonState(
(Bencodex.Types.Dictionary)encoded[nameof(DungeonState)]
);
}
else
{
DungeonState = new DungeonState();
}

if (encoded.ContainsKey((Text)nameof(VillageState)))
{
VillageState = new VillageState(
Expand Down Expand Up @@ -101,6 +97,10 @@ public IValue Serialize()
(Text)nameof(InventoryState),
InventoryState.Serialize()
),
new KeyValuePair<IKey, IValue>(
(Text)nameof(DungeonState),
DungeonState.Serialize()
),
};

if (RelocationState is not null)
Expand Down

0 comments on commit af22e1e

Please sign in to comment.