Order spell and NPC extraction by document position

This commit is contained in:
2026-07-24 14:29:19 +00:00
parent 83fde83a58
commit 9bbf2535dd
8 changed files with 138 additions and 111 deletions

View File

@@ -5,18 +5,19 @@ import (
"gitea.maximumdirect.net/eric/notarius/internal/core/source"
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd"
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/shared"
)
func canonicalizeResponse(response *extractionResponse) {
func canonicalizeResponse(response *extractionResponse, order shared.SourceRefOrder, sourceID string) {
if response == nil {
return
}
for index := range response.SpellCasts {
canonicalizeSpellCast(&response.SpellCasts[index])
canonicalizeSpellCast(&response.SpellCasts[index], order, sourceID)
}
sort.SliceStable(response.SpellCasts, func(i, j int) bool {
left, leftOK := earliestSourceUnit(response.SpellCasts[i])
right, rightOK := earliestSourceUnit(response.SpellCasts[j])
left, leftOK := order.EarliestValid(spellSourceRefs(response.SpellCasts[i].SourceRefs, sourceID))
right, rightOK := order.EarliestValid(spellSourceRefs(response.SpellCasts[j].SourceRefs, sourceID))
if leftOK != rightOK {
return leftOK
}
@@ -27,53 +28,33 @@ func canonicalizeResponse(response *extractionResponse) {
})
}
func canonicalizeSpellCast(spell *spellCastResponse) {
sort.SliceStable(spell.SourceRefs, func(i, j int) bool {
left := spell.SourceRefs[i]
right := spell.SourceRefs[j]
if left.StartUnitID != right.StartUnitID {
return unitSortValue(left.StartUnitID) < unitSortValue(right.StartUnitID)
}
return unitSortValue(left.EndUnitID) < unitSortValue(right.EndUnitID)
})
spell.SourceRefs = dedupeSourceRefs(spell.SourceRefs)
func canonicalizeSpellCast(spell *spellCastResponse, order shared.SourceRefOrder, sourceID string) {
if spell == nil {
return
}
spell.SourceRefs = spellResponseRefs(order.Canonicalize(spellSourceRefs(spell.SourceRefs, sourceID)))
}
func dedupeSourceRefs(refs []spellSourceRefResponse) []spellSourceRefResponse {
if len(refs) < 2 {
return refs
func spellSourceRefs(refs []spellSourceRefResponse, sourceID string) []source.SourceRef {
if refs == nil {
return nil
}
out := refs[:0]
var previous spellSourceRefResponse
values := make([]source.SourceRef, len(refs))
for index, ref := range refs {
if index > 0 && sameSourceRef(previous, ref) {
continue
}
out = append(out, ref)
previous = ref
values[index] = source.SourceRef{SourceID: sourceID, StartUnitID: ref.StartUnitID, EndUnitID: ref.EndUnitID}
}
return out
return values
}
func sameSourceRef(left spellSourceRefResponse, right spellSourceRefResponse) bool {
return left.StartUnitID == right.StartUnitID && left.EndUnitID == right.EndUnitID
}
func earliestSourceUnit(spell spellCastResponse) (int, bool) {
for _, ref := range spell.SourceRefs {
start := ref.StartUnitID
if start > 0 {
return start, true
}
func spellResponseRefs(refs []source.SourceRef) []spellSourceRefResponse {
if refs == nil {
return nil
}
return 0, false
}
func unitSortValue(value int) int {
if value <= 0 {
return int(^uint(0) >> 1)
values := make([]spellSourceRefResponse, len(refs))
for index, ref := range refs {
values[index] = spellSourceRefResponse{StartUnitID: ref.StartUnitID, EndUnitID: ref.EndUnitID}
}
return value
return values
}
func canonicalSpellList(response extractionResponse, sourceID string) dnd.SpellList {