From e92b1bf2c94e439853c08692dc7d46b4e426f44e Mon Sep 17 00:00:00 2001 From: jvyden Date: Tue, 16 Nov 2021 18:40:03 -0500 Subject: [PATCH] Fix bad request instead of 403 on getting slots --- .../Controllers/ListController.cs | 4 +-- ProjectLighthouse/Helpers/RoomHelper.cs | 34 +++++++++++++++++-- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/ProjectLighthouse/Controllers/ListController.cs b/ProjectLighthouse/Controllers/ListController.cs index 765582a5..701c9d12 100644 --- a/ProjectLighthouse/Controllers/ListController.cs +++ b/ProjectLighthouse/Controllers/ListController.cs @@ -29,7 +29,7 @@ namespace LBPUnion.ProjectLighthouse.Controllers public async Task GetLevelQueue(string username) { Token? token = await this.database.TokenFromRequest(this.Request); - if (token == null) return this.BadRequest(); + if (token == null) return this.StatusCode(403, ""); GameVersion gameVersion = token.GameVersion; @@ -91,7 +91,7 @@ namespace LBPUnion.ProjectLighthouse.Controllers public async Task GetFavouriteSlots(string username) { Token? token = await this.database.TokenFromRequest(this.Request); - if (token == null) return this.BadRequest(); + if (token == null) return this.StatusCode(403, ""); GameVersion gameVersion = token.GameVersion; diff --git a/ProjectLighthouse/Helpers/RoomHelper.cs b/ProjectLighthouse/Helpers/RoomHelper.cs index 25211547..c2d574d5 100644 --- a/ProjectLighthouse/Helpers/RoomHelper.cs +++ b/ProjectLighthouse/Helpers/RoomHelper.cs @@ -1,5 +1,6 @@ #nullable enable using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.Linq; using LBPUnion.ProjectLighthouse.Types; using LBPUnion.ProjectLighthouse.Types.Levels; @@ -37,9 +38,12 @@ namespace LBPUnion.ProjectLighthouse.Helpers ( p => { - bool gotValue = MatchHelper.UserLocations.TryGetValue(p.UserId, out string? value) && value != null; + bool gotValue = MatchHelper.UserLocations.TryGetValue(p.UserId, out string? value); - if (gotValue) relevantUserLocations.Add(p.UserId, value!); + if (gotValue && value != null) + { + relevantUserLocations.Add(p.UserId, value); + } return gotValue; } ); @@ -97,7 +101,7 @@ namespace LBPUnion.ProjectLighthouse.Helpers ( new List { - user + user, }, slot ); @@ -112,6 +116,9 @@ namespace LBPUnion.ProjectLighthouse.Helpers }; Rooms.Add(room); + + CleanupRooms(room.Host, room); + return room; } @@ -126,5 +133,26 @@ namespace LBPUnion.ProjectLighthouse.Helpers } return null; } + + [SuppressMessage("ReSharper", "InvertIf")] + public static void CleanupRooms(User? host = null, Room? newRoom = null) + { + // Delete old rooms based on host + if (host != null) + { + Rooms.RemoveAll(r => r.Host == host); + } + + // Remove players in this new room from other rooms + if (newRoom != null) + { + foreach (Room room in Rooms) + { + if (room == newRoom) continue; + + foreach (User newRoomPlayer in newRoom.Players) room.Players.RemoveAll(p => p == newRoomPlayer); + } + } + } } } \ No newline at end of file