Skip to content

Commit

Permalink
Use cached sheet
Browse files Browse the repository at this point in the history
  • Loading branch information
ipdae committed Jun 14, 2024
1 parent ef3052f commit ca44d30
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 9 deletions.
9 changes: 8 additions & 1 deletion NineChronicles.Headless.Tests/ArenaParticipantsWorkerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using Libplanet.Action.State;
using Libplanet.Crypto;
using Libplanet.Mocks;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Options;
using Nekoyume;
using Nekoyume.Action;
using Nekoyume.Model.Arena;
Expand All @@ -13,6 +15,7 @@
using Nekoyume.Model.State;
using Nekoyume.Module;
using Nekoyume.TableData;
using Nekoyume.TableData.Rune;
using Xunit;
using Random = Libplanet.Extensions.ActionEvaluatorCommonComponents.Random;

Expand Down Expand Up @@ -183,7 +186,11 @@ public void GetArenaParticipants()
state = state.SetLegacyState(Addresses.GetSheetAddress(key), s.Serialize());
}
var avatarAddrAndScoresWithRank = ArenaParticipantsWorker.AvatarAddrAndScoresWithRank(participants.AvatarAddresses, currentRoundData, state);
var actual = ArenaParticipantsWorker.GetArenaParticipants(state, participants.AvatarAddresses, avatarAddrAndScoresWithRank);
var cache = new MemoryCache(new OptionsWrapper<MemoryCacheOptions>(new MemoryCacheOptions
{
SizeLimit = null
}));
var actual = ArenaParticipantsWorker.GetArenaParticipants(state, participants.AvatarAddresses, avatarAddrAndScoresWithRank, cache.GetSheet<RuneListSheet>(state), cache.GetSheet<CostumeStatSheet>(state), cache.GetSheet<CharacterSheet>(state), cache.GetSheet<RuneOptionSheet>(state));
Assert.Equal(2, actual.Count);
var first = actual.First();
Assert.Equal(avatarAddress, first.AvatarAddr);
Expand Down
22 changes: 22 additions & 0 deletions NineChronicles.Headless.Tests/MemoryCacheExtensionsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@
using System.Threading.Tasks;
using Bencodex;
using Bencodex.Types;
using Libplanet.Action.State;
using Libplanet.Mocks;
using MessagePack;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Options;
using Nekoyume;
using Nekoyume.Module;
using Nekoyume.TableData;
using Xunit;

Expand Down Expand Up @@ -36,4 +39,23 @@ public async Task Sheet()
await Task.Delay(100);
Assert.False(cache.TryGetValue(cacheKey, out byte[] _));
}

[Fact]
public void GetSheet_With_Type()
{
var cache = new MemoryCache(new OptionsWrapper<MemoryCacheOptions>(new MemoryCacheOptions
{
SizeLimit = null
}));

var sheets = TableSheetsImporter.ImportSheets();
var tableName = nameof(ItemRequirementSheet);
var csv = sheets[tableName];
var sheetAddress = Addresses.GetSheetAddress(tableName);
var value = (Text)csv;
var state = (IWorld) new World(MockWorldState.CreateModern());
state = state.SetLegacyState(sheetAddress, value);
var cachedSheet = cache.GetSheet<ItemRequirementSheet>(state);
Assert.Equal(value, cachedSheet.Serialize());
}
}
28 changes: 20 additions & 8 deletions NineChronicles.Headless/ArenaParticipantsWorker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,14 +193,21 @@ public static ArenaSheet.RoundData GetRoundData(IWorldState worldState, long blo
/// <param name="worldState">The world state from which to retrieve the arena participants.</param>
/// <param name="avatarAddrList">The list of avatar addresses to filter the matching participants.</param>
/// <param name="avatarAddrAndScoresWithRank">The list of avatar addresses with their scores and ranks.</param>
/// <param name="runeListSheet"></param>
/// <param name="costumeStatSheet"></param>
/// <param name="characterSheet"></param>
/// <param name="runeOptionSheet"></param>
/// <returns>A list of arena participants.</returns>
public static List<ArenaParticipant> GetArenaParticipants(IWorldState worldState, List<Address> avatarAddrList, List<(Address avatarAddr, int score, int rank)> avatarAddrAndScoresWithRank)
public static List<ArenaParticipant> GetArenaParticipants(
IWorldState worldState,
List<Address> avatarAddrList,
List<(Address avatarAddr, int score, int rank)> avatarAddrAndScoresWithRank,
RuneListSheet runeListSheet,
CostumeStatSheet costumeStatSheet,
CharacterSheet characterSheet,
RuneOptionSheet runeOptionSheet
)
{
var runeListSheet = worldState.GetSheet<RuneListSheet>();
var costumeSheet = worldState.GetSheet<CostumeStatSheet>();
var characterSheet = worldState.GetSheet<CharacterSheet>();
var runeOptionSheet = worldState.GetSheet<RuneOptionSheet>();
var runeIds = runeListSheet.Values.Select(x => x.Id).ToList();
var row = characterSheet[GameConfig.DefaultAvatarCharacterId];
CollectionSheet collectionSheet = new CollectionSheet();
var collectionStates = worldState.GetCollectionStates(avatarAddrList);
Expand Down Expand Up @@ -265,7 +272,7 @@ List runeSlotList
}
}

var cp = CPHelper.TotalCP(equipments, costumes, runeOptions, avatar.level, row, costumeSheet, collectionModifiers,
var cp = CPHelper.TotalCP(equipments, costumes, runeOptions, avatar.level, row, costumeStatSheet, collectionModifiers,
RuneHelper.CalculateRuneLevelBonus(runeStates, runeListSheet, worldState.GetSheet<RuneLevelBonusSheet>())
);
var portraitId = StateQuery.GetPortraitId(equipments, costumes);
Expand Down Expand Up @@ -315,7 +322,12 @@ public void PrepareArenaParticipants()

var avatarAddrList = participants.AvatarAddresses;
var avatarAddrAndScoresWithRank = AvatarAddrAndScoresWithRank(avatarAddrList, currentRoundData, worldState);
var result = GetArenaParticipants(worldState, avatarAddrList, avatarAddrAndScoresWithRank);
var sheetCache = _cache.SheetCache;
var runeListSheet = sheetCache.GetSheet<RuneListSheet>(worldState);
var costumeStatSheet = sheetCache.GetSheet<CostumeStatSheet>(worldState);
var characterSheet = sheetCache.GetSheet<CharacterSheet>(worldState);
var runeOptionSheet = sheetCache.GetSheet<RuneOptionSheet>(worldState);
var result = GetArenaParticipants(worldState, avatarAddrList, avatarAddrAndScoresWithRank, runeListSheet, costumeStatSheet, characterSheet, runeOptionSheet);
_cache.ArenaParticipantsCache.Set(cacheKey, result, TimeSpan.FromHours(1));
sw.Stop();
_logger.Information("[ArenaParticipantsWorker]Set Arena Cache[{CacheKey}]: {Elapsed}", cacheKey, sw.Elapsed);
Expand Down
28 changes: 28 additions & 0 deletions NineChronicles.Headless/MemoryCacheExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
using System;
using Bencodex;
using Bencodex.Types;
using Libplanet.Action.State;
using MessagePack;
using Microsoft.Extensions.Caching.Memory;
using Nekoyume;
using Nekoyume.Module;
using Nekoyume.TableData;

namespace NineChronicles.Headless;

Expand Down Expand Up @@ -32,4 +36,28 @@ public static bool TryGetSheet<T>(this MemoryCache cache, string cacheKey, out T

return null;
}

public static T GetSheet<T>(this MemoryCache cache, IWorldState worldState) where T : ISheet, new()
{
var cacheKey = Addresses.GetSheetAddress<T>().ToString();
var sheet = new T();
var csv = string.Empty;
if (cache.GetSheet(cacheKey) is { } s)
{
csv = s;
}
else
{
IValue value = Null.Value;
if (worldState.GetSheetCsv<T>() is { } s2)
{
csv = s2;
value = (Text)csv;
}
cache.SetSheet(cacheKey, value, TimeSpan.FromMinutes(1));
}

sheet.Set(csv);
return sheet;
}
}

0 comments on commit ca44d30

Please sign in to comment.