mirror of
https://gitlab.com/Kwoth/nadekobot.git
synced 2025-09-11 09:48:26 -04:00
Fixed .trivia --pokemon showing pokemon with id + 1
This commit is contained in:
@@ -9,5 +9,5 @@ public interface ILocalDataCache
|
||||
Task<IReadOnlyDictionary<string, SearchPokemon>> GetPokemonsAsync();
|
||||
Task<IReadOnlyDictionary<string, SearchPokemonAbility>> GetPokemonAbilitiesAsync();
|
||||
Task<TriviaQuestionModel[]> GetTriviaQuestionsAsync();
|
||||
Task<PokemonNameId[]> GetPokemonMapAsync();
|
||||
Task<IReadOnlyDictionary<int, string>> GetPokemonMapAsync();
|
||||
}
|
@@ -67,11 +67,37 @@ public sealed class LocalDataCache : ILocalDataCache, INService
|
||||
=> await GetOrCreateCachedDataAsync(_pokemonAbilitiesKey, POKEMON_ABILITIES_FILE);
|
||||
|
||||
|
||||
private static TypedKey<PokemonNameId[]> _pokeMapKey
|
||||
= new("pokemon:ab_map");
|
||||
private static TypedKey<IReadOnlyDictionary<int, string>> _pokeMapKey
|
||||
= new("pokemon:ab_map2"); // 2 because ab_map was storing arrays
|
||||
|
||||
public async Task<PokemonNameId[]?> GetPokemonMapAsync()
|
||||
=> await GetOrCreateCachedDataAsync(_pokeMapKey, POKEMON_MAP_PATH);
|
||||
public async Task<IReadOnlyDictionary<int, string>?> GetPokemonMapAsync()
|
||||
=> await _cache.GetOrAddAsync(_pokeMapKey,
|
||||
async () =>
|
||||
{
|
||||
var fileName = POKEMON_MAP_PATH;
|
||||
if (!File.Exists(fileName))
|
||||
{
|
||||
Log.Warning($"{fileName} is missing. Relevant data can't be loaded");
|
||||
return default;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
await using var stream = File.OpenRead(fileName);
|
||||
var arr = await JsonSerializer.DeserializeAsync<PokemonNameId[]>(stream, _opts);
|
||||
|
||||
return (IReadOnlyDictionary<int, string>?)arr?.ToDictionary(x => x.Id, x => x.Name);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error(ex,
|
||||
"Error reading {FileName} file: {ErrorMessage}",
|
||||
fileName,
|
||||
ex.Message);
|
||||
|
||||
return default;
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
private static TypedKey<TriviaQuestionModel[]> _triviaKey
|
||||
|
Reference in New Issue
Block a user