Skip to content

Contest engine

The contest engine is pure code in shared/contest-engine/. A bug here changes contest results.

A contest uses either the legacy engine (from before 0.4, and the default) or v1, set in contests.engine. Moving a contest between them re-scores it in one transaction and is audited. Moving it back restores the old scores exactly. The legacy engine is shared/contests.ts and server/lib/cabrillo.ts. Write new contests as v1.

A template is JSON that describes periods, bands and modes, categories, the exchange, validity, dupes, points, multipliers, bonuses, the score formula, checks and the Cabrillo output. example.template.json shows the shape.

A TypeBox schema in schema.ts produces the types, the validator and template.schema.json. Regenerate the file with npm run build:schema and commit it. A test fails if it is stale.

validateTemplate() migrates old versions, resolves extends, checks references, tokens and Cabrillo columns, and validates each variant as merged. It keeps unknown keys and reports them as rules that are not applied.

  • A rule check produces a warning. The entry form refuses only strict closed-list fields and bands or modes that the contest does not have.
  • Score formulas are points*mults, …*power or …*objectives, each with an optional +bonus.
  • Reference lists are named and versioned, for example lists/arrl-sections@2026.json.
  • The schema is at version 4. Version 2 adds objectives, weightBy, the location scope, bonus maxUnits, the qsy check and highest power. Version 3 adds reference, and version 4 adds schedule (RRULE). Each is opt-in with a migration, and older golden tests must pass unchanged.
  • The combination (id, version, source) is immutable. An edit creates a new version, and a contest moves to it only when someone re-pins it. The source (builtin, local or hub) is part of a template’s identity.

Score a log with scoreLog(template, entry, qsos), or step through it with startScoring, append and finish. checkQso and preview check a single QSO. For each QSO the engine resolves the entity, chooses the variant (once per entry), validates, checks for a dupe, awards points, applies multipliers, runs the checks and aggregates.

scoreLog folds append in time order, so incremental and full scoring agree. A QSO earlier than the last one re-scores everything. An invalid QSO stays in the log with an invalidReason and scores 0, and so does a dupe. Off-time, hour-cap and outside-period QSOs only produce warnings.

Entity resolution uses countryfiles/cty.json (DXCC, WAE, continent and zones). callsign.ts handles / forms, and wpx.ts handles prefixes. fillEntity() never overwrites a value that was typed or looked up. Rebuild the data with node scripts/build-cty.ts.

cabrillo.ts writes the log from output.cabrillo: the contest name, headers with tokens such as {category.catPower}, and either columns or a preset (arrl-ss, cqp, naqp, arrl-fd or standard). It truncates frequency to whole kHz and never rounds. Times are UTC. A QSO that scored nothing for a stated reason becomes X-QSO. Header categories come from categories.derive. cabrillo-read.ts does the inverse.

  • Built-in templates live in shared/contest-engine/templates/*.json, and the app installs them as builtin at start. A changed file needs a new version. A new template needs embedded tests[] and an import. CI runs all of them.
  • A v1 contest pins template_id, template_version and template_source.
  • Writes go through createContact. A backdated QSO rewrites the stored results inside its own transaction. Edits, deletes, imports and the bulk tools call recomputeContest.
  • transmitter_id, my_location and contact_extras are reserved for multi-operator logs, QTCs and rovers. Never remove a reserved hook because it looks unused.
  • The scripts on the Reference card fill in live from the entry form, and the app generates them when a template has none. Band plans in shared/bandplans/*.json are original work, never copied from a league chart.
  • The template editor needs contests.templates. Importing a template from JSON or from an N1MM+ .udc file produces a draft only.

To score a past log offline, run:

npm run contest:replay -- --template <id> --cabrillo <log>

The command uses no database. --start <ISO> shifts the contest period, and --expect <score> turns the run into a check.

The goldens in shared/golden/ and server/golden*.test.ts pin the legacy engine, and golden-v1.test.ts holds the v1 ports to them. cabrillo-v1.test.ts compares output byte for byte, and GOLDEN_UPDATE=1 regenerates the expected files. Add a golden test before you change scoring.