LANCommander/LANCommander.Server/UI/Pages/Dashboard/Charts/TopGamesAverageSessionPlaytime.razor
Pat Hartl add770b227 Refactor Settings
Settings for the server are now combined with the settings from the SDK. This introduces a large breaking change and a migration should be created. This refactor utilizes .NET Configuration and the Options pattern. This will allow for the overriding of any setting using envionment variables. It also means that Settings.yml can be used to override any .NET configuration. A SettingsProvider implementation was created, and any updating of settings was changed from SettingsService.SaveSettings() to SettingsProvider.Update(s => { ... })
2025-11-16 12:31:25 -06:00

78 lines
No EOL
2.1 KiB
Text

@using AntDesign.Charts
@inject PlaySessionService PlaySessionService
<Spin Spinning="Loading">
<div class="dashboard-chart">
<AntDesign.Charts.Column Data="Data" Config="Config" JsConfig="@JsConfig" />
</div>
</Spin>
@code {
ICollection<PlaySession> PlaySessions;
object[] Data;
bool Loading = true;
string JsConfig = @"{
meta: {
playtime: {
alias: 'Average Session Playtime',
formatter: (v) => Math.floor(v / 3600) > 0 ? `${Math.floor(v / 3600)}h ${Math.floor((v % 3600) / 60)}m` : `${Math.floor((v % 3600) / 60)}m`
},
game: {
alias: 'Game'
}
},
label: {
visible: true,
type: 'outer-center',
style: {
fontSize: '1px'
}
}
}";
ColumnConfig Config = new ColumnConfig
{
XField = "game",
YField = "playtime",
ColorField = "game",
};
protected override async Task OnInitializedAsync()
{
Config.Theme = "dark";
Dictionary<string, TimeSpan> playtimes = new Dictionary<string, TimeSpan>();
PlaySessions = await PlaySessionService
.AsNoTracking()
.Include(ps => ps.Game)
.Include(ps => ps.CreatedBy)
.Include(ps => ps.User)
.GetAsync(ps => ps.GameId.HasValue && ps.GameId.Value != Guid.Empty);
foreach (var gameSessions in PlaySessions.GroupBy(s => s.GameId))
{
var total = new TimeSpan();
foreach (var session in gameSessions.Where(gs => gs.Start != null && gs.End != null))
{
total = total.Add(session.End.Value.Subtract(session.Start.Value));
}
playtimes[gameSessions.First().Game.Title] = TimeSpan.FromSeconds(total.TotalSeconds / gameSessions.Count());
}
Data = playtimes.OrderByDescending(pt => pt.Value).Take(20).Select(pt => new
{
Game = pt.Key,
Playtime = (int)pt.Value.TotalSeconds
}).ToArray();
Loading = false;
StateHasChanged();
}
}