@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.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 == Data.Enums.MediaType.Cover))
{
}
}
@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;
ImportService.OnImportComplete += LoadData;
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);
}
string GetItemClasses(LibraryItem item)
{
HashSet classes = new HashSet();
if (item.State == LibraryItemState.Installed)
classes.Add("installed");
if (SelectedGame != null && SelectedGame.Id == item.Key)
classes.Add("selected");
return String.Join(" ", classes);
}
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 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);
}
}
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();
}
}