|
| 1 | +using System.Diagnostics; |
| 2 | +using System.Text.Json; |
| 3 | + |
| 4 | +namespace Pokédex |
| 5 | +{ |
| 6 | + public static class PokemonCache |
| 7 | + { |
| 8 | + #region Variables |
| 9 | + private readonly static string PokemonsPath = AppPath.GetFullPath(@"/Pokedex.json"); |
| 10 | + private const string PokemonsUri = "https://tmare.ndelpech.fr/tps/pokemons"; |
| 11 | + #endregion |
| 12 | + |
| 13 | + #region Methods |
| 14 | + /// <summary> |
| 15 | + /// Get the list of cached Pokémon |
| 16 | + /// </summary> |
| 17 | + /// <returns>Returns the list of Pokémon in cache or null if the cache does not exist</returns> |
| 18 | + public static Pokedex? GetPokedex() |
| 19 | + { |
| 20 | + // Check if the pokemon entries have been cached |
| 21 | + if (File.Exists(PokemonsPath)) |
| 22 | + { |
| 23 | + string pokemonsJson = File.ReadAllText(PokemonsPath); |
| 24 | + |
| 25 | + return Pokedex.ParsePokemons(pokemonsJson); |
| 26 | + } |
| 27 | + return null; |
| 28 | + } |
| 29 | + |
| 30 | + /// <summary> |
| 31 | + /// Download or update cached Pokemon list |
| 32 | + /// </summary> |
| 33 | + public static async Task RefreshCaches() |
| 34 | + { |
| 35 | + using HttpClient httpClient = new(); |
| 36 | + string pokemonEntriesJson = await httpClient.GetStringAsync(PokemonsUri); |
| 37 | + |
| 38 | + // Load the cached pokemon entries |
| 39 | + var cachedPokedex = GetPokedex(); |
| 40 | + PokemonEntry[]? pokemonEntries; |
| 41 | + |
| 42 | + try |
| 43 | + { pokemonEntries = JsonSerializer.Deserialize<PokemonEntry[]>(pokemonEntriesJson); } |
| 44 | + catch (Exception ex) |
| 45 | + { |
| 46 | + Console.WriteLine(ex.Message); |
| 47 | + Debug.WriteLine(ex); |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + if (pokemonEntries != null) |
| 52 | + { |
| 53 | + List<Pokemon> pokemonList = new(); |
| 54 | + |
| 55 | + // Asynchronously download Pokémon by generation |
| 56 | + Task<ICollection<Pokemon>>[] tasksList = new[] |
| 57 | + { |
| 58 | + UpdatePokemons(httpClient, cachedPokedex, pokemonEntries[0..150]), // Gen 1 |
| 59 | + UpdatePokemons(httpClient, cachedPokedex, pokemonEntries[151..250]), // Gen 2 |
| 60 | + UpdatePokemons(httpClient, cachedPokedex, pokemonEntries[251..385]), // Gen 3 |
| 61 | + UpdatePokemons(httpClient, cachedPokedex, pokemonEntries[386..492]), // Gen 4 |
| 62 | + UpdatePokemons(httpClient, cachedPokedex, pokemonEntries[493..648]), // Gen 5 |
| 63 | + UpdatePokemons(httpClient, cachedPokedex, pokemonEntries[649..720]), // Gen 6 |
| 64 | + UpdatePokemons(httpClient, cachedPokedex, pokemonEntries[721..801]), // Gen 7 |
| 65 | + UpdatePokemons(httpClient, cachedPokedex, pokemonEntries[802..897]), // Gen 8 |
| 66 | + }; |
| 67 | + |
| 68 | + // Wait for all downloads to complete |
| 69 | + var results = await Task.WhenAll(tasksList); |
| 70 | + |
| 71 | + // Merge all Pokémon |
| 72 | + foreach (var result in results) |
| 73 | + pokemonList.AddRange(result); |
| 74 | + |
| 75 | + // Save the updated cache is needed |
| 76 | + if (pokemonList.Count != 0) |
| 77 | + { |
| 78 | +#if DEBUG // Make the json is human readable for debugging |
| 79 | + var options = new JsonSerializerOptions { WriteIndented = true }; |
| 80 | +#else // Make the json is compact for saving space |
| 81 | + var options = new JsonSerializerOptions { WriteIndented = false }; |
| 82 | +#endif |
| 83 | + var pokemonsJson = JsonSerializer.Serialize(pokemonList, options); |
| 84 | + |
| 85 | + File.WriteAllText(PokemonsPath, pokemonsJson); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + } |
| 90 | + |
| 91 | + /// <summary> |
| 92 | + /// Download Pokémons if they doesn't exist in the cache or are expired |
| 93 | + /// </summary> |
| 94 | + /// <param name="httpClient">HttpClient</param> |
| 95 | + /// <param name="cachedPokedex">Cached Pokedex</param> |
| 96 | + /// <param name="pokemonEntries">The Pokémons to download</param> |
| 97 | + /// <returns>Returns a list of Pokémon that doesn't exist in the cache or are obsolete</returns> |
| 98 | + private static async Task<ICollection<Pokemon>> UpdatePokemons(HttpClient httpClient, Pokedex? cachedPokedex, PokemonEntry[] pokemonEntries) |
| 99 | + { |
| 100 | + List<Pokemon> pokemonList = new(); |
| 101 | + Pokemon? pokemon; |
| 102 | + |
| 103 | + // Cycle through all Pokémon entries |
| 104 | + foreach (var pokemonEntry in pokemonEntries) |
| 105 | + { |
| 106 | + // Get the updated Pokémon |
| 107 | + pokemon = await UpdatePokemon(httpClient, cachedPokedex, pokemonEntry); |
| 108 | + |
| 109 | + // Add if newer |
| 110 | + if (pokemon != null) |
| 111 | + pokemonList.Add(pokemon); |
| 112 | + } |
| 113 | + |
| 114 | + // Returns the newest Pokémon |
| 115 | + return pokemonList; |
| 116 | + } |
| 117 | + |
| 118 | + /// <summary> |
| 119 | + /// Download a Pokémon if it doesn't exist in the cache or is out-of-date |
| 120 | + /// </summary> |
| 121 | + /// <param name="httpClient">HttpClient</param> |
| 122 | + /// <param name="cachedPokedex">Cached Pokedex</param> |
| 123 | + /// <param name="pokemonEntry">The Pokémon to download</param> |
| 124 | + /// <returns>Return Pokémon if it doesn't exist in the cache or is out-of-date, otherwise returns null</returns> |
| 125 | + private static async Task<Pokemon?> UpdatePokemon(HttpClient httpClient, Pokedex? cachedPokedex, PokemonEntry pokemonEntry) |
| 126 | + { |
| 127 | + Pokemon? pokemon = null; |
| 128 | + |
| 129 | + // If the Pokémon in cache is the same on the API side, we reuse it |
| 130 | + if (cachedPokedex != null && pokemonEntry.LastEdit == cachedPokedex[pokemonEntry.Id]?.LastEdit) |
| 131 | + pokemon = cachedPokedex[pokemonEntry.Id]; |
| 132 | + // If the Pokémon is outdated we download the new one |
| 133 | + else |
| 134 | + { |
| 135 | + // Get the Pokémon |
| 136 | + string pokemonJson = await httpClient.GetStringAsync(pokemonEntry.Url); |
| 137 | + |
| 138 | + // Parse it |
| 139 | + try |
| 140 | + { pokemon = JsonSerializer.Deserialize<Pokemon>(pokemonJson); } |
| 141 | + catch (Exception ex) |
| 142 | + { Debug.WriteLine(ex); } |
| 143 | + finally { } |
| 144 | + } |
| 145 | + |
| 146 | + return pokemon; |
| 147 | + } |
| 148 | + #endregion |
| 149 | + } |
| 150 | +} |
0 commit comments