Skip to content

Commit

Permalink
Refactor GetArenaParticipnats for reduce call get state
Browse files Browse the repository at this point in the history
  • Loading branch information
ipdae committed Dec 19, 2024
1 parent dd13f18 commit ac08860
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 273 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using Libplanet.Crypto;
using Nekoyume.Model.State;

namespace ArenaService;

public struct ArenaParticipant
public struct ArenaParticipantStruct
{
public Address AvatarAddr { get; set; }
public int Score { get; set; }
Expand All @@ -15,11 +14,11 @@ public struct ArenaParticipant
public string NameWithHash { get; set; } = "";
public int Level { get; set; }

public ArenaParticipant()
public ArenaParticipantStruct()
{
}

public ArenaParticipant(
public ArenaParticipantStruct(
Address avatarAddr,
int score,
int rank,
Expand Down
20 changes: 10 additions & 10 deletions ArenaService/ArenaService/ArenaParticipantType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,44 @@

namespace ArenaService;

public class ArenaParticipantType : ObjectGraphType<ArenaParticipant>
public class ArenaParticipantType : ObjectGraphType<ArenaParticipantStruct>
{
public ArenaParticipantType()
{
Field<NonNullGraphType<AddressType>>(
nameof(ArenaParticipant.AvatarAddr),
nameof(ArenaParticipantStruct.AvatarAddr),
description: "Address of avatar.",
resolve: context => context.Source.AvatarAddr);
Field<NonNullGraphType<IntGraphType>>(
nameof(ArenaParticipant.Score),
nameof(ArenaParticipantStruct.Score),
description: "Arena score of avatar.",
resolve: context => context.Source.Score);
Field<NonNullGraphType<IntGraphType>>(
nameof(ArenaParticipant.Rank),
nameof(ArenaParticipantStruct.Rank),
description: "Arena rank of avatar.",
resolve: context => context.Source.Rank);
Field<NonNullGraphType<IntGraphType>>(
nameof(ArenaParticipant.WinScore),
nameof(ArenaParticipantStruct.WinScore),
description: "Score for victory.",
resolve: context => context.Source.WinScore);
Field<NonNullGraphType<IntGraphType>>(
nameof(ArenaParticipant.LoseScore),
nameof(ArenaParticipantStruct.LoseScore),
description: "Score for defeat.",
resolve: context => context.Source.LoseScore);
Field<NonNullGraphType<IntGraphType>>(
nameof(ArenaParticipant.Cp),
nameof(ArenaParticipantStruct.Cp),
description: "Cp of avatar.",
resolve: context => context.Source.Cp);
Field<NonNullGraphType<IntGraphType>>(
nameof(ArenaParticipant.PortraitId),
nameof(ArenaParticipantStruct.PortraitId),
description: "Portrait icon id.",
resolve: context => context.Source.PortraitId);
Field<NonNullGraphType<IntGraphType>>(
nameof(ArenaParticipant.Level),
nameof(ArenaParticipantStruct.Level),
description: "Level of avatar.",
resolve: context => context.Source.Level);
Field<NonNullGraphType<StringGraphType>>(
nameof(ArenaParticipant.NameWithHash),
nameof(ArenaParticipantStruct.NameWithHash),
description: "Name of avatar.",
resolve: context => context.Source.NameWithHash);
}
Expand Down
6 changes: 4 additions & 2 deletions ArenaService/ArenaService/ArenaWorker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,16 @@ public async Task PrepareArenaParticipants()
var blockIndex = tip.Index;
var currentRoundData = await _rpcClient.GetRoundData(tip, cancellationToken);
var participants = await _rpcClient.GetArenaParticipantsState(tip, currentRoundData, cancellationToken);
var championshipId = currentRoundData.ChampionshipId;
var round = currentRoundData.Round;
var cacheKey = $"{currentRoundData.ChampionshipId}_{currentRoundData.Round}";
var scoreCacheKey = $"{cacheKey}_scores";
var prevAddrAndScores = await _service.GetAvatarAddrAndScores(scoreCacheKey);
var prevArenaParticipants = await _service.GetArenaParticipantsAsync(cacheKey);
var expiry = TimeSpan.FromMinutes(5);
if (participants is null)
{
await _service.SetArenaParticipantsAsync(cacheKey, new List<ArenaParticipant>(), expiry);
await _service.SetArenaParticipantsAsync(cacheKey, new List<ArenaParticipantStruct>(), expiry);
_logger.LogInformation("[ArenaParticipantsWorker] participants({CacheKey}) is null. set empty list on {BlockIndex}", cacheKey, blockIndex);
return;
}
Expand All @@ -82,7 +84,7 @@ public async Task PrepareArenaParticipants()
// 전체목록의 랭킹 순서 처리
var avatarAddrAndScoresWithRank = AvatarAddrAndScoresWithRank(avatarAddrAndScores);
// 전체목록의 ArenaParticipant 업데이트
var result = await _rpcClient.GetArenaParticipants(tip, updatedAddressAndScores.Select(i => i.AvatarAddr).ToList(), avatarAddrAndScoresWithRank, prevArenaParticipants, cancellationToken);
var result = await _rpcClient.GetArenaParticipants(tip, championshipId, round, updatedAddressAndScores.Select(i => i.AvatarAddr).ToList(), avatarAddrAndScoresWithRank, prevArenaParticipants, cancellationToken);
// 캐시 업데이트
await _service.SetArenaParticipantsAsync(cacheKey, result, expiry);
await _service.SetSeasonAsync(cacheKey, expiry);
Expand Down
4 changes: 2 additions & 2 deletions ArenaService/ArenaService/IRedisArenaParticipantsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ namespace ArenaService;

public interface IRedisArenaParticipantsService
{
Task<List<ArenaParticipant>> GetArenaParticipantsAsync(string key);
Task SetArenaParticipantsAsync(string key, List<ArenaParticipant> value, TimeSpan? expiry = null);
Task<List<ArenaParticipantStruct>> GetArenaParticipantsAsync(string key);
Task SetArenaParticipantsAsync(string key, List<ArenaParticipantStruct> value, TimeSpan? expiry = null);
Task<string> GetSeasonKeyAsync();
Task SetSeasonAsync(string value, TimeSpan? expiry = null);
Task<List<AvatarAddressAndScore>> GetAvatarAddrAndScores(string key);
Expand Down
8 changes: 4 additions & 4 deletions ArenaService/ArenaService/RedisArenaParticipantsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@ public class RedisArenaParticipantsService(IConnectionMultiplexer redis)

private readonly IDatabase _db = redis.GetDatabase();

public async Task<List<ArenaParticipant>> GetArenaParticipantsAsync(string key)
public async Task<List<ArenaParticipantStruct>> GetArenaParticipantsAsync(string key)
{
RedisValue result = await _db.StringGetAsync(key);
if (result.IsNull)
{
return new List<ArenaParticipant>();
return new List<ArenaParticipantStruct>();
}

return JsonSerializer.Deserialize<List<ArenaParticipant>>(result.ToString())!;
return JsonSerializer.Deserialize<List<ArenaParticipantStruct>>(result.ToString())!;
}

public async Task SetArenaParticipantsAsync(string key, List<ArenaParticipant> value, TimeSpan? expiry = null)
public async Task SetArenaParticipantsAsync(string key, List<ArenaParticipantStruct> value, TimeSpan? expiry = null)
{
var serialized = JsonSerializer.Serialize(value);
await _db.StringSetAsync(key, serialized, expiry);
Expand Down
Loading

0 comments on commit ac08860

Please sign in to comment.