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: introduce "{network}/avatars/{address}" #68

Merged
merged 3 commits into from
May 17, 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
4 changes: 2 additions & 2 deletions Mimir/Controllers/AgentController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Mimir.Controllers;

[ApiController]
[Route("{network}/agent")]
public class AccountController : ControllerBase
public class AgentController : ControllerBase
{
[HttpGet("{agentAddress}/avatars")]
public async Task<AvatarsResponse> GetAvatars(
Expand All @@ -26,7 +26,7 @@ IStateService stateService
}

return new AvatarsResponse(
avatars.Select(e => new Avatar(e.address.ToString(), e.name, e.level, e.actionPoint, e.dailyRewardReceivedIndex)).ToList()
avatars.Select(e => new Avatar(e)).ToList()
);
}
}
47 changes: 41 additions & 6 deletions Mimir/Controllers/AvatarController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Libplanet.Common;
using Libplanet.Crypto;
using Microsoft.AspNetCore.Mvc;
using Mimir.Models.Agent;
using Mimir.Models.Avatar;
using Mimir.Repositories;
using Mimir.Services;
Expand All @@ -14,7 +15,7 @@
namespace Mimir.Controllers;

[ApiController]
[Route("{network}/avatars")]
[Route("{network}/avatars/{address}")]
public class AvatarController(AvatarRepository avatarRepository) : ControllerBase
{
#region temporary snippets
Expand Down Expand Up @@ -98,13 +99,47 @@ public override void WriteJson(JsonWriter writer, object? value, JsonSerializer

#endregion snippets

[HttpGet("{avatarAddress}/inventory")]
[HttpGet]
public async Task<Avatar?> GetState(
string network,
string address,
IStateService stateService)
{
var avatar = avatarRepository.GetAvatar(network, address);
if (avatar is not null)
{
return avatar;
}

Address addr;
try
{
addr = new Address(address);
}
catch (ArgumentException)
{
Response.StatusCode = StatusCodes.Status400BadRequest;
return null;
}

var stateGetter = new StateGetter(stateService);
var avatarState = await stateGetter.GetAvatarStateAsync(addr);
if (avatarState is null)
{
Response.StatusCode = StatusCodes.Status404NotFound;
return null;
}

return new Avatar(avatarState);
}

[HttpGet("inventory")]
public async Task<Inventory?> GetInventory(
string network,
string avatarAddress,
string address,
IStateService stateService)
{
var inventory = avatarRepository.GetInventory(network, avatarAddress);
var inventory = avatarRepository.GetInventory(network, address);
if (inventory is not null)
{
return inventory;
Expand All @@ -113,9 +148,9 @@ public override void WriteJson(JsonWriter writer, object? value, JsonSerializer
Address inventoryAddress;
try
{
inventoryAddress = new Address(avatarAddress);
inventoryAddress = new Address(address);
}
catch (ArgumentException e)
catch (ArgumentException)
{
Response.StatusCode = StatusCodes.Status400BadRequest;
return null;
Expand Down
21 changes: 20 additions & 1 deletion Mimir/Models/Agent/Avatar.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,29 @@
using Nekoyume.Model.State;

namespace Mimir.Models.Agent;

public class Avatar(string avatarAddress, string avatarName, int level, int actionPoint, long dailyRewardReceivedIndex)
public class Avatar(
string agentAddress,
string avatarAddress,
string avatarName,
int level,
int actionPoint,
long dailyRewardReceivedIndex)
{
public string AgentAddress { get; set; } = agentAddress;
public string AvatarAddress { get; set; } = avatarAddress;
public string AvatarName { get; set; } = avatarName;
public int Level { get; set; } = level;
public int ActionPoint { get; private set; } = actionPoint;
public long DailyRewardReceivedIndex { get; private set; } = dailyRewardReceivedIndex;

public Avatar(AvatarState avatarState) : this(
avatarState.agentAddress.ToString(),
avatarState.address.ToString(),
avatarState.name,
avatarState.level,
avatarState.actionPoint,
avatarState.dailyRewardReceivedIndex)
{
}
}
1 change: 1 addition & 0 deletions Mimir/Repositories/ArenaRankingRespository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ private async Task<ArenaRanking> BuildArenaRankingFromDocument(BsonDocument docu
}

var avatar = new Avatar(
document["Avatar"]["Avatar"]["agentAddress"].AsString,
document["Avatar"]["Avatar"]["address"].AsString,
document["Avatar"]["Avatar"]["name"].AsString,
document["Avatar"]["Avatar"]["level"].AsInt32,
Expand Down
28 changes: 28 additions & 0 deletions Mimir/Repositories/AvatarRepository.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Mimir.Models.Agent;
using Mimir.Models.Avatar;
using Mimir.Services;
using MongoDB.Bson;
Expand All @@ -16,6 +17,33 @@ protected override string GetCollectionName()
{
return "avatars";
}

public Avatar? GetAvatar(string network, string avatarAddress)
{
var collection = GetCollection(network);
var filter = Builders<BsonDocument>.Filter.Eq("Avatar.address", avatarAddress);
var document = collection.Find(filter).FirstOrDefault();
if (document is null)
{
return null;
}

try
{
return new Avatar(
document["Avatar"]["agentAddress"].AsString,
document["Avatar"]["address"].AsString,
document["Avatar"]["name"].AsString,
document["Avatar"]["level"].AsInt32,
document["Avatar"]["actionPoint"].AsInt32,
document["Avatar"]["dailyRewardReceivedIndex"].AsInt64
);
}
catch (KeyNotFoundException)
{
return null;
}
}

public Inventory? GetInventory(string network, string avatarAddress)
{
Expand Down