Files
notarius/internal/modules/dnd/enemyevents/enemyevents.go

169 lines
5.8 KiB
Go

// Package enemyevents owns canonical ordering and identity policy for D&D
// enemy-event artifacts.
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"
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/shared"
)
// NormalizeDisplay returns the durable display form for an enemy subject.
func NormalizeDisplay(value string) string { return identity.NormalizeDisplay(value) }
// ComparisonKey returns the shared D&D identity key for an enemy subject.
func ComparisonKey(value string) string { return identity.ComparisonKey(NormalizeDisplay(value)) }
// SupportedKind reports whether kind belongs to the durable enemy-event vocabulary.
func SupportedKind(kind dnd.EnemyEventKind) bool {
_, ok := KindRank(kind)
return ok
}
// KindRank returns the explicit chronological tie-break order for event kinds.
func KindRank(kind dnd.EnemyEventKind) (int, bool) {
switch kind {
case dnd.EnemyEventKindEngaged:
return 0, true
case dnd.EnemyEventKindIncapacitated:
return 1, true
case dnd.EnemyEventKindCaptured:
return 2, true
case dnd.EnemyEventKindFled:
return 3, true
case dnd.EnemyEventKindKilled:
return 4, true
default:
return 0, false
}
}
// SourceRefsEqual reports whether two reference sequences are equal after
// canonical ordering and exact duplicate removal.
func SourceRefsEqual(order shared.SourceRefOrder, left, right []source.SourceRef) bool {
left = order.Canonicalize(left)
right = order.Canonicalize(right)
if (left == nil) != (right == nil) || len(left) != len(right) {
return false
}
for index := range left {
if left[index] != right[index] {
return false
}
}
return true
}
// 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 &&
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
// remain comparable through SourceRefOrder's literal fallback so malformed
// candidates can still be sorted for later validation.
func Less(order shared.SourceRefOrder, left, right dnd.EnemyEvent) bool {
left.SourceRefs = order.Canonicalize(left.SourceRefs)
right.SourceRefs = order.Canonicalize(right.SourceRefs)
return CanonicalLessPrepared(order, left, right, PrepareCanonicalOrder(order, left), PrepareCanonicalOrder(order, right))
}
// CanonicalOrderKey holds invariant comparisons for an owned event.
type CanonicalOrderKey struct {
earliest int
hasEvidence bool
nameComparison, nameDisplay string
kindRank int
kindKnown bool
}
// PrepareCanonicalOrder requires evidence already in canonical order.
func PrepareCanonicalOrder(order shared.SourceRefOrder, event dnd.EnemyEvent) CanonicalOrderKey {
earliest, hasEvidence := order.EarliestValid(event.SourceRefs)
rank, known := KindRank(event.Kind)
return CanonicalOrderKey{earliest: earliest, hasEvidence: hasEvidence, nameComparison: ComparisonKey(event.Name), nameDisplay: NormalizeDisplay(event.Name), kindRank: rank, kindKnown: known}
}
// CanonicalLessPrepared requires keys prepared from these unchanged canonical
// events. Less is the defensive comparator for unproven evidence order.
func CanonicalLessPrepared(order shared.SourceRefOrder, left, right dnd.EnemyEvent, leftKey, rightKey CanonicalOrderKey) bool {
if leftKey.hasEvidence != rightKey.hasEvidence {
return leftKey.hasEvidence
}
if leftKey.hasEvidence && leftKey.earliest != rightKey.earliest {
return leftKey.earliest < rightKey.earliest
}
if leftKey.nameComparison != rightKey.nameComparison {
return leftKey.nameComparison < rightKey.nameComparison
}
if leftKey.nameDisplay != rightKey.nameDisplay {
return leftKey.nameDisplay < rightKey.nameDisplay
}
if leftKey.kindKnown != rightKey.kindKnown {
return leftKey.kindKnown
}
if leftKey.kindKnown && leftKey.kindRank != rightKey.kindRank {
return leftKey.kindRank < rightKey.kindRank
}
if left.Kind != right.Kind {
return left.Kind < right.Kind
}
return sourceRefsLess(order, left.SourceRefs, right.SourceRefs)
}
func sourceRefsLess(order shared.SourceRefOrder, left, right []source.SourceRef) bool {
for index := 0; index < len(left) && index < len(right); index++ {
if left[index] == right[index] {
continue
}
return order.Less(left[index], right[index])
}
return len(left) < len(right)
}