mirror of
https://github.com/LBPUnion/ProjectLighthouse.git
synced 2025-07-29 16:38:37 +00:00
parent
20e6cdb3cd
commit
f5cde3937d
3 changed files with 60 additions and 6 deletions
53
ProjectLighthouse/Controllers/MatchController.cs
Normal file
53
ProjectLighthouse/Controllers/MatchController.cs
Normal 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());
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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}]");
|
||||
}
|
||||
}
|
||||
}
|
7
ProjectLighthouse/Helpers/TimestampHelper.cs
Normal file
7
ProjectLighthouse/Helpers/TimestampHelper.cs
Normal 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;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue