From e7d3d6bd02faefb39765de45bc4ee6572ad01c72 Mon Sep 17 00:00:00 2001 From: jvyden Date: Mon, 25 Oct 2021 02:38:03 -0400 Subject: [PATCH] More match progress --- ProjectLighthouse.sln.DotSettings | 1 + .../Controllers/MatchController.cs | 60 +++++++++++-------- ProjectLighthouse/Helpers/MatchHelper.cs | 33 +++++++++- ProjectLighthouse/Types/Match/RoomState.cs | 9 +++ .../Types/Match/UpdatePlayersInRoom.cs | 8 +++ 5 files changed, 83 insertions(+), 28 deletions(-) create mode 100644 ProjectLighthouse/Types/Match/RoomState.cs create mode 100644 ProjectLighthouse/Types/Match/UpdatePlayersInRoom.cs diff --git a/ProjectLighthouse.sln.DotSettings b/ProjectLighthouse.sln.DotSettings index 07522038..2ffd35fb 100644 --- a/ProjectLighthouse.sln.DotSettings +++ b/ProjectLighthouse.sln.DotSettings @@ -9,6 +9,7 @@ True True True + True True True True diff --git a/ProjectLighthouse/Controllers/MatchController.cs b/ProjectLighthouse/Controllers/MatchController.cs index e45e641d..6d34b60e 100644 --- a/ProjectLighthouse/Controllers/MatchController.cs +++ b/ProjectLighthouse/Controllers/MatchController.cs @@ -1,4 +1,5 @@ #nullable enable +using System; using System.IO; using System.Linq; using System.Threading.Tasks; @@ -23,45 +24,52 @@ namespace LBPUnion.ProjectLighthouse.Controllers { [HttpPost("match")] [Produces("text/json")] public async Task Match() { -// User? user = await this.database.UserFromRequest(this.Request); -// -// if(user == null) return this.StatusCode(403, ""); + + User? user = await this.database.UserFromRequest(this.Request); + + if(user == null) return this.StatusCode(403, ""); #region Parse match data // Example POST /match: [UpdateMyPlayerData,["Player":"FireGamer9872"]] string bodyString = await new StreamReader(this.Request.Body).ReadToEndAsync(); + if(bodyString.Contains("FindBestRoom")) { + return this.Ok("[{\"StatusCode\":200},{\"Players\":[{\"PlayerId\":\"literally1984\",\"matching_res\":0},{\"PlayerId\":\"jvyden\",\"matching_res\":1}],\"Slots\":[[5,0]],\"RoomState\":\"E_ROOM_IN_POD\",\"HostMood\":\"E_MOOD_EVERYONE\",\"LevelCompletionEstimate\":0,\"PassedNoJoinPoint\":0,\"MoveConnected\":false,\"Location\":[\"127.0.0.1\"],\"BuildVersion\":289,\"Language\":1,\"FirstSeenTimestamp\":1427331263756,\"LastSeenTimestamp\":1635112546000,\"GameId\":1,\"NatType\":2,\"Friends\":[],\"Blocked\":[],\"RecentlyLeft\":[],\"FailedJoin\":[]}]"); + } + if(bodyString[0] != '[') return this.BadRequest(); - string matchType = ""; + IMatchData? matchData; + try { + matchData = MatchHelper.Deserialize(bodyString); + } + catch(Exception e) { + Logger.Log("Exception while parsing MatchData: " + e); + Logger.Log("Data: " + bodyString); - int i = 1; - while(true) { - if(bodyString[i] == ',') break; - - matchType += bodyString[i]; - i++; + return this.BadRequest(); } - string matchString = string.Concat(bodyString.Skip(matchType.Length + 2).SkipLast(1)); + if(matchData == null) return this.BadRequest(); + #endregion #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(); + 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}]"); diff --git a/ProjectLighthouse/Helpers/MatchHelper.cs b/ProjectLighthouse/Helpers/MatchHelper.cs index b4917b20..fefa8888 100644 --- a/ProjectLighthouse/Helpers/MatchHelper.cs +++ b/ProjectLighthouse/Helpers/MatchHelper.cs @@ -1,5 +1,34 @@ +#nullable enable +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text.Json; +using LBPUnion.ProjectLighthouse.Types.Match; + namespace LBPUnion.ProjectLighthouse.Helpers { - public class MatchHelper { - + public static class MatchHelper { + public static IMatchData? Deserialize(string data) { + string matchType = ""; + + int i = 1; + while(true) { + if(data[i] == ',') break; + + matchType += data[i]; + i++; + } + + string matchData = $"{{{string.Concat(data.Skip(matchType.Length + 3).SkipLast(2))}}}"; + + return Deserialize(matchType, matchData); + } + + public static IMatchData? Deserialize(string matchType, string matchData) { + return matchType switch { + "UpdateMyPlayerData" => JsonSerializer.Deserialize(matchData), + "UpdatePlayersInRoom" => JsonSerializer.Deserialize(matchData), + _ => null, + }; + } } } \ No newline at end of file diff --git a/ProjectLighthouse/Types/Match/RoomState.cs b/ProjectLighthouse/Types/Match/RoomState.cs new file mode 100644 index 00000000..1cb53079 --- /dev/null +++ b/ProjectLighthouse/Types/Match/RoomState.cs @@ -0,0 +1,9 @@ +namespace LBPUnion.ProjectLighthouse.Types.Match { + public enum RoomState { + Idle = 0, + LookingForPlayersForLevel = 1, + Unknown = 2, + DivingIn = 3, + DivingInWaiting = 4, + } +} \ No newline at end of file diff --git a/ProjectLighthouse/Types/Match/UpdatePlayersInRoom.cs b/ProjectLighthouse/Types/Match/UpdatePlayersInRoom.cs new file mode 100644 index 00000000..a689bcc2 --- /dev/null +++ b/ProjectLighthouse/Types/Match/UpdatePlayersInRoom.cs @@ -0,0 +1,8 @@ +using System.Collections.Generic; + +namespace LBPUnion.ProjectLighthouse.Types.Match { + public class UpdatePlayersInRoom : IMatchData { + public List Players; + public List Reservations; + } +} \ No newline at end of file