Collapse duplicate D&D spell casts during normalization
This commit is contained in:
@@ -4,20 +4,24 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/core/source"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/framework/pipeline"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd"
|
||||
spellcatalog "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/spells/catalog"
|
||||
"golang.org/x/text/cases"
|
||||
)
|
||||
|
||||
const Key = "dnd/spells"
|
||||
|
||||
const (
|
||||
ReasonCodeSpellNameCanonicalized = "spell_name_canonicalized"
|
||||
ReasonCodeSpellNameUnresolved = "spell_name_unresolved"
|
||||
ReasonCodeSourceReferencesNormalized = "source_references_normalized"
|
||||
ReasonCodeSpellNameCanonicalized = "spell_name_canonicalized"
|
||||
ReasonCodeSpellNameUnresolved = "spell_name_unresolved"
|
||||
ReasonCodeSourceReferencesNormalized = "source_references_normalized"
|
||||
ReasonCodeDuplicateSpellCastCollapsed = "duplicate_spell_cast_collapsed"
|
||||
)
|
||||
|
||||
var requiredCapabilities = []string{"merged"}
|
||||
@@ -86,6 +90,8 @@ func (n *Normalizer) Normalize(ctx context.Context, req contracts.TypedNormalize
|
||||
}
|
||||
|
||||
value, warnings := normalizeSpellList(req.MergeOutput.Value, n.effectiveCatalog)
|
||||
value, duplicateWarnings := collapseDuplicateSpellCasts(value, req.Source, n.effectiveCatalog)
|
||||
warnings = append(warnings, duplicateWarnings...)
|
||||
return contracts.TypedNormalizeResult[dnd.SpellList]{Value: value, Warnings: warnings}, nil
|
||||
}
|
||||
|
||||
@@ -179,6 +185,118 @@ func sourceRefLess(left, right source.SourceRef) bool {
|
||||
return left.EndUnitID < right.EndUnitID
|
||||
}
|
||||
|
||||
type duplicateGroup struct {
|
||||
retainedIndex int
|
||||
removed []int
|
||||
}
|
||||
|
||||
func collapseDuplicateSpellCasts(input dnd.SpellList, doc *source.SourceDocument, catalog spellcatalog.EffectiveCatalog) (dnd.SpellList, []contracts.Warning) {
|
||||
if len(input.SpellCasts) == 0 {
|
||||
return input, nil
|
||||
}
|
||||
|
||||
keep := make([]bool, len(input.SpellCasts))
|
||||
groups := make([]duplicateGroup, 0)
|
||||
groupByKey := make(map[string]int)
|
||||
for index, cast := range input.SpellCasts {
|
||||
key, eligible := duplicateKey(cast, doc, catalog)
|
||||
if !eligible {
|
||||
keep[index] = true
|
||||
continue
|
||||
}
|
||||
groupIndex, exists := groupByKey[key]
|
||||
if !exists {
|
||||
groupByKey[key] = len(groups)
|
||||
groups = append(groups, duplicateGroup{retainedIndex: index})
|
||||
keep[index] = true
|
||||
continue
|
||||
}
|
||||
groups[groupIndex].removed = append(groups[groupIndex].removed, index)
|
||||
}
|
||||
|
||||
removedAny := false
|
||||
for _, group := range groups {
|
||||
if len(group.removed) > 0 {
|
||||
removedAny = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !removedAny {
|
||||
return input, nil
|
||||
}
|
||||
|
||||
output := dnd.SpellList{SpellCasts: make([]dnd.SpellCast, 0, len(input.SpellCasts))}
|
||||
for index, cast := range input.SpellCasts {
|
||||
if keep[index] {
|
||||
output.SpellCasts = append(output.SpellCasts, cast)
|
||||
}
|
||||
}
|
||||
|
||||
warnings := make([]contracts.Warning, 0)
|
||||
for _, group := range groups {
|
||||
if len(group.removed) == 0 {
|
||||
continue
|
||||
}
|
||||
warnings = append(warnings, duplicateWarning(group.retainedIndex, group.removed))
|
||||
}
|
||||
return output, warnings
|
||||
}
|
||||
|
||||
func duplicateKey(cast dnd.SpellCast, doc *source.SourceDocument, catalog spellcatalog.EffectiveCatalog) (string, bool) {
|
||||
canonicalName, resolved := catalog.Lookup(cast.Spell)
|
||||
if !resolved || len(cast.SourceRefs) == 0 {
|
||||
return "", false
|
||||
}
|
||||
for _, ref := range cast.SourceRefs {
|
||||
if source.ValidateRef(doc, ref) != nil {
|
||||
return "", false
|
||||
}
|
||||
}
|
||||
|
||||
var key strings.Builder
|
||||
writeKeyString(&key, canonicalName)
|
||||
writeKeyString(&key, cases.Fold().String(strings.Join(strings.Fields(cast.Caster), " ")))
|
||||
for _, ref := range cast.SourceRefs {
|
||||
writeKeyString(&key, ref.SourceID)
|
||||
writeKeyInt(&key, ref.StartUnitID)
|
||||
writeKeyInt(&key, ref.EndUnitID)
|
||||
}
|
||||
return key.String(), true
|
||||
}
|
||||
|
||||
func writeKeyString(builder *strings.Builder, value string) {
|
||||
builder.WriteString(strconv.Itoa(len(value)))
|
||||
builder.WriteByte(':')
|
||||
builder.WriteString(value)
|
||||
}
|
||||
|
||||
func writeKeyInt(builder *strings.Builder, value int) {
|
||||
builder.WriteString(strconv.Itoa(value))
|
||||
builder.WriteByte(';')
|
||||
}
|
||||
|
||||
func duplicateWarning(retainedIndex int, removed []int) contracts.Warning {
|
||||
const maxDisplayedIndices = 20
|
||||
displayed := removed
|
||||
if len(displayed) > maxDisplayedIndices {
|
||||
displayed = displayed[:maxDisplayedIndices]
|
||||
}
|
||||
indices := make([]string, len(displayed))
|
||||
for index, removedIndex := range displayed {
|
||||
indices[index] = strconv.Itoa(removedIndex)
|
||||
}
|
||||
|
||||
message := fmt.Sprintf("retained input index %d; removed input indices [%s]", retainedIndex, strings.Join(indices, ", "))
|
||||
if omitted := len(removed) - len(displayed); omitted > 0 {
|
||||
message += fmt.Sprintf("; %d additional removed input indices omitted", omitted)
|
||||
}
|
||||
return contracts.Warning{
|
||||
Scope: spellCastScope(retainedIndex),
|
||||
ReasonCode: ReasonCodeDuplicateSpellCastCollapsed,
|
||||
Message: message,
|
||||
}
|
||||
}
|
||||
|
||||
func boundedName(name string) string {
|
||||
runes := []rune(name)
|
||||
if len(runes) <= 128 {
|
||||
|
||||
Reference in New Issue
Block a user