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:
Erik
2026-08-27 10:35:28 +00:00
parent 0f84bc8972
commit ef631f1dc4
26 changed files with 1162 additions and 13 deletions

View File

@@ -0,0 +1,150 @@
using Nonemm.Core;
namespace Nonemm.Contests.Udc;
/// A contest read from a `.udc` file. The subset of N1MM's settings this reads
/// covers the exchange, the dupe rule, points and up to three multipliers;
/// settings it does not read are ignored rather than guessed at.
public sealed class UserDefinedContest : Contest
{
private readonly UdcFile file;
private readonly UdcPoints points;
private readonly IReadOnlyList<UdcMultiplier> multipliers;
private readonly IReadOnlyList<ExchangeField> exchangeFields;
public UserDefinedContest(UdcFile file)
{
this.file = file;
points = new UdcPoints(file.Text("PointsPerContact", "1"));
multipliers = ReadMultipliers(file);
exchangeFields = ReadExchangeFields(file);
}
public static UserDefinedContest Load(string path) =>
new(UdcFile.Parse(File.ReadAllText(path)));
public string Name => file.Text("Name").ToUpperInvariant();
public string DisplayName => file.Text("DisplayName", Name);
public string CabrilloName => file.Text("CabrilloName", Name);
public IReadOnlyList<ExchangeField> ExchangeFieldsFor(StationInfo me) => exchangeFields;
public IReadOnlyList<string> MultiplierNames =>
new[] { "Multiplier1Name", "Multiplier2Name", "Multiplier3Name" }
.Select(key => file.Text(key))
.Where(name => name.Length > 0)
.ToList();
public DupeScope DupeScope => file.Number("DupeType", 2) switch
{
1 => DupeScope.Once,
3 => DupeScope.PerBandAndMode,
4 => DupeScope.Never,
_ => DupeScope.PerBand,
};
public bool HasSerialNumbers =>
file.Text("DefaultContestExchange").StartsWith("001", StringComparison.Ordinal);
public IReadOnlyList<ModeCategory> Modes => file.Text("Mode", "CW").ToUpperInvariant() switch
{
"CW" => [ModeCategory.Cw],
"SSB" => [ModeCategory.Phone],
"RTTY" => [ModeCategory.Digital],
_ => [ModeCategory.Cw, ModeCategory.Phone, ModeCategory.Digital],
};
public string SentExchangeFor(StationInfo me) => file.Text("DefaultContestExchange", "599");
public int PointsFor(QsoContext qso) => points.For(qso);
public IReadOnlyList<Multiplier> MultipliersFor(QsoContext qso) =>
multipliers.Select(m => m.For(qso)).OfType<Multiplier>().ToList();
public int TotalScore(ScoreTally tally) =>
multipliers.Count == 0 ? tally.Points : tally.Points * tally.TotalMultipliers;
public CabrilloExchange CabrilloExchange(Qso qso, StationInfo me) =>
new(
[
new CabrilloField(me.Callsign, 13),
new CabrilloField(qso.SentReport, 3),
new CabrilloField(SentExchangePart(qso), 6),
],
[
new CabrilloField(qso.Call.Text, 13),
new CabrilloField(qso.ReceivedReport, 3),
new CabrilloField(ReceivedExchangePart(qso), 6),
]);
private string SentExchangePart(Qso qso) =>
HasSerialNumbers ? $"{qso.SentNumber:000}" : file.Text("DefaultContestExchange");
private static string ReceivedExchangePart(Qso qso) =>
qso.Exchange1.Length > 0 ? qso.Exchange1
: qso.Section.Length > 0 ? qso.Section
: qso.ReceivedNumber > 0 ? $"{qso.ReceivedNumber:000}"
: qso.MiscText;
private static IReadOnlyList<UdcMultiplier> ReadMultipliers(UdcFile file)
{
UdcMultiplierScope shared = (UdcMultiplierScope)file.Number("IsMultPer", 0);
List<UdcMultiplier> found = [];
for (int index = 1; index <= 3; index++)
{
string source = file.Text(index == 1 ? "MultSqlString" : $"MultSqlString{index}");
if (source.Length == 0)
{
continue;
}
UdcMultiplierScope scope = (UdcMultiplierScope)file.Number($"IsMult{index}Per", 0);
if (scope == UdcMultiplierScope.None)
{
scope = shared == UdcMultiplierScope.None ? UdcMultiplierScope.PerBand : shared;
}
found.Add(new UdcMultiplier(
index,
source,
scope,
file.List(index == 1 ? "CountMultOnlyFor" : $"CountMultOnlyFor{index}")));
}
return found;
}
/// `EntryWindowInfo` names the boxes; `FrameText` labels them. Only the
/// received boxes go in the exchange, because the sent side is fixed for
/// the whole contest.
private static IReadOnlyList<ExchangeField> ReadExchangeFields(UdcFile file)
{
string[] labels = file.Text("FrameText").Split(' ', StringSplitOptions.RemoveEmptyEntries);
List<ExchangeField> fields = [];
int labelAt = 0;
foreach (string box in file.List("EntryWindowInfo").Where(p => !p.All(char.IsAsciiDigit)))
{
ExchangeField? field = BoxToField(box, labels.Length > labelAt ? labels[labelAt] : box);
labelAt++;
if (field is not null)
{
fields.Add(field);
}
}
return fields.Count > 0
? fields
: [new ExchangeField("RST", ExchangeSlot.ReceivedReport, ExchangeFieldKind.Report)];
}
private static ExchangeField? BoxToField(string box, string label) => box.ToUpperInvariant() switch
{
"RCVTEXT" => new ExchangeField(label, ExchangeSlot.ReceivedReport, ExchangeFieldKind.Report),
"RCVNRTEXT" => new ExchangeField(label, ExchangeSlot.SerialNumber, ExchangeFieldKind.Number),
"EXCHANGE1TEXT" => new ExchangeField(label, ExchangeSlot.Exchange1, ExchangeFieldKind.Text) { Width = 6 },
"SECTTEXT" => new ExchangeField(label, ExchangeSlot.Section, ExchangeFieldKind.ArrlSection),
"GRIDSQUARETEXT" => new ExchangeField(label, ExchangeSlot.GridSquare, ExchangeFieldKind.Grid),
"NAMETEXT" => new ExchangeField(label, ExchangeSlot.Name, ExchangeFieldKind.Text) { Width = 10 },
"MISCTEXT" => new ExchangeField(label, ExchangeSlot.MiscText, ExchangeFieldKind.Text) { Width = 8 },
"COMMENTTEXT" => new ExchangeField(label, ExchangeSlot.Comment, ExchangeFieldKind.Text, IsRequired: false) { Width = 20 },
_ => null,
};
}