From f5cde3937da2acd62f6f95dce6bb1cb83cb8e470 Mon Sep 17 00:00:00 2001 From: jvyden Date: Tue, 19 Oct 2021 17:38:02 -0400 Subject: [PATCH] Implement player count reading Closes #15 --- .../Controllers/MatchController.cs | 53 +++++++++++++++++++ .../Controllers/UserController.cs | 6 --- ProjectLighthouse/Helpers/TimestampHelper.cs | 7 +++ 3 files changed, 60 insertions(+), 6 deletions(-) create mode 100644 ProjectLighthouse/Controllers/MatchController.cs create mode 100644 ProjectLighthouse/Helpers/TimestampHelper.cs diff --git a/ProjectLighthouse/Controllers/MatchController.cs b/ProjectLighthouse/Controllers/MatchController.cs new file mode 100644 index 00000000..def62efa --- /dev/null +++ b/ProjectLighthouse/Controllers/MatchController.cs @@ -0,0 +1,53 @@ +#nullable enable +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; +using ProjectLighthouse.Helpers; +using ProjectLighthouse.Types; + +namespace ProjectLighthouse.Controllers { + [ApiController] + [Route("LITTLEBIGPLANETPS3_XML/")] + [Produces("text/xml")] + public class MatchController : ControllerBase { + private readonly Database database; + public MatchController(Database database) { + this.database = database; + } + + [HttpPost("match")] + [Produces("text/json")] + public async Task Match() { + User? user = await this.database.UserFromRequest(this.Request); + + if(user == null) return this.StatusCode(403, ""); + LastMatch? lastMatch = await this.database.LastMatches + .Where(l => l.UserId == user.UserId).FirstOrDefaultAsync(); + + // below makes it not look like trash + // ReSharper disable once ConvertIfStatementToNullCoalescingExpression + if(lastMatch == null) { + lastMatch = new LastMatch { + UserId = user.UserId, + }; + this.database.LastMatches.Add(lastMatch); + } + + lastMatch.Timestamp = TimestampHelper.Timestamp; + + await this.database.SaveChangesAsync(); + return this.Ok("[{\"StatusCode\":200}]"); + } + + [HttpGet("playersInPodCount")] + [HttpGet("totalPlayerCount")] + public async Task TotalPlayerCount() { + int recentMatches = await this.database.LastMatches + .Where(l => TimestampHelper.Timestamp - l.Timestamp < 60) + .CountAsync(); + + return this.Ok(recentMatches.ToString()); + } + } +} \ No newline at end of file diff --git a/ProjectLighthouse/Controllers/UserController.cs b/ProjectLighthouse/Controllers/UserController.cs index 81a47c7e..7808166d 100644 --- a/ProjectLighthouse/Controllers/UserController.cs +++ b/ProjectLighthouse/Controllers/UserController.cs @@ -116,11 +116,5 @@ namespace ProjectLighthouse.Controllers { if(database.ChangeTracker.HasChanges()) await database.SaveChangesAsync(); // save the user to the database if we changed anything return this.Ok(); } - - [HttpPost("match")] - [Produces("text/json")] - public IActionResult Match() { - return this.Ok("[{\"StatusCode\":200}]"); - } } } \ No newline at end of file diff --git a/ProjectLighthouse/Helpers/TimestampHelper.cs b/ProjectLighthouse/Helpers/TimestampHelper.cs new file mode 100644 index 00000000..1af33376 --- /dev/null +++ b/ProjectLighthouse/Helpers/TimestampHelper.cs @@ -0,0 +1,7 @@ +using System; + +namespace ProjectLighthouse.Helpers { + public static class TimestampHelper { + public static long Timestamp => (long)DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalSeconds; + } +} \ No newline at end of file