Skip to content

Commit

Permalink
Add PatrolRewardMail
Browse files Browse the repository at this point in the history
  • Loading branch information
ipdae committed Jan 31, 2025
1 parent 5441dc7 commit e7596a2
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
1 change: 1 addition & 0 deletions Lib9c.Models/Factories/MailFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public static Mail Create(IValue bencoded)
"sellCancel" => new SellCancelMail(d),
"seller" => new SellerMail(d),
nameof(UnloadFromMyGaragesRecipientMail) => new UnloadFromMyGaragesRecipientMail(d),
nameof(PatrolRewardMail) => new PatrolRewardMail(d),
_ => throw new UnsupportedArgumentValueException<string>("typeId", typeId),
};
}
Expand Down
61 changes: 61 additions & 0 deletions Lib9c.Models/Mails/PatrolRewardMail.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using Bencodex.Types;
using Lib9c.Models.Exceptions;
using Lib9c.Models.Extensions;
using Libplanet.Types.Assets;
using MongoDB.Bson.Serialization.Attributes;
using ValueKind = Bencodex.Types.ValueKind;

namespace Lib9c.Models.Mails;

/// <summary>
/// <see cref="Nekoyume.Model.Mail.PatrolRewardMail"/>
/// </summary>
[BsonIgnoreExtraElements]
public record PatrolRewardMail : Mail
{
public List<FungibleAssetValue> FungibleAssetValues { get; init; }
public List<(int id, int count)> Items { get; init; }

public override IValue Bencoded
{
get
{
var d = (Dictionary)base.Bencoded;
if (FungibleAssetValues.Count != 0)
{
d = d.SetItem("f", new List(FungibleAssetValues
.Select(f => f.Serialize())));
}

if (Items.Count != 0)
{
d = d.SetItem("i", new List(Items
.Select(tuple => List.Empty.Add(tuple.id).Add(tuple.count))));
}

return d;
}
}

public PatrolRewardMail(IValue bencoded) : base(bencoded)
{
if (bencoded is not Dictionary d)
{
throw new UnsupportedArgumentTypeException<ValueKind>(
nameof(bencoded),
new[] { ValueKind.Dictionary },
bencoded.Kind);
}

FungibleAssetValues = d.ContainsKey("f")
? d["f"].ToList(StateExtensions.ToFungibleAssetValue)
: new List<FungibleAssetValue>();
Items = d.ContainsKey("i")
? d["i"].ToList<(int, int)>(v =>
{
var list = (List)v;
return ((Integer)list[0], (Integer)list[1]);
})
: new List<(int id, int count)>();
}
}

0 comments on commit e7596a2

Please sign in to comment.