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

feat: implements many serializers for ItemBase relates #340

Merged
merged 20 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from 15 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
4 changes: 2 additions & 2 deletions Lib9c.GraphQL/Lib9c.GraphQL.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

<ItemGroup>
<PackageReference Include="GraphQL" Version="7.8.0" />
<PackageReference Include="HotChocolate.AspNetCore" Version="13.9.8" />
<PackageReference Include="HotChocolate.Types.Analyzers" Version="13.9.8">
<PackageReference Include="HotChocolate.AspNetCore" Version="13.9.12" />
<PackageReference Include="HotChocolate.Types.Analyzers" Version="13.9.12">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public CombinationConsumable5Result(IValue bencoded) : base(bencoded)
Materials = ((List)d["materials"])
.Cast<Dictionary>()
.ToDictionary(
value => (Material)ItemFactory.Deserialize((Dictionary)value["material"]),
value => (Material)ItemFactory.Deserialize(value["material"]),
value => value["count"].ToInteger());
Id = d["id"].ToGuid();
Gold = d["gold"].ToBigInteger();
Expand Down
2 changes: 1 addition & 1 deletion Lib9c.Models/AttachmentActionResults/DailyReward2Result.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public DailyReward2Result(IValue bencoded) : base(bencoded)
Materials = ((List)d["materials"])
.Cast<Dictionary>()
.ToDictionary(
value => (Material)ItemFactory.Deserialize((Dictionary)value["material"]),
value => (Material)ItemFactory.Deserialize(value["material"]),
value => value["count"].ToInteger());

