-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)>(); | ||
} | ||
} |