Add ability to parse match post data

This commit is contained in:
jvyden 2021-10-24 16:15:35 -04:00
commit 20316be3c6
No known key found for this signature in database
GPG key ID: 18BCF2BE0262B278
6 changed files with 57 additions and 14 deletions

View file

@ -1,8 +1,11 @@
#nullable enable
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Kettu;
using LBPUnion.ProjectLighthouse.Helpers;
using LBPUnion.ProjectLighthouse.Types;
using LBPUnion.ProjectLighthouse.Types.Match;
using LBPUnion.ProjectLighthouse.Types.Profiles;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
@ -20,24 +23,47 @@ namespace LBPUnion.ProjectLighthouse.Controllers {
[HttpPost("match")]
[Produces("text/json")]
public async Task<IActionResult> Match() {
User? user = await this.database.UserFromRequest(this.Request);
// User? user = await this.database.UserFromRequest(this.Request);
//
// if(user == null) return this.StatusCode(403, "");
if(user == null) return this.StatusCode(403, "");
LastMatch? lastMatch = await this.database.LastMatches
.Where(l => l.UserId == user.UserId).FirstOrDefaultAsync();
#region Parse match data
// Example POST /match: [UpdateMyPlayerData,["Player":"FireGamer9872"]]
string bodyString = await new StreamReader(this.Request.Body).ReadToEndAsync();
if(bodyString[0] != '[') return this.BadRequest();
// 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);
string matchType = "";
int i = 1;
while(true) {
if(bodyString[i] == ',') break;
matchType += bodyString[i];
i++;
}
lastMatch.Timestamp = TimestampHelper.Timestamp;
string matchString = string.Concat(bodyString.Skip(matchType.Length + 2).SkipLast(1));
#endregion
await this.database.SaveChangesAsync();
#region Update LastMatch
// 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();
#endregion
return this.Ok("[{\"StatusCode\":200}]");
}

View file

@ -2,6 +2,7 @@ using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Xml.Serialization;
using Kettu;
using LBPUnion.ProjectLighthouse.Helpers;
using LBPUnion.ProjectLighthouse.Serialization;
using LBPUnion.ProjectLighthouse.Types;
@ -58,6 +59,7 @@ namespace LBPUnion.ProjectLighthouse.Controllers {
FileHelper.EnsureDirectoryCreated(assetsDirectory);
if(FileHelper.ResourceExists(hash)) this.Ok(); // no reason to fail if it's already uploaded
Logger.Log($"Processing resource upload (hash: {hash})");
LbpFile file = new(await BinaryHelper.ReadFromPipeReader(Request.BodyReader));
if(!FileHelper.IsFileSafe(file)) return this.UnprocessableEntity();

View file

@ -12,7 +12,7 @@ namespace LBPUnion.ProjectLighthouse.Helpers.Extensions {
IEnumerable<string> fields = properties
.Select(property => new {
property.Name,
Value = property.GetValue(exception, null)
Value = property.GetValue(exception, null),
})
.Select(x => $"{x.Name} = {(x.Value != null ? x.Value.ToString() : string.Empty)}");

View file

@ -0,0 +1,5 @@
namespace LBPUnion.ProjectLighthouse.Helpers {
public class MatchHelper {
}
}

View file

@ -0,0 +1,5 @@
namespace LBPUnion.ProjectLighthouse.Types.Match {
public interface IMatchData {
}
}

View file

@ -0,0 +1,5 @@
namespace LBPUnion.ProjectLighthouse.Types.Match {
public class UpdateMyPlayerData : IMatchData {
public string Player;
}
}