Index enemy event duplicate identities
This commit is contained in:
@@ -3,6 +3,8 @@
|
||||
package enemyevents
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/core/source"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/npcs/identity"
|
||||
@@ -58,9 +60,49 @@ func SourceRefsEqual(order shared.SourceRefOrder, left, right []source.SourceRef
|
||||
// ExactEqual reports whether events have the same subject identity, kind, and
|
||||
// complete canonical evidence sequence.
|
||||
func ExactEqual(order shared.SourceRefOrder, left, right dnd.EnemyEvent) bool {
|
||||
left.SourceRefs = order.Canonicalize(left.SourceRefs)
|
||||
right.SourceRefs = order.Canonicalize(right.SourceRefs)
|
||||
return CanonicalExactEqual(left, right)
|
||||
}
|
||||
|
||||
type canonicalIdentity struct {
|
||||
Subject string `json:"subject"`
|
||||
Kind dnd.EnemyEventKind `json:"kind"`
|
||||
SourceRefs []source.SourceRef `json:"source_refs"`
|
||||
}
|
||||
|
||||
// CanonicalIdentity returns the complete duplicate-lookup key for an event
|
||||
// whose source references are already canonical.
|
||||
func CanonicalIdentity(event dnd.EnemyEvent) string {
|
||||
encoded, err := json.Marshal(canonicalIdentity{
|
||||
Subject: ComparisonKey(event.Name),
|
||||
Kind: event.Kind,
|
||||
SourceRefs: event.SourceRefs,
|
||||
})
|
||||
if err != nil {
|
||||
panic("encode enemy event identity")
|
||||
}
|
||||
return string(encoded)
|
||||
}
|
||||
|
||||
// CanonicalExactEqual reports whether events have the same complete duplicate
|
||||
// identity without modifying or canonicalizing their source references.
|
||||
func CanonicalExactEqual(left, right dnd.EnemyEvent) bool {
|
||||
return ComparisonKey(left.Name) == ComparisonKey(right.Name) &&
|
||||
left.Kind == right.Kind &&
|
||||
SourceRefsEqual(order, left.SourceRefs, right.SourceRefs)
|
||||
canonicalSourceRefsEqual(left.SourceRefs, right.SourceRefs)
|
||||
}
|
||||
|
||||
func canonicalSourceRefsEqual(left, right []source.SourceRef) bool {
|
||||
if (left == nil) != (right == nil) || len(left) != len(right) {
|
||||
return false
|
||||
}
|
||||
for index := range left {
|
||||
if left[index] != right[index] {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// Less defines the canonical stable event order. Invalid source references
|
||||
|
||||
Reference in New Issue
Block a user