mirror of
https://github.com/LBPUnion/ProjectLighthouse.git
synced 2025-08-07 12:28:39 +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
|
#nullable enable
|
||||||
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using Kettu;
|
||||||
using LBPUnion.ProjectLighthouse.Helpers;
|
using LBPUnion.ProjectLighthouse.Helpers;
|
||||||
using LBPUnion.ProjectLighthouse.Types;
|
using LBPUnion.ProjectLighthouse.Types;
|
||||||
|
using LBPUnion.ProjectLighthouse.Types.Match;
|
||||||
using LBPUnion.ProjectLighthouse.Types.Profiles;
|
using LBPUnion.ProjectLighthouse.Types.Profiles;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
@ -20,24 +23,47 @@ namespace LBPUnion.ProjectLighthouse.Controllers {
|
||||||
[HttpPost("match")]
|
[HttpPost("match")]
|
||||||
[Produces("text/json")]
|
[Produces("text/json")]
|
||||||
public async Task<IActionResult> Match() {
|
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, "");
|
#region Parse match data
|
||||||
LastMatch? lastMatch = await this.database.LastMatches
|
// Example POST /match: [UpdateMyPlayerData,["Player":"FireGamer9872"]]
|
||||||
.Where(l => l.UserId == user.UserId).FirstOrDefaultAsync();
|
|
||||||
|
string bodyString = await new StreamReader(this.Request.Body).ReadToEndAsync();
|
||||||
|
if(bodyString[0] != '[') return this.BadRequest();
|
||||||
|
|
||||||
// below makes it not look like trash
|
string matchType = "";
|
||||||
// ReSharper disable once ConvertIfStatementToNullCoalescingExpression
|
|
||||||
if(lastMatch == null) {
|
int i = 1;
|
||||||
lastMatch = new LastMatch {
|
while(true) {
|
||||||
UserId = user.UserId,
|
if(bodyString[i] == ',') break;
|
||||||
};
|
|
||||||
this.database.LastMatches.Add(lastMatch);
|
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}]");
|
return this.Ok("[{\"StatusCode\":200}]");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,7 @@ using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using System.Xml.Serialization;
|
using System.Xml.Serialization;
|
||||||
|
using Kettu;
|
||||||
using LBPUnion.ProjectLighthouse.Helpers;
|
using LBPUnion.ProjectLighthouse.Helpers;
|
||||||
using LBPUnion.ProjectLighthouse.Serialization;
|
using LBPUnion.ProjectLighthouse.Serialization;
|
||||||
using LBPUnion.ProjectLighthouse.Types;
|
using LBPUnion.ProjectLighthouse.Types;
|
||||||
|
@ -58,6 +59,7 @@ namespace LBPUnion.ProjectLighthouse.Controllers {
|
||||||
FileHelper.EnsureDirectoryCreated(assetsDirectory);
|
FileHelper.EnsureDirectoryCreated(assetsDirectory);
|
||||||
if(FileHelper.ResourceExists(hash)) this.Ok(); // no reason to fail if it's already uploaded
|
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));
|
LbpFile file = new(await BinaryHelper.ReadFromPipeReader(Request.BodyReader));
|
||||||
|
|
||||||
if(!FileHelper.IsFileSafe(file)) return this.UnprocessableEntity();
|
if(!FileHelper.IsFileSafe(file)) return this.UnprocessableEntity();
|
||||||
|
|
|
@ -12,7 +12,7 @@ namespace LBPUnion.ProjectLighthouse.Helpers.Extensions {
|
||||||
IEnumerable<string> fields = properties
|
IEnumerable<string> fields = properties
|
||||||
.Select(property => new {
|
.Select(property => new {
|
||||||
property.Name,
|
property.Name,
|
||||||
Value = property.GetValue(exception, null)
|
Value = property.GetValue(exception, null),
|
||||||
})
|
})
|
||||||
.Select(x => $"{x.Name} = {(x.Value != null ? x.Value.ToString() : string.Empty)}");
|
.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