Skip to content

Commit ca44d30

Browse files
committed
Use cached sheet
1 parent ef3052f commit ca44d30

4 files changed

Lines changed: 78 additions & 9 deletions

File tree

NineChronicles.Headless.Tests/ArenaParticipantsWorkerTest.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
using Libplanet.Action.State;
66
using Libplanet.Crypto;
77
using Libplanet.Mocks;
8+
using Microsoft.Extensions.Caching.Memory;
9+
using Microsoft.Extensions.Options;
810
using Nekoyume;
911
using Nekoyume.Action;
1012
using Nekoyume.Model.Arena;
@@ -13,6 +15,7 @@
1315
using Nekoyume.Model.State;
1416
using Nekoyume.Module;
1517
using Nekoyume.TableData;
18+
using Nekoyume.TableData.Rune;
1619
using Xunit;
1720
using Random = Libplanet.Extensions.ActionEvaluatorCommonComponents.Random;
1821

@@ -183,7 +186,11 @@ public void GetArenaParticipants()
183186
state = state.SetLegacyState(Addresses.GetSheetAddress(key), s.Serialize());
184187
}
185188
var avatarAddrAndScoresWithRank = ArenaParticipantsWorker.AvatarAddrAndScoresWithRank(participants.AvatarAddresses, currentRoundData, state);
186-
var actual = ArenaParticipantsWorker.GetArenaParticipants(state, participants.AvatarAddresses, avatarAddrAndScoresWithRank);
189+
var cache = new MemoryCache(new OptionsWrapper<MemoryCacheOptions>(new MemoryCacheOptions
190+
{
191+
SizeLimit = null
192+
}));
193+
var actual = ArenaParticipantsWorker.GetArenaParticipants(state, participants.AvatarAddresses, avatarAddrAndScoresWithRank, cache.GetSheet<RuneListSheet>(state), cache.GetSheet<CostumeStatSheet>(state), cache.GetSheet<CharacterSheet>(state), cache.GetSheet<RuneOptionSheet>(state));
187194
Assert.Equal(2, actual.Count);
188195
var first = actual.First();
189196
Assert.Equal(avatarAddress, first.AvatarAddr);

NineChronicles.Headless.Tests/MemoryCacheExtensionsTest.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22
using System.Threading.Tasks;
33
using Bencodex;
44
using Bencodex.Types;
5+
using Libplanet.Action.State;
6+
using Libplanet.Mocks;
57
using MessagePack;
68
using Microsoft.Extensions.Caching.Memory;
79
using Microsoft.Extensions.Options;
810
using Nekoyume;
11+
using Nekoyume.Module;
912
using Nekoyume.TableData;
1013
using Xunit;
1114

@@ -36,4 +39,23 @@ public async Task Sheet()
3639
await Task.Delay(100);
3740
Assert.False(cache.TryGetValue(cacheKey, out byte[] _));
3841
}
42+
43+
[Fact]
44+
public void GetSheet_With_Type()
45+
{
46+
var cache = new MemoryCache(new OptionsWrapper<MemoryCacheOptions>(new MemoryCacheOptions
47+
{
48+
SizeLimit = null
49+
}));
50+
51+
var sheets = TableSheetsImporter.ImportSheets();
52+
var tableName = nameof(ItemRequirementSheet);
53+
var csv = sheets[tableName];
54+
var sheetAddress = Addresses.GetSheetAddress(tableName);
55+
var value = (Text)csv;
56+
var state = (IWorld) new World(MockWorldState.CreateModern());
57+
state = state.SetLegacyState(sheetAddress, value);
58+
var cachedSheet = cache.GetSheet<ItemRequirementSheet>(state);
59+
Assert.Equal(value, cachedSheet.Serialize());
60+
}
3961
}

