Show users currently online on front page

This commit is contained in:
jvyden 2021-11-25 00:51:27 -05:00
commit 8facca8b39
No known key found for this signature in database
GPG key ID: 18BCF2BE0262B278
2 changed files with 24 additions and 6 deletions

View file

@ -1,4 +1,5 @@
@page "/"
@using LBPUnion.ProjectLighthouse.Types
@model LBPUnion.ProjectLighthouse.Pages.LandingPage
@{
@ -11,15 +12,23 @@
<p>You are currently logged in as <b>@Model.User.Username</b>.</p>
}
@if (Model.PlayersOnline == 1)
@if (Model.PlayersOnlineCount == 1)
{
<p>There is 1 user currently online.</p>
<p>There is 1 user currently online:</p>
@foreach (User user in Model.PlayersOnline)
{
<a href="/user/@user.UserId">@user.Username</a>
}
}
else if (Model.PlayersOnline == 0)
else if (Model.PlayersOnlineCount == 0)
{
<p>There are no users online. Why not hop on?</p>
}
else
{
<p>There are currently @Model.PlayersOnline users online.</p>
<p>There are currently @Model.PlayersOnlineCount users online:</p>
@foreach (User user in Model.PlayersOnline)
{
<a href="/user/@user.UserId">@user.Username</a>
}
}

View file

@ -1,9 +1,13 @@
#nullable enable
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using JetBrains.Annotations;
using LBPUnion.ProjectLighthouse.Helpers;
using LBPUnion.ProjectLighthouse.Pages.Layouts;
using LBPUnion.ProjectLighthouse.Types;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
namespace LBPUnion.ProjectLighthouse.Pages
{
@ -12,12 +16,17 @@ namespace LBPUnion.ProjectLighthouse.Pages
public LandingPage(Database database) : base(database)
{}
public int PlayersOnline;
public int PlayersOnlineCount;
public List<User> PlayersOnline;
[UsedImplicitly]
public async Task<IActionResult> OnGet()
{
this.PlayersOnline = await StatisticsHelper.RecentMatches();
this.PlayersOnlineCount = await StatisticsHelper.RecentMatches();
List<int> userIds = await this.Database.LastMatches.Where(l => TimestampHelper.Timestamp - l.Timestamp < 300).Select(l => l.UserId).ToListAsync();
this.PlayersOnline = await this.Database.Users.Where(u => userIds.Contains(u.UserId)).ToListAsync();
return this.Page();
}
}