Skip to content

Commit

Permalink
feat: Add Remove installed kitchen equipment action & query
Browse files Browse the repository at this point in the history
  • Loading branch information
upa-r-upa committed Apr 15, 2024
1 parent 8995b83 commit 142af6e
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
namespace Savor22b.Action;

using Libplanet.Action;
using Savor22b.States;
using Savor22b.Action.Util;
using System.Collections.Immutable;
using Bencodex.Types;
using Libplanet.Headless.Extensions;
using Libplanet.State;

[ActionType(nameof(RemoveInstalledKitchenEquipmentAction))]
public class RemoveInstalledKitchenEquipmentAction : SVRAction
{
public RemoveInstalledKitchenEquipmentAction() { }

public RemoveInstalledKitchenEquipmentAction(Guid installedEquipmentStateId)
{
InstalledEquipmentStateId = installedEquipmentStateId;
}

public Guid InstalledEquipmentStateId { get; private set; }

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

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

public override IAccountStateDelta Execute(IActionContext ctx)
{
if (ctx.Rehearsal)
{
return ctx.PreviousStates;
}

IAccountStateDelta states = ctx.PreviousStates;

RootState rootState = states.GetState(ctx.Signer) is Dictionary rootStateEncoded
? new RootState(rootStateEncoded)
: new RootState();

Validation.EnsureReplaceInProgress(rootState, ctx.BlockIndex);
Validation.EnsureVillageStateExists(rootState);

KitchenState kitchenState = rootState.VillageState!.HouseState.KitchenState;

kitchenState.RemoveInstalledEquipment(InstalledEquipmentStateId);

return states.SetState(ctx.Signer, rootState.Serialize());
}
}
26 changes: 16 additions & 10 deletions backend/app/Savor22b/GraphTypes/Query/Query.cs
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,9 @@ swarm is null
ByteUtil.ParseHex(context.GetArgument<string>("publicKey"))
);
var action = new CancelRegisteredTradeGoodAction(context.GetArgument<Guid>("productId"));
var action = new CancelRegisteredTradeGoodAction(
context.GetArgument<Guid>("productId")
);
return new GetUnsignedTransactionHex(
action,
Expand Down Expand Up @@ -602,11 +604,15 @@ swarm is null
var publicKey = new PublicKey(
ByteUtil.ParseHex(context.GetArgument<string>("publicKey"))
);
var price = FungibleAssetValue.Parse(Currencies.KeyCurrency, context.GetArgument<int>("price").ToString());
var price = FungibleAssetValue.Parse(
Currencies.KeyCurrency,
context.GetArgument<int>("price").ToString()
);
var action = new UpdateTradeGoodAction(
context.GetArgument<Guid>("productId"),
price);
price
);
return new GetUnsignedTransactionHex(
action,
Expand Down Expand Up @@ -681,14 +687,15 @@ swarm is null
);
var foodStateId = context.GetArgument<Guid?>("foodStateId");
var itemStateIds = context.GetArgument<List<Guid>?>("itemStateIds");
var price = FungibleAssetValue.Parse(Currencies.KeyCurrency, context.GetArgument<int>("price").ToString());
var price = FungibleAssetValue.Parse(
Currencies.KeyCurrency,
context.GetArgument<int>("price").ToString()
);
if (foodStateId is not null)
{
return new GetUnsignedTransactionHex(
new RegisterTradeGoodAction(
price,
foodStateId.Value),
new RegisterTradeGoodAction(price, foodStateId.Value),
publicKey,
_blockChain,
_swarm
Expand All @@ -697,9 +704,7 @@ swarm is null
else if (itemStateIds is not null)
{
return new GetUnsignedTransactionHex(
new RegisterTradeGoodAction(
price,
itemStateIds.ToImmutableList()),
new RegisterTradeGoodAction(price, itemStateIds.ToImmutableList()),
publicKey,
_blockChain,
_swarm
Expand All @@ -719,6 +724,7 @@ swarm is null
AddField(new VillageField(blockChain, subject));
AddField(new ShowMeTheMoney(blockChain, swarm));
AddField(new ConquestDungeonActionQuery(blockChain, swarm));
AddField(new RemoveInstalledKitchenEquipmentActionQuery(blockChain, swarm));
}

private List<RecipeResponse> combineRecipeData()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
namespace Savor22b.GraphTypes.Query;

using GraphQL;
using GraphQL.Resolvers;
using GraphQL.Types;
using Libplanet.Blockchain;
using Libplanet.Crypto;
using Libplanet.Net;
using Savor22b.Action;

public class RemoveInstalledKitchenEquipmentActionQuery : FieldType
{
public RemoveInstalledKitchenEquipmentActionQuery(BlockChain blockChain, Swarm swarm)
: base()
{
Name = "createAction_RemoveInstalledKitchenEquipmentActionQuery";
Type = typeof(NonNullGraphType<StringGraphType>);
Description = "설치된 큰 조리도구를 설치 제거합니다.";
Arguments = new QueryArguments(
new QueryArgument<NonNullGraphType<StringGraphType>>
{
Name = "publicKey",
Description = "대상 유저의 40-hex 형태의 address 입니다.",
},
new QueryArgument<NonNullGraphType<GuidGraphType>>
{
Name = "installedEquipmentStateId",
Description = "제거하려는 설치된 큰 조리도구의 State Id(Guid) 입니다.",
}
);
Resolver = new FuncFieldResolver<string>(context =>
{
try
{
var publicKey = new PublicKey(
Libplanet.ByteUtil.ParseHex(context.GetArgument<string>("publicKey"))
);
var action = new RemoveInstalledKitchenEquipmentAction(
context.GetArgument<Guid>("installedEquipmentStateId")
);
return new GetUnsignedTransactionHex(
action,
publicKey,
blockChain,
swarm
).UnsignedTransactionHex;
}
catch (Exception e)
{
throw new ExecutionError(e.Message);
}
});
}
}
24 changes: 24 additions & 0 deletions backend/app/Savor22b/States/KitchenState.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace Savor22b.States;

using Bencodex.Types;
using Savor22b.Action.Exceptions;

public class KitchenState : State
{
Expand Down Expand Up @@ -79,6 +80,29 @@ int spaceNumber
}
}

public void RemoveInstalledEquipment(Guid stateId)
{
if (FirstApplianceSpace.InstalledKitchenEquipmentStateId == stateId)
{
FirstApplianceSpace.UnInstallKitchenEquipment();
return;
}

if (SecondApplianceSpace.InstalledKitchenEquipmentStateId == stateId)
{
SecondApplianceSpace.UnInstallKitchenEquipment();
return;
}

if (ThirdApplianceSpace.InstalledKitchenEquipmentStateId == stateId)
{
ThirdApplianceSpace.UnInstallKitchenEquipment();
return;
}

throw new InvalidValueException($"The equipment with the id {stateId} is not installed.");
}

public ApplianceSpaceState GetApplianceSpaceStateByNumber(int spaceNumber)
{
switch (spaceNumber)
Expand Down

0 comments on commit 142af6e

Please sign in to comment.