// Sort through Rooms to find next one public static int NextRoomID() { // Find vacant Rooms. If none, create one and add it to list List<int> roomIDs = new List<int>(); vacantRoomInit: var allRooms = new RoomRepository().All().ToList(); var vacantRoomsWithUsers = (from room in allRooms where Enumerable.Range(1, 4).Contains(UserProfileController.GetUserProfileCount(room.RoomID)) select new { RoomID = room.RoomID }).ToList(); var emptyRooms = (from room in allRooms where UserProfileController.GetUserProfileCount(room.RoomID) == 0 select new { RoomID = room.RoomID }).ToList(); if (vacantRoomsWithUsers.Count == 0 && emptyRooms.Count > 0) { // All rooms with users are full but an empty one exists, return empty room return emptyRooms.First().RoomID; } else if (vacantRoomsWithUsers.Count == 0 && emptyRooms.Count == 0) { // Create new Room, re-populate vacantRoomIDs or obtain new RoomID another way var roomRep = new RoomRepository(); Room newRoom = new Room { Name = "Public Room" }; roomRep.Create(newRoom); roomRep.SaveChanges(); goto vacantRoomInit; } profileInit: // Change this to reflect online users only at deployment var allProfiles = new UserProfileRepository().All().ToList(); var profiles = (from p in allProfiles.Where(p => Membership.GetUser(p.aspUserID).IsOnline) where vacantRoomsWithUsers.Select(r => r.RoomID).Contains(p.RoomID) && !VisitedRooms.Select(r => r.RoomID).Contains(p.RoomID) select p).ToList(); if (profiles.Count == 0) { VisitedRooms.Clear(); goto profileInit; } // Obtain room with earliest JoinRoomTime var resultRoom = (from p in profiles group p by p.RoomID into results orderby results.Max(x => x.JoinRoomTime) select new { RoomID = results.Key }).First(); VisitedRooms.Add(new RoomRepository().GetById(resultRoom.RoomID)); return resultRoom.RoomID; }
public static List<UserProfile> GetUserProfiles(int roomID) { var profiles = new UserProfileRepository().All().Where(p => p.RoomID == roomID).ToList(); profiles = profiles.Where(p => Membership.GetUser(p.aspUserID).IsOnline).ToList(); return profiles; }
public static int GetUserProfileCount(int roomID) { var results = new UserProfileRepository().All().Where(p => p.RoomID == roomID).ToList(); results = results.Where(p => (Membership.GetUser(p.aspUserID).IsOnline)).ToList(); return results.Count; }