From b72770c50e833f952d9498650f1becfe1f81b577 Mon Sep 17 00:00:00 2001 From: Hyun Seungmin Date: Fri, 13 Sep 2024 12:18:38 +0900 Subject: [PATCH] remove Mimir.Enums.CollectionNames --- Mimir.MongoDB/Bson/ActionPointDocument.cs | 4 +- Mimir.MongoDB/Bson/AgentDocument.cs | 3 +- Mimir.MongoDB/Bson/AllRuneDocument.cs | 5 +- Mimir.MongoDB/Bson/ArenaRankingDocument.cs | 3 +- Mimir.MongoDB/Bson/BalanceDocument.cs | 5 +- Mimir.MongoDB/Bson/CollectionDocument.cs | 5 +- .../Bson/CombinationSlotStateDocument.cs | 6 +- Mimir.MongoDB/Bson/DailyRewardDocument.cs | 4 +- Mimir.MongoDB/Bson/InventoryDocument.cs | 4 +- Mimir.MongoDB/Bson/ItemSlotDocument.cs | 5 +- Mimir.MongoDB/Bson/PetStateDocument.cs | 4 +- Mimir.MongoDB/Bson/ProductDocument.cs | 2 + Mimir.MongoDB/Bson/ProductsStateDocument.cs | 4 +- Mimir.MongoDB/Bson/QuestListDocument.cs | 2 + Mimir.MongoDB/Bson/RaiderStateDocument.cs | 4 +- Mimir.MongoDB/Bson/RuneSlotDocument.cs | 5 +- Mimir.MongoDB/Bson/SheetDocument.cs | 6 +- Mimir.MongoDB/Bson/StakeDocument.cs | 10 +- .../Bson/WorldBossKillRewardRecordDocument.cs | 6 +- Mimir.MongoDB/Bson/WorldBossStateDocument.cs | 6 +- .../Bson/WorldInformationDocument.cs | 2 + Mimir/Enums/CollectionNames.cs | 19 --- Mimir/GraphQL/Queries/Query.cs | 50 +++----- Mimir/GraphQL/Resolvers/ArenaResolver.cs | 14 +- Mimir/GraphQL/Resolvers/InventoryResolver.cs | 85 ++++++------ Mimir/GraphQL/Types/InventoryType.cs | 78 +++++------ Mimir/Repositories/ActionPointRepository.cs | 23 +--- Mimir/Repositories/AgentRepository.cs | 5 +- Mimir/Repositories/AllRuneRepository.cs | 31 ++--- Mimir/Repositories/ArenaRepository.cs | 121 +++++++++--------- Mimir/Repositories/AvatarRepository.cs | 5 +- Mimir/Repositories/CollectionRepository.cs | 27 ++-- Mimir/Repositories/DailyRewardRepository.cs | 24 +--- Mimir/Repositories/InventoryRepository.cs | 39 ++---- Mimir/Repositories/ItemSlotRepository.cs | 48 ++----- Mimir/Repositories/MetadataRepository.cs | 14 +- Mimir/Repositories/ProductRepository.cs | 22 ++-- Mimir/Repositories/RuneSlotRepository.cs | 60 ++------- Mimir/Repositories/StakeRepository.cs | 59 +++------ Mimir/Repositories/TableSheetsRepository.cs | 11 +- 40 files changed, 346 insertions(+), 484 deletions(-) delete mode 100644 Mimir/Enums/CollectionNames.cs diff --git a/Mimir.MongoDB/Bson/ActionPointDocument.cs b/Mimir.MongoDB/Bson/ActionPointDocument.cs index c14bdc6b..bbc42ce1 100644 --- a/Mimir.MongoDB/Bson/ActionPointDocument.cs +++ b/Mimir.MongoDB/Bson/ActionPointDocument.cs @@ -1,5 +1,7 @@ using Libplanet.Crypto; +using MongoDB.Bson.Serialization.Attributes; namespace Mimir.MongoDB.Bson; -public record ActionPointDocument(Address Address, int Object) : MimirBsonDocument(Address) { } +[BsonIgnoreExtraElements] +public record ActionPointDocument(Address Address, int Object) : MimirBsonDocument(Address); diff --git a/Mimir.MongoDB/Bson/AgentDocument.cs b/Mimir.MongoDB/Bson/AgentDocument.cs index 33aeb218..4a16dd1c 100644 --- a/Mimir.MongoDB/Bson/AgentDocument.cs +++ b/Mimir.MongoDB/Bson/AgentDocument.cs @@ -5,5 +5,4 @@ namespace Mimir.MongoDB.Bson; [BsonIgnoreExtraElements] -public record AgentDocument(Address Address, AgentState Object) - : MimirBsonDocument(Address); +public record AgentDocument(Address Address, AgentState Object) : MimirBsonDocument(Address); diff --git a/Mimir.MongoDB/Bson/AllRuneDocument.cs b/Mimir.MongoDB/Bson/AllRuneDocument.cs index 69f1e544..e1098e3a 100644 --- a/Mimir.MongoDB/Bson/AllRuneDocument.cs +++ b/Mimir.MongoDB/Bson/AllRuneDocument.cs @@ -1,7 +1,8 @@ using Lib9c.Models.States; using Libplanet.Crypto; +using MongoDB.Bson.Serialization.Attributes; namespace Mimir.MongoDB.Bson; -public record AllRuneDocument(Address Address, AllRuneState Object) - : MimirBsonDocument(Address) { } +[BsonIgnoreExtraElements] +public record AllRuneDocument(Address Address, AllRuneState Object) : MimirBsonDocument(Address); diff --git a/Mimir.MongoDB/Bson/ArenaRankingDocument.cs b/Mimir.MongoDB/Bson/ArenaRankingDocument.cs index b4683f30..905030c6 100644 --- a/Mimir.MongoDB/Bson/ArenaRankingDocument.cs +++ b/Mimir.MongoDB/Bson/ArenaRankingDocument.cs @@ -12,8 +12,7 @@ public record ArenaRankingDocument( int Round, ArenaInformation ArenaInformation, ArenaScore ArenaScore, - SimplifiedAvatarState SimpleAvatar -) + SimplifiedAvatarState SimpleAvatar) { public int Rank { get; set; } } diff --git a/Mimir.MongoDB/Bson/BalanceDocument.cs b/Mimir.MongoDB/Bson/BalanceDocument.cs index 1f465e6f..913180dc 100644 --- a/Mimir.MongoDB/Bson/BalanceDocument.cs +++ b/Mimir.MongoDB/Bson/BalanceDocument.cs @@ -1,7 +1,8 @@ using Libplanet.Crypto; using Libplanet.Types.Assets; +using MongoDB.Bson.Serialization.Attributes; namespace Mimir.MongoDB.Bson; -public record BalanceDocument(Address Address, FungibleAssetValue Object) - : MimirBsonDocument(Address) { } +[BsonIgnoreExtraElements] +public record BalanceDocument(Address Address, FungibleAssetValue Object) : MimirBsonDocument(Address); diff --git a/Mimir.MongoDB/Bson/CollectionDocument.cs b/Mimir.MongoDB/Bson/CollectionDocument.cs index db8b20f5..513d9050 100644 --- a/Mimir.MongoDB/Bson/CollectionDocument.cs +++ b/Mimir.MongoDB/Bson/CollectionDocument.cs @@ -1,5 +1,6 @@ using Lib9c.Models.States; using Libplanet.Crypto; +using MongoDB.Bson.Serialization.Attributes; namespace Mimir.MongoDB.Bson; @@ -7,5 +8,5 @@ namespace Mimir.MongoDB.Bson; /// Not MongoDB collection, it is a NineChronicles' collection state. /// /// -public record CollectionDocument(Address Address, CollectionState Object) - : MimirBsonDocument(Address) { } +[BsonIgnoreExtraElements] +public record CollectionDocument(Address Address, CollectionState Object) : MimirBsonDocument(Address); diff --git a/Mimir.MongoDB/Bson/CombinationSlotStateDocument.cs b/Mimir.MongoDB/Bson/CombinationSlotStateDocument.cs index 092ccd3d..4b90dba8 100644 --- a/Mimir.MongoDB/Bson/CombinationSlotStateDocument.cs +++ b/Mimir.MongoDB/Bson/CombinationSlotStateDocument.cs @@ -1,11 +1,13 @@ using Lib9c.Models.States; using Libplanet.Crypto; +using MongoDB.Bson.Serialization.Attributes; namespace Mimir.MongoDB.Bson; +[BsonIgnoreExtraElements] public record CombinationSlotStateDocument( Address Address, Address AvatarAddress, int SlotIndex, - CombinationSlotState Object -) : MimirBsonDocument(Address) { } + CombinationSlotState Object) + : MimirBsonDocument(Address); diff --git a/Mimir.MongoDB/Bson/DailyRewardDocument.cs b/Mimir.MongoDB/Bson/DailyRewardDocument.cs index 76f64884..d10fcdf5 100644 --- a/Mimir.MongoDB/Bson/DailyRewardDocument.cs +++ b/Mimir.MongoDB/Bson/DailyRewardDocument.cs @@ -1,5 +1,7 @@ using Libplanet.Crypto; +using MongoDB.Bson.Serialization.Attributes; namespace Mimir.MongoDB.Bson; -public record DailyRewardDocument(Address Address, long Object) : MimirBsonDocument(Address) { } +[BsonIgnoreExtraElements] +public record DailyRewardDocument(Address Address, long Object) : MimirBsonDocument(Address); diff --git a/Mimir.MongoDB/Bson/InventoryDocument.cs b/Mimir.MongoDB/Bson/InventoryDocument.cs index f23d3a26..8a30ff4e 100644 --- a/Mimir.MongoDB/Bson/InventoryDocument.cs +++ b/Mimir.MongoDB/Bson/InventoryDocument.cs @@ -1,6 +1,8 @@ using Lib9c.Models.Items; using Libplanet.Crypto; +using MongoDB.Bson.Serialization.Attributes; namespace Mimir.MongoDB.Bson; -public record InventoryDocument(Address Address, Inventory Object) : MimirBsonDocument(Address) { } +[BsonIgnoreExtraElements] +public record InventoryDocument(Address Address, Inventory Object) : MimirBsonDocument(Address); diff --git a/Mimir.MongoDB/Bson/ItemSlotDocument.cs b/Mimir.MongoDB/Bson/ItemSlotDocument.cs index 2311ee5c..f61c2e27 100644 --- a/Mimir.MongoDB/Bson/ItemSlotDocument.cs +++ b/Mimir.MongoDB/Bson/ItemSlotDocument.cs @@ -1,7 +1,8 @@ using Lib9c.Models.States; using Libplanet.Crypto; +using MongoDB.Bson.Serialization.Attributes; namespace Mimir.MongoDB.Bson; -public record ItemSlotDocument(Address Address, ItemSlotState Object) - : MimirBsonDocument(Address) { } +[BsonIgnoreExtraElements] +public record ItemSlotDocument(Address Address, ItemSlotState Object) : MimirBsonDocument(Address); diff --git a/Mimir.MongoDB/Bson/PetStateDocument.cs b/Mimir.MongoDB/Bson/PetStateDocument.cs index b741bae5..ce94cf64 100644 --- a/Mimir.MongoDB/Bson/PetStateDocument.cs +++ b/Mimir.MongoDB/Bson/PetStateDocument.cs @@ -1,6 +1,8 @@ using Lib9c.Models.States; using Libplanet.Crypto; +using MongoDB.Bson.Serialization.Attributes; namespace Mimir.MongoDB.Bson; -public record PetStateDocument(Address Address, PetState Object) : MimirBsonDocument(Address) { } +[BsonIgnoreExtraElements] +public record PetStateDocument(Address Address, PetState Object) : MimirBsonDocument(Address); diff --git a/Mimir.MongoDB/Bson/ProductDocument.cs b/Mimir.MongoDB/Bson/ProductDocument.cs index c92cd26d..626e806f 100644 --- a/Mimir.MongoDB/Bson/ProductDocument.cs +++ b/Mimir.MongoDB/Bson/ProductDocument.cs @@ -1,8 +1,10 @@ using Lib9c.Models.Market; using Libplanet.Crypto; +using MongoDB.Bson.Serialization.Attributes; namespace Mimir.MongoDB.Bson; +[BsonIgnoreExtraElements] public record ProductDocument : MimirBsonDocument { public Address AvatarAddress { get; init; } diff --git a/Mimir.MongoDB/Bson/ProductsStateDocument.cs b/Mimir.MongoDB/Bson/ProductsStateDocument.cs index a9fc6dac..21361c5f 100644 --- a/Mimir.MongoDB/Bson/ProductsStateDocument.cs +++ b/Mimir.MongoDB/Bson/ProductsStateDocument.cs @@ -1,7 +1,9 @@ using Lib9c.Models.States; using Libplanet.Crypto; +using MongoDB.Bson.Serialization.Attributes; namespace Mimir.MongoDB.Bson; +[BsonIgnoreExtraElements] public record ProductsStateDocument(Address Address, ProductsState Object, Address AvatarAddress) - : MimirBsonDocument(Address) { } + : MimirBsonDocument(Address); diff --git a/Mimir.MongoDB/Bson/QuestListDocument.cs b/Mimir.MongoDB/Bson/QuestListDocument.cs index 0804b756..0e39bae6 100644 --- a/Mimir.MongoDB/Bson/QuestListDocument.cs +++ b/Mimir.MongoDB/Bson/QuestListDocument.cs @@ -1,6 +1,8 @@ // using Libplanet.Crypto; // using Nekoyume.Model.Quest; +// using MongoDB.Bson.Serialization.Attributes; // namespace Mimir.MongoDB.Bson; +// [BsonIgnoreExtraElements] // public record QuestListDocument(Address Address, QuestList Object) : IMimirBsonDocument(Address) { } diff --git a/Mimir.MongoDB/Bson/RaiderStateDocument.cs b/Mimir.MongoDB/Bson/RaiderStateDocument.cs index be4756be..8849d818 100644 --- a/Mimir.MongoDB/Bson/RaiderStateDocument.cs +++ b/Mimir.MongoDB/Bson/RaiderStateDocument.cs @@ -1,6 +1,8 @@ using Lib9c.Models.States; using Libplanet.Crypto; +using MongoDB.Bson.Serialization.Attributes; namespace Mimir.MongoDB.Bson; -public record RaiderStateDocument(Address Address, RaiderState Object) : MimirBsonDocument(Address) { } +[BsonIgnoreExtraElements] +public record RaiderStateDocument(Address Address, RaiderState Object) : MimirBsonDocument(Address); diff --git a/Mimir.MongoDB/Bson/RuneSlotDocument.cs b/Mimir.MongoDB/Bson/RuneSlotDocument.cs index dfda9b56..882b6a65 100644 --- a/Mimir.MongoDB/Bson/RuneSlotDocument.cs +++ b/Mimir.MongoDB/Bson/RuneSlotDocument.cs @@ -1,7 +1,8 @@ using Lib9c.Models.States; using Libplanet.Crypto; +using MongoDB.Bson.Serialization.Attributes; namespace Mimir.MongoDB.Bson; -public record RuneSlotDocument(Address Address, RuneSlotState Object) - : MimirBsonDocument(Address) { } +[BsonIgnoreExtraElements] +public record RuneSlotDocument(Address Address, RuneSlotState Object) : MimirBsonDocument(Address); diff --git a/Mimir.MongoDB/Bson/SheetDocument.cs b/Mimir.MongoDB/Bson/SheetDocument.cs index 3db2e9a5..8f8ace2e 100644 --- a/Mimir.MongoDB/Bson/SheetDocument.cs +++ b/Mimir.MongoDB/Bson/SheetDocument.cs @@ -1,12 +1,14 @@ using Bencodex.Types; using Libplanet.Crypto; +using MongoDB.Bson.Serialization.Attributes; using Nekoyume.TableData; namespace Mimir.MongoDB.Bson; +[BsonIgnoreExtraElements] public record SheetDocument( Address Address, ISheet Object, string Name, - IValue RawState -) : MimirBsonDocument(Address); + IValue RawState) + : MimirBsonDocument(Address); diff --git a/Mimir.MongoDB/Bson/StakeDocument.cs b/Mimir.MongoDB/Bson/StakeDocument.cs index 6d8f0ff0..5b94b194 100644 --- a/Mimir.MongoDB/Bson/StakeDocument.cs +++ b/Mimir.MongoDB/Bson/StakeDocument.cs @@ -1,6 +1,8 @@ -// using Libplanet.Crypto; -// using Nekoyume.Model.Stake; +using Libplanet.Crypto; +using MongoDB.Bson.Serialization.Attributes; +using Nekoyume.Model.Stake; -// namespace Mimir.MongoDB.Bson; +namespace Mimir.MongoDB.Bson; -// public record StakeDocument(Address Address, StakeStateV2 Object) : IMimirBsonDocument(Address) { } +// [BsonIgnoreExtraElements] +// public record StakeDocument(Address Address, StakeStateV2 Object) : MimirBsonDocument(Address); diff --git a/Mimir.MongoDB/Bson/WorldBossKillRewardRecordDocument.cs b/Mimir.MongoDB/Bson/WorldBossKillRewardRecordDocument.cs index 8fd549d1..0a65eae6 100644 --- a/Mimir.MongoDB/Bson/WorldBossKillRewardRecordDocument.cs +++ b/Mimir.MongoDB/Bson/WorldBossKillRewardRecordDocument.cs @@ -1,10 +1,12 @@ using Lib9c.Models.States; using Libplanet.Crypto; +using MongoDB.Bson.Serialization.Attributes; namespace Mimir.MongoDB.Bson; +[BsonIgnoreExtraElements] public record WorldBossKillRewardRecordDocument( Address Address, Address AvatarAddress, - WorldBossKillRewardRecord Object -) : MimirBsonDocument(Address) { } + WorldBossKillRewardRecord Object) + : MimirBsonDocument(Address); diff --git a/Mimir.MongoDB/Bson/WorldBossStateDocument.cs b/Mimir.MongoDB/Bson/WorldBossStateDocument.cs index f3000505..6608577f 100644 --- a/Mimir.MongoDB/Bson/WorldBossStateDocument.cs +++ b/Mimir.MongoDB/Bson/WorldBossStateDocument.cs @@ -1,10 +1,12 @@ using Lib9c.Models.States; using Libplanet.Crypto; +using MongoDB.Bson.Serialization.Attributes; namespace Mimir.MongoDB.Bson; +[BsonIgnoreExtraElements] public record WorldBossStateDocument( Address Address, int RaidId, - WorldBossState Object -) : MimirBsonDocument(Address) { } + WorldBossState Object) + : MimirBsonDocument(Address); diff --git a/Mimir.MongoDB/Bson/WorldInformationDocument.cs b/Mimir.MongoDB/Bson/WorldInformationDocument.cs index 8080c023..265b85b1 100644 --- a/Mimir.MongoDB/Bson/WorldInformationDocument.cs +++ b/Mimir.MongoDB/Bson/WorldInformationDocument.cs @@ -1,10 +1,12 @@ using Bencodex.Types; 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 Object { get; init; } diff --git a/Mimir/Enums/CollectionNames.cs b/Mimir/Enums/CollectionNames.cs deleted file mode 100644 index 45836df3..00000000 --- a/Mimir/Enums/CollectionNames.cs +++ /dev/null @@ -1,19 +0,0 @@ -namespace Mimir.Enums; - -public static class CollectionNames -{ - public const string Arena = "arena"; - public const string Avatar = "avatar"; - public const string Product = "product"; - public const string ActionPoint = "action_point"; - public const string Agent = "agent"; - public const string AllRune = "all_rune"; - public const string Collection = "collection"; - public const string DailyReward = "daily_reward"; - public const string Inventory = "inventory"; - public const string ItemSlot = "item_slot"; - public const string Metadata = "metadata"; - public const string RuneSlot = "rune_slot"; - public const string Stake = "stake"; - public const string TableSheet = "table_sheet"; -} diff --git a/Mimir/GraphQL/Queries/Query.cs b/Mimir/GraphQL/Queries/Query.cs index 179bee0a..56071e93 100644 --- a/Mimir/GraphQL/Queries/Query.cs +++ b/Mimir/GraphQL/Queries/Query.cs @@ -9,28 +9,31 @@ namespace Mimir.GraphQL.Queries; public class Query { /// - /// Get a agent state by address. + /// Get metadata by collection name. + /// + /// The name of the collection. + /// + /// The metadata + public async Task GetMetadataAsync(string collectionName, [Service] MetadataRepository repo) + => await repo.GetByCollectionAsync(collectionName); + + /// + /// Get an agent state by address. /// /// The address of the agent. /// /// The agent state - public async Task GetAgentAsync(Address address, [Service] AgentRepository repo) - { - var doc = await repo.GetByAddressAsync(address); - return doc.Object; - } + public async Task GetAgentAsync(Address address, [Service] AgentRepository repo) => + (await repo.GetByAddressAsync(address)).Object; /// - /// Get a avatar state by address. + /// Get an avatar state by address. /// /// The address of the avatar. /// /// The avatar state - public async Task GetAvatarAsync(Address address, [Service] AvatarRepository repo) - { - var doc = await repo.GetByAddressAsync(address); - return doc.Object; - } + public async Task GetAvatarAsync(Address address, [Service] AvatarRepository repo) => + (await repo.GetByAddressAsync(address)).Object; /// /// Get an action point by address. @@ -38,10 +41,8 @@ public async Task GetAvatarAsync(Address address, [Service] AvatarR /// The address of the avatar. /// /// The action point. - public async Task GetActionPointAsync( - Address address, - [Service] ActionPointRepository repo - ) => await repo.GetByAddressAsync(address); + public async Task GetActionPointAsync(Address address, [Service] ActionPointRepository repo) => + (await repo.GetByAddressAsync(address)).Object; /// /// Get the daily reward received block index by address. @@ -49,24 +50,11 @@ [Service] ActionPointRepository repo /// The address of the avatar. /// /// The daily reward received block index. - public async Task GetDailyRewardReceivedBlockIndexAsync( - Address address, - [Service] DailyRewardRepository repo - ) => await repo.GetByAddressAsync(address); + public async Task GetDailyRewardReceivedBlockIndexAsync(Address address, [Service] DailyRewardRepository repo) + => (await repo.GetByAddressAsync(address)).Object; /// /// Get arena sub-fields. /// public ArenaObject GetArena() => new(); - - /// - /// Get metadata by collection name. - /// - /// The name of the collection. - /// - /// The metadata - public async Task GetMetadataAsync( - string collectionName, - [Service] MetadataRepository repo - ) => await repo.GetByCollectionAsync(collectionName); } diff --git a/Mimir/GraphQL/Resolvers/ArenaResolver.cs b/Mimir/GraphQL/Resolvers/ArenaResolver.cs index 3ad97aa5..0e2327dc 100644 --- a/Mimir/GraphQL/Resolvers/ArenaResolver.cs +++ b/Mimir/GraphQL/Resolvers/ArenaResolver.cs @@ -1,6 +1,6 @@ using HotChocolate.Resolvers; using Libplanet.Crypto; -using Mimir.Enums; +using Mimir.MongoDB; using Mimir.MongoDB.Bson; using Mimir.Repositories; using Nekoyume.TableData; @@ -20,7 +20,8 @@ public class ArenaResolver return arenaRound; } - var metadataDocument = await metadataRepo.GetByCollectionAsync(CollectionNames.Arena); + var collectionName = CollectionNames.GetCollectionName(); + var metadataDocument = await metadataRepo.GetByCollectionAsync(collectionName); arenaRound = tableSheetsRepo.GetArenaRound(metadataDocument.LatestBlockIndex); context.ScopedContextData = context.ScopedContextData.Add("arenaRound", arenaRound); return arenaRound; @@ -57,7 +58,8 @@ public static async Task> GetLeaderboardAsync( ); } - var metadata = await metadataRepo.GetByCollectionAsync(CollectionNames.Arena); + var collectionName = CollectionNames.GetCollectionName(); + var metadata = await metadataRepo.GetByCollectionAsync(collectionName); arenaRound ??= await GetRoundAsync(context, metadataRepo, tableSheetsRepo, arenaRound); return await arenaRankingRepo.GetLeaderboardAsync( metadata.LatestBlockIndex, @@ -75,7 +77,8 @@ public static async Task> GetLeaderboardAsync( [Service] TableSheetsRepository tableSheetsRepo, [ScopedState("arenaRound")] ArenaSheet.RoundData? arenaRound) { - var metadata = await metadataRepo.GetByCollectionAsync(CollectionNames.Arena); + var collectionName = CollectionNames.GetCollectionName(); + var metadata = await metadataRepo.GetByCollectionAsync(collectionName); arenaRound ??= await GetRoundAsync(context, metadataRepo, tableSheetsRepo, arenaRound); return await arenaRankingRepo.GetRankingByAvatarAddressAsync( metadata.LatestBlockIndex, @@ -92,7 +95,8 @@ public static async Task> GetLeaderboardByAvatarAddre [Service] TableSheetsRepository tableSheetsRepo, [ScopedState("arenaRound")] ArenaSheet.RoundData? arenaRound) { - var metadata = await metadataRepo.GetByCollectionAsync(CollectionNames.Arena); + var collectionName = CollectionNames.GetCollectionName(); + var metadata = await metadataRepo.GetByCollectionAsync(collectionName); arenaRound ??= await GetRoundAsync(context, metadataRepo, tableSheetsRepo, arenaRound); return await arenaRankingRepo.GetLeaderboardByAvatarAddressAsync( metadata.LatestBlockIndex, diff --git a/Mimir/GraphQL/Resolvers/InventoryResolver.cs b/Mimir/GraphQL/Resolvers/InventoryResolver.cs index 3c7dc6ca..bbeefb00 100644 --- a/Mimir/GraphQL/Resolvers/InventoryResolver.cs +++ b/Mimir/GraphQL/Resolvers/InventoryResolver.cs @@ -1,14 +1,13 @@ using HotChocolate.Resolvers; -using Mimir.GraphQL.Factories; +using Lib9c.Models.Items; using Mimir.GraphQL.Objects; -using Mimir.Models; using Mimir.Repositories; namespace Mimir.GraphQL.Resolvers; public class InventoryResolver { - public static Inventory GetInventory( + public static async Task GetInventoryAsync( IResolverContext context, [Service] InventoryRepository inventoryRepo, [Parent] InventoryObject inventoryObject, @@ -20,48 +19,48 @@ public static Inventory GetInventory( } var avatarAddress = inventoryObject.Address; - inventory = inventoryRepo.GetInventory(avatarAddress); + inventory = (await inventoryRepo.GetByAddressAsync(avatarAddress)).Object; context.ScopedContextData = context.ScopedContextData.Add("inventory", inventory); return inventory; } - public static ItemObject[] GetConsumables( - IResolverContext context, - [Service] InventoryRepository inventoryRepo, - [Parent] InventoryObject inventoryObject, - [ScopedState("inventory")] Inventory? inventory) => - GetInventory(context, inventoryRepo, inventoryObject, inventory) - .Consumables - .Select(ItemObjectFactory.Create) - .ToArray(); - - public static ItemObject[] GetCostumes( - IResolverContext context, - [Service] InventoryRepository inventoryRepo, - [Parent] InventoryObject inventoryObject, - [ScopedState("inventory")] Inventory? inventory) => - GetInventory(context, inventoryRepo, inventoryObject, inventory) - .Costumes - .Select(ItemObjectFactory.Create) - .ToArray(); - - public static ItemObject[] GetEquipments( - IResolverContext context, - [Service] InventoryRepository inventoryRepo, - [Parent] InventoryObject inventoryObject, - [ScopedState("inventory")] Inventory? inventory) => - GetInventory(context, inventoryRepo, inventoryObject, inventory) - .Equipments - .Select(ItemObjectFactory.Create) - .ToArray(); - - public static ItemObject[] GetMaterials( - IResolverContext context, - [Service] InventoryRepository inventoryRepo, - [Parent] InventoryObject inventoryObject, - [ScopedState("inventory")] Inventory? inventory) => - GetInventory(context, inventoryRepo, inventoryObject, inventory) - .Materials - .Select(ItemObjectFactory.Create) - .ToArray(); + // public static ItemObject[] GetConsumables( + // IResolverContext context, + // [Service] InventoryRepository inventoryRepo, + // [Parent] InventoryObject inventoryObject, + // [ScopedState("inventory")] Inventory? inventory) => + // GetInventoryAsync(context, inventoryRepo, inventoryObject, inventory) + // .Consumables + // .Select(ItemObjectFactory.Create) + // .ToArray(); + // + // public static ItemObject[] GetCostumes( + // IResolverContext context, + // [Service] InventoryRepository inventoryRepo, + // [Parent] InventoryObject inventoryObject, + // [ScopedState("inventory")] Inventory? inventory) => + // GetInventoryAsync(context, inventoryRepo, inventoryObject, inventory) + // .Costumes + // .Select(ItemObjectFactory.Create) + // .ToArray(); + // + // public static ItemObject[] GetEquipments( + // IResolverContext context, + // [Service] InventoryRepository inventoryRepo, + // [Parent] InventoryObject inventoryObject, + // [ScopedState("inventory")] Inventory? inventory) => + // GetInventoryAsync(context, inventoryRepo, inventoryObject, inventory) + // .Equipments + // .Select(ItemObjectFactory.Create) + // .ToArray(); + // + // public static ItemObject[] GetMaterials( + // IResolverContext context, + // [Service] InventoryRepository inventoryRepo, + // [Parent] InventoryObject inventoryObject, + // [ScopedState("inventory")] Inventory? inventory) => + // GetInventoryAsync(context, inventoryRepo, inventoryObject, inventory) + // .Materials + // .Select(ItemObjectFactory.Create) + // .ToArray(); } diff --git a/Mimir/GraphQL/Types/InventoryType.cs b/Mimir/GraphQL/Types/InventoryType.cs index f18f56ef..fc9b6bbb 100644 --- a/Mimir/GraphQL/Types/InventoryType.cs +++ b/Mimir/GraphQL/Types/InventoryType.cs @@ -1,39 +1,39 @@ -using Lib9c.GraphQL.Types; -using Mimir.GraphQL.Objects; -using Mimir.GraphQL.Resolvers; - -namespace Mimir.GraphQL.Types; - -public class InventoryType : ObjectType -{ - protected override void Configure(IObjectTypeDescriptor descriptor) - { - descriptor - .Field(f => f.Address) - .Type>(); - descriptor - .Field("consumables") - .Description("The consumables in the inventory.") - .Type>>() - .ResolveWith(_ => - InventoryResolver.GetConsumables(default!, default!, default!, default!)); - descriptor - .Field("costumes") - .Description("The costumes in the inventory.") - .Type>>() - .ResolveWith(_ => - InventoryResolver.GetCostumes(default!, default!, default!, default!)); - descriptor - .Field("equipments") - .Description("The equipments in the inventory.") - .Type>>() - .ResolveWith(_ => - InventoryResolver.GetEquipments(default!, default!, default!, default!)); - descriptor - .Field("materials") - .Description("The materials in the inventory.") - .Type>>() - .ResolveWith(_ => - InventoryResolver.GetMaterials(default!, default!, default!, default!)); - } -} +// using Lib9c.GraphQL.Types; +// using Mimir.GraphQL.Objects; +// using Mimir.GraphQL.Resolvers; +// +// namespace Mimir.GraphQL.Types; +// +// public class InventoryType : ObjectType +// { +// protected override void Configure(IObjectTypeDescriptor descriptor) +// { +// descriptor +// .Field(f => f.Address) +// .Type>(); +// descriptor +// .Field("consumables") +// .Description("The consumables in the inventory.") +// .Type>>() +// .ResolveWith(_ => +// InventoryResolver.GetConsumables(default!, default!, default!, default!)); +// descriptor +// .Field("costumes") +// .Description("The costumes in the inventory.") +// .Type>>() +// .ResolveWith(_ => +// InventoryResolver.GetCostumes(default!, default!, default!, default!)); +// descriptor +// .Field("equipments") +// .Description("The equipments in the inventory.") +// .Type>>() +// .ResolveWith(_ => +// InventoryResolver.GetEquipments(default!, default!, default!, default!)); +// descriptor +// .Field("materials") +// .Description("The materials in the inventory.") +// .Type>>() +// .ResolveWith(_ => +// InventoryResolver.GetMaterials(default!, default!, default!, default!)); +// } +// } diff --git a/Mimir/Repositories/ActionPointRepository.cs b/Mimir/Repositories/ActionPointRepository.cs index c3bdb6fb..ff582714 100644 --- a/Mimir/Repositories/ActionPointRepository.cs +++ b/Mimir/Repositories/ActionPointRepository.cs @@ -1,19 +1,19 @@ using Libplanet.Crypto; -using Mimir.Enums; using Mimir.Exceptions; -using Mimir.MongoDB.Exceptions; +using Mimir.MongoDB; +using Mimir.MongoDB.Bson; using Mimir.Services; -using MongoDB.Bson; using MongoDB.Driver; namespace Mimir.Repositories; public class ActionPointRepository(MongoDbService dbService) { - public async Task GetByAddressAsync(Address address) + public async Task GetByAddressAsync(Address address) { - var collection = dbService.GetCollection(CollectionNames.ActionPoint); - var filter = Builders.Filter.Eq("Address", address.ToHex()); + var collectionName = CollectionNames.GetCollectionName(); + var collection = dbService.GetCollection(collectionName); + var filter = Builders.Filter.Eq("Address", address.ToHex()); var document = await collection.Find(filter).FirstOrDefaultAsync(); if (document is null) { @@ -22,15 +22,6 @@ public async Task GetByAddressAsync(Address address) $"'Address' equals to '{address.ToHex()}'"); } - try - { - return document["Object"].AsInt32; - } - catch (KeyNotFoundException e) - { - throw new KeyNotFoundInBsonDocumentException( - "document[\"State\"][\"Object\"].AsInt32", - e); - } + return document; } } diff --git a/Mimir/Repositories/AgentRepository.cs b/Mimir/Repositories/AgentRepository.cs index 0b8d263c..eaea4f8f 100644 --- a/Mimir/Repositories/AgentRepository.cs +++ b/Mimir/Repositories/AgentRepository.cs @@ -1,6 +1,6 @@ using Libplanet.Crypto; -using Mimir.Enums; using Mimir.Exceptions; +using Mimir.MongoDB; using Mimir.MongoDB.Bson; using Mimir.Services; using MongoDB.Driver; @@ -11,7 +11,8 @@ public class AgentRepository(MongoDbService dbService) { public async Task GetByAddressAsync(Address agentAddress) { - var collection = dbService.GetCollection(CollectionNames.Agent); + var collectionName = CollectionNames.GetCollectionName(); + var collection = dbService.GetCollection(collectionName); var filter = Builders.Filter.Eq("Address", agentAddress.ToHex()); var document = await collection.Find(filter).FirstOrDefaultAsync(); if (document is null) diff --git a/Mimir/Repositories/AllRuneRepository.cs b/Mimir/Repositories/AllRuneRepository.cs index 12de0783..69d21860 100644 --- a/Mimir/Repositories/AllRuneRepository.cs +++ b/Mimir/Repositories/AllRuneRepository.cs @@ -1,21 +1,21 @@ +using Lib9c.Models.States; using Libplanet.Crypto; -using Mimir.Enums; using Mimir.Exceptions; -using Mimir.Models.Assets; -using Mimir.MongoDB.Exceptions; +using Mimir.MongoDB; +using Mimir.MongoDB.Bson; using Mimir.Services; -using MongoDB.Bson; using MongoDB.Driver; namespace Mimir.Repositories; public class AllRuneRepository(MongoDbService dbService) { - public List GetRunes(Address avatarAddress) + public Task GetByAddressAsync(Address avatarAddress) { - var collection = dbService.GetCollection(CollectionNames.AllRune); - var filter = Builders.Filter.Eq("Address", avatarAddress.ToHex()); - var document = collection.Find(filter).FirstOrDefault(); + var collectionName = CollectionNames.GetCollectionName(); + var collection = dbService.GetCollection(collectionName); + var filter = Builders.Filter.Eq("Address", avatarAddress.ToHex()); + var document = collection.Find(filter).FirstOrDefaultAsync(); if (document is null) { throw new DocumentNotFoundInMongoCollectionException( @@ -23,19 +23,6 @@ public List GetRunes(Address avatarAddress) $"'Address' equals to '{avatarAddress.ToHex()}'"); } - try - { - var runesDoc = document["Object"]["Runes"].AsBsonDocument; - return runesDoc - .Select(rune => rune.Value.AsBsonDocument) - .Select(runeValue => new Rune(runeValue)) - .ToList(); - } - catch (KeyNotFoundException e) - { - throw new UnexpectedTypeOfBsonValueException( - "document[\"State\"][\"Object\"][\"Runes\"].AsBsonDocument", - e); - } + return document; } } diff --git a/Mimir/Repositories/ArenaRepository.cs b/Mimir/Repositories/ArenaRepository.cs index 0572d23b..c161920a 100644 --- a/Mimir/Repositories/ArenaRepository.cs +++ b/Mimir/Repositories/ArenaRepository.cs @@ -1,6 +1,6 @@ using Libplanet.Crypto; using Microsoft.Extensions.Caching.Memory; -using Mimir.Enums; +using Mimir.MongoDB; using Mimir.MongoDB.Bson; using Mimir.Services; using MongoDB.Bson; @@ -34,65 +34,68 @@ private async Task> GetOrCreateLeaderboardAsync( await _leaderboardCacheLock.WaitAsync(); try { - if (!_leaderboardCache.TryGetValue(cacheKey, out leaderboard)) + if (_leaderboardCache.TryGetValue(cacheKey, out leaderboard)) { - var collection = dbService.GetCollection(CollectionNames.Arena); - var pipelines = new List - { - new( - "$match", - new BsonDocument( - "$and", - new BsonArray - { - new BsonDocument("ChampionshipId", championshipId), - new BsonDocument("Round", round) - } - ) - ), - new( - "$lookup", - new BsonDocument - { - { "from", "avatar" }, - { "localField", "Address" }, - { "foreignField", "Address" }, - { "as", "SimpleAvatar" } - } - ), - new( - "$unwind", - new BsonDocument - { - { "path", "$SimpleAvatar" }, - { "preserveNullAndEmptyArrays", true } - } - ), - new( - "$project", - new BsonDocument - { - { "SimpleAvatar.Object.EventMap", 0 }, - { "SimpleAvatar.Object.ItemMap", 0 }, - { "SimpleAvatar.Object.MailBox", 0 }, - { "SimpleAvatar.Object.MonsterMap", 0 }, - { "SimpleAvatar.Object.StageMap", 0 }, - } - ), - new( - "$addFields", - new BsonDocument + return leaderboard!; + } + + var collectionName = CollectionNames.GetCollectionName(); + var collection = dbService.GetCollection(collectionName); + var pipelines = new List + { + new( + "$match", + new BsonDocument( + "$and", + new BsonArray { - { "SimpleAvatar", "$SimpleAvatar.Object" }, + new BsonDocument("ChampionshipId", championshipId), + new BsonDocument("Round", round) } - ), - new("$sort", new BsonDocument("ArenaScore.Score", -1)), - }; - - var aggregation = await collection.Aggregate(pipelines).ToListAsync(); - leaderboard = UpdateRank(aggregation); - _leaderboardCache.Set(cacheKey, leaderboard, CacheEntryOptions); - } + ) + ), + new( + "$lookup", + new BsonDocument + { + { "from", "avatar" }, + { "localField", "Address" }, + { "foreignField", "Address" }, + { "as", "SimpleAvatar" } + } + ), + new( + "$unwind", + new BsonDocument + { + { "path", "$SimpleAvatar" }, + { "preserveNullAndEmptyArrays", true } + } + ), + new( + "$project", + new BsonDocument + { + { "SimpleAvatar.Object.EventMap", 0 }, + { "SimpleAvatar.Object.ItemMap", 0 }, + { "SimpleAvatar.Object.MailBox", 0 }, + { "SimpleAvatar.Object.MonsterMap", 0 }, + { "SimpleAvatar.Object.StageMap", 0 }, + } + ), + new( + "$addFields", + new BsonDocument + { + { "SimpleAvatar", "$SimpleAvatar.Object" }, + } + ), + new("$sort", new BsonDocument("ArenaScore.Score", -1)), + }; + + var aggregation = await collection.Aggregate(pipelines).ToListAsync(); + leaderboard = UpdateRank(aggregation); + _leaderboardCache.Set(cacheKey, leaderboard, CacheEntryOptions); } finally { @@ -207,7 +210,7 @@ public async Task> GetLeaderboardByAvatarAddressAsync arenaType, ArenaScore.ArenaScoreDefault); } - + return await GetLeaderboardByScoreAsync( blockIndex, championshipId, @@ -215,7 +218,7 @@ public async Task> GetLeaderboardByAvatarAddressAsync arenaType, doc.ArenaScore.Score); } - + public async Task> GetLeaderboardByScoreAsync( long blockIndex, int championshipId, diff --git a/Mimir/Repositories/AvatarRepository.cs b/Mimir/Repositories/AvatarRepository.cs index e58c5508..b8b1798e 100644 --- a/Mimir/Repositories/AvatarRepository.cs +++ b/Mimir/Repositories/AvatarRepository.cs @@ -1,6 +1,6 @@ using Libplanet.Crypto; -using Mimir.Enums; using Mimir.Exceptions; +using Mimir.MongoDB; using Mimir.MongoDB.Bson; using Mimir.Services; using MongoDB.Driver; @@ -11,8 +11,9 @@ public class AvatarRepository(MongoDbService dbService) { public async Task GetByAddressAsync(Address address) { + var collectionName = CollectionNames.GetCollectionName(); + var collection = dbService.GetCollection(collectionName); var filter = Builders.Filter.Eq("Address", address.ToHex()); - var collection = dbService.GetCollection(CollectionNames.Avatar); var doc = await collection.Find(filter).FirstOrDefaultAsync(); if (doc is null) { diff --git a/Mimir/Repositories/CollectionRepository.cs b/Mimir/Repositories/CollectionRepository.cs index a463ec75..0c57e014 100644 --- a/Mimir/Repositories/CollectionRepository.cs +++ b/Mimir/Repositories/CollectionRepository.cs @@ -1,21 +1,20 @@ using Libplanet.Crypto; -using Mimir.Enums; using Mimir.Exceptions; -using Mimir.Models.Assets; -using Mimir.MongoDB.Exceptions; +using Mimir.MongoDB; +using Mimir.MongoDB.Bson; using Mimir.Services; -using MongoDB.Bson; using MongoDB.Driver; namespace Mimir.Repositories; public class CollectionRepository(MongoDbService dbService) { - public Collection GetCollection(Address avatarAddress) + public Task GetByAddressAsync(Address avatarAddress) { - var collection = dbService.GetCollection(CollectionNames.Collection); - var filter = Builders.Filter.Eq("Address", avatarAddress.ToHex()); - var document = collection.Find(filter).FirstOrDefault(); + var collectionName = CollectionNames.GetCollectionName(); + var collection = dbService.GetCollection(collectionName); + var filter = Builders.Filter.Eq("Address", avatarAddress.ToHex()); + var document = collection.Find(filter).FirstOrDefaultAsync(); if (document is null) { throw new DocumentNotFoundInMongoCollectionException( @@ -23,16 +22,6 @@ public Collection GetCollection(Address avatarAddress) $"'Address' equals to '{avatarAddress.ToHex()}'"); } - try - { - var doc = document["Object"].AsBsonDocument; - return new Collection(doc); - } - catch (KeyNotFoundException e) - { - throw new KeyNotFoundInBsonDocumentException( - "document[\"State\"][\"Object\"].AsBsonDocument", - e); - } + return document; } } diff --git a/Mimir/Repositories/DailyRewardRepository.cs b/Mimir/Repositories/DailyRewardRepository.cs index 76aa7f16..63993dae 100644 --- a/Mimir/Repositories/DailyRewardRepository.cs +++ b/Mimir/Repositories/DailyRewardRepository.cs @@ -1,20 +1,19 @@ using Libplanet.Crypto; -using Mimir.Enums; using Mimir.Exceptions; -using Mimir.MongoDB.Bson.Extensions; -using Mimir.MongoDB.Exceptions; +using Mimir.MongoDB; +using Mimir.MongoDB.Bson; using Mimir.Services; -using MongoDB.Bson; using MongoDB.Driver; namespace Mimir.Repositories; public class DailyRewardRepository(MongoDbService dbService) { - public async Task GetByAddressAsync(Address address) + public async Task GetByAddressAsync(Address address) { - var collection = dbService.GetCollection(CollectionNames.DailyReward); - var filter = Builders.Filter.Eq("Address", address.ToHex()); + var collectionName = CollectionNames.GetCollectionName(); + var collection = dbService.GetCollection(collectionName); + var filter = Builders.Filter.Eq("Address", address.ToHex()); var document = await collection.Find(filter).FirstOrDefaultAsync(); if (document is null) { @@ -23,15 +22,6 @@ public async Task GetByAddressAsync(Address address) $"'Address' equals to '{address.ToHex()}'"); } - try - { - return document["Object"].ToLong(); - } - catch (KeyNotFoundException e) - { - throw new KeyNotFoundInBsonDocumentException( - "document[\"State\"][\"Object\"]", - e); - } + return document; } } diff --git a/Mimir/Repositories/InventoryRepository.cs b/Mimir/Repositories/InventoryRepository.cs index bb5a180f..3ba58b2b 100644 --- a/Mimir/Repositories/InventoryRepository.cs +++ b/Mimir/Repositories/InventoryRepository.cs @@ -1,48 +1,27 @@ using Libplanet.Crypto; -using Mimir.Enums; using Mimir.Exceptions; -using Mimir.Models; -using Mimir.MongoDB.Exceptions; +using Mimir.MongoDB; +using Mimir.MongoDB.Bson; using Mimir.Services; -using MongoDB.Bson; using MongoDB.Driver; namespace Mimir.Repositories; public class InventoryRepository(MongoDbService dbService) { - public Inventory GetInventory(Address avatarAddress) => - GetInventory( - dbService.GetCollection(CollectionNames.Inventory), - avatarAddress - ); - - private static Inventory GetInventory( - IMongoCollection collection, - Address avatarAddress - ) + public async Task GetByAddressAsync(Address avatarAddress) { - var filter = Builders.Filter.Eq("Address", avatarAddress.ToHex()); - var document = collection.Find(filter).FirstOrDefault(); + var collectionName = CollectionNames.GetCollectionName(); + var collection = dbService.GetCollection(collectionName); + var filter = Builders.Filter.Eq("Address", avatarAddress.ToHex()); + var document = await collection.Find(filter).FirstOrDefaultAsync(); if (document is null) { throw new DocumentNotFoundInMongoCollectionException( collection.CollectionNamespace.CollectionName, - $"'Address' equals to '{avatarAddress.ToHex()}'" - ); + $"'Address' equals to '{avatarAddress.ToHex()}'"); } - try - { - var doc = document["Object"].AsBsonDocument; - return new Inventory(doc); - } - catch (KeyNotFoundException e) - { - throw new KeyNotFoundInBsonDocumentException( - "document[\"State\"][\"Object\"].AsBsonDocument", - e - ); - } + return document; } } diff --git a/Mimir/Repositories/ItemSlotRepository.cs b/Mimir/Repositories/ItemSlotRepository.cs index c7ede408..c9af80fb 100644 --- a/Mimir/Repositories/ItemSlotRepository.cs +++ b/Mimir/Repositories/ItemSlotRepository.cs @@ -1,25 +1,24 @@ using Libplanet.Crypto; -using Mimir.Enums; using Mimir.Exceptions; -using Mimir.MongoDB.Exceptions; +using Mimir.MongoDB; +using Mimir.MongoDB.Bson; using Mimir.Services; -using MongoDB.Bson; using MongoDB.Driver; using Nekoyume.Model.EnumType; -using Nekoyume.Model.State; namespace Mimir.Repositories; public class ItemSlotRepository(MongoDbService dbService) { - public ItemSlotState GetItemSlot( - Address avatarAddress, - BattleType battleType) + public async Task GetByAddressAsync(Address avatarAddress, BattleType battleType) { - var itemSlotAddress = ItemSlotState.DeriveAddress(avatarAddress, battleType); - var collection = dbService.GetCollection(CollectionNames.ItemSlot); - var filter = Builders.Filter.Eq("Address", itemSlotAddress.ToHex()); - var document = collection.Find(filter).FirstOrDefault(); + var itemSlotAddress = Nekoyume.Model.State.ItemSlotState.DeriveAddress( + avatarAddress, + battleType); + var collectionName = CollectionNames.GetCollectionName(); + var collection = dbService.GetCollection(collectionName); + var filter = Builders.Filter.Eq("Address", itemSlotAddress.ToHex()); + var document = await collection.Find(filter).FirstOrDefaultAsync(); if (document is null) { throw new DocumentNotFoundInMongoCollectionException( @@ -27,31 +26,6 @@ public ItemSlotState GetItemSlot( $"'Address' equals to '{itemSlotAddress.ToHex()}'"); } - try - { - var obj = document["Object"].AsBsonDocument; - var costumes = obj["Costumes"].AsBsonArray - .Select(e => Guid.Parse(e.AsString)) - .ToList(); - var equipments = obj["Equipments"].AsBsonArray - .Select(e => Guid.Parse(e.AsString)) - .ToList(); - var itemSlot = new ItemSlotState(battleType); - itemSlot.UpdateCostumes(costumes); - itemSlot.UpdateEquipment(equipments); - return itemSlot; - } - catch (KeyNotFoundException e) - { - throw new KeyNotFoundInBsonDocumentException( - "document[\"State\"][\"Object\"] or its children keys", - e); - } - catch (InvalidCastException e) - { - throw new UnexpectedTypeOfBsonValueException( - "document[\"State\"][\"Object\"].AsBsonDocument or its children values", - e); - } + return document; } } diff --git a/Mimir/Repositories/MetadataRepository.cs b/Mimir/Repositories/MetadataRepository.cs index 4ccccdc7..6dd533ff 100644 --- a/Mimir/Repositories/MetadataRepository.cs +++ b/Mimir/Repositories/MetadataRepository.cs @@ -1,5 +1,5 @@ -using Mimir.Enums; using Mimir.Exceptions; +using Mimir.MongoDB; using Mimir.MongoDB.Bson; using Mimir.Services; using MongoDB.Driver; @@ -10,24 +10,24 @@ public class MetadataRepository(MongoDbService dbService) { public async Task GetByCollectionAsync(string collectionName) { - var collection = dbService.GetCollection(CollectionNames.Metadata); + var collection = dbService.GetCollection( + CollectionNames.GetCollectionName()); return await GetLatestBlockIndexAsync(collection, collectionName, null); } public async Task GetByCollectionAndTypeAsync( string pollerType, - string collectionName - ) + string collectionName) { - var collection = dbService.GetCollection(CollectionNames.Metadata); + var collection = dbService.GetCollection( + CollectionNames.GetCollectionName()); return await GetLatestBlockIndexAsync(collection, collectionName, pollerType); } private static async Task GetLatestBlockIndexAsync( IMongoCollection collection, string collectionName, - string? pollerType - ) + string? pollerType) { var builder = Builders.Filter; var filter = builder.Eq("CollectionName", collectionName); diff --git a/Mimir/Repositories/ProductRepository.cs b/Mimir/Repositories/ProductRepository.cs index d0b48392..1d66d1ee 100644 --- a/Mimir/Repositories/ProductRepository.cs +++ b/Mimir/Repositories/ProductRepository.cs @@ -3,6 +3,8 @@ using Lib9c.Models.Factories; using Lib9c.Models.Market; using Mimir.Enums; +using Mimir.MongoDB; +using Mimir.MongoDB.Bson; using Mimir.Services; using MongoDB.Bson; using MongoDB.Driver; @@ -14,26 +16,26 @@ public class ProductRepository(MongoDbService dbService) { private static readonly Codec Codec = new(); - public List GetProducts(long skip, int limit) => - GetProducts( - dbService.GetCollection(CollectionNames.Product), + public List GetProducts(long skip, int limit) + { + var collectionName = CollectionNames.GetCollectionName(); + return GetProducts( + dbService.GetCollection(collectionName), skip, limit ); + } private List GetProducts( - IMongoCollection collection, + IMongoCollection collection, long skip, - int limit - ) + int limit) { var pipelines = new List { new("$skip", skip), new("$limit", limit) }; - var aggregation = collection.Aggregate(pipelines).ToList(); - List products = aggregation + var products = aggregation .Select(async doc => - await GetRawState(dbService.GetGridFs(), doc["RawStateFileId"].AsObjectId) - ) + await GetRawState(dbService.GetGridFs(), doc["RawStateFileId"].AsObjectId)) .Select(doc => doc.Result) .Where(doc => doc != null) .Select(DeserializeProduct) diff --git a/Mimir/Repositories/RuneSlotRepository.cs b/Mimir/Repositories/RuneSlotRepository.cs index 007feeb8..c4ce7559 100644 --- a/Mimir/Repositories/RuneSlotRepository.cs +++ b/Mimir/Repositories/RuneSlotRepository.cs @@ -1,67 +1,31 @@ using Libplanet.Crypto; -using Lib9c.Models.Runes; -using Mimir.Enums; using Mimir.Exceptions; -using Mimir.MongoDB.Exceptions; +using Mimir.MongoDB; +using Mimir.MongoDB.Bson; using Mimir.Services; -using MongoDB.Bson; using MongoDB.Driver; using Nekoyume.Model.EnumType; -using RuneSlotState = Lib9c.Models.States.RuneSlotState; namespace Mimir.Repositories; public class RuneSlotRepository(MongoDbService dbService) { - public RuneSlotState GetRuneSlotState( - Address avatarAddress, - BattleType battleType) + public async Task GetByAddressAsync(Address avatarAddress, BattleType battleType) { - var runeSlotAddress = Nekoyume.Model.State.RuneSlotState.DeriveAddress(avatarAddress, battleType); - var collection = dbService.GetCollection(CollectionNames.RuneSlot); - var filter = Builders.Filter.Eq("Address", runeSlotAddress.ToHex()); - var document = collection.Find(filter).FirstOrDefault(); + var itemSlotAddress = Nekoyume.Model.State.RuneSlotState.DeriveAddress( + avatarAddress, + battleType); + var collectionName = CollectionNames.GetCollectionName(); + var collection = dbService.GetCollection(collectionName); + var filter = Builders.Filter.Eq("Address", itemSlotAddress.ToHex()); + var document = await collection.Find(filter).FirstOrDefaultAsync(); if (document is null) { throw new DocumentNotFoundInMongoCollectionException( collection.CollectionNamespace.CollectionName, - $"'Address' equals to '{runeSlotAddress.ToHex()}'"); + $"'Address' equals to '{itemSlotAddress.ToHex()}'"); } - try - { - var slots = document["Object"]["Slots"].AsBsonArray - .OfType() - .Select(doc => - { - var slotIndex = doc["Index"].AsInt32; - var runeSlotType = (RuneSlotType)doc["RuneSlotType"].AsInt32; - var runeType = (RuneType)doc["RuneType"].AsInt32; - var isLock = doc["IsLock"].AsBoolean; - var runeSheetId = doc.Contains("RuneId") - ? doc["RuneId"].AsNullableInt32 - : null; - return new RuneSlot( - slotIndex, - runeSlotType, - runeType, - isLock, - runeSheetId); - }) - .ToList(); - return new RuneSlotState(battleType, slots); - } - catch (KeyNotFoundException e) - { - throw new KeyNotFoundInBsonDocumentException( - "document[\"State\"][\"Object\"][\"slots\"] or its children keys", - e); - } - catch (InvalidCastException e) - { - throw new UnexpectedTypeOfBsonValueException( - "document[\"State\"][\"Object\"][\"slots\"].AsBsonArray or its children values", - e); - } + return document; } } diff --git a/Mimir/Repositories/StakeRepository.cs b/Mimir/Repositories/StakeRepository.cs index ae729926..deb0f858 100644 --- a/Mimir/Repositories/StakeRepository.cs +++ b/Mimir/Repositories/StakeRepository.cs @@ -1,53 +1,26 @@ using Libplanet.Crypto; -using Mimir.Enums; using Mimir.Exceptions; -using Mimir.MongoDB.Bson.Extensions; -using Mimir.MongoDB.Exceptions; +using Mimir.MongoDB; using Mimir.Services; -using MongoDB.Bson; using MongoDB.Driver; -using Nekoyume.Model.Stake; namespace Mimir.Repositories; public class StakeRepository(MongoDbService dbService) { - public StakeStateV2 GetStakeState(Address agentAddress) - { - var collection = dbService.GetCollection(CollectionNames.Stake); - var filter = Builders.Filter.Eq("Address", agentAddress.ToHex()); - var document = collection.Find(filter).FirstOrDefault(); - if (document is null) - { - throw new DocumentNotFoundInMongoCollectionException( - collection.CollectionNamespace.CollectionName, - $"'Address' equals to '{agentAddress.ToHex()}'"); - } - - try - { - var doc = document["Object"].AsBsonDocument; - var contractDoc = doc["Contract"].AsBsonDocument; - var contract = new Contract( - contractDoc["StakeRegularFixedRewardSheetTableName"].AsString, - contractDoc["StakeRegularRewardSheetTableName"].AsString, - contractDoc["RewardInterval"].ToLong(), - contractDoc["LockupInterval"].ToLong()); - var startedBlockIndex = doc["StartedBlockIndex"].ToLong(); - var receivedBlockIndex = doc["ReceivedBlockIndex"].ToLong(); - return new StakeStateV2(contract, startedBlockIndex, receivedBlockIndex); - } - catch (KeyNotFoundException e) - { - throw new KeyNotFoundInBsonDocumentException( - "document[\"State\"][\"Object\"] or its children keys", - e); - } - catch (InvalidCastException e) - { - throw new UnexpectedTypeOfBsonValueException( - "document[\"State\"][\"Object\"].AsBsonDocument or its children values", - e); - } - } + // public async Task GetByAddressAsync(Address agentAddress) + // { + // var collectionName = CollectionNames.GetCollectionName(); + // var collection = dbService.GetCollection(collectionName); + // var filter = Builders.Filter.Eq("Address", agentAddress.ToHex()); + // var document = await collection.Find(filter).FirstOrDefaultAsync(); + // if (document is null) + // { + // throw new DocumentNotFoundInMongoCollectionException( + // collection.CollectionNamespace.CollectionName, + // $"'Address' equals to '{agentAddress.ToHex()}'"); + // } + // + // return document; + // } } diff --git a/Mimir/Repositories/TableSheetsRepository.cs b/Mimir/Repositories/TableSheetsRepository.cs index aba1dc19..6d9412d1 100644 --- a/Mimir/Repositories/TableSheetsRepository.cs +++ b/Mimir/Repositories/TableSheetsRepository.cs @@ -1,6 +1,8 @@ using Bencodex; using Bencodex.Types; using Mimir.Enums; +using Mimir.MongoDB; +using Mimir.MongoDB.Bson; using Mimir.MongoDB.Bson.Extensions; using Mimir.MongoDB.Exceptions; using Mimir.Services; @@ -20,7 +22,8 @@ public class TableSheetsRepository(MongoDbService dbService) public ArenaSheet.RoundData GetArenaRound(long blockIndex) { - var collection = dbService.GetCollection(CollectionNames.TableSheet); + var collectionName = CollectionNames.GetCollectionName(); + var collection = dbService.GetCollection(collectionName); return GetArenaRound(collection, blockIndex); } @@ -95,7 +98,8 @@ long blockIndex public async Task GetSheetNamesAsync() { - var collection = dbService.GetCollection(CollectionNames.TableSheet); + var collectionName = CollectionNames.GetCollectionName(); + var collection = dbService.GetCollection(collectionName); var filter = Builders.Filter.Exists("Name"); var projection = Builders.Projection.Include("Name").Exclude("_id"); var docs = await collection @@ -109,7 +113,8 @@ public async Task GetSheetAsync( string sheetName, SheetFormat sheetFormat = SheetFormat.Csv) { - var collection = dbService.GetCollection(CollectionNames.TableSheet); + var collectionName = CollectionNames.GetCollectionName(); + var collection = dbService.GetCollection(collectionName); return await GetSheetAsync(collection, dbService.GetDatabase(), sheetName, sheetFormat); }