public override void AddUser(DIUser user) { if (user.Division == null) { user.Division = "Unassigned"; } if (SubUnits.All(x => x.UnitId != user.Division)) { SubUnits.Add(new DIDivision(user.Division)); } SubUnits.First(x => x.UnitId == user.Division).AddUser(user); if (user.Position == DIPosition.HouseGeneral) { _houseGeneralId = user.Id; } else if (user.Position == DIPosition.FirstCommander) { _firstCommanderId = user.Id; } else if (user.Position == DIPosition.HouseAide) { _houseAideId = user.Id; } }
public override void AddUser(DIUser user) { _members.Add(user); if (user.Position == DIPosition.RosterLeader) { _rosterLeaderId = user.Id; } }
public void Update() { var oldUsers = Members.ToList(); SubUnits.Clear(); try { var handler = new HttpClientHandler(); handler.CookieContainer = new CookieContainer(); ConfigUtil.Config.AuthConfig.Cookies .Select(x => new System.Net.Cookie(x.Name, x.Value, x.Path, x.Domain)) .ToList() .ForEach(x => handler.CookieContainer.Add(x)); var result = new DIHttpClient(handler).GetStringAsync("mdr/").GetAwaiter().GetResult(); var doc = new HtmlDocument(); doc.LoadHtml(result); foreach (var node in doc.DocumentNode.SelectNodes("//a")) { var user = new DIUser(); if (!Enum.TryParse(node.Attributes["class"]?.Value.Replace("-", "") ?? "", true, out user.Rank)) { continue; } user.Name = node.InnerText.Split(' ')[0].Trim(); user.Id = Convert.ToInt32(ProfileUrlRegex.Match(node.Attributes["href"].Value).Groups[2].Value); AddDIUnitInformation(user, node); DIUser oldUser = null; if ((oldUser = oldUsers.FirstOrDefault(x => x.Id == user.Id)) != null) { user.DiscordId = oldUser.DiscordId; user.ImageUrl = oldUser.ImageUrl; } AddUser(user); } } catch (Exception e) { e.ToString(); } }
private static void AddDIUnitInformationFromNode(DIUser user, HtmlNode node) { AddDIPosition(user, node); do { node = node.ParentNode; if (node == node.OwnerDocument.DocumentNode) { break; } if (node.Name == "li" && node.HasClass("RL-li")) { user.Position = DIPosition.RosterLeader; continue; } if (user.Roster == null && node.Name == "div" && node.HasClass("roster-container")) { var header = node.FirstPreviousSibling(x => x.Name == "h5" && x.HasClass("roster-header")); user.Roster = Regex.Match(header.InnerHtml, @"^(.+)\([0-9]+\)").Groups[1].Value.TrimEnd(); } else if (user.Team == null && node.Name == "div" && node.HasClass("team-wrapper")) { var header = node.FirstPreviousSibling(x => x.Name == "h4"); user.Team = Regex.Match(header.InnerHtml, @"^(.+)\([0-9]+\)").Groups[1].Value.TrimEnd(); } else if (user.Division == null && node.Name == "div" && node.HasClass("div-container")) { user.Division = node .FirstChild(x => x.Name == "div" && x.HasClass("div-header")) .FirstChild(x => x.Name == "h3" && x.HasClass("division-title")) .GetClasses() .First(x => x != "division-title"); } else if (user.House == null && node.Name == "div" && node.HasClass("house-container")) { var house = node .GetClasses() .First(x => x != "house-container"); user.House = $"House {char.ToUpper(house[0]) + house.Substring(1)}"; break; } } while (true); }
public override void AddUser(DIUser user) { if (user.House == null) { user.House = "Unassigned"; } if (SubUnits.All(x => x.UnitId != user.House)) { SubUnits.Add(new DIHouse(user.House)); } SubUnits.First(x => x.UnitId == user.House).AddUser(user); }
public override void AddUser(DIUser user) { if (user.Roster == null) { user.Roster = "Unassigned"; } if (SubUnits.All(x => x.UnitId != user.Roster)) { SubUnits.Add(new DIRoster(user.Roster)); } SubUnits.First(x => x.UnitId == user.Roster).AddUser(user); if (user.Position == DIPosition.TeamLeader) { _teamLeaderId = user.Id; } else if (user.Position == DIPosition.SecondInCommand) { _secondInCommandId = user.Id; } }
public override void AddUser(DIUser user) { if (user.Team == null) { user.Team = "Unassigned"; } if (SubUnits.All(x => x.UnitId != user.Team)) { SubUnits.Add(new DITeam(user.Team)); } SubUnits.First(x => x.UnitId == user.Team).AddUser(user); if (user.Position == DIPosition.DivisionCommander) { _divisionCommanderId = user.Id; } else if (user.Position == DIPosition.DivisionVice) { _divisionViceId = user.Id; } }
private static void AddDIPosition(DIUser user, HtmlNode node) { var position = node .FirstPreviousSibling(x => x.Name == "a" && x.HasClass("position"))? .GetClasses()? .First(x => x != "topdisplay" && x != "position"); if (position == "HG") { user.Position = DIPosition.HouseGeneral; } else if (position == "FC") { user.Position = DIPosition.FirstCommander; } else if (position == "DC") { user.Position = DIPosition.DivisionCommander; } else if (position == "DV") { user.Position = DIPosition.DivisionVice; } else if (position == "TL") { user.Position = DIPosition.TeamLeader; } else if (position == "twoIC") { user.Position = DIPosition.SecondInCommand; } else if (position == "RL") { user.Position = DIPosition.RosterLeader; } }
public abstract void AddUser(DIUser user);
private static void AddDIUnitInformation(DIUser user, HtmlNode node) { AddDIUnitInformationFromNode(user, node); if (user.House == null) { throw new Exception("User could not be assigned a house!"); } if (user.Division == null) // House Leadership { string[] additions = { "HG", "FC" }; var house = user.House.Split(' ')[1].ToLowerInvariant(); var rosterAdditionNode = node .SelectSingleNode($"//div[@class='house-container {house}']") .Descendants() .FirstOrDefault(x => x.Name == "a" && x.HasClass("roster-addition") && additions.Contains(x.InnerText) && x.Attributes["title"]?.Value.Contains(user.Name) == true); if (rosterAdditionNode != null) { user.Roster = Regex .Match(rosterAdditionNode.ParentNode.InnerText.Split('-')[0].Trim(), @"^(.+)\([0-9]+\)$") .Groups[1].Value.Trim(); if (rosterAdditionNode.InnerText == "HG") { user.Position = DIPosition.HouseGeneral; } else if (rosterAdditionNode.InnerText == "FC") { user.Position = DIPosition.FirstCommander; } AddDIUnitInformationFromNode(user, rosterAdditionNode); } else { return; } } if (user.Team == null) // Division Leadership { string[] additions = { "DC", "DV" }; var divisionNode = node .SelectNodes("//div[@class='div-container']") // Find the correct division .FirstOrDefault(x => x .FirstChild(y => y.Name == "div" && y.HasClass("div-header")) .FirstChild(y => y.Name == "h3" && y.HasClass("division-title")) .GetClasses() .First(y => y != "division-title") == user.Division); var rosterAdditionNode = divisionNode? .Descendants()? .FirstOrDefault(x => x.Name == "a" && x.HasClass("roster-addition") && additions.Contains(x.InnerText) && x.Attributes["title"]?.Value.Contains(user.Name) == true); if (rosterAdditionNode != null) { user.Roster = Regex.Match(rosterAdditionNode.ParentNode.InnerText.Split('-')[0].Trim(), @"^(.+)\([0-9]+\)$").Groups[1].Value.Trim(); if (rosterAdditionNode.InnerText == "DC") { user.Position = DIPosition.DivisionCommander; } else if (rosterAdditionNode.InnerText == "DV") { user.Position = DIPosition.DivisionVice; } AddDIUnitInformationFromNode(user, rosterAdditionNode); } else { return; } } if (user.Roster == null) // Team Leadership { string[] additions = { "TL", "2IC", "COACH" }; var divisionNode = node .SelectNodes("//div[contains(@class,'div-container')]") // Find the correct division .FirstOrDefault(x => x .FirstChild(y => y.Name == "div" && y.HasClass("div-header")) .FirstChild(y => y.Name == "h3" && y.HasClass("division-title")) .GetClasses() .First(y => y != "division-title") == user.Division); var teamNode = divisionNode? .Descendants()? .Where(x => x.Name == "div" && x.GetClasses().Contains("team-wrapper")) .FirstOrDefault(x => x.FirstPreviousSibling(y => y.Name == "h4").InnerText.StartsWith(user.Team)); var rosterAdditionNode = teamNode? .Descendants()? .FirstOrDefault(x => x.Name == "a" && x.HasClass("roster-addition") && additions.Contains(x.InnerText) && x.Attributes["title"]?.Value.Contains(user.Name) == true); if (rosterAdditionNode != null) { user.Roster = Regex.Match(rosterAdditionNode.ParentNode.InnerText.Split('-')[0].Trim(), @"^(.+)\([0-9]+\)").Groups[1].Value.Trim(); if (rosterAdditionNode.InnerText == "TL") { user.Position = DIPosition.TeamLeader; } else if (rosterAdditionNode.InnerText == "2IC") { user.Position = DIPosition.SecondInCommand; } } } }