Skip to content

Commit

Permalink
Merge pull request #579 from Atralupus/restore-rune-slot
Browse files Browse the repository at this point in the history
Restore all rune slot related codes
  • Loading branch information
Atralupus authored Dec 27, 2024
2 parents 4fe172b + 8c1b311 commit 9134973
Show file tree
Hide file tree
Showing 17 changed files with 606 additions and 18 deletions.
12 changes: 8 additions & 4 deletions Lib9c.Models/Runes/RuneSlot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ public record RuneSlot : IBencodable
public bool IsLock { get; init; }
public int? RuneId { get; init; }

public RuneSlot() { }

public IValue Bencoded
{
get
{
var l = List.Empty
.Add(Index.Serialize())
var l = List
.Empty.Add(Index.Serialize())
.Add(RuneSlotType.Serialize())
.Add(RuneType.Serialize())
.Add(IsLock.Serialize());
Expand All @@ -46,7 +48,8 @@ public RuneSlot(IValue bencoded)
throw new UnsupportedArgumentValueException<ValueKind>(
nameof(bencoded),
new[] { ValueKind.List },
bencoded.Kind);
bencoded.Kind
);
}

Index = l[0].ToInteger();
Expand All @@ -64,7 +67,8 @@ public RuneSlot(
RuneSlotType runeSlotType,
RuneType runeType,
bool isLock,
int? runeId = null)
int? runeId = null
)
{
Index = index;
RuneSlotType = runeSlotType;
Expand Down
2 changes: 2 additions & 0 deletions Lib9c.Models/States/RuneSlotState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public record RuneSlotState : IBencodable
public IValue Bencoded =>
List.Empty.Add(BattleType.Serialize()).Add(new List(Slots.Select(x => x.Bencoded)));

public RuneSlotState() { }

public RuneSlotState(IValue bencoded)
{
if (bencoded is not List l)
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"AvatarAddress":"0000000000000000000000000000000000000000","Object":{"BattleType":"Adventure","Costumes":[],"Equipments":["0468bfd9-7c76-4379-b1bc-10a3da90c584","0c4c772f-70d9-4a0d-b7e6-4cf25ad7f8f9","13164d27-1948-44a9-9bc7-b23a00aacc5f","2d9d4118-0e3b-445e-aa16-8203e50d2523","4da15d1b-503c-4e08-8476-6f405192258c","98c3abdb-17bc-49a0-8f9c-a61f48662dc7","a45c62d6-e612-4a0e-957a-9ecb7ae6f04f"]},"Metadata":{"SchemaVersion":2,"StoredBlockIndex":0}}
{"ItemSlotAddress":"0000000000000000000000000000000000000000","Object":{"BattleType":"Adventure","Costumes":[],"Equipments":["0468bfd9-7c76-4379-b1bc-10a3da90c584","0c4c772f-70d9-4a0d-b7e6-4cf25ad7f8f9","13164d27-1948-44a9-9bc7-b23a00aacc5f","2d9d4118-0e3b-445e-aa16-8203e50d2523","4da15d1b-503c-4e08-8476-6f405192258c","98c3abdb-17bc-49a0-8f9c-a61f48662dc7","a45c62d6-e612-4a0e-957a-9ecb7ae6f04f"]},"Metadata":{"SchemaVersion":2,"StoredBlockIndex":0}}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"RuneSlotAddress":"0000000000000000000000000000000000000000","Object":{"BattleType":"Adventure","Slots":[{"Index":0,"RuneSlotType":"Default","RuneType":"Stat","IsLock":false,"RuneId":10012},{"Index":1,"RuneSlotType":"Ncg","RuneType":"Stat","IsLock":true},{"Index":2,"RuneSlotType":"Stake","RuneType":"Stat","IsLock":true},{"Index":3,"RuneSlotType":"Default","RuneType":"Skill","IsLock":false},{"Index":4,"RuneSlotType":"Ncg","RuneType":"Skill","IsLock":true},{"Index":5,"RuneSlotType":"Stake","RuneType":"Skill","IsLock":true},{"Index":6,"RuneSlotType":"Crystal","RuneType":"Stat","IsLock":true},{"Index":7,"RuneSlotType":"Crystal","RuneType":"Skill","IsLock":true}]},"Metadata":{"SchemaVersion":1,"StoredBlockIndex":0}}
20 changes: 20 additions & 0 deletions Mimir.MongoDB.Tests/Bson/RuneSlotDocumentTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Lib9c.Models.States;
using Mimir.MongoDB.Bson;
using Mimir.MongoDB.Tests.TestDatas;

namespace Mimir.MongoDB.Tests.Bson;

public class RuneSlotDocumentTest
{
[Fact]
public Task JsonSnapshot()
{
var docs = new RuneSlotDocument(
default,
default,
default,
new RuneSlotState(TestDataHelpers.LoadState("RuneSlotState.bin"))
);
return Verify(docs.ToJson());
}
}
4 changes: 4 additions & 0 deletions Mimir.MongoDB.Tests/Mimir.MongoDB.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
<ParentFile>PetStateDocumentTest</ParentFile>
<DependentUpon>QuestListDocumentTest.cs</DependentUpon>
</None>
<None Update="Bson\StakeDocumentTest.JsonSnapshot.verified.txt">
<ParentFile>RuneSlotDocumentTest</ParentFile>
<DependentUpon>StakeDocumentTest.cs</DependentUpon>
</None>
<None Update="Bson\WorldBossStateDocumentTest.JsonSnapshot.verified.txt">
<ParentFile>StakeDocumentTest</ParentFile>
<DependentUpon>WorldBossDocumentTest.cs</DependentUpon>
Expand Down
6 changes: 3 additions & 3 deletions Mimir.MongoDB/Bson/ItemSlotDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Mimir.MongoDB.Bson;
[BsonIgnoreExtraElements]
public record ItemSlotDocument(
[property: BsonIgnore, JsonIgnore] long StoredBlockIndex,
[property: BsonIgnore, JsonIgnore] Address Address,
Address AvatarAddress,
[property: BsonIgnore, JsonIgnore] Address AvatarAddress,
Address ItemSlotAddress,
ItemSlotState Object
) : MimirBsonDocument(Address.ToHex(), new DocumentMetadata(2, StoredBlockIndex));
) : MimirBsonDocument(AvatarAddress.ToHex(), new DocumentMetadata(2, StoredBlockIndex));
14 changes: 14 additions & 0 deletions Mimir.MongoDB/Bson/RuneSlotDocument.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Lib9c.Models.States;
using Libplanet.Crypto;
using MongoDB.Bson.Serialization.Attributes;
using Newtonsoft.Json;

