2024-02-16 17:24:16 -06:00
|
|
|
@page "/Dashboard/SessionPlaytime"
|
2025-02-01 02:21:34 -06:00
|
|
|
@inject GameService GameService
|
2024-02-16 17:24:16 -06:00
|
|
|
|
|
|
|
|
<PageHeader Title="Session Playtime" />
|
|
|
|
|
|
|
|
|
|
<Table TItem="GameAverageSessionData" DataSource="SessionData" Loading="@Loading" Responsive PageSize="100">
|
|
|
|
|
<Column TData="string" Title="Icon">
|
|
|
|
|
<Image Src="@GetIcon(context.Game)" Height="32" Width="32" Preview="false"></Image>
|
|
|
|
|
</Column>
|
|
|
|
|
<PropertyColumn Property="d => d.Game.Title" Title="Game" Sortable Filterable />
|
|
|
|
|
<PropertyColumn Property="d => d.LastPlayed" Title="Last Played" Sortable Filterable />
|
|
|
|
|
<PropertyColumn Property="d => d.SessionCount" Title="Sessions" Sortable Filterable />
|
|
|
|
|
<PropertyColumn Property="d => d.TotalTime" Title="Total Time" Sortable />
|
|
|
|
|
<PropertyColumn Property="d => d.AverageTime" Title="Average Session" Sortable DefaultSortOrder="SortDirection.Descending" />
|
|
|
|
|
</Table>
|
|
|
|
|
|
|
|
|
|
@code {
|
|
|
|
|
Dictionary<string, TimeSpan> Playtimes = new Dictionary<string, TimeSpan>();
|
|
|
|
|
|
|
|
|
|
ICollection<Game> Games { get; set; } = new List<Game>();
|
|
|
|
|
IEnumerable<GameAverageSessionData> SessionData = new List<GameAverageSessionData>();
|
|
|
|
|
|
|
|
|
|
bool Loading = true;
|
|
|
|
|
|
|
|
|
|
protected override async Task OnAfterRenderAsync(bool firstRender)
|
|
|
|
|
{
|
|
|
|
|
if (firstRender)
|
2024-11-22 02:05:44 -06:00
|
|
|
await LoadData();
|
2024-02-16 17:24:16 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task LoadData()
|
|
|
|
|
{
|
|
|
|
|
Loading = true;
|
2025-02-01 02:21:34 -06:00
|
|
|
|
|
|
|
|
Games = await GameService
|
|
|
|
|
.Include(g => g.PlaySessions)
|
|
|
|
|
.Include(g => g.Media.Where(m => m.Type == SDK.Enums.MediaType.Icon))
|
|
|
|
|
.GetAsync();
|
2024-02-16 17:24:16 -06:00
|
|
|
|
|
|
|
|
SessionData = Games.Select(g =>
|
|
|
|
|
{
|
|
|
|
|
var data = new GameAverageSessionData()
|
|
|
|
|
{
|
|
|
|
|
Game = g,
|
|
|
|
|
TotalTime = new TimeSpan()
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (g.PlaySessions != null && g.PlaySessions.Any(g => g.End != null && g.Start != null))
|
|
|
|
|
{
|
|
|
|
|
var playSessions = g.PlaySessions.Where(s => s.Start != null && s.End != null);
|
|
|
|
|
|
|
|
|
|
data.LastPlayed = playSessions.OrderByDescending(s => s.End).First().End.Value;
|
|
|
|
|
data.SessionCount = playSessions.Count();
|
|
|
|
|
|
|
|
|
|
foreach (var session in playSessions)
|
|
|
|
|
{
|
|
|
|
|
data.TotalTime = data.TotalTime.Add(session.End.Value.Subtract(session.Start.Value));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
data.AverageTime = TimeSpan.FromSeconds(data.TotalTime.TotalSeconds / data.SessionCount);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return data;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Loading = false;
|
|
|
|
|
|
|
|
|
|
await InvokeAsync(StateHasChanged);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string GetIcon(Game game)
|
|
|
|
|
{
|
2024-06-19 19:36:04 -05:00
|
|
|
var media = game?.Media?.FirstOrDefault(m => m.Type == SDK.Enums.MediaType.Icon);
|
2024-02-16 17:24:16 -06:00
|
|
|
|
|
|
|
|
if (media != null)
|
|
|
|
|
return $"/api/Media/{media.Id}/Download?fileId={media.FileId}";
|
|
|
|
|
else
|
|
|
|
|
return "/favicon.ico";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class GameAverageSessionData
|
|
|
|
|
{
|
|
|
|
|
public Game Game { get; set; }
|
|
|
|
|
public DateTime LastPlayed { get; set; }
|
|
|
|
|
public TimeSpan TotalTime { get; set; }
|
|
|
|
|
public int SessionCount { get; set; }
|
|
|
|
|
public TimeSpan AverageTime { get; set; }
|
|
|
|
|
}
|
|
|
|
|
}
|