Implement player count reading

Closes #15
This commit is contained in:
jvyden 2021-10-19 17:38:02 -04:00
commit f5cde3937d
No known key found for this signature in database
GPG key ID: 18BCF2BE0262B278
3 changed files with 60 additions and 6 deletions

View file

@ -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<IActionResult> 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<IActionResult> TotalPlayerCount() {
int recentMatches = await this.database.LastMatches
.Where(l => TimestampHelper.Timestamp - l.Timestamp < 60)
.CountAsync();
return this.Ok(recentMatches.ToString());
}
}
}

View file

@ -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}]");
}
}
}

View file

@ -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;
}
}