113 lines
3.6 KiB
Go
113 lines
3.6 KiB
Go
package itemoccurrences
|
|
|
|
import (
|
|
"fmt"
|
|
"sort"
|
|
|
|
"gitea.maximumdirect.net/eric/notarius/internal/core/source"
|
|
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd"
|
|
itemregistry "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/items/registry"
|
|
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/shared"
|
|
)
|
|
|
|
type orderedItemOccurrenceResponse struct {
|
|
value itemOccurrenceResponse
|
|
earliest int
|
|
hasEvidence bool
|
|
}
|
|
|
|
func canonicalizeResponse(response *extractionResponse, order shared.SourceRefOrder, sourceID string, registry *itemregistry.Registry) error {
|
|
if response == nil {
|
|
return nil
|
|
}
|
|
ordered := make([]orderedItemOccurrenceResponse, len(response.Occurrences))
|
|
for index := range response.Occurrences {
|
|
canonical, found := registry.Lookup(response.Occurrences[index].Name)
|
|
if !found {
|
|
return fmt.Errorf("occurrences[%d].name is not in the item registry", index)
|
|
}
|
|
response.Occurrences[index].Name = canonical.Name
|
|
earliest, hasEvidence := canonicalizeItemOccurrence(&response.Occurrences[index], order, sourceID)
|
|
ordered[index] = orderedItemOccurrenceResponse{
|
|
value: response.Occurrences[index],
|
|
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 {
|
|
response.Occurrences[index] = ordered[index].value
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func canonicalizeItemOccurrence(occurrence *itemOccurrenceResponse, order shared.SourceRefOrder, sourceID string) (int, bool) {
|
|
if occurrence == nil {
|
|
return 0, false
|
|
}
|
|
refs := order.Canonicalize(itemOccurrenceSourceRefs(occurrence.SourceRefs, sourceID))
|
|
occurrence.SourceRefs = itemOccurrenceResponseRefs(refs)
|
|
return order.EarliestValid(refs)
|
|
}
|
|
|
|
func canonicalItemOccurrenceList(response extractionResponse, sourceID string, registry *itemregistry.Registry) (dnd.ItemOccurrenceList, error) {
|
|
if response.Occurrences == nil {
|
|
return dnd.ItemOccurrenceList{}, nil
|
|
}
|
|
occurrences := make([]dnd.ItemOccurrence, 0, len(response.Occurrences))
|
|
for index, occurrence := range response.Occurrences {
|
|
item, found := registry.Lookup(occurrence.Name)
|
|
if !found {
|
|
return dnd.ItemOccurrenceList{}, fmt.Errorf("occurrences[%d].name is not in the item registry", index)
|
|
}
|
|
occurrences = append(occurrences, dnd.ItemOccurrence{
|
|
ItemID: item.ID,
|
|
Name: item.Name,
|
|
Kind: dnd.ItemOccurrenceKind(occurrence.Kind),
|
|
Quantity: cloneQuantity(occurrence.Quantity),
|
|
From: occurrence.From,
|
|
To: occurrence.To,
|
|
SourceRefs: itemOccurrenceSourceRefs(occurrence.SourceRefs, sourceID),
|
|
})
|
|
}
|
|
return dnd.ItemOccurrenceList{Occurrences: occurrences}, nil
|
|
}
|
|
|
|
func itemOccurrenceSourceRefs(refs []itemOccurrenceSourceRefResponse, sourceID string) []source.SourceRef {
|
|
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
|
|
}
|
|
|
|
func itemOccurrenceResponseRefs(refs []source.SourceRef) []itemOccurrenceSourceRefResponse {
|
|
if refs == nil {
|
|
return nil
|
|
}
|
|
values := make([]itemOccurrenceSourceRefResponse, len(refs))
|
|
for index, ref := range refs {
|
|
values[index] = itemOccurrenceSourceRefResponse{StartSegment: ref.StartUnitID, EndSegment: ref.EndUnitID}
|
|
}
|
|
return values
|
|
}
|
|
|
|
func cloneQuantity(value *int) *int {
|
|
if value == nil {
|
|
return nil
|
|
}
|
|
quantity := *value
|
|
return &quantity
|
|
}
|