Created
December 5, 2021 23:23
-
-
Save RobertBouillon/3ec79c2acb88b411a39e43aacb4c00ce to your computer and use it in GitHub Desktop.
Start List Report
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Newtonsoft.Json; | |
using Spin.Pillars.FileSystem; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using USS.Tempus.Formats.Progression; | |
namespace USS.Tempus.Reports | |
{ | |
public class StartListReport : Report | |
{ | |
public static StartListReport Load(File file) => JsonConvert.DeserializeObject<StartListReport>(file.ReadAllText()); | |
public static StartListReport Generate(ProgressionRound round) => new() | |
{ | |
CompetitionName = round.Distance.Division.Division.Competition.Name, | |
RoundName = round.Name, | |
Groups = round.Groups.OrderByDescending(x => x.Letter).Select(CreateGroup).ToList(), | |
EventNumber = round.Number | |
}; | |
private static Group CreateGroup(ProgressionGroup group) => new( | |
Letter: group.Letter, | |
DirectQualifiers: group.DirectQualifiers, | |
ExtraQualifiers: group.ExtraQualifiers, | |
Heats: group.Races.Count, | |
Races: group.Races.Select(CreateRace).ToList() | |
); | |
private static Race CreateRace(ProgressionRace race) => new( | |
Number: race.Number, | |
Heat: race.Heat, | |
Assignments: race.SkaterAssignments.Select(CreateAssignment).ToList() | |
); | |
private static Assignment CreateAssignment(SkaterAssignment assignment) => new( | |
Position: assignment.Position, | |
SkaterNumber: assignment.Skater.Number.Value, | |
SkaterName: assignment.Skater.DisplayName, | |
SkaterAffiliation: assignment.Skater.Affiliation | |
); | |
public override string Name => "Start List"; | |
public string CompetitionName { get; set; } | |
public string RoundName { get; set; } | |
public List<Group> Groups { get; set; } | |
public int EventNumber { get; private set; } | |
public record Group(char Letter, int? DirectQualifiers, int? ExtraQualifiers, int Heats, List<Race> Races); | |
public record Race(int Number, int Heat, List<Assignment> Assignments); | |
public record Assignment(int Position, int SkaterNumber, string SkaterName, string SkaterAffiliation); | |
public override string GenerateCSV() | |
{ | |
StringBuilder sb = new StringBuilder(); | |
sb.AppendLine(CompetitionName); | |
sb.AppendLine(RoundName); | |
sb.AppendLine("Start List"); | |
sb.AppendLine(); | |
foreach (var group in Groups) | |
{ | |
if (group.DirectQualifiers.HasValue) | |
{ | |
sb.Append($"Group {group.Letter} Qualifying {group.DirectQualifiers}"); | |
if (group.ExtraQualifiers.HasValue) | |
sb.AppendLine($"+({group.ExtraQualifiers.Value})"); | |
else | |
sb.AppendLine(); | |
} | |
else | |
sb.AppendLine($"Final {group.Letter}"); | |
foreach (var race in group.Races) | |
{ | |
if (group.Races.Count > 1) | |
sb.AppendLine($"#{race.Number}: Heat {race.Heat} of {group.Races.Count} ({group.Letter})"); | |
foreach (var assignment in race.Assignments) | |
sb.AppendLine($"{assignment.Position},{assignment.SkaterNumber},{assignment.SkaterName},{assignment.SkaterAffiliation}"); | |
sb.AppendLine(); | |
} | |
} | |
return sb.ToString(); | |
} | |
public override void GenerateExcel(File destination) => | |
ExcelTemplate | |
.Load(Env.ReportTemplatesDirectory.GetFile("Start List.xlsx")) | |
.Generate(this, destination); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment