public void LoadUsers(List<String> Users) { //We use this boolean to know when to spend computational time to do group management over again Boolean ChangeInUsers = false; List<Notification> TempNotifications = new List<Notification>(); //Since these lists are sorted we can make use of //techniques that will enhance the efficiency of the history traversal Users.Sort(); int count = ActiveUsers.Count; for (int i = 0; i < count; i++) { User user = ActiveUsers[i]; if (user is Guest) { ActiveUsers.RemoveAt(i); i--; count = ActiveUsers.Count; } } NumberOfGuests = 0; Groups[0].NumberOfUsers = NumberOfGuests; //If the list of active users are empty // then just create one where all the users present are added if (HistoryMatrix.Count == 0) { foreach (String id in Users) { User cur; if (id.Equals("Guest")) { cur = new Guest(); NumberOfGuests++; Groups[0].NumberOfUsers = NumberOfGuests; TempNotifications.Add(new Notification("New guest detected.", 2)); } else { cur = new RegisteredUser(); cur.Load(id); ChangeInUsers = true; String Name = ((RegisteredUser)cur).FullName; int type; if (((RegisteredUser)cur).Gender.Equals("Female")) type = 0; else type = 2; TempNotifications.Add(new Notification("Registered user detected.", type)); //TempNotifications.Add(new Notification(Name +" detected.", type)); } ActiveUsers.Add(cur); } } // otherwise if the list is not empty we remove users that are no longer present // and add users that are new to the list else { Boolean temp; temp = AddToList(Users, TempNotifications); if (temp) { ChangeInUsers = true; temp = false; } temp = PruneList(Users, TempNotifications); if (temp) ChangeInUsers = true; } HistoryMatrix.Add(Users); // if the history count is more than 5 (which means that 600ms*5 = every 3 seconds) // the oldest part of the list needs to be forgotten // I changed this to 10 instead of 5 which is perhaps better if (HistoryMatrix.Count > 10) { HistoryMatrix.RemoveAt(0); } // the management of the active users will be complete at this point // this is where group management comes into play if (ChangeInUsers) { foreach (User user in ActiveUsers) { if (user is RegisteredUser) { String SQL = String.Format("SELECT GroupID FROM UserGroup WHERE UserID = '{0}';", ((RegisteredUser)user).UserID); try { DataTable DT = OcDBLink.getDataTable(SQL); for (int x = 0; x < DT.Rows.Count; x++) { DataRow curRow = DT.Rows[x]; String groupID = curRow.ItemArray[0].ToString(); Boolean found = false; foreach (Group group in Groups) { if (group.GroupID.Equals(groupID)) { found = true; group.NumberOfUsers++; ChangeInGroups = true; } } if (!found) { Group cur = new Group(groupID); cur.NumberOfUsers++; Groups.Add(cur); ChangeInGroups = true; } } } catch { } } } } int counter = 0; Boolean newGuest = false; for (int i = 0; i < TempNotifications.Count; i++) { if (TempNotifications[i].Type != 3) { counter++; } else { if (!newGuest) { Notifications.Enqueue(TempNotifications[i]); newGuest = true; } TempNotifications.RemoveAt(i); } } if (counter > 3) Notifications.Enqueue(new Notification("Group of users detected.", 1)); else for (int i = 0; i < TempNotifications.Count; i++) Notifications.Enqueue(TempNotifications[i]); }
private Boolean AddToList(List<String> Users, List<Notification> TempNotifications) { Boolean Return = false; // To add to a list first we need to be sure the user is not already in the list // Then ascertain if the person was detected atleast once before (this will be used to improve the accuracy of the users being selected) foreach (String newUser in Users) { Boolean CanAdd = true; // because the list is sorted, it is plausible to do the binary search method. // but this method will not be used because it only becomes more efficient than linear search after a certain size // the list's sizes are too small for it to make a significant impact foreach (User u in ActiveUsers) { if (u is RegisteredUser) { RegisteredUser user = (RegisteredUser)u; if (user.UserID.Equals(newUser)) { CanAdd = false; break; } } } if (CanAdd) { foreach (String user in HistoryMatrix[HistoryMatrix.Count - 1]) { if (user.Equals(newUser)) { User cur; if (user.Equals("Guest")) { cur = new Guest(); NumberOfGuests++; Groups[0].NumberOfUsers = NumberOfGuests; TempNotifications.Add(new Notification("New guest detected.", 2)); } else { cur = new RegisteredUser(); cur.Load(user); Return = true; String Name = ((RegisteredUser)cur).FullName; int type; if (((RegisteredUser)cur).Gender.Equals("Female")) type = 0; else type = 2; TempNotifications.Add(new Notification("Registered user detected.", type)); //TempNotifications.Add(new Notification(Name + " detected.", type)); } ActiveUsers.Add(cur); break; } } } } return Return; }