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

Handle dungeon id nullable value #191

Merged
merged 1 commit into from
May 25, 2024
Merged
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
34 changes: 25 additions & 9 deletions backend/app/Savor22b/Action/RegisterTradeGoodAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ namespace Savor22b.Action;
using Libplanet.Assets;
using Libplanet.Headless.Extensions;
using Libplanet.State;
using Savor22b.Action.Exceptions;
using Savor22b.States;
using Savor22b.States.Trade;
using Savor22b.Action.Exceptions;

[ActionType(nameof(RegisterTradeGoodAction))]
public class RegisterTradeGoodAction : SVRAction
Expand Down Expand Up @@ -53,16 +53,23 @@ public RegisterTradeGoodAction(FungibleAssetValue price, int dungeonId)
{
[nameof(Price)] = Price.ToBencodex(),
[nameof(FoodStateId)] = FoodStateId.Serialize(),
[nameof(ItemStateIds)] = ItemStateIds is null ? Null.Value : (List)ItemStateIds.Select(i => i.Serialize()),
[nameof(ItemStateIds)] = ItemStateIds is null
? Null.Value
: (List)ItemStateIds.Select(i => i.Serialize()),
[nameof(DungeonId)] = DungeonId.Serialize(),
}.ToImmutableDictionary();

protected override void LoadPlainValueInternal(IImmutableDictionary<string, IValue> plainValue)
{
Price = plainValue[nameof(Price)].ToFungibleAssetValue();
FoodStateId = plainValue[nameof(FoodStateId)].ToGuid();
ItemStateIds = plainValue[nameof(ItemStateIds)] is Null ? null : ((List)plainValue[nameof(ItemStateIds)]).Select(e => e.ToGuid()).ToImmutableList();
DungeonId = plainValue[nameof(DungeonId)].ToInteger();
ItemStateIds =
plainValue[nameof(ItemStateIds)] is Null
? null
: ((List)plainValue[nameof(ItemStateIds)])
.Select(e => e.ToGuid())
.ToImmutableList();
DungeonId = plainValue[nameof(DungeonId)].ToNullableInteger();
}

public override IAccountStateDelta Execute(IActionContext ctx)
Expand All @@ -77,10 +84,12 @@ public override IAccountStateDelta Execute(IActionContext ctx)
RootState rootState = states.GetState(ctx.Signer) is Dictionary rootStateEncoded
? new RootState(rootStateEncoded)
: new RootState();
TradeInventoryState tradeInventoryState = states.GetState(TradeInventoryState.StateAddress) is Dictionary tradeInventoryStateEncoded
TradeInventoryState tradeInventoryState = states.GetState(TradeInventoryState.StateAddress)
is Dictionary tradeInventoryStateEncoded
? new TradeInventoryState(tradeInventoryStateEncoded)
: new TradeInventoryState();
GlobalDungeonState globalDungeonState = states.GetState(GlobalDungeonState.StateAddress) is Dictionary globalDungeonStateEncoded
GlobalDungeonState globalDungeonState = states.GetState(GlobalDungeonState.StateAddress)
is Dictionary globalDungeonStateEncoded
? new GlobalDungeonState(globalDungeonStateEncoded)
: new GlobalDungeonState();

Expand All @@ -89,7 +98,12 @@ public override IAccountStateDelta Execute(IActionContext ctx)
if (FoodStateId is not null)
{
var foodState = inventoryState.GetRefrigeratorItem(FoodStateId.Value);
var foodGoodState = new FoodGoodState(ctx.Signer, ctx.Random.GenerateRandomGuid(), Price, foodState);
var foodGoodState = new FoodGoodState(
ctx.Signer,
ctx.Random.GenerateRandomGuid(),
Price,
foodState
);
inventoryState = inventoryState.RemoveRefrigeratorItem(FoodStateId.Value);
tradeInventoryState = tradeInventoryState.RegisterGood(foodGoodState);
}
Expand All @@ -106,7 +120,8 @@ public override IAccountStateDelta Execute(IActionContext ctx)
ctx.Signer,
ctx.Random.GenerateRandomGuid(),
Price,
itemStates.ToImmutableList());
itemStates.ToImmutableList()
);

tradeInventoryState = tradeInventoryState.RegisterGood(itemsGoodState);
}
Expand All @@ -116,7 +131,8 @@ public override IAccountStateDelta Execute(IActionContext ctx)
ctx.Signer,
ctx.Random.GenerateRandomGuid(),
Price,
DungeonId.Value);
DungeonId.Value
);

if (!globalDungeonState.IsDungeonConquestAddress(DungeonId.Value, ctx.Signer))
{
Expand Down
Loading