diff --git a/R7.Enrollment/Components/EntrantComparer.cs b/R7.Enrollment/Components/EntrantComparer.cs index 7a76a77..8cf26a8 100644 --- a/R7.Enrollment/Components/EntrantComparer.cs +++ b/R7.Enrollment/Components/EntrantComparer.cs @@ -6,7 +6,7 @@ namespace R7.Enrollment.Components { public class EntrantComparer: IComparer { - private IList _entranceDisciplines; + private readonly IList _entranceDisciplines; public EntrantComparer (IList entranceDisciplines) { diff --git a/R7.Enrollment/Components/EntrantRanker.cs b/R7.Enrollment/Components/EntrantRanker.cs new file mode 100644 index 0000000..6877c12 --- /dev/null +++ b/R7.Enrollment/Components/EntrantRanker.cs @@ -0,0 +1,20 @@ +using System.Linq; +using R7.Enrollment.Models; + +namespace R7.Enrollment.Components +{ + public class EntrantRanker + { + public void RankEntrants (Competition competition) + { + var entrantComparer = new EntrantComparer (competition.EntranceDisciplines); + var rank = 1; + foreach (var entrant in competition.Entrants.OrderByDescending (entr => entr, entrantComparer)) { + if (entrant.IsRanked ()) { + entrant.Rank = rank; + rank++; + } + } + } + } +} diff --git a/R7.Enrollment/Data/TandemRatingsDb.cs b/R7.Enrollment/Data/TandemRatingsDb.cs index c9af0d1..05a5af8 100644 --- a/R7.Enrollment/Data/TandemRatingsDb.cs +++ b/R7.Enrollment/Data/TandemRatingsDb.cs @@ -1,5 +1,6 @@ using System; using System.Xml.Linq; +using R7.Enrollment.Components; using R7.Enrollment.Models; namespace R7.Enrollment.Data @@ -15,11 +16,20 @@ public void Load (string path) var xml = XDocument.Load (path); if (xml.Root.Name == "enrEntrantRatingEnvironmentNode") { EntrantRatingEnvironment = ParseEntrantRatingEnvironmentNode (xml.Root); + RankEntrants (EntrantRatingEnvironment); return; } throw new ArgumentException ($"Unsupported node type: {xml.Root.Name}"); } + private void RankEntrants (EntrantRatingEnvironment env) + { + var entrantRanker = new EntrantRanker (); + foreach (var competition in env.Competitions) { + entrantRanker.RankEntrants (competition); + } + } + EntrantRatingEnvironment ParseEntrantRatingEnvironmentNode (XElement root) { var env = new EntrantRatingEnvironment (); diff --git a/R7.Enrollment/Models/Entrant.cs b/R7.Enrollment/Models/Entrant.cs index b7b9703..16ce390 100644 --- a/R7.Enrollment/Models/Entrant.cs +++ b/R7.Enrollment/Models/Entrant.cs @@ -29,6 +29,8 @@ public class Entrant public int StatusCode { get; set; } + public int? Rank { get; set; } + public bool IsRanked () => StatusCode != (int) EntrantStatus.TookAwayTheDocs && StatusCode != (int) EntrantStatus.RefusedToEnroll && diff --git a/R7.Enrollment/Renderers/RatingsHtmlRenderer.cs b/R7.Enrollment/Renderers/RatingsHtmlRenderer.cs index 2bbfd61..76174cc 100644 --- a/R7.Enrollment/Renderers/RatingsHtmlRenderer.cs +++ b/R7.Enrollment/Renderers/RatingsHtmlRenderer.cs @@ -104,13 +104,8 @@ public void RenderCompetition (Competition competition, XmlWriter html) html.WriteAttributeString ("class", "table table-bordered table-striped table-hover"); RenderEntrantsTableHeader (competition, html); - var entrantComparer = new EntrantComparer (competition.EntranceDisciplines); - var rank = 1; - foreach (var entrant in competition.Entrants.OrderByDescending (entr => entr, entrantComparer)) { - RenderEntrantTableRow (entrant, html, rank, competition.Plan); - if (entrant.IsRanked ()) { - rank++; - } + foreach (var entrant in competition.Entrants) { + RenderEntrantTableRow (entrant, html, competition.Plan); } // end table @@ -158,7 +153,7 @@ public void RenderEntrantsTableHeader (Competition competition, XmlWriter html) html.WriteEndElement (); } - public void RenderEntrantTableRow (Entrant entrant, XmlWriter html, int rank, int plan) + public void RenderEntrantTableRow (Entrant entrant, XmlWriter html, int plan) { html.WriteStartElement ("tr"); @@ -166,14 +161,14 @@ public void RenderEntrantTableRow (Entrant entrant, XmlWriter html, int rank, in if (_snilsComparer.SnilsNotNullAndEquals (entrant.Snils, Settings.Snils) || entrant.PersonalNumber == Settings.PersonalNumber) { cssClass += " enr-target-entrant-row"; } - if (entrant.IsRanked () && rank == plan) { + if (entrant.IsRanked () && entrant.Rank != null && entrant.Rank.Value == plan) { cssClass += " enr-entrant-row-cutoff"; } if (!string.IsNullOrEmpty (cssClass)) { html.WriteAttributeString ("class", cssClass); } - html.WriteElementString ("td", entrant.IsRanked () ? rank.ToString () : string.Empty); + html.WriteElementString ("td", entrant.Rank?.ToString () ?? string.Empty); if (!Settings.Depersonalize) { html.WriteElementString ("td", entrant.Name);