From 1774acda9569af1fc69516729d08d74e5b350af0 Mon Sep 17 00:00:00 2001 From: jvyden Date: Wed, 22 Dec 2021 17:31:38 -0500 Subject: [PATCH 1/4] Fix users not being correctly removed --- ProjectLighthouse.Tests/Tests/DatabaseTests.cs | 4 ++-- ProjectLighthouse.Tests/Tests/SlotTests.cs | 4 ++-- ProjectLighthouse/Database.cs | 2 ++ 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/ProjectLighthouse.Tests/Tests/DatabaseTests.cs b/ProjectLighthouse.Tests/Tests/DatabaseTests.cs index 01d49d1f..9c45708d 100644 --- a/ProjectLighthouse.Tests/Tests/DatabaseTests.cs +++ b/ProjectLighthouse.Tests/Tests/DatabaseTests.cs @@ -16,8 +16,8 @@ namespace LBPUnion.ProjectLighthouse.Tests User userA = await database.CreateUser("createUserTwiceTest" + rand, HashHelper.GenerateAuthToken()); User userB = await database.CreateUser("createUserTwiceTest" + rand, HashHelper.GenerateAuthToken()); - database.Users.Remove(userA); - database.Users.Remove(userB); + await database.RemoveUser(userA); + await database.RemoveUser(userB); await database.SaveChangesAsync(); } diff --git a/ProjectLighthouse.Tests/Tests/SlotTests.cs b/ProjectLighthouse.Tests/Tests/SlotTests.cs index 7af3d264..761583e9 100644 --- a/ProjectLighthouse.Tests/Tests/SlotTests.cs +++ b/ProjectLighthouse.Tests/Tests/SlotTests.cs @@ -79,8 +79,8 @@ namespace LBPUnion.ProjectLighthouse.Tests database.Slots.Remove(slotA); database.Slots.Remove(slotB); - database.Users.Remove(userA); - database.Users.Remove(userB); + await database.RemoveUser(userA); + await database.RemoveUser(userB); await database.SaveChangesAsync(); } diff --git a/ProjectLighthouse/Database.cs b/ProjectLighthouse/Database.cs index e28957ba..25a9fb23 100644 --- a/ProjectLighthouse/Database.cs +++ b/ProjectLighthouse/Database.cs @@ -277,6 +277,8 @@ namespace LBPUnion.ProjectLighthouse this.Comments.RemoveRange(this.Comments.Where(c => c.PosterUserId == user.UserId)); this.Photos.RemoveRange(this.Photos.Where(p => p.CreatorId == user.UserId)); + this.Users.Remove(user); + await this.SaveChangesAsync(); } From b0ada585dba90ab55ab5a6064c8fad542d0791c2 Mon Sep 17 00:00:00 2001 From: jvyden Date: Wed, 22 Dec 2021 17:36:51 -0500 Subject: [PATCH 2/4] Fix crash when removing a user when user.Location is null --- ProjectLighthouse/Database.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ProjectLighthouse/Database.cs b/ProjectLighthouse/Database.cs index 25a9fb23..a8b5b45e 100644 --- a/ProjectLighthouse/Database.cs +++ b/ProjectLighthouse/Database.cs @@ -259,7 +259,7 @@ namespace LBPUnion.ProjectLighthouse public async Task RemoveUser(User user) { - this.Locations.Remove(user.Location); + if (user.Location != null) this.Locations.Remove(user.Location); LastContact? lastContact = await this.LastContacts.FirstOrDefaultAsync(l => l.UserId == user.UserId); if (lastContact != null) this.LastContacts.Remove(lastContact); From 75812988ff7afeb3051ca425c683f7e760a7db19 Mon Sep 17 00:00:00 2001 From: jvyden Date: Wed, 22 Dec 2021 17:56:39 -0500 Subject: [PATCH 3/4] Make RoomHelper thread-safe --- ProjectLighthouse/Helpers/RoomHelper.cs | 54 +++++++++++++++---------- 1 file changed, 33 insertions(+), 21 deletions(-) diff --git a/ProjectLighthouse/Helpers/RoomHelper.cs b/ProjectLighthouse/Helpers/RoomHelper.cs index e89979fa..4d8cb234 100644 --- a/ProjectLighthouse/Helpers/RoomHelper.cs +++ b/ProjectLighthouse/Helpers/RoomHelper.cs @@ -26,9 +26,15 @@ namespace LBPUnion.ProjectLighthouse.Helpers public static FindBestRoomResponse? FindBestRoom(User user, string location) { - bool anyRoomsLookingForPlayers = Rooms.Any(r => r.IsLookingForPlayers); + bool anyRoomsLookingForPlayers; + List rooms; + + lock(Rooms) + { + anyRoomsLookingForPlayers = Rooms.Any(r => r.IsLookingForPlayers); + rooms = anyRoomsLookingForPlayers ? Rooms.Where(r => anyRoomsLookingForPlayers && r.IsLookingForPlayers).ToList() : Rooms; + } - List rooms = anyRoomsLookingForPlayers ? Rooms.Where(r => anyRoomsLookingForPlayers && r.IsLookingForPlayers).ToList() : Rooms; foreach (Room room in rooms) // Look for rooms looking for players before moving on to rooms that are idle. { @@ -117,7 +123,7 @@ namespace LBPUnion.ProjectLighthouse.Helpers }; CleanupRooms(room.Host, room); - Rooms.Add(room); + lock(Rooms) Rooms.Add(room); Logger.Log($"Created room (id: {room.RoomId}) for host {room.Host.Username} (id: {room.Host.UserId})", LoggerLevelMatch.Instance); return room; @@ -125,7 +131,10 @@ namespace LBPUnion.ProjectLighthouse.Helpers public static Room? FindRoomByUser(User user, bool createIfDoesNotExist = false) { - foreach (Room room in Rooms.Where(room => room.Players.Any(player => user == player))) return room; + lock(Rooms) + { + foreach (Room room in Rooms.Where(room => room.Players.Any(player => user == player))) return room; + } return createIfDoesNotExist ? CreateRoom(user) : null; } @@ -133,25 +142,28 @@ namespace LBPUnion.ProjectLighthouse.Helpers [SuppressMessage("ReSharper", "InvertIf")] public static void CleanupRooms(User? host = null, Room? newRoom = null) { - // Delete old rooms based on host - if (host != null) - try - { - Rooms.RemoveAll(r => r.Host == host); - } - catch - { - // TODO: detect the room that failed and remove it - } + lock(Rooms) + { + // Delete old rooms based on host + if (host != null) + try + { + Rooms.RemoveAll(r => r.Host == host); + } + catch + { + // TODO: detect the room that failed and remove it + } - // Remove players in this new room from other rooms - if (newRoom != null) - foreach (Room room in Rooms) - { - if (room == newRoom) continue; + // 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); - } + foreach (User newRoomPlayer in newRoom.Players) room.Players.RemoveAll(p => p == newRoomPlayer); + } + } } } } \ No newline at end of file From b15d56f6e99a1b45d01c2699bdb02f17f6fca183 Mon Sep 17 00:00:00 2001 From: jvyden Date: Wed, 22 Dec 2021 18:49:44 -0500 Subject: [PATCH 4/4] Fix tests not using randomized usernames --- ProjectLighthouse.Tests/Tests/DatabaseTests.cs | 7 +++++-- ProjectLighthouse.Tests/Tests/SlotTests.cs | 11 +++++++---- ProjectLighthouse/Database.cs | 7 +++++-- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/ProjectLighthouse.Tests/Tests/DatabaseTests.cs b/ProjectLighthouse.Tests/Tests/DatabaseTests.cs index 9c45708d..19416bcb 100644 --- a/ProjectLighthouse.Tests/Tests/DatabaseTests.cs +++ b/ProjectLighthouse.Tests/Tests/DatabaseTests.cs @@ -2,6 +2,7 @@ using System; using System.Threading.Tasks; using LBPUnion.ProjectLighthouse.Helpers; using LBPUnion.ProjectLighthouse.Types; +using Xunit; namespace LBPUnion.ProjectLighthouse.Tests { @@ -16,8 +17,10 @@ namespace LBPUnion.ProjectLighthouse.Tests User userA = await database.CreateUser("createUserTwiceTest" + rand, HashHelper.GenerateAuthToken()); User userB = await database.CreateUser("createUserTwiceTest" + rand, HashHelper.GenerateAuthToken()); - await database.RemoveUser(userA); - await database.RemoveUser(userB); + Assert.NotNull(userA); + Assert.NotNull(userB); + + await database.RemoveUser(userA); // Only remove userA since userA and userB are the same user await database.SaveChangesAsync(); } diff --git a/ProjectLighthouse.Tests/Tests/SlotTests.cs b/ProjectLighthouse.Tests/Tests/SlotTests.cs index 761583e9..4eef03b6 100644 --- a/ProjectLighthouse.Tests/Tests/SlotTests.cs +++ b/ProjectLighthouse.Tests/Tests/SlotTests.cs @@ -1,3 +1,4 @@ +using System; using System.Net.Http; using System.Threading.Tasks; using LBPUnion.ProjectLighthouse.Helpers; @@ -15,8 +16,10 @@ namespace LBPUnion.ProjectLighthouse.Tests { await using Database database = new(); - User userA = await database.CreateUser("unitTestUser0", HashHelper.GenerateAuthToken()); - User userB = await database.CreateUser("unitTestUser1", HashHelper.GenerateAuthToken()); + Random r = new(); + + User userA = await database.CreateUser($"unitTestUser{r.Next()}", HashHelper.GenerateAuthToken()); + User userB = await database.CreateUser($"unitTestUser{r.Next()}", HashHelper.GenerateAuthToken()); Location l = new() { @@ -57,9 +60,9 @@ namespace LBPUnion.ProjectLighthouse.Tests LoginResult loginResult = await this.Authenticate(); HttpResponseMessage respMessageA = await this.AuthenticatedRequest - ("LITTLEBIGPLANETPS3_XML/slots/by?u=unitTestUser0&pageStart=1&pageSize=1", loginResult.AuthTicket); + ($"LITTLEBIGPLANETPS3_XML/slots/by?u={userA.Username}&pageStart=1&pageSize=1", loginResult.AuthTicket); HttpResponseMessage respMessageB = await this.AuthenticatedRequest - ("LITTLEBIGPLANETPS3_XML/slots/by?u=unitTestUser1&pageStart=1&pageSize=1", loginResult.AuthTicket); + ($"LITTLEBIGPLANETPS3_XML/slots/by?u={userB.Username}&pageStart=1&pageSize=1", loginResult.AuthTicket); Assert.True(respMessageA.IsSuccessStatusCode); Assert.True(respMessageB.IsSuccessStatusCode); diff --git a/ProjectLighthouse/Database.cs b/ProjectLighthouse/Database.cs index a8b5b45e..a8ab002d 100644 --- a/ProjectLighthouse/Database.cs +++ b/ProjectLighthouse/Database.cs @@ -256,9 +256,10 @@ namespace LBPUnion.ProjectLighthouse public async Task PhotoFromSubject(PhotoSubject subject) => await this.Photos.FirstOrDefaultAsync(p => p.PhotoSubjectIds.Contains(subject.PhotoSubjectId.ToString())); - - public async Task RemoveUser(User user) + public async Task RemoveUser(User? user) { + if (user == null) return; + if (user.Location != null) this.Locations.Remove(user.Location); LastContact? lastContact = await this.LastContacts.FirstOrDefaultAsync(l => l.UserId == user.UserId); if (lastContact != null) this.LastContacts.Remove(lastContact); @@ -270,11 +271,13 @@ namespace LBPUnion.ProjectLighthouse this.PhotoSubjects.RemoveRange(this.PhotoSubjects.Where(s => s.UserId == user.UserId)); this.HeartedLevels.RemoveRange(this.HeartedLevels.Where(h => h.UserId == user.UserId)); this.VisitedLevels.RemoveRange(this.VisitedLevels.Where(v => v.UserId == user.UserId)); + this.RatedReviews.RemoveRange(this.RatedReviews.Where(r => r.UserId == user.UserId)); this.QueuedLevels.RemoveRange(this.QueuedLevels.Where(q => q.UserId == user.UserId)); this.RatedLevels.RemoveRange(this.RatedLevels.Where(r => r.UserId == user.UserId)); this.GameTokens.RemoveRange(this.GameTokens.Where(t => t.UserId == user.UserId)); this.WebTokens.RemoveRange(this.WebTokens.Where(t => t.UserId == user.UserId)); this.Comments.RemoveRange(this.Comments.Where(c => c.PosterUserId == user.UserId)); + this.Reviews.RemoveRange(this.Reviews.Where(r => r.ReviewerId == user.UserId)); this.Photos.RemoveRange(this.Photos.Where(p => p.CreatorId == user.UserId)); this.Users.Remove(user);