Skip to content

Commit

Permalink
Call get state updated avatar only
Browse files Browse the repository at this point in the history
  • Loading branch information
ipdae committed Sep 11, 2024
1 parent 0c192cc commit bc934d6
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 17 deletions.
7 changes: 4 additions & 3 deletions ArenaService/ArenaService/ArenaParticipant.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ public ArenaParticipant(
Address avatarAddr,
int score,
int rank,
AvatarState avatarState,
string nameWithHash,
int level,
int portraitId,
int winScore,
int loseScore,
Expand All @@ -36,8 +37,8 @@ public ArenaParticipant(
LoseScore = loseScore;
Cp = cp;
PortraitId = portraitId;
NameWithHash = avatarState.NameWithHash;
Level = avatarState.level;
NameWithHash = nameWithHash;
Level = level;
}

public void Update(int winScore, int loseScore)
Expand Down
20 changes: 12 additions & 8 deletions ArenaService/ArenaService/ArenaWorker.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
using System.Diagnostics;
using Bencodex.Types;
using Libplanet.Action.State;
using Libplanet.Crypto;
using Nekoyume.Model.Arena;
using Nekoyume.Module;
using Nekoyume.TableData;

namespace ArenaService;

Expand Down Expand Up @@ -66,6 +60,9 @@ public async Task PrepareArenaParticipants()
var currentRoundData = await _rpcClient.GetRoundData(tip);
var participants = await _rpcClient.GetArenaParticipantsState(tip, currentRoundData);
var cacheKey = $"{currentRoundData.ChampionshipId}_{currentRoundData.Round}";
var scoreCacheKey = $"{cacheKey}_score";
var prevAddrAndScores = await _service.GetAvatarAddrAndScores(scoreCacheKey);
var prevArenaParticipants = await _service.GetArenaParticipantsAsync(cacheKey);
var expiry = TimeSpan.FromMinutes(5);
if (participants is null)
{
Expand All @@ -75,10 +72,17 @@ public async Task PrepareArenaParticipants()
}

var avatarAddrList = participants.AvatarAddresses;
var avatarAddrAndScoresWithRank = await _rpcClient.AvatarAddrAndScoresWithRank(tip, avatarAddrList, currentRoundData);
var result = await _rpcClient.GetArenaParticipants(tip, avatarAddrList, avatarAddrAndScoresWithRank);
// 최신상태의 아바타 주소, 점수를 조회
var avatarAddrAndScores = await _rpcClient.GetAvatarAddrAndScores(tip, avatarAddrList, currentRoundData);
// 이전상태의 아바타 주소, 점수를 비교해서 추가되거나 점수가 변경된 대상만 찾음
var filtered = avatarAddrAndScores.Except(prevAddrAndScores).ToList();
// 전체목록의 랭킹 순서 처리
var avatarAddrAndScoresWithRank = _rpcClient.AvatarAddrAndScoresWithRank(avatarAddrAndScores);
// 전체목록의 ArenaParticipant 업데이트
var result = await _rpcClient.GetArenaParticipants(tip, filtered.Select(i => i.avatarAddr).ToList(), avatarAddrAndScoresWithRank, prevArenaParticipants);
await _service.SetArenaParticipantsAsync(cacheKey, result, expiry);
await _service.SetSeasonAsync(cacheKey, expiry);
await _service.SetAvatarAddrAndScores(scoreCacheKey, avatarAddrAndScores);
sw.Stop();
_logger.LogInformation("[ArenaParticipantsWorker]Set Arena Cache[{CacheKey}]: {Elapsed}", cacheKey, sw.Elapsed);
}
Expand Down
4 changes: 2 additions & 2 deletions ArenaService/ArenaService/IRedisArenaParticipantsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ public interface IRedisArenaParticipantsService
Task SetArenaParticipantsAsync(string key, List<ArenaParticipant> value, TimeSpan? expiry = null);
Task<string> GetSeasonKeyAsync();
Task SetSeasonAsync(string value, TimeSpan? expiry = null);
Task<List<ArenaScoreAndRank>> GetAvatarAddrAndScoresWithRank(string key);
Task SetAvatarAddrAndScoresWithRank(string key, List<ArenaScoreAndRank> value, TimeSpan? expiry = null);
Task<List<(Address avatarAddr, int score)>> GetAvatarAddrAndScores(string key);
Task SetAvatarAddrAndScores(string key, List<(Address avatarAddr, int score)> value, TimeSpan? expiry = null);
}
9 changes: 5 additions & 4 deletions ArenaService/ArenaService/RedisArenaParticipantsService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text.Json;
using Libplanet.Crypto;
using StackExchange.Redis;

namespace ArenaService;
Expand Down Expand Up @@ -43,18 +44,18 @@ public async Task SetSeasonAsync(string value, TimeSpan? expiry = null)
await _db.StringSetAsync(SeasonKey, value, expiry);
}

public async Task<List<ArenaScoreAndRank>> GetAvatarAddrAndScoresWithRank(string key)
public async Task<List<(Address avatarAddr, int score)>> GetAvatarAddrAndScores(string key)
{
RedisValue result = await _db.StringGetAsync(key);
if (result.IsNull)
{
return new List<ArenaScoreAndRank>();
return new List<(Address avatarAddr, int score)>();
}

return JsonSerializer.Deserialize<List<ArenaScoreAndRank>>(result.ToString())!;
return JsonSerializer.Deserialize<List<(Address avatarAddr, int score)>>(result.ToString())!;
}

public async Task SetAvatarAddrAndScoresWithRank(string key, List<ArenaScoreAndRank> value, TimeSpan? expiry = null)
public async Task SetAvatarAddrAndScores(string key, List<(Address avatarAddr, int score)> value, TimeSpan? expiry = null)
{
var serialized = JsonSerializer.Serialize(value);
await _db.StringSetAsync(key, serialized, expiry);
Expand Down

0 comments on commit bc934d6

Please sign in to comment.