// Package npcinteractions encodes durable D&D NPC interaction artifacts. package npcinteractions import ( "bytes" "embed" "encoding/json" "fmt" "io" "strings" "gitea.maximumdirect.net/eric/notarius/internal/framework/contracts" "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd" ) const ( SchemaID = "notarius.dnd.npc_interactions" SchemaName = "notarius_dnd_npc_interactions_v1" SchemaVersion = "v1" MediaType = "application/json" ) //go:embed assets/schemas/dnd_npc_interactions.v1.json var schemaAssets embed.FS var _ contracts.ArtifactCodec[dnd.NPCInteractionList] = (*Codec)(nil) type Codec struct{} func New() *Codec { return &Codec{} } func (c *Codec) Kind() contracts.ArtifactKind { return dnd.NPCInteractionListKind } func (c *Codec) Schema() contracts.ArtifactSchema { raw, err := schemaAssets.ReadFile("assets/schemas/dnd_npc_interactions.v1.json") if err != nil { return contracts.ArtifactSchema{} } return contracts.ArtifactSchema{ ID: SchemaID, Name: SchemaName, Version: SchemaVersion, JSONSchema: append([]byte(nil), raw...), } } func (c *Codec) MediaType() string { return MediaType } func (c *Codec) Metadata(value dnd.NPCInteractionList) map[string]any { return map[string]any{"interaction_count": len(value.Interactions)} } func (c *Codec) Encode(value dnd.NPCInteractionList) ([]byte, error) { if err := validate(value); err != nil { return nil, fmt.Errorf("encode dnd npc interaction list: %w", err) } return c.EncodeCandidate(value) } // EncodeCandidate provides the durable representation before semantic // validators have approved a value. func (c *Codec) EncodeCandidate(value dnd.NPCInteractionList) ([]byte, error) { content, err := json.Marshal(value) if err != nil { return nil, fmt.Errorf("encode dnd npc interaction list: %w", err) } return content, nil } func (c *Codec) Decode(content []byte) (dnd.NPCInteractionList, error) { value, err := c.DecodeCandidate(content) if err != nil { return dnd.NPCInteractionList{}, err } if err := validate(value); err != nil { return dnd.NPCInteractionList{}, fmt.Errorf("decode dnd npc interaction list: %w", err) } return value, nil } // DecodeCandidate reads one strict durable JSON value before semantic // validators have approved it. func (c *Codec) DecodeCandidate(content []byte) (dnd.NPCInteractionList, error) { decoder := json.NewDecoder(bytes.NewReader(content)) decoder.DisallowUnknownFields() var value dnd.NPCInteractionList if err := decoder.Decode(&value); err != nil { return dnd.NPCInteractionList{}, fmt.Errorf("decode dnd npc interaction list: %w", err) } var trailing any if err := decoder.Decode(&trailing); err != io.EOF { return dnd.NPCInteractionList{}, fmt.Errorf("decode dnd npc interaction list: multiple JSON values") } return value, nil } func validate(value dnd.NPCInteractionList) error { if value.Interactions == nil { return fmt.Errorf("interactions must be present") } for index, interaction := range value.Interactions { prefix := fmt.Sprintf("interactions[%d]", index) if strings.TrimSpace(interaction.Name) == "" { return fmt.Errorf("%s.name must not be empty", prefix) } if !validInteractionKind(interaction.Kind) { return fmt.Errorf("%s.kind must be supported", prefix) } if len(interaction.SourceRefs) == 0 { return fmt.Errorf("%s.source_refs must contain at least one reference", prefix) } for refIndex, ref := range interaction.SourceRefs { refPrefix := fmt.Sprintf("%s.source_refs[%d]", prefix, refIndex) if strings.TrimSpace(ref.SourceID) == "" { return fmt.Errorf("%s.source_id must not be empty", refPrefix) } if ref.StartUnitID <= 0 { return fmt.Errorf("%s.start_unit_id must be positive", refPrefix) } if ref.EndUnitID <= 0 { return fmt.Errorf("%s.end_unit_id must be positive", refPrefix) } } } return nil } func validInteractionKind(value dnd.NPCInteractionKind) bool { switch value { case dnd.NPCInteractionKindMentioned, dnd.NPCInteractionKindNoncombatPresence, dnd.NPCInteractionKindDialogue, dnd.NPCInteractionKindCombatAlly, dnd.NPCInteractionKindCombatOpponent, dnd.NPCInteractionKindOther: return true default: return false } }