diff --git a/ProjectLighthouse.Tests/Tests/DatabaseTests.cs b/ProjectLighthouse.Tests/Tests/DatabaseTests.cs index 01d49d1f..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()); - database.Users.Remove(userA); - database.Users.Remove(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 7af3d264..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); @@ -79,8 +82,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..a8ab002d 100644 --- a/ProjectLighthouse/Database.cs +++ b/ProjectLighthouse/Database.cs @@ -256,10 +256,11 @@ 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) { - this.Locations.Remove(user.Location); + 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,13 +271,17 @@ 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); + await this.SaveChangesAsync(); } 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