mirror of
https://github.com/LBPUnion/ProjectLighthouse.git
synced 2025-07-29 08:28:39 +00:00
Merge branch 'main' into account-banning
This commit is contained in:
commit
79b8f001b7
4 changed files with 55 additions and 32 deletions
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -256,10 +256,11 @@ namespace LBPUnion.ProjectLighthouse
|
|||
|
||||
public async Task<Photo?> 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();
|
||||
}
|
||||
|
||||
|
|
|
@ -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<Room> rooms;
|
||||
|
||||
lock(Rooms)
|
||||
{
|
||||
anyRoomsLookingForPlayers = Rooms.Any(r => r.IsLookingForPlayers);
|
||||
rooms = anyRoomsLookingForPlayers ? Rooms.Where(r => anyRoomsLookingForPlayers && r.IsLookingForPlayers).ToList() : Rooms;
|
||||
}
|
||||
|
||||
List<Room> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue