diff --git a/ProjectLighthouse/Controllers/MatchController.cs b/ProjectLighthouse/Controllers/MatchController.cs index 1b19196e..cab8ceeb 100644 --- a/ProjectLighthouse/Controllers/MatchController.cs +++ b/ProjectLighthouse/Controllers/MatchController.cs @@ -135,10 +135,11 @@ namespace LBPUnion.ProjectLighthouse.Controllers else return this.BadRequest(); } - Room newRoom = RoomHelper.CreateRoom(users, createRoom.RoomSlot); - // Delete old rooms based on host - RoomHelper.Rooms.RemoveAll(r => r.Host == newRoom.Host); + RoomHelper.Rooms.RemoveAll(r => r.Host == user); + + // Create a new one as requested + Room newRoom = RoomHelper.CreateRoom(users, createRoom.RoomSlot); // Remove players in this new room from other rooms foreach (Room room in RoomHelper.Rooms) diff --git a/ProjectLighthouse/Helpers/RoomHelper.cs b/ProjectLighthouse/Helpers/RoomHelper.cs index 9ce5569a..25211547 100644 --- a/ProjectLighthouse/Helpers/RoomHelper.cs +++ b/ProjectLighthouse/Helpers/RoomHelper.cs @@ -17,6 +17,10 @@ namespace LBPUnion.ProjectLighthouse.Helpers SlotId = 0, }; + private static int roomIdIncrement = 0; + + internal static int RoomIdIncrement => roomIdIncrement++ - 1; + public static FindBestRoomResponse? FindBestRoom(User user, string location) { bool anyRoomsLookingForPlayers = Rooms.Any(r => r.IsLookingForPlayers); @@ -99,16 +103,28 @@ namespace LBPUnion.ProjectLighthouse.Helpers ); public static Room CreateRoom(List users, RoomSlot? slot = null) { - Room room = new(); - - room.Players = users; - room.State = RoomState.Idle; - room.Slot = slot ?? PodSlot; + Room room = new() + { + RoomId = RoomIdIncrement, + Players = users, + State = RoomState.Idle, + Slot = slot ?? PodSlot, + }; Rooms.Add(room); return room; } - public static Room? FindRoomByUser(User user) => Rooms.FirstOrDefault(r => r.Players.Contains(user)); + public static Room? FindRoomByUser(User user) + { + foreach (Room room in Rooms) + { + foreach (User player in room.Players) + { + if (user == player) return room; + } + } + return null; + } } } \ No newline at end of file diff --git a/ProjectLighthouse/Types/Match/Room.cs b/ProjectLighthouse/Types/Match/Room.cs index 88e967be..213e565f 100644 --- a/ProjectLighthouse/Types/Match/Room.cs +++ b/ProjectLighthouse/Types/Match/Room.cs @@ -5,6 +5,8 @@ namespace LBPUnion.ProjectLighthouse.Types.Match { public class Room { + public int RoomId; + public List Players; public RoomState State; public RoomSlot Slot; @@ -13,5 +15,18 @@ namespace LBPUnion.ProjectLighthouse.Types.Match public bool IsLookingForPlayers => this.State == RoomState.DivingIntoLevel || this.State == RoomState.DivingInWaiting; public User Host => this.Players[0]; + + #nullable enable + public static bool operator ==(Room? room1, Room? room2) + { + if (ReferenceEquals(room1, room2)) return true; + if ((object?)room1 == null || (object?)room2 == null) return false; + + return room1.RoomId == room2.RoomId; + } + public static bool operator !=(Room? room1, Room? room2) => !(room1 == room2); + + public override int GetHashCode() => this.RoomId; + #nullable disable } } \ No newline at end of file diff --git a/ProjectLighthouse/Types/User.cs b/ProjectLighthouse/Types/User.cs index 6f7f7b6b..9668256d 100644 --- a/ProjectLighthouse/Types/User.cs +++ b/ProjectLighthouse/Types/User.cs @@ -197,10 +197,10 @@ namespace LBPUnion.ProjectLighthouse.Types } [SuppressMessage("ReSharper", "ConditionIsAlwaysTrueOrFalse")] - public static bool operator ==(User user1, User user2) + public static bool operator ==(User? user1, User? user2) { if (ReferenceEquals(user1, user2)) return true; - if ((object)user1 == null || (object)user2 == null) return false; + if ((object?)user1 == null || (object?)user2 == null) return false; return user1.UserId == user2.UserId; }