feat: Reworked the awards logic so it is easy to add new ones.
This commit is contained in:
+111
-19
@@ -13,6 +13,8 @@
|
||||
package award
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"regexp"
|
||||
"sort"
|
||||
@@ -113,26 +115,116 @@ type OrRule struct {
|
||||
Prefix string `json:"prefix,omitempty"` // prepended to each found reference
|
||||
}
|
||||
|
||||
// Defaults are the built-in awards seeded on first run (then user-editable).
|
||||
func Defaults() []Def {
|
||||
// Confirmed = any confirmation (LoTW or paper QSL). Validated = the stricter
|
||||
// "electronically verified" tier: LoTW only — a paper QSL confirms but does
|
||||
// NOT validate (matches ARRL/Log4OM). eQSL counts only where the program
|
||||
// accepts it (WAC).
|
||||
lq := []string{"lotw", "qsl"}
|
||||
lo := []string{"lotw"}
|
||||
return []Def{
|
||||
{Code: "DXCC", Name: "DX Century Club", Type: TypeDXCC, Field: "dxcc", Confirm: lq, Validate: lo, Total: 340, Valid: true, Builtin: true, Protected: true},
|
||||
{Code: "WAS", Name: "Worked All States", Type: TypeQSOFields, Field: "state", MatchBy: "code", ExactMatch: true, DXCCFilter: []int{291, 110, 6}, Confirm: lq, Validate: lo, Total: 50, Valid: true, Builtin: true, Protected: true},
|
||||
{Code: "WAZ", Name: "Worked All Zones (CQ)", Type: TypeQSOFields, Field: "cqz", MatchBy: "code", ExactMatch: true, Confirm: lq, Validate: lo, Total: 40, Valid: true, Builtin: true, Protected: true},
|
||||
{Code: "WAC", Name: "Worked All Continents", Type: TypeQSOFields, Field: "cont", MatchBy: "code", ExactMatch: true, Confirm: []string{"lotw", "qsl", "eqsl"}, Validate: lo, Total: 6, Valid: true, Builtin: true, Protected: true},
|
||||
{Code: "WPX", Name: "Worked All Prefixes (CQ WPX)", Type: TypeQSOFields, Field: "prefix", Dynamic: true, Confirm: lq, Validate: lo, Total: 0, Valid: true, Builtin: true, Protected: true},
|
||||
{Code: "DDFM", Name: "Départements Français Métropolitains", Type: TypeQSOFields, Field: "note", Pattern: `(?i)\b(D\d{1,2}[AB]?)\b`, DXCCFilter: []int{227}, Confirm: lq, Validate: lo, Total: 96, Valid: true, Builtin: true, Protected: true},
|
||||
{Code: "IOTA", Name: "Islands On The Air", Type: TypeReference, Field: "iota", Dynamic: true, Confirm: []string{"qsl"}, Validate: lo, Total: 0, Valid: true, Builtin: true, Protected: true},
|
||||
{Code: "POTA", Name: "Parks On The Air", Type: TypeReference, Field: "pota_ref", Dynamic: true, Confirm: lq, Validate: lo, Total: 0, Valid: true, Builtin: true, Protected: true},
|
||||
{Code: "SOTA", Name: "Summits On The Air", Type: TypeReference, Field: "sota_ref", Dynamic: true, Confirm: lq, Validate: lo, Total: 0, Valid: true, Builtin: true, Protected: true},
|
||||
{Code: "WWFF", Name: "World Wide Flora & Fauna", Type: TypeReference, Field: "wwff", Dynamic: true, Confirm: lq, Validate: lo, Total: 0, Valid: true, Builtin: true, Protected: true},
|
||||
// catalogFS holds the built-in award definitions as DATA, not Go code.
|
||||
//
|
||||
// An award is data — a field to scan, a pattern, a scope. Coding it in Go meant a
|
||||
// recompile and a release for every new one, which is absurd for something that
|
||||
// changes far more often than the engine that reads it. They now live one JSON per
|
||||
// award in catalog/, embedded in the binary: adding an award is adding a file.
|
||||
//
|
||||
// This is the SEED only. Once a user has awards in their database, that database
|
||||
// is the source of truth — the catalog never overwrites their edits behind their
|
||||
// back.
|
||||
//
|
||||
//go:embed catalog/*.json
|
||||
var catalogFS embed.FS
|
||||
|
||||
// CatalogEntry is one award in the catalog: its definition AND its reference list.
|
||||
//
|
||||
// The references are the point. An award's definition is a few lines; what makes
|
||||
// WAPC worth anything is its 34 provinces and the city regexes attached to them.
|
||||
// A catalog that shipped definitions only would hand every user an empty shell —
|
||||
// which is exactly the trap this design is meant to avoid.
|
||||
//
|
||||
// References stay as raw JSON so this package (the matching ENGINE) never has to
|
||||
// import the reference-store package. The caller decodes them.
|
||||
type CatalogEntry struct {
|
||||
Def Def `json:"def"`
|
||||
References json.RawMessage `json:"references,omitempty"`
|
||||
}
|
||||
|
||||
// catalogFile is what a file in catalog/ may contain. Two shapes are accepted:
|
||||
//
|
||||
// {"def": {...}, "references": [...]} — a catalog entry
|
||||
// {"version":1, "awards":[{"def":…,"references":…}]} — an exported bundle
|
||||
//
|
||||
// Accepting the bundle shape is deliberate: it means an award EXPORTED from the UI
|
||||
// can be dropped straight into catalog/ and shipped to everyone, with no
|
||||
// conversion step. That is the whole loop — create an award, export it, drop it in,
|
||||
// everybody has it.
|
||||
type catalogFile struct {
|
||||
Def Def `json:"def"`
|
||||
References json.RawMessage `json:"references,omitempty"`
|
||||
Awards []CatalogEntry `json:"awards,omitempty"`
|
||||
// A bare Def (the original catalog shape) is still read via the fields above
|
||||
// being empty and Code being set at the top level.
|
||||
Code string `json:"code,omitempty"`
|
||||
}
|
||||
|
||||
// Catalog returns every built-in award: definition + references, sorted by code.
|
||||
//
|
||||
// Sorted because an embed.FS walk is alphabetical and relying on that implicitly is
|
||||
// how a user's award list quietly reorders itself on a rebuild.
|
||||
func Catalog() []CatalogEntry {
|
||||
entries, err := catalogFS.ReadDir("catalog")
|
||||
if err != nil {
|
||||
return nil // embedded: can only fail if the build is broken
|
||||
}
|
||||
out := make([]CatalogEntry, 0, len(entries))
|
||||
for _, e := range entries {
|
||||
if e.IsDir() || !strings.HasSuffix(e.Name(), ".json") {
|
||||
continue
|
||||
}
|
||||
b, err := catalogFS.ReadFile("catalog/" + e.Name())
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
var f catalogFile
|
||||
if err := json.Unmarshal(b, &f); err != nil {
|
||||
// A malformed file must not take the other awards down with it.
|
||||
continue
|
||||
}
|
||||
switch {
|
||||
case len(f.Awards) > 0: // an exported bundle, dropped in as-is
|
||||
for _, a := range f.Awards {
|
||||
if strings.TrimSpace(a.Def.Code) != "" {
|
||||
out = append(out, a)
|
||||
}
|
||||
}
|
||||
case strings.TrimSpace(f.Def.Code) != "": // {"def":…,"references":…}
|
||||
out = append(out, CatalogEntry{Def: f.Def, References: f.References})
|
||||
case strings.TrimSpace(f.Code) != "": // a bare Def
|
||||
var d Def
|
||||
if err := json.Unmarshal(b, &d); err == nil {
|
||||
out = append(out, CatalogEntry{Def: d})
|
||||
}
|
||||
}
|
||||
}
|
||||
sort.Slice(out, func(i, j int) bool { return out[i].Def.Code < out[j].Def.Code })
|
||||
return out
|
||||
}
|
||||
|
||||
// Defaults are the built-in award definitions seeded on first run.
|
||||
func Defaults() []Def {
|
||||
cat := Catalog()
|
||||
out := make([]Def, 0, len(cat))
|
||||
for _, e := range cat {
|
||||
out = append(out, e.Def)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// CatalogRefs returns the raw JSON reference list a catalog award ships with, if
|
||||
// any. Awards whose list is seeded from code (DXCC entities, French departments)
|
||||
// or fetched online (POTA/SOTA/WWFF) carry none.
|
||||
func CatalogRefs(code string) (json.RawMessage, bool) {
|
||||
code = strings.ToUpper(strings.TrimSpace(code))
|
||||
for _, e := range Catalog() {
|
||||
if strings.ToUpper(e.Def.Code) == code && len(e.References) > 0 {
|
||||
return e.References, true
|
||||
}
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
|
||||
// Migrate upgrades award definitions saved before the richer model existed.
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package award
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"sort"
|
||||
"testing"
|
||||
|
||||
@@ -378,3 +379,128 @@ func TestComputePredefinedList(t *testing.T) {
|
||||
t.Errorf("RAC refs = %d, want 3 (%v)", len(r.Refs), refCodes(r))
|
||||
}
|
||||
}
|
||||
|
||||
// The built-in awards moved from Go code into embedded JSON (catalog/*.json).
|
||||
// A refactor is only a refactor if the behaviour is identical, so pin down what
|
||||
// the catalog MUST still produce — a typo in a JSON file would otherwise silently
|
||||
// disable an award, and nobody would notice until a QSO stopped counting.
|
||||
func TestCatalogDefaults(t *testing.T) {
|
||||
defs := Defaults()
|
||||
byCode := map[string]Def{}
|
||||
for _, d := range defs {
|
||||
byCode[d.Code] = d
|
||||
}
|
||||
|
||||
want := []string{"DDFM", "DXCC", "IOTA", "POTA", "SOTA", "WAC", "WAS", "WAZ", "WPX", "WWFF"}
|
||||
if len(defs) != len(want) {
|
||||
t.Fatalf("catalog has %d awards, want %d (%v)", len(defs), len(want), byCode)
|
||||
}
|
||||
for _, c := range want {
|
||||
d, ok := byCode[c]
|
||||
if !ok {
|
||||
t.Errorf("%s missing from the embedded catalog", c)
|
||||
continue
|
||||
}
|
||||
// Valid=false would hide the award entirely; Builtin=false would let a
|
||||
// "reset to defaults" delete it. Both are silent failures.
|
||||
if !d.Valid || !d.Builtin {
|
||||
t.Errorf("%s: valid=%v builtin=%v — both must be true", c, d.Valid, d.Builtin)
|
||||
}
|
||||
if d.Type == "" || d.Field == "" {
|
||||
t.Errorf("%s: type=%q field=%q — neither may be empty", c, d.Type, d.Field)
|
||||
}
|
||||
if len(d.Confirm) == 0 {
|
||||
t.Errorf("%s: no confirmation sources — nothing would ever count as confirmed", c)
|
||||
}
|
||||
}
|
||||
|
||||
// Spot-check the two that carry real matching logic, since a mangled escape in
|
||||
// JSON is exactly the failure this test exists to catch.
|
||||
if got := byCode["DDFM"].Pattern; got != `(?i)\b(D\d{1,2}[AB]?)\b` {
|
||||
t.Errorf("DDFM pattern = %q — the regex did not survive the JSON round-trip", got)
|
||||
}
|
||||
if _, err := compileAwardRE(byCode["DDFM"].Pattern); err != nil {
|
||||
t.Errorf("DDFM pattern does not compile: %v", err)
|
||||
}
|
||||
if got := byCode["WAS"].DXCCFilter; len(got) != 3 || got[0] != 291 {
|
||||
t.Errorf("WAS DXCC filter = %v, want [291 110 6]", got)
|
||||
}
|
||||
if !byCode["WAS"].ExactMatch || byCode["WAS"].MatchBy != "code" {
|
||||
t.Errorf("WAS: exact=%v matchBy=%q, want true/code", byCode["WAS"].ExactMatch, byCode["WAS"].MatchBy)
|
||||
}
|
||||
if !byCode["WPX"].Dynamic {
|
||||
t.Error("WPX must be dynamic — its references aren't a fixed list")
|
||||
}
|
||||
// Deterministic order: an embed walk that reorders would shuffle the user's list.
|
||||
for i := 1; i < len(defs); i++ {
|
||||
if defs[i-1].Code > defs[i].Code {
|
||||
t.Errorf("catalog not sorted by code: %q before %q", defs[i-1].Code, defs[i].Code)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// The whole point of the catalog: an award you EXPORT from the UI can be dropped
|
||||
// straight into catalog/ and shipped to everyone — no conversion step. So the
|
||||
// loader must accept the exported bundle shape, and it must carry the REFERENCES,
|
||||
// because a WAPC without its provinces and their city regexes is an empty shell.
|
||||
func TestCatalogAcceptsExportedBundle(t *testing.T) {
|
||||
// Exactly what ExportAward writes.
|
||||
exported := []byte(`{
|
||||
"version": 1,
|
||||
"exported_at": "2026-07-13T10:00:00Z",
|
||||
"awards": [
|
||||
{
|
||||
"def": {"code":"WAPC","name":"Worked All Provinces of China","valid":true,
|
||||
"type":"QSOFIELDS","field":"address","match_by":"description",
|
||||
"dxcc_filter":[318],"confirm":["lotw","qsl"],"total":34},
|
||||
"references": [
|
||||
{"code":"JS","name":"Jiangsu","pattern":"\\bJiangyin\\b","valid":true},
|
||||
{"code":"ZJ","name":"Zhejiang","valid":true}
|
||||
]
|
||||
}
|
||||
]
|
||||
}`)
|
||||
|
||||
var f catalogFile
|
||||
if err := json.Unmarshal(exported, &f); err != nil {
|
||||
t.Fatalf("an exported bundle must parse as a catalog file: %v", err)
|
||||
}
|
||||
if len(f.Awards) != 1 {
|
||||
t.Fatalf("got %d awards, want 1", len(f.Awards))
|
||||
}
|
||||
e := f.Awards[0]
|
||||
if e.Def.Code != "WAPC" || e.Def.Field != "address" || e.Def.MatchBy != "description" {
|
||||
t.Errorf("definition lost in the round-trip: %+v", e.Def)
|
||||
}
|
||||
if len(e.References) == 0 {
|
||||
t.Fatal("references dropped — this is the whole value of sharing an award")
|
||||
}
|
||||
// The per-reference regex must survive: it is what makes the award actually work.
|
||||
var refs []struct {
|
||||
Code string `json:"code"`
|
||||
Pattern string `json:"pattern"`
|
||||
}
|
||||
if err := json.Unmarshal(e.References, &refs); err != nil {
|
||||
t.Fatalf("references not decodable: %v", err)
|
||||
}
|
||||
if len(refs) != 2 || refs[0].Code != "JS" || refs[0].Pattern != `\bJiangyin\b` {
|
||||
t.Errorf("reference regex did not survive: %+v", refs)
|
||||
}
|
||||
if _, err := compileAwardRE(refs[0].Pattern); err != nil {
|
||||
t.Errorf("the shipped regex does not compile: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// A single malformed file in the catalog must not take the other awards down with
|
||||
// it — one bad JSON should cost you one award, not all of them.
|
||||
func TestCatalogSurvivesOneBadFile(t *testing.T) {
|
||||
if len(Catalog()) < 10 {
|
||||
t.Fatalf("catalog has %d entries, want the 10 built-ins", len(Catalog()))
|
||||
}
|
||||
var f catalogFile
|
||||
if err := json.Unmarshal([]byte(`{ this is not json `), &f); err == nil {
|
||||
t.Error("expected a parse error on malformed JSON")
|
||||
}
|
||||
// Catalog() skips unparseable files rather than returning nil, so the others
|
||||
// still load. (Verified structurally: the loader `continue`s on error.)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
{
|
||||
"def": {
|
||||
"code": "DDFM",
|
||||
"name": "Départements Français Métropolitains",
|
||||
"valid": true,
|
||||
"protected": true,
|
||||
"type": "QSOFIELDS",
|
||||
"field": "note",
|
||||
"pattern": "(?i)\\b(D\\d{1,2}[AB]?)\\b",
|
||||
"dxcc_filter": [
|
||||
227
|
||||
],
|
||||
"confirm": [
|
||||
"lotw",
|
||||
"qsl"
|
||||
],
|
||||
"validate": [
|
||||
"lotw"
|
||||
],
|
||||
"total": 96,
|
||||
"builtin": true
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
{
|
||||
"def": {
|
||||
"code": "DXCC",
|
||||
"name": "DX Century Club",
|
||||
"valid": true,
|
||||
"protected": true,
|
||||
"type": "DXCC",
|
||||
"field": "dxcc",
|
||||
"pattern": "",
|
||||
"dxcc_filter": null,
|
||||
"confirm": [
|
||||
"lotw",
|
||||
"qsl"
|
||||
],
|
||||
"validate": [
|
||||
"lotw"
|
||||
],
|
||||
"total": 340,
|
||||
"builtin": true
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
{
|
||||
"def": {
|
||||
"code": "IOTA",
|
||||
"name": "Islands On The Air",
|
||||
"valid": true,
|
||||
"protected": true,
|
||||
"type": "REFERENCE",
|
||||
"field": "iota",
|
||||
"pattern": "",
|
||||
"dynamic": true,
|
||||
"dxcc_filter": null,
|
||||
"confirm": [
|
||||
"qsl"
|
||||
],
|
||||
"validate": [
|
||||
"lotw"
|
||||
],
|
||||
"total": 0,
|
||||
"builtin": true
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
{
|
||||
"def": {
|
||||
"code": "POTA",
|
||||
"name": "Parks On The Air",
|
||||
"valid": true,
|
||||
"protected": true,
|
||||
"type": "REFERENCE",
|
||||
"field": "pota_ref",
|
||||
"pattern": "",
|
||||
"dynamic": true,
|
||||
"dxcc_filter": null,
|
||||
"confirm": [
|
||||
"lotw",
|
||||
"qsl"
|
||||
],
|
||||
"validate": [
|
||||
"lotw"
|
||||
],
|
||||
"total": 0,
|
||||
"builtin": true
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
{
|
||||
"def": {
|
||||
"code": "SOTA",
|
||||
"name": "Summits On The Air",
|
||||
"valid": true,
|
||||
"protected": true,
|
||||
"type": "REFERENCE",
|
||||
"field": "sota_ref",
|
||||
"pattern": "",
|
||||
"dynamic": true,
|
||||
"dxcc_filter": null,
|
||||
"confirm": [
|
||||
"lotw",
|
||||
"qsl"
|
||||
],
|
||||
"validate": [
|
||||
"lotw"
|
||||
],
|
||||
"total": 0,
|
||||
"builtin": true
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
{
|
||||
"def": {
|
||||
"code": "WAC",
|
||||
"name": "Worked All Continents",
|
||||
"valid": true,
|
||||
"protected": true,
|
||||
"type": "QSOFIELDS",
|
||||
"field": "cont",
|
||||
"match_by": "code",
|
||||
"exact_match": true,
|
||||
"pattern": "",
|
||||
"dxcc_filter": null,
|
||||
"confirm": [
|
||||
"lotw",
|
||||
"qsl",
|
||||
"eqsl"
|
||||
],
|
||||
"validate": [
|
||||
"lotw"
|
||||
],
|
||||
"total": 6,
|
||||
"builtin": true
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
{
|
||||
"def": {
|
||||
"code": "WAS",
|
||||
"name": "Worked All States",
|
||||
"valid": true,
|
||||
"protected": true,
|
||||
"type": "QSOFIELDS",
|
||||
"field": "state",
|
||||
"match_by": "code",
|
||||
"exact_match": true,
|
||||
"pattern": "",
|
||||
"dxcc_filter": [
|
||||
291,
|
||||
110,
|
||||
6
|
||||
],
|
||||
"confirm": [
|
||||
"lotw",
|
||||
"qsl"
|
||||
],
|
||||
"validate": [
|
||||
"lotw"
|
||||
],
|
||||
"total": 50,
|
||||
"builtin": true
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
{
|
||||
"def": {
|
||||
"code": "WAZ",
|
||||
"name": "Worked All Zones (CQ)",
|
||||
"valid": true,
|
||||
"protected": true,
|
||||
"type": "QSOFIELDS",
|
||||
"field": "cqz",
|
||||
"match_by": "code",
|
||||
"exact_match": true,
|
||||
"pattern": "",
|
||||
"dxcc_filter": null,
|
||||
"confirm": [
|
||||
"lotw",
|
||||
"qsl"
|
||||
],
|
||||
"validate": [
|
||||
"lotw"
|
||||
],
|
||||
"total": 40,
|
||||
"builtin": true
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
{
|
||||
"def": {
|
||||
"code": "WPX",
|
||||
"name": "Worked All Prefixes (CQ WPX)",
|
||||
"valid": true,
|
||||
"protected": true,
|
||||
"type": "QSOFIELDS",
|
||||
"field": "prefix",
|
||||
"pattern": "",
|
||||
"dynamic": true,
|
||||
"dxcc_filter": null,
|
||||
"confirm": [
|
||||
"lotw",
|
||||
"qsl"
|
||||
],
|
||||
"validate": [
|
||||
"lotw"
|
||||
],
|
||||
"total": 0,
|
||||
"builtin": true
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
{
|
||||
"def": {
|
||||
"code": "WWFF",
|
||||
"name": "World Wide Flora & Fauna",
|
||||
"valid": true,
|
||||
"protected": true,
|
||||
"type": "REFERENCE",
|
||||
"field": "wwff",
|
||||
"pattern": "",
|
||||
"dynamic": true,
|
||||
"dxcc_filter": null,
|
||||
"confirm": [
|
||||
"lotw",
|
||||
"qsl"
|
||||
],
|
||||
"validate": [
|
||||
"lotw"
|
||||
],
|
||||
"total": 0,
|
||||
"builtin": true
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user