Add the rest of the major contests, .udc files and the registry
ARRL DX, IARU HF, Sweepstakes, RTTY Roundup, NAQP and general logging join CQ WW and CQ WPX. The Cabrillo QSO line is now built column by column by the contest, because Sweepstakes puts the callsign after the serial number where everyone else puts it first. User-defined contests read the subset of N1MM's .udc settings that covers the exchange, the dupe rule, points and up to three multipliers. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
77
src/Nonemm.Contests/ContestRegistry.cs
Normal file
77
src/Nonemm.Contests/ContestRegistry.cs
Normal file
@@ -0,0 +1,77 @@
|
||||
using Nonemm.Contests.Rules;
|
||||
using Nonemm.Contests.Udc;
|
||||
using Nonemm.Core;
|
||||
|
||||
namespace Nonemm.Contests;
|
||||
|
||||
/// The contests on offer: the ones written into the program, plus whatever
|
||||
/// `.udc` files the operator has put in the user-defined contests folder.
|
||||
public sealed class ContestRegistry
|
||||
{
|
||||
private static readonly IReadOnlyList<ContestChoice> BuiltIn =
|
||||
[
|
||||
new("CQWW", "CQ World Wide DX", [ModeCategory.Cw, ModeCategory.Phone], m => new CqWorldWide(m)),
|
||||
new("CQWPX", "CQ WPX", [ModeCategory.Cw, ModeCategory.Phone, ModeCategory.Digital], m => new CqWpx(m)),
|
||||
new("ARRLDX", "ARRL International DX", [ModeCategory.Cw, ModeCategory.Phone], m => new ArrlDx(m)),
|
||||
new("IARUHF", "IARU HF World Championship", [ModeCategory.Cw, ModeCategory.Phone], _ => new IaruHf()),
|
||||
new("SS", "ARRL Sweepstakes", [ModeCategory.Cw, ModeCategory.Phone], m => new Sweepstakes(m)),
|
||||
new("ARRLRTTY", "ARRL RTTY Roundup", [ModeCategory.Digital], _ => new RttyRoundup()),
|
||||
new("NAQP", "North American QSO Party", [ModeCategory.Cw, ModeCategory.Phone, ModeCategory.Digital], m => new NorthAmericanQsoParty(m)),
|
||||
new("DX", "General logging", [], _ => new GeneralLogging()),
|
||||
];
|
||||
|
||||
private readonly Dictionary<string, ContestChoice> byName;
|
||||
|
||||
private ContestRegistry(Dictionary<string, ContestChoice> byName) => this.byName = byName;
|
||||
|
||||
public static ContestRegistry Create(IEnumerable<UdcFile>? userDefined = null)
|
||||
{
|
||||
Dictionary<string, ContestChoice> byName = BuiltIn.ToDictionary(
|
||||
c => c.Name,
|
||||
StringComparer.OrdinalIgnoreCase);
|
||||
foreach (UdcFile file in userDefined ?? [])
|
||||
{
|
||||
UserDefinedContest contest = new(file);
|
||||
byName[contest.Name] = new ContestChoice(
|
||||
contest.Name,
|
||||
contest.DisplayName,
|
||||
contest.Modes,
|
||||
_ => new UserDefinedContest(file));
|
||||
}
|
||||
return new ContestRegistry(byName);
|
||||
}
|
||||
|
||||
/// Reads every `.udc` file in the folder. A file that will not parse is
|
||||
/// reported rather than silently left out.
|
||||
public static ContestRegistry FromFolder(string folder, out IReadOnlyList<string> problems)
|
||||
{
|
||||
List<UdcFile> files = [];
|
||||
List<string> failures = [];
|
||||
if (Directory.Exists(folder))
|
||||
{
|
||||
foreach (string path in Directory.EnumerateFiles(folder, "*.udc"))
|
||||
{
|
||||
try
|
||||
{
|
||||
files.Add(UdcFile.Parse(File.ReadAllText(path)));
|
||||
}
|
||||
catch (Exception e) when (e is FormatException or IOException)
|
||||
{
|
||||
failures.Add($"{path}: {e.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
problems = failures;
|
||||
return Create(files);
|
||||
}
|
||||
|
||||
public IReadOnlyList<ContestChoice> Choices =>
|
||||
byName.Values.OrderBy(c => c.DisplayName, StringComparer.Ordinal).ToList();
|
||||
|
||||
public Contest Create(string name, ModeCategory mode) =>
|
||||
byName.TryGetValue(name, out ContestChoice? choice)
|
||||
? choice.Create(mode)
|
||||
: throw new KeyNotFoundException($"no contest named '{name}'");
|
||||
|
||||
public bool Has(string name) => byName.ContainsKey(name);
|
||||
}
|
||||
Reference in New Issue
Block a user