Adopt the shared party roster contract

This commit is contained in:
2026-11-30 00:03:54 +00:00
parent f923d75562
commit d1bf1fbfff
30 changed files with 233 additions and 238 deletions

View File

@@ -1,25 +1,26 @@
package config
import (
"bytes"
"fmt"
"math"
"sort"
"strings"
"unicode"
"gitea.maximumdirect.net/eric/narratio/internal/artifactpolicy"
"gitea.maximumdirect.net/eric/narratio/internal/domaininput"
configkit "go.maximumdirect.net/configkit"
"gopkg.in/yaml.v3"
)
const (
// PartySchemaVersion identifies the canonical campaign party document.
PartySchemaVersion = "narratio.party.v1"
PartySchemaVersion = "maximumdirect.rpg.party.v1"
// PlayersSchemaVersion identifies the derived players-only document.
PlayersSchemaVersion = "narratio.players.v1"
)
// CanonicalParty is the normalized domain value for narratio.party.v1.
// CanonicalParty is the derived domain value for a shared party.
type CanonicalParty struct {
Characters []PartyCharacter
authoring *configkit.Result[PartyConfig]
@@ -61,9 +62,10 @@ type PartyClass struct {
}
// PartyConfig is the authored roster schema retained by ConfigKit.
type PartyConfig struct {
SchemaVersion string `yaml:"schema_version"`
Characters map[string]partyCharacter `yaml:"characters"`
type PartyConfig = map[string]any
type partyView struct {
Characters map[string]partyCharacter
}
type partyCharacter struct {
@@ -77,7 +79,7 @@ type partyPlayer struct {
type partyCharacterDetails struct {
Name string `yaml:"name"`
Aliases []string `yaml:"alias,omitempty"`
Aliases []string `yaml:"aliases,omitempty"`
Classes []partyClass `yaml:"classes"`
}
@@ -110,13 +112,19 @@ func loadParty(source configkit.Source, imports bool) (*CanonicalParty, error) {
Path: configkit.Keys("schema_version"),
Check: validatePartyVersion,
},
Validate: validatePartyDocument,
}
result, err := configkit.Load(source, configkit.Options[PartyConfig]{Imports: imports, Definition: definition})
if err != nil {
return nil, fmt.Errorf("load party config: %w", err)
}
return canonicalPartyFromResult(result), nil
if err := domaininput.ValidateParty(result.Config); err != nil {
return nil, fmt.Errorf("validate party: %w", err)
}
view := interpretParty(result.Config)
if problem := firstPartyDomainError(&view); problem != nil {
return nil, fmt.Errorf("party %s: %s", problem.path, problem.message)
}
return canonicalPartyFromResult(result, view), nil
}
func validatePartyVersion(version configkit.Version, validation *configkit.Validation) error {
@@ -129,13 +137,7 @@ func validatePartyVersion(version configkit.Version, validation *configkit.Valid
return nil
}
func validatePartyDocument(document *PartyConfig, validation *configkit.Validation) {
if problem := firstPartyDomainError(document); problem != nil {
validation.Error(problem.path, problem.message)
}
}
func firstPartyDomainError(document *PartyConfig) *partyDomainError {
func firstPartyDomainError(document *partyView) *partyDomainError {
charactersPath := configkit.Keys("characters")
if document == nil || len(document.Characters) == 0 {
return &partyDomainError{path: charactersPath, message: "characters must be non-empty"}
@@ -163,7 +165,7 @@ func firstPartyDomainError(document *PartyConfig) *partyDomainError {
}
visibleNames := append(append([]string(nil), seenNames...), character.Character.Name)
for index, alias := range character.Character.Aliases {
aliasPath := path.Key("character").Key("alias").Index(index)
aliasPath := path.Key("character").Key("aliases").Index(index)
if err := validatePartyDisplayName(alias); err != "" {
return &partyDomainError{path: aliasPath, message: err}
}
@@ -216,89 +218,69 @@ func partyNameAmbiguous(name string, existing []string) bool {
return false
}
func canonicalPartyFromResult(result *configkit.Result[PartyConfig]) *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, 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]
func interpretParty(document map[string]any) partyView {
view := partyView{Characters: make(map[string]partyCharacter)}
for id, raw := range document["characters"].(map[string]any) {
entry := raw.(map[string]any)
details := entry["character"].(map[string]any)
var character partyCharacter
if player, ok := entry["player"].(map[string]any); ok {
character.Player.Name, _ = player["name"].(string)
}
character.Character.Name, _ = details["name"].(string)
if aliases, ok := details["aliases"].([]any); ok {
for _, alias := range aliases {
character.Character.Aliases = append(character.Character.Aliases, alias.(string))
}
}
if classes, ok := details["classes"].([]any); ok {
for _, item := range classes {
class := item.(map[string]any)
value := partyClass{Name: class["name"].(string)}
switch level := class["level"].(type) {
case int64:
n := int(level)
value.Level = &n
case float64:
n := int(level)
value.Level = &n
}
}
if index, ok := sourceOrder[entry.location.SourceID]; ok {
entry.source = index
character.Character.Classes = append(character.Character.Classes, value)
}
}
ordered = append(ordered, entry)
view.Characters[id] = character
}
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
}
if ordered[left].location.Column != ordered[right].location.Column {
return ordered[left].location.Column < ordered[right].location.Column
}
return ordered[left].id < ordered[right].id
})
party := &CanonicalParty{Characters: make([]PartyCharacter, 0, len(ordered)), authoring: result}
for _, orderedCharacter := range ordered {
input := result.Config.Characters[orderedCharacter.id]
return view
}
func canonicalPartyFromResult(result *configkit.Result[PartyConfig], view partyView) *CanonicalParty {
ids := make([]string, 0, len(view.Characters))
for id := range view.Characters {
ids = append(ids, id)
}
sort.Strings(ids)
party := &CanonicalParty{Characters: make([]PartyCharacter, 0, len(ids)), authoring: result}
for _, id := range ids {
input := view.Characters[id]
classes := make([]PartyClass, len(input.Character.Classes))
for index, class := range input.Character.Classes {
classes[index] = PartyClass{Name: class.Name, Level: class.Level}
}
party.Characters = append(party.Characters, PartyCharacter{
ID: orderedCharacter.id,
Player: PartyPlayer{Name: input.Player.Name},
Character: PartyCharacterDetails{
Name: input.Character.Name, Aliases: append([]string(nil), input.Character.Aliases...), Classes: classes,
},
})
party.Characters = append(party.Characters, PartyCharacter{ID: id, Player: PartyPlayer{Name: input.Player.Name}, Character: PartyCharacterDetails{Name: input.Character.Name, Aliases: append([]string(nil), input.Character.Aliases...), Classes: classes}})
}
return party
}
// YAML renders the effective roster as a standalone canonical party document.
// YAML renders the completed native roster as a standalone document.
func (p *CanonicalParty) YAML() ([]byte, error) {
if p == nil {
if p == nil || p.authoring == 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 {
var output bytes.Buffer
if err := p.authoring.WriteEffective(&output); err != nil {
return nil, fmt.Errorf("render canonical party: %w", err)
}
return data, nil
return output.Bytes(), nil
}
// ClassSummary preserves the declaration order required by the party contract.