From 8facca8b3997605e4973565cb1e4768564654bc1 Mon Sep 17 00:00:00 2001 From: jvyden Date: Thu, 25 Nov 2021 00:51:27 -0500 Subject: [PATCH] Show users currently online on front page --- ProjectLighthouse/Pages/LandingPage.cshtml | 17 +++++++++++++---- ProjectLighthouse/Pages/LandingPage.cshtml.cs | 13 +++++++++++-- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/ProjectLighthouse/Pages/LandingPage.cshtml b/ProjectLighthouse/Pages/LandingPage.cshtml index f320a553..312f9d1d 100644 --- a/ProjectLighthouse/Pages/LandingPage.cshtml +++ b/ProjectLighthouse/Pages/LandingPage.cshtml @@ -1,4 +1,5 @@ @page "/" +@using LBPUnion.ProjectLighthouse.Types @model LBPUnion.ProjectLighthouse.Pages.LandingPage @{ @@ -11,15 +12,23 @@

You are currently logged in as @Model.User.Username.

} -@if (Model.PlayersOnline == 1) +@if (Model.PlayersOnlineCount == 1) { -

There is 1 user currently online.

+

There is 1 user currently online:

+ @foreach (User user in Model.PlayersOnline) + { + @user.Username + } } -else if (Model.PlayersOnline == 0) +else if (Model.PlayersOnlineCount == 0) {

There are no users online. Why not hop on?

} else { -

There are currently @Model.PlayersOnline users online.

+

There are currently @Model.PlayersOnlineCount users online:

+ @foreach (User user in Model.PlayersOnline) + { + @user.Username + } } \ No newline at end of file diff --git a/ProjectLighthouse/Pages/LandingPage.cshtml.cs b/ProjectLighthouse/Pages/LandingPage.cshtml.cs index 1edb223a..a9b452b5 100644 --- a/ProjectLighthouse/Pages/LandingPage.cshtml.cs +++ b/ProjectLighthouse/Pages/LandingPage.cshtml.cs @@ -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 PlayersOnline; [UsedImplicitly] public async Task OnGet() { - this.PlayersOnline = await StatisticsHelper.RecentMatches(); + this.PlayersOnlineCount = await StatisticsHelper.RecentMatches(); + + List 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(); } }