Show champs ingame
This commit is contained in:
@@ -9,7 +9,9 @@ using LeagueAPI;
|
||||
using LeagueAPI.ARAM;
|
||||
using LeagueAPI.Models.ChampSelect;
|
||||
using LeagueAPI.Models.DDragon.Champions;
|
||||
using LeagueAPI.Models.GameClient;
|
||||
using LeagueAPI.Models.ReadyCheck;
|
||||
using LeagueAPI.Utils;
|
||||
|
||||
namespace ARAMUtility.ViewModel;
|
||||
|
||||
@@ -46,7 +48,7 @@ public partial class MainViewModel : ObservableObject, IDisposable
|
||||
[ObservableProperty, NotifyPropertyChangedFor(nameof(ReconnectButtonVisibility))]
|
||||
public partial ConnectionStatus Status { get; private set; } = ConnectionStatus.Disconnected;
|
||||
public Visibility ReconnectButtonVisibility => Status == ConnectionStatus.Disconnected ? Visibility.Visible : Visibility.Collapsed;
|
||||
|
||||
|
||||
[ObservableProperty]
|
||||
public partial bool AutoAccept { get; set; } = true;
|
||||
|
||||
@@ -56,6 +58,8 @@ public partial class MainViewModel : ObservableObject, IDisposable
|
||||
private readonly LcuWebsocket _lcuWebsocket = new();
|
||||
private Task _lcuWebsocketTask;
|
||||
private readonly ARAMBalanceService _aramBalanceService = new();
|
||||
private readonly ProcessWatcher _leagueWatcher = new();
|
||||
private readonly CachingHttpClient _localCachingClient = new(insecure: true);
|
||||
|
||||
public MainViewModel()
|
||||
{
|
||||
@@ -71,6 +75,8 @@ public partial class MainViewModel : ObservableObject, IDisposable
|
||||
};
|
||||
#endif
|
||||
_lcuWebsocketTask = _lcuWebsocket.Connect();
|
||||
|
||||
_leagueWatcher.LeagueOfLegendsExeFound += UpdateChampionListsFromGame;
|
||||
}
|
||||
|
||||
internal async void OnInit(object? sender, RoutedEventArgs e)
|
||||
@@ -152,14 +158,17 @@ public partial class MainViewModel : ObservableObject, IDisposable
|
||||
private async Task FillChampionLists()
|
||||
{
|
||||
string defaultImagePath = await ResourceService.GetChampionIconPathAsync(-1);
|
||||
while (TeamChampions.Count < TEAM_CHAMPIONS_MAX)
|
||||
Dispatcher.Invoke(() =>
|
||||
{
|
||||
TeamChampions.Add(GetEmptyChampionViewModel());
|
||||
}
|
||||
while (BenchChampions.Count < BENCH_CHAMPIONS_MAX)
|
||||
{
|
||||
BenchChampions.Add(GetEmptyChampionViewModel());
|
||||
}
|
||||
while (TeamChampions.Count < TEAM_CHAMPIONS_MAX)
|
||||
{
|
||||
TeamChampions.Add(GetEmptyChampionViewModel());
|
||||
}
|
||||
while (BenchChampions.Count < BENCH_CHAMPIONS_MAX)
|
||||
{
|
||||
BenchChampions.Add(GetEmptyChampionViewModel());
|
||||
}
|
||||
});
|
||||
|
||||
ChampionViewModel GetEmptyChampionViewModel()
|
||||
{
|
||||
@@ -185,29 +194,74 @@ public partial class MainViewModel : ObservableObject, IDisposable
|
||||
|
||||
await FillChampionLists();
|
||||
_championUpdateSemaphore.Release();
|
||||
}
|
||||
|
||||
async Task UpdateChampions(ObservableCollection<ChampionViewModel> viewModel, IEnumerable<int> championIds)
|
||||
private async Task UpdateChampions(ObservableCollection<ChampionViewModel> viewModel, IEnumerable<int> championIds)
|
||||
{
|
||||
try
|
||||
{
|
||||
viewModel.Clear();
|
||||
await _aramBalanceService.EnsureIsLoadedAsync();
|
||||
|
||||
foreach (int championId in championIds)
|
||||
Dispatcher.Invoke(() => viewModel.Clear());
|
||||
}
|
||||
catch (NotSupportedException)
|
||||
{
|
||||
Dispatcher.Invoke(() =>
|
||||
{
|
||||
ChampionData? championData = await _client.GetChampionByIdAsync(championId);
|
||||
if (championData is null || championData.Name is null)
|
||||
while (viewModel.Count > 0)
|
||||
{
|
||||
continue;
|
||||
viewModel.RemoveAt(0);
|
||||
}
|
||||
});
|
||||
}
|
||||
await _aramBalanceService.EnsureIsLoadedAsync();
|
||||
|
||||
string imagePath = await ResourceService.GetChampionIconPathAsync(championId);
|
||||
ChampionViewModel vm = new(championData with { AramBalance = _aramBalanceService.GetAramChampion(championData.Name) })
|
||||
{
|
||||
IsNeededForChallenge = _needChampionIds.Contains(championData.Id),
|
||||
ImagePath = imagePath,
|
||||
};
|
||||
viewModel.Add(vm);
|
||||
foreach (int championId in championIds)
|
||||
{
|
||||
ChampionData? championData = await _client.GetChampionByIdAsync(championId);
|
||||
if (championData is null || championData.Name is null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
string imagePath = await ResourceService.GetChampionIconPathAsync(championId);
|
||||
ChampionViewModel vm = new(championData with { AramBalance = _aramBalanceService.GetAramChampion(championData.Name) })
|
||||
{
|
||||
IsNeededForChallenge = _needChampionIds.Contains(championData.Id),
|
||||
ImagePath = imagePath,
|
||||
};
|
||||
Dispatcher.Invoke(() => { viewModel.Add(vm); });
|
||||
}
|
||||
}
|
||||
|
||||
private async void UpdateChampionListsFromGame(object? sender, EventArgs e)
|
||||
{
|
||||
await Task.Delay(TimeSpan.FromSeconds(5));
|
||||
string playerlistJson = await _localCachingClient.GetStringAsync("https://127.0.0.1:2999/liveclientdata/playerlist", TimeSpan.FromSeconds(30));
|
||||
PlayerResponse[]? allPlayerResponse = JsonSerializer.Deserialize<PlayerResponse[]>(playerlistJson);
|
||||
if (allPlayerResponse is not { Length: > 0 })
|
||||
{
|
||||
return;
|
||||
}
|
||||
List<int> blueTeam = [];
|
||||
List<int> redTeam = [];
|
||||
foreach (PlayerResponse player in allPlayerResponse)
|
||||
{
|
||||
if (player.Team != TeamId.ORDER && player.Team != TeamId.CHAOS)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
int championId = await _client.GetChampionIdByNameAsync(player.ChampionName);
|
||||
if (player.Team == TeamId.ORDER)
|
||||
{
|
||||
blueTeam.Add(championId);
|
||||
}
|
||||
if (player.Team == TeamId.CHAOS)
|
||||
{
|
||||
redTeam.Add(championId);
|
||||
}
|
||||
}
|
||||
await UpdateChampions(TeamChampions, blueTeam);
|
||||
await UpdateChampions(BenchChampions, redTeam);
|
||||
await FillChampionLists();
|
||||
}
|
||||
|
||||
[RelayCommand(AllowConcurrentExecutions = false)]
|
||||
@@ -253,8 +307,9 @@ public partial class MainViewModel : ObservableObject, IDisposable
|
||||
if (disposing)
|
||||
{
|
||||
_allChampions = [];
|
||||
_client.Dispose();
|
||||
_lcuWebsocket.Dispose();
|
||||
_client?.Dispose();
|
||||
_lcuWebsocket?.Dispose();
|
||||
_localCachingClient?.Dispose();
|
||||
}
|
||||
|
||||
_isDisposed = true;
|
||||
|
||||
Reference in New Issue
Block a user