@page "/" @page "/{id:guid}" @using LANCommander.Client.Data.Models @using LANCommander.Client.Enums @using LANCommander.Client.Models @using System.Diagnostics @using LANCommander.Client.UI.Library.Components @using LANCommander.SDK.Helpers @inject SDK.Client Client @inject NavigationManager NavigationManager @inject LibraryService LibraryService @inject DownloadService DownloadService @inject GameService GameService @inject ModalService ModalService @inject ConfirmService ConfirmService @inject ImportService ImportService
@if (SelectedGame != null) {
@if (SelectedGame.Media.Any(m => m.Type == SDK.Enums.MediaType.Logo)) {
@if (SelectedItem.State == LibraryItemState.Installed) { } else if (SelectedItem.State == LibraryItemState.NotInstalled) { } else if (SelectedItem.State == LibraryItemState.Queued) { } else if (SelectedItem.State == LibraryItemState.UpdateAvailable) { } else if (SelectedItem.State == LibraryItemState.Installing) { }
@if (SelectedGame.Media.Any(m => m.Type == SDK.Enums.MediaType.Cover)) { }
}
@code { [Parameter] public Guid Id { get; set; } LibraryItem SelectedItem { get; set; } Data.Models.Game SelectedGame { get; set; } IEnumerable SelectedGameActions { get; set; } protected override async Task OnInitializedAsync() { DownloadService.OnQueueChanged += OnQueueChanged; DownloadService.OnInstallFail += OnInstallFail; DownloadService.OnInstallComplete += OnInstallComplete; GameService.OnUninstallComplete += OnUninstallComplete; LibraryService.GroupSelector = (libraryItem) => { if (libraryItem.DataItem is Game game) return game.Collections.Select(c => c.Name).ToArray(); else return new string[] { }; }; LoadData(); } protected override async Task OnParametersSetAsync() { if (Id != Guid.Empty) await OnLibraryItemSelected(LibraryService.GetLibraryItem(Id)); await InvokeAsync(StateHasChanged); } void LoadData() { LibraryService.RefreshLibraryItemsAsync().Wait(); InvokeAsync(StateHasChanged); } async Task OnLibraryItemSelected(LibraryItem item) { if (item != null && item.DataItem is Data.Models.Game) { SelectedItem = item; SelectedGame = item.DataItem as Data.Models.Game; if (SelectedItem.State == LibraryItemState.Installed) SelectedGameActions = (await GameService.GetActionsAsync(SelectedGame)).Where(a => !a.IsPrimaryAction); } } async Task Install(LibraryItem item) { await LibraryService.Install(item); } async Task OnQueueChanged() { var queueItem = DownloadService.Queue.FirstOrDefault(i => SelectedItem != null && i.Id == SelectedItem.Key); if (queueItem != null) { SelectedItem = await LibraryService.GetLibraryItemAsync(SelectedItem); switch (queueItem.Status) { case DownloadStatus.Downloading: case DownloadStatus.InstallingRedistributables: case DownloadStatus.InstallingMods: case DownloadStatus.InstallingExpansions: case DownloadStatus.RunningScripts: case DownloadStatus.DownloadingSaves: SelectedItem.State = LibraryItemState.Installing; break; case DownloadStatus.Idle: SelectedItem.State = LibraryItemState.Queued; break; case DownloadStatus.Failed: case DownloadStatus.Canceled: SelectedItem.State = LibraryItemState.NotInstalled; break; case DownloadStatus.Complete: SelectedItem.State = LibraryItemState.Installed; break; } await OnLibraryItemSelected(SelectedItem); await InvokeAsync(StateHasChanged); } } async Task OnInstallComplete(Data.Models.Game game) { LoadData(); if (SelectedItem.DataItem is Game selectedGame) { if (selectedGame.Id == game.Id) await OnLibraryItemSelected(LibraryService.LibraryItems.FirstOrDefault(i => i.Key == selectedGame.Id)); } } async Task OnInstallFail(Data.Models.Game game) { var result = await ConfirmService.Show( $"The installation for {game.Title} has failed. Retry download?", "Installation Failed", ConfirmButtons.RetryCancel, ConfirmIcon.Error ); await LibraryService.LibraryChanged(); if (result == ConfirmResult.Retry) { var libraryItem = LibraryService.GetLibraryItem(game.Id); Install(libraryItem); } } async Task OnUninstallComplete(Data.Models.Game game) { LoadData(); if (SelectedItem.DataItem is Game selectedGame) { if (selectedGame.Id == game.Id) await OnLibraryItemSelected(LibraryService.LibraryItems.FirstOrDefault(i => i.Key == selectedGame.Id)); } } string GetPlayTime(Data.Models.Game game) { var totalTime = new TimeSpan(game.PlaySessions .Where(ps => ps.End != null && ps.Start != null) .Select(ps => ps.End.Value.Subtract(ps.Start.Value)) .Sum(ts => ts.Ticks)); if (totalTime.TotalMinutes < 1) return "None"; else if (totalTime.TotalHours < 1) return totalTime.TotalMinutes.ToString("0") + " minutes"; else return totalTime.TotalHours.ToString("0.##") + " hours"; } string GetLastPlayed(Data.Models.Game game) { var lastSession = game.PlaySessions.Where(ps => ps.End != null && ps.Start != null).OrderByDescending(ps => ps.End).FirstOrDefault(); if (lastSession == null) return "Never"; else return lastSession.End.Value.ToRelativeDate(); } }