Id = d["id"].ToGuid();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public ItemEnhancement11Result(IValue bencoded) : base(bencoded)
ActionPoint = d["actionPoint"].ToInteger();
EnhancementResult = d["enhancementResult"].ToEnum<ItemEnhancement9.EnhancementResult>();
PreItemUsable = d.ContainsKey("preItemUsable")
? (ItemUsable)ItemFactory.Deserialize((Dictionary)d["preItemUsable"])
? (ItemUsable)ItemFactory.Deserialize(d["preItemUsable"])
: null;
Crystal = new FungibleAssetValue(d["c"]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public ItemEnhancement12Result(IValue bencoded) : base(bencoded)
ActionPoint = d["actionPoint"].ToInteger();
EnhancementResult = d["enhancementResult"].ToEnum<ItemEnhancement9.EnhancementResult>();
PreItemUsable = d.ContainsKey("preItemUsable")
? (ItemUsable)ItemFactory.Deserialize((Dictionary)d["preItemUsable"])
? (ItemUsable)ItemFactory.Deserialize(d["preItemUsable"])
: null;
Crystal = new FungibleAssetValue(d["c"]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public ItemEnhancement13Result(IValue bencoded) : base(bencoded)
ActionPoint = d["actionPoint"].ToInteger();
EnhancementResult = d["enhancementResult"].ToEnum<ItemEnhancement9.EnhancementResult>();
PreItemUsable = d.ContainsKey("preItemUsable")
? (ItemUsable)ItemFactory.Deserialize((Dictionary)d["preItemUsable"])
? (ItemUsable)ItemFactory.Deserialize(d["preItemUsable"])
: null;
Crystal = new FungibleAssetValue(d["c"]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public ItemEnhancement9Result(IValue bencoded) : base(bencoded)
ActionPoint = d["actionPoint"].ToInteger();
EnhancementResult = d["enhancementResult"].ToEnum<ItemEnhancement9.EnhancementResult>();
PreItemUsable = d.ContainsKey("preItemUsable")
? (ItemUsable)ItemFactory.Deserialize((Dictionary)d["preItemUsable"])
? (ItemUsable)ItemFactory.Deserialize(d["preItemUsable"])
: null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public RapidCombination0Result(IValue bencoded) : base(bencoded)
Cost = ((List)d["cost"])
.Cast<Dictionary>()
.ToDictionary(
value => (Material)ItemFactory.Deserialize((Dictionary)value["material"]),
value => (Material)ItemFactory.Deserialize(value["material"]),
value => value["count"].ToInteger());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public RapidCombination5Result(IValue bencoded) : base(bencoded)
Cost = ((List)d["cost"])
.Cast<Dictionary>()
.ToDictionary(
value => (Material)ItemFactory.Deserialize((Dictionary)value["material"]),
value => (Material)ItemFactory.Deserialize(value["material"]),
value => value["count"].ToInteger());
}
}
43 changes: 27 additions & 16 deletions Lib9c.Models/Factories/ItemFactory.cs
Original file line number Diff line number Diff line change
@@ -1,57 +1,68 @@
using Bencodex.Types;
using Lib9c.Models.Exceptions;
using Lib9c.Models.Items;
using ItemType = Nekoyume.Model.Item.ItemType;
using ItemSubType = Nekoyume.Model.Item.ItemSubType;
using static Lib9c.SerializeKeys;
using Lib9c.Models.Extensions;
using ValueKind = Bencodex.Types.ValueKind;

namespace Lib9c.Models.Factories;

public static class ItemFactory
{
public static ItemBase Deserialize(Dictionary serialized)
public static ItemBase Deserialize(IValue bencoded)
{
if (serialized.TryGetValue((Text)"item_type", out var type) &&
serialized.TryGetValue((Text)"item_sub_type", out var subType))
if (bencoded is not Dictionary d)
{
throw new UnsupportedArgumentTypeException<ValueKind>(
nameof(bencoded),
new[] { ValueKind.Dictionary },
bencoded.Kind);
}

if (d.TryGetValue((Text)"item_type", out var type) &&
d.TryGetValue((Text)"item_sub_type", out var subType))
{
var itemType = type.ToEnum<ItemType>();
var itemSubType = subType.ToEnum<ItemSubType>();

switch (itemType)
{
case ItemType.Consumable:
return new Consumable(serialized);
return new Consumable(d);
case ItemType.Costume:
return new Costume(serialized);
return new Costume(d);
case ItemType.Equipment:
switch (itemSubType)
{
case ItemSubType.Weapon:
return new Weapon(serialized);
return new Weapon(d);
case ItemSubType.Armor:
return new Armor(serialized);
return new Armor(d);
case ItemSubType.Belt:
return new Belt(serialized);
return new Belt(d);
case ItemSubType.Necklace:
return new Necklace(serialized);
return new Necklace(d);
case ItemSubType.Ring:
return new Ring(serialized);
return new Ring(d);
case ItemSubType.Aura:
return new Aura(serialized);
return new Aura(d);
case ItemSubType.Grimoire:
return new Grimoire(serialized);
return new Grimoire(d);
}

break;
case ItemType.Material:
return serialized.ContainsKey(RequiredBlockIndexKey)
? new TradableMaterial(serialized)
: new Material(serialized);
return d.ContainsKey(RequiredBlockIndexKey)
? new TradableMaterial(d)
: new Material(d);

default:
throw new ArgumentOutOfRangeException(nameof(itemType));
}
}

throw new ArgumentException($"Can't Deserialize Item {serialized}");
throw new ArgumentException($"Can't Deserialize Item {d}");
}
}
4 changes: 4 additions & 0 deletions Lib9c.Models/Items/Armor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ namespace Lib9c.Models.Items;
/// </summary>
public record Armor : Equipment
{
public Armor()
{
}

public Armor(IValue bencoded) : base(bencoded)
{
}
Expand Down
4 changes: 4 additions & 0 deletions Lib9c.Models/Items/Aura.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ namespace Lib9c.Models.Items;
/// </summary>
public record Aura : Equipment
{
public Aura()
{
}

public Aura(IValue bencoded) : base(bencoded)
{
}
Expand Down
4 changes: 4 additions & 0 deletions Lib9c.Models/Items/Belt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ namespace Lib9c.Models.Items;
/// </summary>
public record Belt : Equipment
{
public Belt()
{
}

public Belt(IValue bencoded) : base(bencoded)
{
}
Expand Down
11 changes: 8 additions & 3 deletions Lib9c.Models/Items/Consumable.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,30 @@
using Bencodex;
using Bencodex.Types;
using Lib9c.Models.Exceptions;
using Lib9c.Models.Extensions;
using Lib9c.Models.Stats;
using MongoDB.Bson.Serialization.Attributes;
using ValueKind = Bencodex.Types.ValueKind;

namespace Lib9c.Models.Items;

/// <summary>
/// <see cref="Nekoyume.Model.Item.Consumable"/>
/// </summary>
public record Consumable : ItemUsable, IBencodable
public record Consumable : ItemUsable
{
public List<DecimalStat> Stats { get; }
public List<DecimalStat> Stats { get; init; }

[BsonIgnore, GraphQLIgnore]
public override IValue Bencoded => ((Dictionary)base.Bencoded)
.Add("stats", new List(Stats
.OrderBy(i => i.StatType)
.ThenByDescending(i => i.BaseValue)
.Select(s => s.BencodedWithoutAdditionalValue)));

public Consumable()

Check warning on line 24 in Lib9c.Models/Items/Consumable.cs

View workflow job for this annotation

GitHub Actions / Test (Mimir.Worker.Tests)

Non-nullable property 'Stats' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 24 in Lib9c.Models/Items/Consumable.cs

View workflow job for this annotation

GitHub Actions / Test (Mimir.Worker.Tests)

Non-nullable property 'Stats' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.
{
}

public Consumable(IValue bencoded) : base(bencoded)
{
if (bencoded is not Dictionary d)
Expand Down
17 changes: 11 additions & 6 deletions Lib9c.Models/Items/Costume.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
using Bencodex;
using Bencodex.Types;
using Lib9c.Models.Exceptions;
using ValueKind = Bencodex.Types.ValueKind;
using static Lib9c.SerializeKeys;
using Lib9c.Models.Extensions;
using MongoDB.Bson.Serialization.Attributes;

namespace Lib9c.Models.Items;

/// <summary>
/// <see cref="Nekoyume.Model.Item.Costume"/>
/// </summary>
public record Costume : ItemBase, IBencodable
public record Costume : ItemBase
{
public bool Equipped { get; }
public string SpineResourcePath { get; }
public Guid ItemId { get; }
public long RequiredBlockIndex { get; }
public bool Equipped { get; init; }
public string SpineResourcePath { get; init; }
public Guid ItemId { get; init; }
public long RequiredBlockIndex { get; init; }

[BsonIgnore, GraphQLIgnore]
public override IValue Bencoded
{
get
Expand All @@ -32,7 +33,11 @@
}
}

public Costume()

Check warning on line 36 in Lib9c.Models/Items/Costume.cs

View workflow job for this annotation

GitHub Actions / Test (Mimir.Worker.Tests)

Non-nullable property 'SpineResourcePath' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 36 in Lib9c.Models/Items/Costume.cs

View workflow job for this annotation

GitHub Actions / Test (Mimir.Worker.Tests)

Non-nullable property 'SpineResourcePath' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.
{
}

public Costume(IValue bencoded) : base(bencoded)

Check warning on line 40 in Lib9c.Models/Items/Costume.cs

View workflow job for this annotation

GitHub Actions / Test (Mimir.Worker.Tests)

Non-nullable property 'SpineResourcePath' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 40 in Lib9c.Models/Items/Costume.cs

View workflow job for this annotation

GitHub Actions / Test (Mimir.Worker.Tests)

Non-nullable property 'SpineResourcePath' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.
{
if (bencoded is not Dictionary d)
{
Expand Down
9 changes: 7 additions & 2 deletions Lib9c.Models/Items/Equipment.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
using Bencodex;
using Bencodex.Types;
using Lib9c.Models.Exceptions;
using Lib9c.Models.Stats;
using ValueKind = Bencodex.Types.ValueKind;
using static Lib9c.SerializeKeys;
using Lib9c.Models.Extensions;
using MongoDB.Bson.Serialization.Attributes;

namespace Lib9c.Models.Items;

/// <summary>
/// <see cref="Nekoyume.Model.Item.Equipment"/>
/// </summary>
public record Equipment : ItemUsable, IBencodable
public record Equipment : ItemUsable
{
public bool Equipped { get; init; }
public int Level { get; init; }
Expand All @@ -22,6 +22,7 @@
public int OptionCountFromCombination { get; init; }
public bool MadeWithMimisbrunnrRecipe { get; init; }

[BsonIgnore, GraphQLIgnore]
public override IValue Bencoded
{
get
Expand Down Expand Up @@ -52,6 +53,10 @@
}
}

public Equipment()

Check warning on line 56 in Lib9c.Models/Items/Equipment.cs

View workflow job for this annotation

GitHub Actions / Test (Mimir.Worker.Tests)

Non-nullable property 'Stat' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 56 in Lib9c.Models/Items/Equipment.cs

View workflow job for this annotation

GitHub Actions / Test (Mimir.Worker.Tests)

Non-nullable property 'Stat' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 56 in Lib9c.Models/Items/Equipment.cs

View workflow job for this annotation

GitHub Actions / Test (Mimir.Worker.Tests)

Non-nullable property 'SpineResourcePath' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.
{
}

public Equipment(IValue bencoded) : base(bencoded)
{
if (bencoded is not Dictionary d)
Expand Down
4 changes: 4 additions & 0 deletions Lib9c.Models/Items/Grimoire.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ namespace Lib9c.Models.Items;

public record Grimoire : Equipment
{
public Grimoire()
{
}

public Grimoire(IValue bencoded) : base(bencoded)
{
}
Expand Down
2 changes: 2 additions & 0 deletions Lib9c.Models/Items/Inventory.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Bencodex;
using Bencodex.Types;
using Lib9c.Models.Exceptions;
using MongoDB.Bson.Serialization.Attributes;
using ValueKind = Bencodex.Types.ValueKind;

namespace Lib9c.Models.Items;
Expand All @@ -12,6 +13,7 @@ public record Inventory : IBencodable
{
public List<InventoryItem> Items { get; init; }

[BsonIgnore, GraphQLIgnore]
public IValue Bencoded => new List(Items
.OrderBy(i => i.Item.Id)
.ThenByDescending(i => i.Count)
Expand Down
4 changes: 3 additions & 1 deletion Lib9c.Models/Items/InventoryItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Bencodex.Types;
using Lib9c.Models.Exceptions;
using Lib9c.Models.Extensions;
using MongoDB.Bson.Serialization.Attributes;
using Nekoyume.Model.Item;
using ItemFactory = Lib9c.Models.Factories.ItemFactory;
using ValueKind = Bencodex.Types.ValueKind;
Expand All @@ -17,6 +18,7 @@ public record InventoryItem : IBencodable
public int Count { get; init; }
public ILock? Lock { get; init; }

[BsonIgnore, GraphQLIgnore]
public IValue Bencoded
{
get
Expand All @@ -40,7 +42,7 @@ public InventoryItem(IValue bencoded)
bencoded.Kind);
}

Item = ItemFactory.Deserialize((Dictionary)d["item"]);
Item = ItemFactory.Deserialize(d["item"]);
Count = (Integer)d["count"];
if (d.ContainsKey("l"))
{
Expand Down
6 changes: 6 additions & 0 deletions Lib9c.Models/Items/ItemBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Bencodex.Types;
using Lib9c.Models.Exceptions;
using Lib9c.Models.Extensions;
using MongoDB.Bson.Serialization.Attributes;
using ValueKind = Bencodex.Types.ValueKind;

namespace Lib9c.Models.Items;
Expand All @@ -17,13 +18,18 @@ public record ItemBase : IBencodable
public Nekoyume.Model.Item.ItemSubType ItemSubType { get; init; }
public Nekoyume.Model.Elemental.ElementalType ElementalType { get; init; }

[BsonIgnore, GraphQLIgnore]
public virtual IValue Bencoded => Dictionary.Empty
.Add("id", Id.Serialize())
.Add("item_type", ItemType.Serialize())
.Add("item_sub_type", ItemSubType.Serialize())
.Add("grade", Grade.Serialize())
.Add("elemental_type", ElementalType.Serialize());

public ItemBase()
{
}

public ItemBase(IValue bencoded)
{
if (bencoded is not Dictionary d)
Expand Down
Loading
Loading