package itemevents import ( "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) { if response == nil { return } 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], 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 } } func canonicalizeItemOccurrence(event *itemOccurrenceResponse, order shared.SourceRefOrder, sourceID string) (int, bool) { if event == nil { return 0, false } refs := order.Canonicalize(itemOccurrenceSourceRefs(event.SourceRefs, sourceID)) event.SourceRefs = itemOccurrenceResponseRefs(refs) return order.EarliestValid(refs) } func canonicalItemOccurrenceList(response extractionResponse, sourceID string, registry *itemregistry.Registry) dnd.ItemOccurrenceList { if response.Occurrences == nil { return dnd.ItemOccurrenceList{} } 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, Name: event.Name, Kind: dnd.ItemOccurrenceKind(event.Kind), Quantity: cloneQuantity(event.Quantity), From: event.From, To: event.To, SourceRefs: itemOccurrenceSourceRefs(event.SourceRefs, sourceID), }) } return dnd.ItemOccurrenceList{Occurrences: occurrences} } 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 }