Skip to content

Commit

Permalink
Merge pull request #84 from Atralupus/feat/world-information-handler
Browse files Browse the repository at this point in the history
Add world information handler
  • Loading branch information
Atralupus authored May 28, 2024
2 parents 44db71d + 20f07c5 commit 39c39e3
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
1 change: 1 addition & 0 deletions Mimir.Worker/Handler/AddressHandlerMappings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ static AddressHandlerMappings()

HandlerMappings[Addresses.Avatar] = new AvatarStateHandler();
HandlerMappings[Addresses.Inventory] = new InventoryStateHandler();
HandlerMappings[Addresses.WorldInformation] = new WorldInformationStateHandler();
}

private static void InitializeHandlers()
Expand Down
49 changes: 49 additions & 0 deletions Mimir.Worker/Handler/WorldInformationStateHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using Bencodex;
using Bencodex.Types;
using Libplanet.Crypto;
using Mimir.Worker.Models;
using Nekoyume.Model;
using Nekoyume.Model.State;

namespace Mimir.Worker.Handler;

public class WorldInformationStateHandler : IStateHandler<StateData>
{
private class WorldInformationState : State
{
public WorldInformation WorldInformation;

public WorldInformationState(Address address, WorldInformation worldInformation)
: base(address)
{
WorldInformation = worldInformation;
}
}

public StateData ConvertToStateData(Address address, IValue rawState)
{
var worldInformation = ConvertToState(rawState);
return new StateData(address, new WorldInformationState(address, worldInformation));
}

public StateData ConvertToStateData(Address address, string rawState)
{
Codec Codec = new();
var state = Codec.Decode(Convert.FromHexString(rawState));
var worldInformation = ConvertToState(state);

return new StateData(address, new WorldInformationState(address, worldInformation));
}

private WorldInformation ConvertToState(IValue state)
{
if (state is Dictionary dict)
{
return new WorldInformation(dict);
}
else
{
throw new ArgumentException("Invalid state type. Expected Dictionary.", nameof(state));
}
}
}

0 comments on commit 39c39e3

Please sign in to comment.