Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reactivate world information #332

Merged
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion Lib9c.Models/States/WorldBossKillRewardRecord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace Lib9c.Models.States;

public class WorldBossKillRewardRecord : IBencodable
public record WorldBossKillRewardRecord : IBencodable
{
public Dictionary<int, bool> RewardRecordDictionary { get; init; }

Expand Down
2 changes: 1 addition & 1 deletion Lib9c.Models/States/WorldBossState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace Lib9c.Models.States;

public class WorldBossState : IBencodable
public record WorldBossState : IBencodable
{
public int Id { get; init; }
public int Level { get; init; }
Expand Down
33 changes: 33 additions & 0 deletions Lib9c.Models/States/WorldInformationState.cs
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))
);
}
55 changes: 55 additions & 0 deletions Lib9c.Models/WorldInformation/World.cs
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(),
}
);
}
19 changes: 3 additions & 16 deletions Mimir.MongoDB/Bson/WorldInformationDocument.cs
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);
4 changes: 2 additions & 2 deletions Mimir.MongoDB/CollectionNames.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
{
public static class CollectionNames
{
public static readonly Currency OdinNCGCurrency = Currency.Legacy(

Check warning on line 10 in Mimir.MongoDB/CollectionNames.cs

View workflow job for this annotation

GitHub Actions / Test (Mimir.Worker.Tests)

'Currency.Legacy(string, byte, Address)' is obsolete: 'Obsolete! Use Currency.Capped() or Currency.Uncapped() for new currencies.'
"NCG",
2,
new Address("0x47d082a115c63e7b58b1532d20e631538eafadde"));

public static readonly Currency HeimdallNCGCurrency = Currency.Legacy(

Check warning on line 15 in Mimir.MongoDB/CollectionNames.cs

View workflow job for this annotation

GitHub Actions / Test (Mimir.Worker.Tests)

'Currency.Legacy(string, byte, IImmutableSet<Address>?)' is obsolete: 'Obsolete! Use Currency.Capped() or Currency.Uncapped() for new currencies.'
"NCG",
2,
null);
Expand All @@ -27,7 +27,7 @@
CollectionAndAddressMappings.Add(Nekoyume.Addresses.ActionPoint, "action_point");
CollectionAndAddressMappings.Add(Nekoyume.Addresses.DailyReward, "daily_reward");
CollectionAndAddressMappings.Add(Nekoyume.Addresses.Inventory, "inventory");
// CollectionAndAddressMappings.Add(Nekoyume.Addresses.WorldInformation, "world_information");
CollectionAndAddressMappings.Add(Nekoyume.Addresses.WorldInformation, "world_information");
// CollectionAndAddressMappings.Add(Nekoyume.Addresses.QuestList, "quest_list");
CollectionAndAddressMappings.Add(Nekoyume.Addresses.RuneState, "all_rune");
CollectionAndAddressMappings.Add(Nekoyume.Addresses.Collection, "collection");
Expand Down Expand Up @@ -81,7 +81,7 @@
CollectionAndStateTypeMappings.Add(typeof(ProductsStateDocument), "products");
CollectionAndStateTypeMappings.Add(typeof(ProductDocument), "product");
// CollectionAndStateTypeMappings.Add(typeof(QuestListDocument), "quest_list");
// CollectionAndStateTypeMappings.Add(typeof(WorldInformationDocument), "world_information");
CollectionAndStateTypeMappings.Add(typeof(WorldInformationDocument), "world_information");
CollectionAndStateTypeMappings.Add(typeof(ItemSlotDocument), "item_slot");
CollectionAndStateTypeMappings.Add(typeof(RuneSlotDocument), "rune_slot");
CollectionAndStateTypeMappings.Add(typeof(WorldBossStateDocument), "world_boss");
Expand Down
2 changes: 1 addition & 1 deletion Mimir.Worker/Handler/AddressHandlerMappings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ static AddressHandlerMappings()
HandlerMappings.Add(Addresses.ActionPoint, new ActionPointStateHandler());
HandlerMappings.Add(Addresses.DailyReward, new DailyRewardStateHandler());
HandlerMappings.Add(Addresses.Inventory, new InventoryStateHandler());
// HandlerMappings.Add(Addresses.WorldInformation, new WorldInformationStateHandler());
HandlerMappings.Add(Addresses.WorldInformation, new WorldInformationStateHandler());
// HandlerMappings.Add(Addresses.QuestList, new QuestListStateHandler());
HandlerMappings.Add(Addresses.RuneState, new AllRuneStateHandler());
HandlerMappings.Add(Addresses.Collection, new CollectionStateHandler());
Expand Down
9 changes: 1 addition & 8 deletions Mimir.Worker/Handler/AllRuneStateHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,7 @@ public class AllRuneStateHandler : IStateHandler
{
public MimirBsonDocument ConvertToDocument(StateDiffContext context)
{
if (context.RawState is not List value)
{
throw new InvalidCastException(
$"{nameof(context.RawState)} Invalid state type. Expected {nameof(List)}, got {context.RawState.GetType().Name}."
);
}

var allRuneState = new AllRuneState(value);
var allRuneState = new AllRuneState(context.RawState);
return new AllRuneDocument(context.Address, allRuneState);
}
}
13 changes: 4 additions & 9 deletions Mimir.Worker/Handler/WorldInformationStateHandler.cs
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)
);
}
}
6 changes: 3 additions & 3 deletions Mimir.Worker/Poller/TxPoller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,16 @@ MongoDbService dbService
[
new PatchTableHandler(stateService, dbService),
// World
// new HackAndSlashHandler(stateService, dbService),
// new HackAndSlashSweepHandler(stateService, dbService),
new HackAndSlashHandler(stateService, dbService),
new HackAndSlashSweepHandler(stateService, dbService),

// Arena
new JoinArenaHandler(stateService, dbService),
new BattleArenaHandler(stateService, dbService),
// Raid
new RaidHandler(stateService, dbService),
// Event Dungeon
// new EventDungeonBattleHandler(stateService, dbService),
new EventDungeonBattleHandler(stateService, dbService),

// Market
new ProductsHandler(stateService, dbService),
Expand Down
Loading