-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #84 from Atralupus/feat/world-information-handler
Add world information handler
- Loading branch information
Showing
2 changed files
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
} |