89 lines
2.7 KiB
Go
89 lines
2.7 KiB
Go
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"
|
|
)
|
|
|
|
type orderedInteractionResponse struct {
|
|
value interactionResponse
|
|
earliest int
|
|
hasEvidence bool
|
|
}
|
|
|
|
func canonicalizeResponse(response *extractionResponse, order shared.SourceRefOrder, sourceID string) {
|
|
if response == nil {
|
|
return
|
|
}
|
|
ordered := make([]orderedInteractionResponse, len(response.Interactions))
|
|
for index := range response.Interactions {
|
|
earliest, hasEvidence := canonicalizeInteraction(&response.Interactions[index], order, sourceID)
|
|
ordered[index] = orderedInteractionResponse{
|
|
value: response.Interactions[index],
|
|
earliest: earliest,
|
|
hasEvidence: hasEvidence,
|
|
}
|
|
}
|
|
sort.SliceStable(ordered, func(i, j int) bool {
|
|
if ordered[i].hasEvidence != ordered[j].hasEvidence {
|
|
return ordered[i].hasEvidence
|
|
}
|
|
if !ordered[i].hasEvidence {
|
|
return false
|
|
}
|
|
return ordered[i].earliest < ordered[j].earliest
|
|
})
|
|
for index := range ordered {
|
|
response.Interactions[index] = ordered[index].value
|
|
}
|
|
}
|
|
|
|
func canonicalizeInteraction(interaction *interactionResponse, order shared.SourceRefOrder, sourceID string) (int, bool) {
|
|
if interaction == nil {
|
|
return 0, false
|
|
}
|
|
refs := order.Canonicalize(canonicalSourceRefs(interaction.SourceRefs, sourceID))
|
|
interaction.SourceRefs = interactionResponseRefs(refs)
|
|
return order.EarliestValid(refs)
|
|
}
|
|
|
|
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
|
|
}
|