namespace Mimir.MongoDB.Bson;

[BsonIgnoreExtraElements]
public record RuneSlotDocument(
[property: BsonIgnore, JsonIgnore] long StoredBlockIndex,
[property: BsonIgnore, JsonIgnore] Address AvatarAddress,
Address RuneSlotAddress,
RuneSlotState Object
) : MimirBsonDocument(AvatarAddress.ToHex(), new DocumentMetadata(1, StoredBlockIndex));
1 change: 1 addition & 0 deletions Mimir.MongoDB/CollectionNames.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ private static void RegisterCollectionAndStateMappings()
CollectionAndStateTypeMappings.Add(typeof(PledgeDocument), "pledge");
CollectionAndStateTypeMappings.Add(typeof(ProductsStateDocument), "products");
CollectionAndStateTypeMappings.Add(typeof(ProductDocument), "product");
CollectionAndStateTypeMappings.Add(typeof(RuneSlotDocument), "rune_slot");
// CollectionAndStateTypeMappings.Add(typeof(QuestListDocument), "quest_list");
CollectionAndStateTypeMappings.Add(typeof(RaiderStateDocument), "raider");
CollectionAndStateTypeMappings.Add(typeof(SheetDocument), "table_sheet");
Expand Down
40 changes: 40 additions & 0 deletions Mimir.MongoDB/Repositories/RuneSlotRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using Libplanet.Crypto;
using Mimir.MongoDB.Bson;
using Mimir.MongoDB.Exceptions;
using Mimir.MongoDB.Services;
using MongoDB.Driver;
using Nekoyume.Model.EnumType;

namespace Mimir.MongoDB.Repositories;

public interface IRuneSlotRepository
{
Task<RuneSlotDocument> GetByAddressAsync(Address avatarAddress, BattleType battleType);
}

public class RuneSlotRepository(MongoDbService dbService) : IRuneSlotRepository
{
public async Task<RuneSlotDocument> GetByAddressAsync(
Address avatarAddress,
BattleType battleType
)
{
var runeSlotAddress = Nekoyume.Model.State.RuneSlotState.DeriveAddress(
avatarAddress,
battleType
);
var collectionName = CollectionNames.GetCollectionName<RuneSlotDocument>();
var collection = dbService.GetCollection<RuneSlotDocument>(collectionName);
var filter = Builders<RuneSlotDocument>.Filter.Eq("_id", runeSlotAddress.ToHex());
var document = await collection.Find(filter).FirstOrDefaultAsync();
if (document is null)
{
throw new DocumentNotFoundInMongoCollectionException(
collection.CollectionNamespace.CollectionName,
$"'Address' equals to '{runeSlotAddress.ToHex()}'"
);
}

return document;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"data": {
"runeSlot": {
"battleType": "ADVENTURE",
"slots": [
{
"index": 1,
"runeSlotType": "DEFAULT",
"runeType": "STAT",
"isLock": false,
"runeId": 10
}
]
}
}
}
63 changes: 63 additions & 0 deletions Mimir.Tests/QueryTests/RuneSlotTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using Lib9c.Models.Runes;
using Lib9c.Models.States;
using Libplanet.Crypto;
using Mimir.MongoDB.Bson;
using Mimir.MongoDB.Repositories;
using Moq;

namespace Mimir.Tests.QueryTests;

public class RuneSlotTest
{
[Fact]
public async Task GraphQL_Query_RuneSlot_Returns_CorrectValue()
{
var address = new Address("0x0000000001000000000200000000030000000004");
var state = new RuneSlotState
{
BattleType = Nekoyume.Model.EnumType.BattleType.Adventure,
Slots = new List<RuneSlot>
{
new RuneSlot
{
Index = 1,
RuneSlotType = Nekoyume.Model.EnumType.RuneSlotType.Default,
RuneType = Nekoyume.Model.EnumType.RuneType.Stat,
IsLock = false,
RuneId = 10
}
},
};
var mockRepo = new Mock<IRuneSlotRepository>();
mockRepo
.Setup(repo =>
repo.GetByAddressAsync(
It.IsAny<Address>(),
Nekoyume.Model.EnumType.BattleType.Adventure
)
)
.ReturnsAsync(new RuneSlotDocument(1, address, address, state));
var serviceProvider = TestServices.Builder.With(mockRepo.Object).Build();
var query = $$"""
query {
runeSlot(address: "{{address}}", battleType: ADVENTURE) {
battleType,
slots {
index
runeSlotType
runeType
isLock
runeId
}
}
}
""";

var result = await TestServices.ExecuteRequestAsync(
serviceProvider,
b => b.SetDocument(query)
);

await Verify(result);
}
}
Loading

0 comments on commit 9134973

Please sign in to comment.