mirror of
https://github.com/LBPUnion/ProjectLighthouse.git
synced 2025-08-06 11:58:38 +00:00
Add ability to parse match post data
This commit is contained in:
parent
3f557cd911
commit
20316be3c6
6 changed files with 57 additions and 14 deletions
|
@ -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}]");
|
||||
}
|
||||
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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)}");
|
||||
|
||||
|
|
5
ProjectLighthouse/Helpers/MatchHelper.cs
Normal file
5
ProjectLighthouse/Helpers/MatchHelper.cs
Normal file
|
@ -0,0 +1,5 @@
|
|||
namespace LBPUnion.ProjectLighthouse.Helpers {
|
||||
public class MatchHelper {
|
||||
|
||||
}
|
||||
}
|
5
ProjectLighthouse/Types/Match/IMatchData.cs
Normal file
5
ProjectLighthouse/Types/Match/IMatchData.cs
Normal file
|
@ -0,0 +1,5 @@
|
|||
namespace LBPUnion.ProjectLighthouse.Types.Match {
|
||||
public interface IMatchData {
|
||||
|
||||
}
|
||||
}
|
5
ProjectLighthouse/Types/Match/UpdateMyPlayerData.cs
Normal file
5
ProjectLighthouse/Types/Match/UpdateMyPlayerData.cs
Normal file
|
@ -0,0 +1,5 @@
|
|||
namespace LBPUnion.ProjectLighthouse.Types.Match {
|
||||
public class UpdateMyPlayerData : IMatchData {
|
||||
public string Player;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue