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

Expand inventory item properties #55

Merged
merged 3 commits into from
May 14, 2024
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
26 changes: 24 additions & 2 deletions Mimir/Models/Avatar/Inventory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,41 @@ namespace Mimir.Models.Avatar;

public class Inventory
{
public List<Consumable> Consumables { get; set; }
public List<Costume> Costumes { get; set; }
public List<Equipment> Equipments { get; set; }
public List<Material> Materials { get; set; }

public Inventory(Nekoyume.Model.Item.Inventory inventory)
{
Consumables = inventory.Consumables
.Select(c => new Consumable(c))
.ToList();
Costumes = inventory.Costumes
.Select(c => new Costume(c))
.ToList();
Equipments = inventory.Equipments
.Select(e => new Equipment(e))
.ToList();
Materials = inventory.Items
.Where(i => i.item is Nekoyume.Model.Item.Material)
.Select(i => new Material((Nekoyume.Model.Item.Material)i.item, i.count))
.ToList();
}
public Inventory(BsonValue inventory)

public Inventory(BsonDocument inventory)
{
Consumables = inventory["Consumables"].AsBsonArray
.Select(c => new Consumable(c.AsBsonDocument))
.ToList();
Costumes = inventory["Costumes"].AsBsonArray
.Select(c => new Costume(c.AsBsonDocument))
.ToList();
Equipments = inventory["Equipments"].AsBsonArray
.Select(e => new Equipment(e.AsBsonDocument))
.ToList();
Materials = inventory["Materials"].AsBsonArray
.Select(m => new Material(m.AsBsonDocument))
.ToList();
}
}
15 changes: 15 additions & 0 deletions Mimir/Models/Items/Consumable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using MongoDB.Bson;

namespace Mimir.Models.Items;

public class Consumable : Item
{
public Consumable(Nekoyume.Model.Item.Consumable consumable) :
base(consumable, count: 1)
{
}

public Consumable(BsonDocument consumable) : base(consumable)
{
}
}
15 changes: 15 additions & 0 deletions Mimir/Models/Items/Costume.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using MongoDB.Bson;

namespace Mimir.Models.Items;

public class Costume : Item
{
public Costume(Nekoyume.Model.Item.Costume costume) :
base(costume, count: 1)
{
}

public Costume(BsonDocument costume) : base(costume)
{
}
}
10 changes: 7 additions & 3 deletions Mimir/Models/Items/Equipment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@

namespace Mimir.Models.Items;

public class Equipment : NonFungibleItem
public class Equipment : Item
{
public Guid NonFungibleId { get; set; }
public int Level { get; set; }

public Equipment(Nekoyume.Model.Item.Equipment equipment) : base(equipment)
public Equipment(Nekoyume.Model.Item.Equipment equipment) :
base(equipment, count: 1)
{
NonFungibleId = equipment.NonFungibleId;
Level = equipment.level;
}

public Equipment(BsonValue equipment) : base(equipment)
public Equipment(BsonDocument equipment) : base(equipment)
{
NonFungibleId = Guid.Parse(equipment["NonFungibleId"].AsString);
Level = equipment["level"].AsInt32;
}
}
31 changes: 29 additions & 2 deletions Mimir/Models/Items/Item.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,46 @@
using MongoDB.Bson;
using Nekoyume.Model.Elemental;
using Nekoyume.Model.Item;

namespace Mimir.Models.Items;

public class Item
{
public int ItemSheetId { get; set; }
public int Grade { get; set; }
public ItemType ItemType { get; set; }
public ItemSubType ItemSubType { get; set; }
public ElementalType ElementalType { get; set; }

public Item(IItem item)
public int? Count { get; set; }
public long? RequiredBlockIndex { get; set; }

public Item(ItemBase item, int? count)
{
ItemSheetId = item.Id;
Grade = item.Grade;
ItemType = item.ItemType;
ItemSubType = item.ItemSubType;
ElementalType = item.ElementalType;

Count = count;
RequiredBlockIndex = item switch
{
INonFungibleItem nonFungibleItem => nonFungibleItem.RequiredBlockIndex,
ITradableItem tradableItem => tradableItem.RequiredBlockIndex,
_ => null
};
}

public Item(BsonValue item)
public Item(BsonDocument item)
{
ItemSheetId = item["ItemSheetId"].AsInt32;
Grade = item["Grade"].AsInt32;
ItemType = (ItemType)item["ItemType"].AsInt32;
ItemSubType = (ItemSubType)item["ItemSubType"].AsInt32;
ElementalType = (ElementalType)item["ElementalType"].AsInt32;

Count = item["Count"].AsNullableInt32;
RequiredBlockIndex = item["RequiredBlockIndex"].AsNullableInt64;
}
}
15 changes: 15 additions & 0 deletions Mimir/Models/Items/Material.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using MongoDB.Bson;

namespace Mimir.Models.Items;

public class Material : Item
{
public Material(Nekoyume.Model.Item.Material material, int count) :
base(material, count)
{
}

public Material(BsonDocument material) : base(material)
{
}
}
21 changes: 0 additions & 21 deletions Mimir/Models/Items/NonFungibleItem.cs

This file was deleted.

17 changes: 15 additions & 2 deletions Mimir/Repositories/AvatarRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ namespace Mimir.Repositories;
public class AvatarRepository : BaseRepository<BsonDocument>
{
public AvatarRepository(MongoDBCollectionService mongoDBCollectionService)
: base(mongoDBCollectionService) { }
: base(mongoDBCollectionService)
{
}

protected override string GetCollectionName()
{
Expand All @@ -21,7 +23,18 @@ protected override string GetCollectionName()
var filter = Builders<BsonDocument>.Filter.Eq("Avatar.address", avatarAddress);
var projection = Builders<BsonDocument>.Projection.Include("Avatar.inventory.Equipments");
var document = collection.Find(filter).Project(projection).FirstOrDefault();
if (document is null)
{
return null;
}

return document is null ? null : new Inventory(document["Avatar"]["inventory"]);
try
{
return new Inventory(document["Avatar"]["inventory"].AsBsonDocument);
}
catch (KeyNotFoundException)
{
return null;
}
}
}