@page "/" @page "/{id:guid}" @using LANCommander.Client.Enums @using LANCommander.Client.Models @using System.Diagnostics @using LANCommander.SDK.Helpers @inject SDK.Client Client @inject NavigationManager NavigationManager @inject LibraryService LibraryService @inject DownloadService DownloadService @inject GameService GameService @inject ModalService ModalService @inject ImportService ImportService
@foreach (var collection in LibraryItems.Where(i => i != null && i.DataItem is Data.Models.Collection)) {
@context.Name
}
@if (SelectedGame != null) {

@SelectedGame.Title

@if (SelectedGame.Media.Any(m => m.Type == Data.Enums.MediaType.Background)) { }
@if (SelectedItem.State == LibraryItemState.NotInstalled) { } else if (SelectedItem.State == LibraryItemState.Installed) { @if (SelectedGameActions.Count() > 0) { @foreach (var action in SelectedGameActions) { @action.Name } } Uninstall } else if (SelectedItem.State == LibraryItemState.UpdateAvailable) { } else if (SelectedItem.State == LibraryItemState.Installing) { }
}
@code { [Parameter] public Guid Id { get; set; } LibraryItem SelectedItem { get; set; } Data.Models.Game SelectedGame { get; set; } IEnumerable SelectedGameActions { get; set; } IEnumerable LibraryItems { get; set; } = new List(); protected override async Task OnInitializedAsync() { LibraryService.OnLibraryChanged += LoadData; DownloadService.OnQueueChanged += OnQueueChanged; LoadData(); } protected override async Task OnParametersSetAsync() { if (Id != Guid.Empty) await OnLibraryItemSelected(LibraryItems.SelectMany(i => i.Children).FirstOrDefault(i => i.Key == Id)); await InvokeAsync(StateHasChanged); } async void LoadData() { LibraryItems = await LibraryService.GetLibraryItemsAsync(); if (Id != Guid.Empty) await OnLibraryItemSelected(LibraryItems.SelectMany(i => i.Children).FirstOrDefault(i => i.Key == Id)); await InvokeAsync(StateHasChanged); } async Task OnLibraryItemSelected(LibraryItem item) { if (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 Uninstall(LibraryItem item) { var game = item.DataItem as Data.Models.Game; var manifest = ManifestHelper.Read(game.InstallDirectory, game.Id); var message = $"Would you like to uninstall {game.Title}?"; if (manifest.SavePaths != null && manifest.SavePaths.Any()) message += " Your save files have been uploaded to the server."; else message += " Your save files may be unrecoverable if they are contained within the game's install directory."; var confirmed = await ModalService.ConfirmAsync(new ConfirmOptions { OkText = "Uninstall", CancelText = "Cancel", Title = "Uninstall", Content = message, OkType = "danger", Icon = @ }); if (confirmed) await LibraryService.Uninstall(item); } async Task Run(Data.Models.Game game) { var actions = await GameService.GetActionsAsync(game); var primaryActions = actions.Where(a => a.IsPrimaryAction); if (primaryActions.Count() == 1) LibraryService.Run(game, primaryActions.First()); else { var modalOptions = new ModalOptions() { Title = game.Title, Maximizable = false, DefaultMaximized = false, Closable = true, Footer = null, Centered = true, WrapClassName = "game-actions-dialog" }; var model = new ActionSelectorDialogOptions { Actions = actions.Where(a => a.IsPrimaryAction), Game = game }; var modalRef = ModalService.CreateModal(modalOptions, model); } } async Task OnQueueChanged() { var queueItem = DownloadService.Queue.FirstOrDefault(i => 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); } } 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)); 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 lastSession.End.Value.ToString("MMMM d"); else return "Never"; } }