Render imported party rosters as canonical inputs

This commit is contained in:
2026-11-17 22:18:57 +00:00
parent 1657c9f65d
commit 05f5607968
10 changed files with 190 additions and 34 deletions

View File

@@ -1,7 +1,6 @@
package config
import (
"bytes"
"fmt"
"math"
"sort"
@@ -20,11 +19,8 @@ const (
PlayersSchemaVersion = "narratio.players.v1"
)
// CanonicalParty is the normalized domain value for narratio.party.v1. Raw is
// retained independently so later preparation can copy the authored party file
// byte-for-byte rather than serializing normalized values.
// CanonicalParty is the normalized domain value for narratio.party.v1.
type CanonicalParty struct {
Raw []byte
Characters []PartyCharacter
}
@@ -70,13 +66,13 @@ type partyPlayer struct {
type partyCharacterDetails struct {
Name string `yaml:"name"`
Aliases []string `yaml:"alias"`
Aliases []string `yaml:"alias,omitempty"`
Classes []partyClass `yaml:"classes"`
}
type partyClass struct {
Name string `yaml:"name"`
Level *int `yaml:"level"`
Level *int `yaml:"level,omitempty"`
}
type partyDomainError struct {
@@ -89,6 +85,15 @@ func ParseParty(data []byte) (*CanonicalParty, error) {
if int64(len(data)) > maxConfigDocumentBytes {
return nil, fmt.Errorf("parse party config: party document exceeds %d-byte limit", maxConfigDocumentBytes)
}
return loadParty(configkit.Bytes("party.yml", data), false)
}
// LoadParty loads an authored party file and its native imports.
func LoadParty(path string) (*CanonicalParty, error) {
return loadParty(configkit.File(path), true)
}
func loadParty(source configkit.Source, imports bool) (*CanonicalParty, error) {
definition := configkit.Definition[partyDocument]{
Version: &configkit.VersionRule{
Path: configkit.Keys("schema_version"),
@@ -96,11 +101,11 @@ func ParseParty(data []byte) (*CanonicalParty, error) {
},
Validate: validatePartyDocument,
}
result, err := configkit.Load(configkit.Bytes("party.yml", data), configkit.Options[partyDocument]{Definition: definition})
result, err := configkit.Load(source, configkit.Options[partyDocument]{Imports: imports, Definition: definition})
if err != nil {
return nil, describeConfigKitLoadError("party", err)
}
return canonicalPartyFromResult(result, data), nil
return canonicalPartyFromResult(result), nil
}
func validatePartyVersion(version configkit.Version, validation *configkit.Validation) error {
@@ -200,21 +205,37 @@ func partyNameAmbiguous(name string, existing []string) bool {
return false
}
func canonicalPartyFromResult(result *configkit.Result[partyDocument], raw []byte) *CanonicalParty {
func canonicalPartyFromResult(result *configkit.Result[partyDocument]) *CanonicalParty {
type orderedCharacter struct {
id string
location configkit.Location
source int
}
metadata := result.Metadata()
sourceOrder := make(map[string]int)
for index, source := range result.Sources() {
sourceOrder[source.ID] = index
}
ordered := make([]orderedCharacter, 0, len(result.Config.Characters))
for id := range result.Config.Characters {
entry := orderedCharacter{id: id, location: configkit.Location{Line: math.MaxInt}}
entry := orderedCharacter{id: id, source: math.MaxInt, location: configkit.Location{Line: math.MaxInt, Column: math.MaxInt}}
if info, ok := metadata.Lookup(configkit.Keys("characters", id)); ok && len(info.Origin.Locations) != 0 {
entry.location = info.Origin.Locations[0]
for _, inheritance := range info.Origin.Inheritance {
if inheritance.To.String() == configkit.Keys("characters", id).String() && len(inheritance.Locations) != 0 {
entry.location = inheritance.Locations[0]
}
}
if index, ok := sourceOrder[entry.location.SourceID]; ok {
entry.source = index
}
}
ordered = append(ordered, entry)
}
sort.Slice(ordered, func(left, right int) bool {
if ordered[left].source != ordered[right].source {
return ordered[left].source < ordered[right].source
}
if ordered[left].location.Line != ordered[right].location.Line {
return ordered[left].location.Line < ordered[right].location.Line
}
@@ -223,7 +244,7 @@ func canonicalPartyFromResult(result *configkit.Result[partyDocument], raw []byt
}
return ordered[left].id < ordered[right].id
})
party := &CanonicalParty{Raw: bytes.Clone(raw), Characters: make([]PartyCharacter, 0, len(ordered))}
party := &CanonicalParty{Characters: make([]PartyCharacter, 0, len(ordered))}
for _, orderedCharacter := range ordered {
input := result.Config.Characters[orderedCharacter.id]
classes := make([]PartyClass, len(input.Character.Classes))
@@ -241,6 +262,34 @@ func canonicalPartyFromResult(result *configkit.Result[partyDocument], raw []byt
return party
}
// YAML renders the effective roster as a standalone canonical party document.
func (p *CanonicalParty) YAML() ([]byte, error) {
if p == nil {
return nil, fmt.Errorf("canonical party is required")
}
characters := &yaml.Node{Kind: yaml.MappingNode, Tag: "!!map"}
for _, entry := range p.Characters {
value := partyCharacter{Player: partyPlayer{Name: entry.Player.Name}, Character: partyCharacterDetails{Name: entry.Character.Name, Aliases: entry.Character.Aliases}}
for _, class := range entry.Character.Classes {
value.Character.Classes = append(value.Character.Classes, partyClass{Name: class.Name, Level: class.Level})
}
var node yaml.Node
if err := node.Encode(value); err != nil {
return nil, fmt.Errorf("render canonical party: %w", err)
}
characters.Content = append(characters.Content, &yaml.Node{Kind: yaml.ScalarNode, Tag: "!!str", Value: entry.ID}, &node)
}
document := &yaml.Node{Kind: yaml.MappingNode, Tag: "!!map", Content: []*yaml.Node{
{Kind: yaml.ScalarNode, Tag: "!!str", Value: "schema_version"}, {Kind: yaml.ScalarNode, Tag: "!!str", Value: PartySchemaVersion},
{Kind: yaml.ScalarNode, Tag: "!!str", Value: "characters"}, characters,
}}
data, err := yaml.Marshal(document)
if err != nil {
return nil, fmt.Errorf("render canonical party: %w", err)
}
return data, nil
}
// ClassSummary preserves the declaration order required by the party contract.
func (c PartyCharacter) ClassSummary() string {
parts := make([]string, 0, len(c.Character.Classes))