-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #51 from planetarium/fallback-non-cached-avatar-to…
…-get-state feat: return non cached avatar with get state.
- Loading branch information
Showing
5 changed files
with
156 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,123 @@ | ||
using System.Numerics; | ||
using Bencodex; | ||
using Libplanet.Common; | ||
using Libplanet.Crypto; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Mimir.Models.Avatar; | ||
using Mimir.Repositories; | ||
using Mimir.Services; | ||
using Mimir.Util; | ||
using Nekoyume.Model.State; | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Linq; | ||
|
||
namespace Mimir.Controllers; | ||
|
||
[ApiController] | ||
[Route("{network}/avatars")] | ||
public class AvatarController(AvatarRepository avatarRepository) : ControllerBase | ||
{ | ||
#region temporary snippets | ||
|
||
// This is a snippet from Mimir/Util/BigIntegerToStringConverter.cs | ||
private class BigIntegerToStringConverter : JsonConverter | ||
{ | ||
public override bool CanConvert(Type objectType) | ||
{ | ||
return objectType == typeof(BigInteger); | ||
} | ||
|
||
public override object ReadJson(JsonReader reader, Type objectType, object? existingValue, | ||
JsonSerializer serializer) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer) | ||
{ | ||
writer.WriteValue(value?.ToString()); | ||
} | ||
} | ||
|
||
// This is a snippet from Mimir/Util/StateJsonConverter.cs | ||
private class StateJsonConverter : JsonConverter | ||
{ | ||
public override bool CanConvert(Type objectType) | ||
{ | ||
return typeof(IState).IsAssignableFrom(objectType); | ||
} | ||
|
||
public override object ReadJson( | ||
JsonReader reader, | ||
Type objectType, | ||
object? existingValue, | ||
JsonSerializer serializer) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer) | ||
{ | ||
if (value is null) | ||
{ | ||
return; | ||
} | ||
|
||
var jo = JObject.FromObject(value, JsonSerializer.CreateDefault(new JsonSerializerSettings | ||
{ | ||
ReferenceLoopHandling = ReferenceLoopHandling.Ignore, | ||
Converters = new List<JsonConverter> { new BigIntegerToStringConverter() }, | ||
Formatting = Formatting.Indented, | ||
NullValueHandling = NullValueHandling.Ignore | ||
})); | ||
|
||
var iValue = value switch | ||
{ | ||
AvatarState avatarState => avatarState.SerializeList(), | ||
IState state => state.Serialize(), | ||
_ => null | ||
}; | ||
|
||
if (iValue != null) | ||
{ | ||
var rawValue = ByteUtil.Hex(new Codec().Encode(iValue)); | ||
jo.Add("Raw", rawValue); | ||
} | ||
|
||
jo.WriteTo(writer); | ||
} | ||
} | ||
|
||
// This is a snippet from Mimir.Worker/Models/State/BaseData.cs | ||
private static JsonSerializerSettings JsonSerializerSettings => new() | ||
{ | ||
Converters = { new StateJsonConverter() }, | ||
Formatting = Formatting.Indented, | ||
NullValueHandling = NullValueHandling.Ignore | ||
}; | ||
|
||
#endregion snippets | ||
|
||
[HttpGet("{avatarAddress}/inventory")] | ||
public Inventory? GetInventory(string network, string avatarAddress) | ||
public async Task<Inventory?> GetInventory( | ||
string network, | ||
string avatarAddress, | ||
IStateService stateService) | ||
{ | ||
var inventory = avatarRepository.GetInventory(network, avatarAddress); | ||
if (inventory is null) | ||
if (inventory is not null) | ||
{ | ||
return inventory; | ||
} | ||
|
||
var stateGetter = new StateGetter(stateService); | ||
var inventoryState = await stateGetter.GetInventoryStateAsync(new Address(avatarAddress)); | ||
if (inventoryState is null) | ||
{ | ||
Response.StatusCode = StatusCodes.Status404NotFound; | ||
return null; | ||
} | ||
|
||
return inventory; | ||
return new Inventory(inventoryState); | ||
} | ||
} |
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
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 |
---|---|---|
@@ -1,11 +1,21 @@ | ||
using MongoDB.Bson; | ||
using Nekoyume.Model.Item; | ||
|
||
namespace Mimir.Models.Items; | ||
|
||
public class NonFungibleItem(BsonValue nonFungibleItem) : Item(nonFungibleItem) | ||
public class NonFungibleItem : Item | ||
{ | ||
public Guid NonFungibleId { get; set; } = | ||
Guid.TryParse(nonFungibleItem["NonFungibleId"].AsString, out var nonFungibleId) | ||
public Guid NonFungibleId { get; set; } | ||
|
||
public NonFungibleItem(INonFungibleItem nonFungibleItem) : base(nonFungibleItem) | ||
{ | ||
NonFungibleId = nonFungibleItem.NonFungibleId; | ||
} | ||
|
||
public NonFungibleItem(BsonValue nonFungibleItem) : base(nonFungibleItem) | ||
{ | ||
NonFungibleId = Guid.TryParse(nonFungibleItem["NonFungibleId"].AsString, out var nonFungibleId) | ||
? nonFungibleId | ||
: Guid.Empty; | ||
} | ||
} |