NineChronicles.Headless/ArenaParticipantsWorker.cs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -193,14 +193,21 @@ public static ArenaSheet.RoundData GetRoundData(IWorldState worldState, long blo
193193
/// <param name="worldState">The world state from which to retrieve the arena participants.</param>
194194
/// <param name="avatarAddrList">The list of avatar addresses to filter the matching participants.</param>
195195
/// <param name="avatarAddrAndScoresWithRank">The list of avatar addresses with their scores and ranks.</param>
196+
/// <param name="runeListSheet"></param>
197+
/// <param name="costumeStatSheet"></param>
198+
/// <param name="characterSheet"></param>
199+
/// <param name="runeOptionSheet"></param>
196200
/// <returns>A list of arena participants.</returns>
197-
public static List<ArenaParticipant> GetArenaParticipants(IWorldState worldState, List<Address> avatarAddrList, List<(Address avatarAddr, int score, int rank)> avatarAddrAndScoresWithRank)
201+
public static List<ArenaParticipant> GetArenaParticipants(
202+
IWorldState worldState,
203+
List<Address> avatarAddrList,
204+
List<(Address avatarAddr, int score, int rank)> avatarAddrAndScoresWithRank,
205+
RuneListSheet runeListSheet,
206+
CostumeStatSheet costumeStatSheet,
207+
CharacterSheet characterSheet,
208+
RuneOptionSheet runeOptionSheet
209+
)
198210
{
199-
var runeListSheet = worldState.GetSheet<RuneListSheet>();
200-
var costumeSheet = worldState.GetSheet<CostumeStatSheet>();
201-
var characterSheet = worldState.GetSheet<CharacterSheet>();
202-
var runeOptionSheet = worldState.GetSheet<RuneOptionSheet>();
203-
var runeIds = runeListSheet.Values.Select(x => x.Id).ToList();
204211
var row = characterSheet[GameConfig.DefaultAvatarCharacterId];
205212
CollectionSheet collectionSheet = new CollectionSheet();
206213
var collectionStates = worldState.GetCollectionStates(avatarAddrList);
@@ -265,7 +272,7 @@ List runeSlotList
265272
}
266273
}
267274

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

316323
var avatarAddrList = participants.AvatarAddresses;
317324
var avatarAddrAndScoresWithRank = AvatarAddrAndScoresWithRank(avatarAddrList, currentRoundData, worldState);
318-
var result = GetArenaParticipants(worldState, avatarAddrList, avatarAddrAndScoresWithRank);
325+
var sheetCache = _cache.SheetCache;
326+
var runeListSheet = sheetCache.GetSheet<RuneListSheet>(worldState);
327+
var costumeStatSheet = sheetCache.GetSheet<CostumeStatSheet>(worldState);
328+
var characterSheet = sheetCache.GetSheet<CharacterSheet>(worldState);
329+
var runeOptionSheet = sheetCache.GetSheet<RuneOptionSheet>(worldState);
330+
var result = GetArenaParticipants(worldState, avatarAddrList, avatarAddrAndScoresWithRank, runeListSheet, costumeStatSheet, characterSheet, runeOptionSheet);
319331
_cache.ArenaParticipantsCache.Set(cacheKey, result, TimeSpan.FromHours(1));
320332
sw.Stop();
321333
_logger.Information("[ArenaParticipantsWorker]Set Arena Cache[{CacheKey}]: {Elapsed}", cacheKey, sw.Elapsed);

NineChronicles.Headless/MemoryCacheExtensions.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
using System;
22
using Bencodex;
33
using Bencodex.Types;
4+
using Libplanet.Action.State;
45
using MessagePack;
56
using Microsoft.Extensions.Caching.Memory;
7+
using Nekoyume;
8+
using Nekoyume.Module;
9+
using Nekoyume.TableData;
610

711
namespace NineChronicles.Headless;
812

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

3337
return null;
3438
}
39+
40+
public static T GetSheet<T>(this MemoryCache cache, IWorldState worldState) where T : ISheet, new()
41+
{
42+
var cacheKey = Addresses.GetSheetAddress<T>().ToString();
43+
var sheet = new T();
44+
var csv = string.Empty;
45+
if (cache.GetSheet(cacheKey) is { } s)
46+
{
47+
csv = s;
48+
}
49+
else
50+
{
51+
IValue value = Null.Value;
52+
if (worldState.GetSheetCsv<T>() is { } s2)
53+
{
54+
csv = s2;
55+
value = (Text)csv;
56+
}
57+
cache.SetSheet(cacheKey, value, TimeSpan.FromMinutes(1));
58+
}
59+
60+
sheet.Set(csv);
61+
return sheet;
62+
}
3563
}

0 commit comments

Comments
 (0)