Skip to content

Commit

Permalink
Introduce EntrantRanker class #23
Browse files Browse the repository at this point in the history
  • Loading branch information
roman-yagodin committed Jul 31, 2021
1 parent fab0def commit 78ad51b
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 11 deletions.
2 changes: 1 addition & 1 deletion R7.Enrollment/Components/EntrantComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace R7.Enrollment.Components
{
public class EntrantComparer: IComparer<Entrant>
{
private IList<EntranceDiscipline> _entranceDisciplines;
private readonly IList<EntranceDiscipline> _entranceDisciplines;

public EntrantComparer (IList<EntranceDiscipline> entranceDisciplines)
{
Expand Down
20 changes: 20 additions & 0 deletions R7.Enrollment/Components/EntrantRanker.cs
Original file line number Diff line number Diff line change
@@ -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++;
}
}
}
}
}
10 changes: 10 additions & 0 deletions R7.Enrollment/Data/TandemRatingsDb.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Xml.Linq;
using R7.Enrollment.Components;
using R7.Enrollment.Models;

namespace R7.Enrollment.Data
Expand All @@ -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 ();
Expand Down
2 changes: 2 additions & 0 deletions R7.Enrollment/Models/Entrant.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 &&
Expand Down
15 changes: 5 additions & 10 deletions R7.Enrollment/Renderers/RatingsHtmlRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -158,22 +153,22 @@ 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");

var cssClass = string.Empty;
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);
Expand Down

0 comments on commit 78ad51b

Please sign in to comment.