v2 (WPF)
This commit is contained in:
@@ -1,15 +1,11 @@
|
||||
using System.Net.Http.Json;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using System.Text.RegularExpressions;
|
||||
using CliWrap;
|
||||
using CliWrap.Buffered;
|
||||
using LeagueAPI.Models.Challenges;
|
||||
using LeagueAPI.Models.ChampSelect;
|
||||
using LeagueAPI.Models.DDragon;
|
||||
using LeagueAPI.Models.DDragon.Champions;
|
||||
using LeagueAPI.Utils;
|
||||
using static System.Runtime.InteropServices.JavaScript.JSType;
|
||||
|
||||
namespace LeagueAPI;
|
||||
|
||||
@@ -23,7 +19,6 @@ public class APIClient : IDisposable
|
||||
private bool _isDisposed;
|
||||
|
||||
private readonly LcuHttpClient _lcuHttpClient;
|
||||
private readonly HttpClient _dDragonHttpClient;
|
||||
|
||||
private string? _latestVersion;
|
||||
|
||||
@@ -32,7 +27,6 @@ public class APIClient : IDisposable
|
||||
public APIClient()
|
||||
{
|
||||
_lcuHttpClient = new(new LcuHttpClientHandler());
|
||||
_dDragonHttpClient = new HttpClient() { BaseAddress = new Uri(DDRAGON_BASE_URL) };
|
||||
}
|
||||
|
||||
public async Task<Dictionary<string, LolChallengesUIChallenge>> GetAllChallengesAsync()
|
||||
@@ -51,23 +45,23 @@ public class APIClient : IDisposable
|
||||
return allRandomAllChampions.CompletedIds ?? [];
|
||||
}
|
||||
|
||||
public async Task<int[]> GetSelectableChampionIdsAsync()
|
||||
public async Task<(IEnumerable<int> teamChampions, IEnumerable<int> benchChampions)> GetSelectableChampionIdsAsync()
|
||||
{
|
||||
ChampSelectSession? session = await _lcuHttpClient.GetContentAsync<ChampSelectSession>("/lol-champ-select/v1/session");
|
||||
return GetSelectableChampionIds(session);
|
||||
}
|
||||
|
||||
public int[] GetSelectableChampionIds(ChampSelectSession? session)
|
||||
public static (IEnumerable<int> teamChampions, IEnumerable<int> benchChampions) GetSelectableChampionIds(ChampSelectSession? session)
|
||||
{
|
||||
if (session is null || !session.BenchEnabled)
|
||||
if (session is null || !session.BenchEnabled || string.IsNullOrEmpty(session.Id))
|
||||
{
|
||||
return [];
|
||||
return ([], []);
|
||||
}
|
||||
|
||||
IEnumerable<int> benchChampions = session.BenchChampions.Select(b => b.ChampionId);
|
||||
IEnumerable<int> teamChampions = session.MyTeam.Select(c => c.ChampionId);
|
||||
|
||||
return [.. benchChampions, .. teamChampions];
|
||||
return (teamChampions, benchChampions);
|
||||
}
|
||||
|
||||
private record struct LobbyChangeGame([property: JsonPropertyName("queueId")] int QueueId);
|
||||
@@ -87,20 +81,12 @@ public class APIClient : IDisposable
|
||||
}
|
||||
|
||||
#region DDragon
|
||||
private async Task<string> DDragonGetAsync(string path)
|
||||
private static async Task<string> DDragonGetAsync(string path)
|
||||
{
|
||||
HttpResponseMessage response = await _dDragonHttpClient.GetAsync(path);
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
Command cmd = Cli.Wrap("curl")
|
||||
.WithArguments($"{DDRAGON_BASE_URL}{path}");
|
||||
BufferedCommandResult result = await cmd.ExecuteBufferedAsync();
|
||||
return result.IsSuccess ? result.StandardOutput : throw new Exception($"Failed to fetch from Datadragon: {path}");
|
||||
}
|
||||
return await response.Content.ReadAsStringAsync();
|
||||
return await CachingHttpClient.GetStringAsync($"{DDRAGON_BASE_URL}{path}");
|
||||
}
|
||||
|
||||
private async Task<T?> DDragonGetAsync<T>(string path)
|
||||
private static async Task<T?> DDragonGetAsync<T>(string path)
|
||||
{
|
||||
string json = await DDragonGetAsync(path);
|
||||
return JsonSerializer.Deserialize<T>(json);
|
||||
@@ -159,7 +145,6 @@ public class APIClient : IDisposable
|
||||
if (disposing)
|
||||
{
|
||||
_lcuHttpClient?.Dispose();
|
||||
_dDragonHttpClient?.Dispose();
|
||||
}
|
||||
_championResponseCache?.Clear();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user