fix: Improved awards matches
This commit is contained in:
+25
-42
@@ -74,10 +74,13 @@ type Def struct {
|
||||
Dynamic bool `json:"dynamic,omitempty"` // references not predefined (any value counts)
|
||||
AddPrefixes []string `json:"add_prefixes,omitempty"` // possible reference additional prefixes
|
||||
|
||||
// OrRules are ADDITIONAL searches OR'd with the primary one above: a QSO
|
||||
// earns a reference if the primary match OR any of these match. Lets a
|
||||
// French department (DDFM) be found from "D74" in the note AND from a postal
|
||||
// code "74140" in the address (pattern captures "74", Prefix "D" → "D74").
|
||||
// OrRules are ordered FALLBACK searches for the primary one above: they are
|
||||
// tried IN ORDER and only while nothing has matched yet — the first rule that
|
||||
// yields a reference wins and the rest are skipped (short-circuit, like a
|
||||
// chain of "else if"). Lets a Chinese province (WAPC) be found first from the
|
||||
// province NAME in the address, and only if that fails, from a city regex
|
||||
// (e.g. "\bJiangyin\b" → JS) — so a QSO already resolved by name isn't also
|
||||
// re-tagged, possibly differently, by a later rule.
|
||||
OrRules []OrRule `json:"or_rules,omitempty"`
|
||||
|
||||
// --- Scope ---
|
||||
@@ -455,17 +458,7 @@ func MatchQSO(d Def, metas []RefMeta, q *qso.QSO) []string {
|
||||
}
|
||||
}
|
||||
rl := NewRefList(metas)
|
||||
found := candidates(&d, re, q, rl, len(metas) > 0)
|
||||
// Merge operator-assigned references (manual override). These let the
|
||||
// operator tag a QSO for an award whose field/description matching can't
|
||||
// auto-detect the reference — e.g. WAPC scans the ADDRESS field for a
|
||||
// province NAME, so a contact whose address doesn't spell it out needs the
|
||||
// province picked by hand. For a predefined award the override is still
|
||||
// validated against its reference list.
|
||||
if man := manualRefs(q, d.Code); len(man) > 0 {
|
||||
found = mergeManual(found, man, rl, len(metas) > 0 && !d.Dynamic)
|
||||
}
|
||||
return found
|
||||
return candidates(&d, re, q, rl, len(metas) > 0)
|
||||
}
|
||||
|
||||
// ManualRefsKey is the ADIF extras key under which OpsLog stores per-QSO,
|
||||
@@ -500,30 +493,6 @@ func manualRefs(q *qso.QSO, code string) []string {
|
||||
return out
|
||||
}
|
||||
|
||||
// mergeManual appends operator-assigned codes to the auto-found set, deduped.
|
||||
// When the award is predefined, only references present and valid in its list
|
||||
// are kept (so a typo can't invent a reference).
|
||||
func mergeManual(found, manual []string, rl refList, predefined bool) []string {
|
||||
seen := map[string]struct{}{}
|
||||
for _, c := range found {
|
||||
seen[normalizeRef(c)] = struct{}{}
|
||||
}
|
||||
for _, c := range manual {
|
||||
c = normalizeRef(c)
|
||||
if _, dup := seen[c]; dup {
|
||||
continue
|
||||
}
|
||||
if predefined {
|
||||
if m, ok := rl.byCode[c]; !ok || !m.Valid {
|
||||
continue
|
||||
}
|
||||
}
|
||||
seen[c] = struct{}{}
|
||||
found = append(found, c)
|
||||
}
|
||||
return found
|
||||
}
|
||||
|
||||
// Confirmed reports whether a QSO satisfies any of the given confirmation
|
||||
// sources (lotw|qsl|eqsl). Exported for the statistics view.
|
||||
func Confirmed(q *qso.QSO, sources []string) bool { return confirmed(q, sources) }
|
||||
@@ -620,9 +589,12 @@ func searchOne(field, matchBy string, re *regexp.Regexp, exact bool, leading, tr
|
||||
func candidates(d *Def, re *regexp.Regexp, q *qso.QSO, rl refList, hasList bool) []string {
|
||||
predefined := hasList && !d.Dynamic
|
||||
|
||||
// Primary search, then each OR rule — a QSO earns a reference if any matches.
|
||||
// Primary search first; the OR rules are ordered FALLBACKS — try the next
|
||||
// only while nothing has matched yet, and stop at the first that yields a
|
||||
// reference (short-circuit). So a province already found by NAME isn't also
|
||||
// re-derived, possibly differently, from a later city-regex rule.
|
||||
found := searchOne(d.Field, d.MatchBy, re, d.ExactMatch, d.LeadingStr, d.TrailingStr, "", q, rl, predefined)
|
||||
for i := range d.OrRules {
|
||||
for i := 0; len(found) == 0 && i < len(d.OrRules); i++ {
|
||||
r := &d.OrRules[i]
|
||||
var rre *regexp.Regexp
|
||||
if p := strings.TrimSpace(r.Pattern); p != "" {
|
||||
@@ -632,7 +604,18 @@ func candidates(d *Def, re *regexp.Regexp, q *qso.QSO, rl refList, hasList bool)
|
||||
}
|
||||
rre = c
|
||||
}
|
||||
found = append(found, searchOne(r.Field, r.MatchBy, rre, r.ExactMatch, r.LeadingStr, r.TrailingStr, r.Prefix, q, rl, predefined)...)
|
||||
found = searchOne(r.Field, r.MatchBy, rre, r.ExactMatch, r.LeadingStr, r.TrailingStr, r.Prefix, q, rl, predefined)
|
||||
}
|
||||
|
||||
// Merge operator-assigned references (manual override, ManualRefsKey). Lets
|
||||
// the operator tag a QSO for an award whose field/description matching can't
|
||||
// auto-detect the reference — e.g. WAPC scans ADDRESS for a province NAME, so
|
||||
// a contact whose address doesn't spell it out needs the province picked by
|
||||
// hand. Applied HERE (not just in MatchQSO) so Compute — which powers the
|
||||
// awards panel and the per-QSO refs editor — honours overrides too. For a
|
||||
// predefined award the ref is still validated against the list below.
|
||||
for _, c := range manualRefs(q, d.Code) {
|
||||
found = append(found, normalizeRef(c))
|
||||
}
|
||||
|
||||
if !predefined {
|
||||
|
||||
@@ -128,6 +128,81 @@ func TestComputeMatchByDescription(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// OR rules are ordered fallbacks: the primary match wins and short-circuits the
|
||||
// rules; when the primary finds nothing, the first OR rule that hits wins and
|
||||
// later rules are skipped. DDFM-style: a French department from the NOTE first,
|
||||
// else captured from a postal code in the address (Prefix "D" → "D74").
|
||||
func TestComputeOrRuleFallback(t *testing.T) {
|
||||
def := Def{Code: "DDFM", Type: TypeQSOFields, Field: "notes", Pattern: `\b(D\d{2})\b`,
|
||||
DXCCFilter: []int{227}, Valid: true,
|
||||
OrRules: []OrRule{
|
||||
{Field: "address", MatchBy: "pattern", Pattern: `\b(\d{2})\d{3}\b`, Prefix: "D"},
|
||||
},
|
||||
}
|
||||
refMetas := map[string][]RefMeta{"DDFM": {
|
||||
{Code: "D74", Name: "Haute-Savoie", Valid: true},
|
||||
{Code: "D75", Name: "Paris", Valid: true},
|
||||
}}
|
||||
worked := func(q qso.QSO) []string {
|
||||
r := Compute([]Def{def}, []qso.QSO{q}, refMetas, nil)[0]
|
||||
var out []string
|
||||
for _, rf := range r.Refs {
|
||||
if rf.Worked {
|
||||
out = append(out, rf.Ref)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
// Primary (note "D74") matches → the postal OR rule is skipped, so the
|
||||
// address's 75001 does NOT also add D75. Exactly the user's ask: first
|
||||
// condition wins, no fall-through.
|
||||
if got := worked(qso.QSO{Callsign: "F4ABC", DXCC: ip(227), Notes: "worked D74", Address: "75001 Paris"}); len(got) != 1 || got[0] != "D74" {
|
||||
t.Errorf("primary wins: got %v, want [D74] (no postal fall-through to D75)", got)
|
||||
}
|
||||
// No note dept → fall through to the OR rule → postal 74140 → D74.
|
||||
if got := worked(qso.QSO{Callsign: "F4ABC", DXCC: ip(227), Notes: "", Address: "74140 Annecy"}); len(got) != 1 || got[0] != "D74" {
|
||||
t.Errorf("OR fallback: got %v, want [D74]", got)
|
||||
}
|
||||
}
|
||||
|
||||
// A manually-assigned override (ManualRefsKey) must count in Compute — not just
|
||||
// MatchQSO — so a custom award like WAPC (matches ADDRESS by description, writes
|
||||
// no QSO field) sticks after the operator picks the province by hand. The engine
|
||||
// only auto-detects "Jiangsu" when the address spells it out; here it doesn't, so
|
||||
// the ref exists solely as the override.
|
||||
func TestComputeManualOverride(t *testing.T) {
|
||||
def := Def{Code: "WAPC", Type: TypeQSOFields, Field: "address", MatchBy: "description",
|
||||
DXCCFilter: []int{318}, Confirm: []string{"lotw", "qsl"}, Valid: true}
|
||||
qsos := []qso.QSO{
|
||||
// Address doesn't name the province → only the override tags it.
|
||||
{Callsign: "BA1ABC", Band: "20m", DXCC: ip(318), Address: "Beijing Rd 5",
|
||||
Extras: map[string]string{ManualRefsKey: "WAPC@JS"}},
|
||||
}
|
||||
refMetas := map[string][]RefMeta{"WAPC": {
|
||||
{Code: "JS", Name: "Jiangsu", Valid: true},
|
||||
{Code: "GD", Name: "Guangdong", Valid: true},
|
||||
}}
|
||||
r := Compute([]Def{def}, qsos, refMetas, nil)[0]
|
||||
if r.Worked != 1 {
|
||||
t.Fatalf("WAPC worked = %d, want 1 (Jiangsu via manual override); got %v", r.Worked, refCodes(r))
|
||||
}
|
||||
var worked []string
|
||||
for _, rf := range r.Refs {
|
||||
if rf.Worked {
|
||||
worked = append(worked, rf.Ref)
|
||||
}
|
||||
}
|
||||
if len(worked) != 1 || worked[0] != "JS" {
|
||||
t.Errorf("worked refs = %v, want [JS]", worked)
|
||||
}
|
||||
|
||||
// An override for a ref NOT in the predefined list is rejected (no typo refs).
|
||||
qsos[0].Extras[ManualRefsKey] = "WAPC@ZZ"
|
||||
if w := Compute([]Def{def}, qsos, refMetas, nil)[0].Worked; w != 0 {
|
||||
t.Errorf("bogus override ZZ: worked = %d, want 0", w)
|
||||
}
|
||||
}
|
||||
|
||||
// A description-match award must also honour a REFERENCE's own regex (used to
|
||||
// broaden matching past the plain name) AND match case-insensitively — a log
|
||||
// QTH "ITABASHIKU, TOKIO" (uppercase, "Tokio" spelling) must count for Tokyo
|
||||
|
||||
Reference in New Issue
Block a user