Files
notarius/internal/modules/dnd/extract/itemevents/canonicalize.go

106 lines
3.2 KiB
Go
Raw Normal View History

2026-07-25 21:49:24 +00:00
package itemevents
import (
"sort"
"gitea.maximumdirect.net/eric/notarius/internal/core/source"
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd"
2026-08-05 19:55:34 +00:00
itemregistry "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/items/registry"
2026-07-25 21:49:24 +00:00
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/shared"
)
2026-08-05 19:55:34 +00:00
type orderedItemOccurrenceResponse struct {
value itemOccurrenceResponse
2026-07-25 21:49:24 +00:00
earliest int
hasEvidence bool
}
func canonicalizeResponse(response *extractionResponse, order shared.SourceRefOrder, sourceID string) {
if response == nil {
return
}
2026-08-05 19:55:34 +00:00
ordered := make([]orderedItemOccurrenceResponse, len(response.Occurrences))
for index := range response.Occurrences {
earliest, hasEvidence := canonicalizeItemOccurrence(&response.Occurrences[index], order, sourceID)
ordered[index] = orderedItemOccurrenceResponse{
value: response.Occurrences[index],
2026-07-25 21:49:24 +00:00
earliest: earliest,
hasEvidence: hasEvidence,
}
}
sort.SliceStable(ordered, func(left, right int) bool {
if ordered[left].hasEvidence != ordered[right].hasEvidence {
return ordered[left].hasEvidence
}
if !ordered[left].hasEvidence {
return false
}
return ordered[left].earliest < ordered[right].earliest
})
for index := range ordered {
2026-08-05 19:55:34 +00:00
response.Occurrences[index] = ordered[index].value
2026-07-25 21:49:24 +00:00
}
}
2026-08-05 19:55:34 +00:00
func canonicalizeItemOccurrence(event *itemOccurrenceResponse, order shared.SourceRefOrder, sourceID string) (int, bool) {
2026-07-25 21:49:24 +00:00
if event == nil {
return 0, false
}
2026-08-05 19:55:34 +00:00
refs := order.Canonicalize(itemOccurrenceSourceRefs(event.SourceRefs, sourceID))
event.SourceRefs = itemOccurrenceResponseRefs(refs)
2026-07-25 21:49:24 +00:00
return order.EarliestValid(refs)
}
2026-08-05 19:55:34 +00:00
func canonicalItemOccurrenceList(response extractionResponse, sourceID string, registry *itemregistry.Registry) dnd.ItemOccurrenceList {
if response.Occurrences == nil {
return dnd.ItemOccurrenceList{}
2026-07-25 21:49:24 +00:00
}
2026-08-05 19:55:34 +00:00
occurrences := make([]dnd.ItemOccurrence, 0, len(response.Occurrences))
for _, event := range response.Occurrences {
item, found := registry.LookupID(event.ItemID)
if !found || item.Name != event.Name {
continue
}
occurrences = append(occurrences, dnd.ItemOccurrence{
ItemID: event.ItemID,
2026-07-25 21:49:24 +00:00
Name: event.Name,
2026-08-05 19:55:34 +00:00
Kind: dnd.ItemOccurrenceKind(event.Kind),
2026-07-25 21:49:24 +00:00
Quantity: cloneQuantity(event.Quantity),
From: event.From,
To: event.To,
2026-08-05 19:55:34 +00:00
SourceRefs: itemOccurrenceSourceRefs(event.SourceRefs, sourceID),
})
2026-07-25 21:49:24 +00:00
}
2026-08-05 19:55:34 +00:00
return dnd.ItemOccurrenceList{Occurrences: occurrences}
2026-07-25 21:49:24 +00:00
}
2026-08-05 19:55:34 +00:00
func itemOccurrenceSourceRefs(refs []itemOccurrenceSourceRefResponse, sourceID string) []source.SourceRef {
2026-07-25 21:49:24 +00:00
if refs == nil {
return nil
}
values := make([]source.SourceRef, len(refs))
for index, ref := range refs {
values[index] = source.SourceRef{SourceID: sourceID, StartUnitID: ref.StartSegment, EndUnitID: ref.EndSegment}
}
return values
}
2026-08-05 19:55:34 +00:00
func itemOccurrenceResponseRefs(refs []source.SourceRef) []itemOccurrenceSourceRefResponse {
2026-07-25 21:49:24 +00:00
if refs == nil {
return nil
}
2026-08-05 19:55:34 +00:00
values := make([]itemOccurrenceSourceRefResponse, len(refs))
2026-07-25 21:49:24 +00:00
for index, ref := range refs {
2026-08-05 19:55:34 +00:00
values[index] = itemOccurrenceSourceRefResponse{StartSegment: ref.StartUnitID, EndSegment: ref.EndUnitID}
2026-07-25 21:49:24 +00:00
}
return values
}
func cloneQuantity(value *int) *int {
if value == nil {
return nil
}
quantity := *value
return &quantity
}