-
Notifications
You must be signed in to change notification settings - Fork 18
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 #332 from Atralupus/feat/reactivate-worldinformation
Reactivate world information
- Loading branch information
Showing
10 changed files
with
104 additions
and
41 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
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,33 @@ | ||
using Bencodex; | ||
using Bencodex.Types; | ||
using Lib9c.Models.Exceptions; | ||
using Lib9c.Models.Extensions; | ||
using Lib9c.Models.WorldInformation; | ||
using ValueKind = Bencodex.Types.ValueKind; | ||
|
||
namespace Lib9c.Models.States; | ||
|
||
public record WorldInformationState : IBencodable | ||
{ | ||
public Dictionary<int, World> WorldDictionary { get; init; } | ||
|
||
public WorldInformationState(IValue bencoded) | ||
{ | ||
if (bencoded is not Dictionary d) | ||
{ | ||
throw new UnsupportedArgumentValueException<ValueKind>( | ||
nameof(bencoded), | ||
new[] { ValueKind.Dictionary }, | ||
bencoded.Kind | ||
); | ||
} | ||
|
||
WorldDictionary = d.ToDictionary(kv => kv.Key.ToInteger(), kv => new World(kv.Value)); | ||
} | ||
|
||
public IValue Bencoded => | ||
WorldDictionary.Aggregate( | ||
List.Empty, | ||
(current, kv) => current.Add(List.Empty.Add(kv.Key.Serialize()).Add(kv.Value.Bencoded)) | ||
); | ||
} |
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,55 @@ | ||
using Bencodex; | ||
using Bencodex.Types; | ||
using Lib9c.Models.Exceptions; | ||
using Lib9c.Models.Extensions; | ||
using ValueKind = Bencodex.Types.ValueKind; | ||
|
||
namespace Lib9c.Models.WorldInformation; | ||
|
||
public record World : IBencodable | ||
{ | ||
public int Id { get; init; } | ||
public string Name { get; init; } | ||
public int StageBegin { get; init; } | ||
public int StageEnd { get; init; } | ||
public long UnlockedBlockIndex { get; init; } | ||
public long StageClearedBlockIndex { get; init; } | ||
public int StageClearedId { get; init; } | ||
|
||
public bool IsUnlocked => UnlockedBlockIndex != -1; | ||
public bool IsStageCleared => StageClearedBlockIndex != -1; | ||
|
||
public World(IValue bencoded) | ||
{ | ||
if (bencoded is not Dictionary d) | ||
{ | ||
throw new UnsupportedArgumentValueException<ValueKind>( | ||
nameof(bencoded), | ||
new[] { ValueKind.Dictionary }, | ||
bencoded.Kind | ||
); | ||
} | ||
|
||
Id = d.GetInteger("Id"); | ||
Name = d.GetString("Name"); | ||
StageBegin = d.GetInteger("StageBegin"); | ||
StageEnd = d.GetInteger("StageEnd"); | ||
UnlockedBlockIndex = d.GetLong("UnlockedBlockIndex"); | ||
StageClearedBlockIndex = d.GetLong("StageClearedBlockIndex"); | ||
StageClearedId = d.GetInteger("StageClearedId"); | ||
} | ||
|
||
public IValue Bencoded => | ||
new Dictionary( | ||
new Dictionary<IKey, IValue> | ||
{ | ||
[(Text)"Id"] = Id.Serialize(), | ||
[(Text)"Name"] = Name.Serialize(), | ||
[(Text)"StageBegin"] = StageBegin.Serialize(), | ||
[(Text)"StageEnd"] = StageEnd.Serialize(), | ||
[(Text)"UnlockedBlockIndex"] = UnlockedBlockIndex.Serialize(), | ||
[(Text)"StageClearedBlockIndex"] = StageClearedBlockIndex.Serialize(), | ||
[(Text)"StageClearedId"] = StageClearedId.Serialize(), | ||
} | ||
); | ||
} |
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 |
---|---|---|
@@ -1,22 +1,9 @@ | ||
using Bencodex.Types; | ||
using Lib9c.Models.States; | ||
using Libplanet.Crypto; | ||
using Lib9c.Models.Extensions; | ||
using MongoDB.Bson.Serialization.Attributes; | ||
using Nekoyume.Model; | ||
|
||
namespace Mimir.MongoDB.Bson; | ||
|
||
[BsonIgnoreExtraElements] | ||
public record WorldInformationDocument : MimirBsonDocument | ||
{ | ||
public IDictionary<int, WorldInformation.World> Object { get; init; } | ||
|
||
public WorldInformationDocument(Address Address, WorldInformation worldInformation) | ||
: base(Address) | ||
{ | ||
Object = ((Dictionary)worldInformation.Serialize()).ToDictionary( | ||
kv => kv.Key.ToInteger(), | ||
kv => new WorldInformation.World((Dictionary)kv.Value) | ||
); | ||
} | ||
} | ||
public record WorldInformationDocument(Address Address, WorldInformationState Object) | ||
: MimirBsonDocument(Address); |
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
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 |
---|---|---|
@@ -1,20 +1,15 @@ | ||
using Bencodex.Types; | ||
using Lib9c.Models.States; | ||
using Mimir.MongoDB.Bson; | ||
using Nekoyume.Model; | ||
|
||
namespace Mimir.Worker.Handler; | ||
|
||
public class WorldInformationStateHandler : IStateHandler | ||
{ | ||
public MimirBsonDocument ConvertToDocument(StateDiffContext context) | ||
{ | ||
if (context.RawState is Dictionary dict) | ||
{ | ||
return new WorldInformationDocument(context.Address, new WorldInformation(dict)); | ||
} | ||
|
||
throw new InvalidCastException( | ||
$"{nameof(context.RawState)} Invalid state type. Expected Dictionary." | ||
return new WorldInformationDocument( | ||
context.Address, | ||
new WorldInformationState(context.RawState) | ||
); | ||
} | ||
} |
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