Skip to content

Commit

Permalink
Merge pull request #332 from Atralupus/feat/reactivate-worldinformation
Browse files Browse the repository at this point in the history
Reactivate world information
  • Loading branch information
Atralupus authored Sep 18, 2024
2 parents 7eaad2f + 1943171 commit fdcd873
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 41 deletions.
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 @@ -27,7 +27,7 @@ static CollectionNames()
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 @@ static CollectionNames()
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

0 comments on commit fdcd873

Please sign in to comment.