package npcinteractions import ( "sort" "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, order shared.SourceRefOrder, sourceID string) { if response == nil { return } for index := range response.Interactions { canonicalizeInteraction(&response.Interactions[index], order, sourceID) } sort.SliceStable(response.Interactions, func(i, j int) bool { left, leftOK := order.EarliestValid(canonicalSourceRefs(response.Interactions[i].SourceRefs, sourceID)) right, rightOK := order.EarliestValid(canonicalSourceRefs(response.Interactions[j].SourceRefs, sourceID)) if leftOK != rightOK { return leftOK } if !leftOK { return false } return left < right }) } func canonicalizeInteraction(interaction *interactionResponse, order shared.SourceRefOrder, sourceID string) { if interaction == nil { return } interaction.SourceRefs = interactionResponseRefs(order.Canonicalize(canonicalSourceRefs(interaction.SourceRefs, sourceID))) } func canonicalInteractionList(response extractionResponse, sourceID string) dnd.NPCInteractionList { interactions := make([]dnd.NPCInteraction, len(response.Interactions)) for index, interaction := range response.Interactions { interactions[index] = dnd.NPCInteraction{ Name: interaction.Name, Kind: dnd.NPCInteractionKind(interaction.Kind), SourceRefs: canonicalSourceRefs(interaction.SourceRefs, sourceID), } } if response.Interactions == nil { interactions = nil } return dnd.NPCInteractionList{Interactions: interactions} } func canonicalSourceRefs(refs []interactionSourceRefResponse, sourceID string) []source.SourceRef { if refs == nil { return nil } out := make([]source.SourceRef, len(refs)) for index, ref := range refs { out[index] = source.SourceRef{SourceID: sourceID, StartUnitID: ref.StartUnitID, EndUnitID: ref.EndUnitID} } return out } func interactionResponseRefs(refs []source.SourceRef) []interactionSourceRefResponse { if refs == nil { return nil } values := make([]interactionSourceRefResponse, len(refs)) for index, ref := range refs { values[index] = interactionSourceRefResponse{StartUnitID: ref.StartUnitID, EndUnitID: ref.EndUnitID} } return values }