Reuse duplicate grouping for combat turns and spells

This commit is contained in:
2026-11-26 02:54:33 +00:00
parent f9dd53f4c2
commit 26da344f27
7 changed files with 84 additions and 88 deletions

View File

@@ -150,59 +150,25 @@ func canonicalizeSourceRefs(order shared.SourceRefOrder, input []source.SourceRe
return canonical, orderChanged, len(input) - len(canonical)
}
type duplicateGroup struct {
retainedIndex int
removed []int
}
func collapseDuplicateSpellCasts(input dnd.SpellList, documentIndex source.DocumentIndex, catalog spellcatalog.EffectiveCatalog) (dnd.SpellList, []diagnostics.Finding) {
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, documentIndex, 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 {
// Spell lookup and evidence eligibility remain local to this family.
retained, groups := shared.GroupDuplicates(len(input.SpellCasts), func(index int) (string, bool) {
return duplicateKey(input.SpellCasts[index], documentIndex, catalog)
})
// Keep the original list and its nil/empty behavior when nothing collapses.
if len(groups) == 0 {
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)
}
for _, index := range retained {
output.SpellCasts = append(output.SpellCasts, input.SpellCasts[index])
}
findings := make([]diagnostics.Finding, 0)
findings := make([]diagnostics.Finding, 0, len(groups))
for _, group := range groups {
if len(group.removed) == 0 {
continue
}
findings = append(findings, duplicateFinding(group.retainedIndex, group.removed))
findings = append(findings, duplicateFinding(group.Representative, group.Removed))
}
return output, findings
}