Adopt shared campaign and session configuration
This commit is contained in:
@@ -1,20 +1,13 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
|
||||
configkit "go.maximumdirect.net/configkit"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
type renderedCampaignInputs struct {
|
||||
SpeakersFile string `yaml:"speakers_file,omitempty"`
|
||||
AutocorrectFile string `yaml:"autocorrect_file,omitempty"`
|
||||
PartyFile string `yaml:"party_file,omitempty"`
|
||||
SpellCatalogFile string `yaml:"spell_catalog_file,omitempty"`
|
||||
Extra map[string]any `yaml:",inline"`
|
||||
}
|
||||
|
||||
// RenderCampaignDocument serializes the loaded effective campaign document.
|
||||
func RenderCampaignDocument(cfg *Config) ([]byte, error) {
|
||||
if cfg == nil {
|
||||
@@ -27,13 +20,34 @@ func RenderCampaignDocument(cfg *Config) ([]byte, error) {
|
||||
if doc == nil {
|
||||
return nil, fmt.Errorf("campaign document is required")
|
||||
}
|
||||
value := struct {
|
||||
CampaignID string `yaml:"campaign_id"`
|
||||
SessionTemplateFile string `yaml:"session_template_file,omitempty"`
|
||||
Inputs renderedCampaignInputs `yaml:"inputs"`
|
||||
}{doc.CampaignID, doc.SessionTemplateFile, renderedCampaignInputs{
|
||||
doc.Inputs.SpeakersFile, doc.Inputs.AutocorrectFile, doc.Inputs.PartyFile, doc.Inputs.SpellCatalogFile, glossaryFields(doc.ConfigKitResult() != nil, campaignMetadata(doc), doc.Inputs.Setting, doc.Inputs.System, doc.Inputs.GlossaryFile, doc.Inputs.Glossary, false),
|
||||
}}
|
||||
if result := doc.ConfigKitResult(); result != nil {
|
||||
var output bytes.Buffer
|
||||
if err := result.WriteEffective(&output); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return output.Bytes(), nil
|
||||
}
|
||||
inputs := map[string]any{"speakers_file": doc.Inputs.SpeakersFile}
|
||||
if doc.Inputs.GlossaryFile != nil {
|
||||
inputs["glossary_file"] = *doc.Inputs.GlossaryFile
|
||||
}
|
||||
if doc.Inputs.Glossary != nil {
|
||||
inputs["glossary"] = doc.Inputs.Glossary
|
||||
}
|
||||
if doc.Inputs.Autocorrect != nil {
|
||||
inputs["autocorrect"] = doc.Inputs.Autocorrect
|
||||
}
|
||||
narratio := map[string]any{"inputs": inputs}
|
||||
if doc.SessionTemplateFile != "" {
|
||||
narratio["session_template_file"] = doc.SessionTemplateFile
|
||||
}
|
||||
value := map[string]any{"schema_version": "maximumdirect.rpg.campaign.v1", "id": doc.CampaignID, "references": map[string]any{"party": doc.Inputs.PartyFile}, "narratio": narratio}
|
||||
if doc.Inputs.Setting != nil {
|
||||
value["setting"] = *doc.Inputs.Setting
|
||||
}
|
||||
if doc.Inputs.System != nil {
|
||||
value["system"] = *doc.Inputs.System
|
||||
}
|
||||
return yaml.Marshal(value)
|
||||
}
|
||||
|
||||
@@ -67,17 +81,24 @@ func RenderSessionDocument(cfg *Config, sessionID string) ([]byte, error) {
|
||||
return &value
|
||||
}
|
||||
inputs := struct {
|
||||
AudioDir string `yaml:"audio_dir,omitempty"`
|
||||
AudioFiles []string `yaml:"audio_files,omitempty"`
|
||||
AudioS3 *SessionAudioS3Input `yaml:"audio_s3,omitempty"`
|
||||
SpeakersFile *string `yaml:"speakers_file,omitempty"`
|
||||
AutocorrectFile *string `yaml:"autocorrect_file,omitempty"`
|
||||
SpellCatalogFile *string `yaml:"spell_catalog_file,omitempty"`
|
||||
Extra map[string]any `yaml:",inline"`
|
||||
AudioDir string `yaml:"audio_dir,omitempty"`
|
||||
AudioFiles []string `yaml:"audio_files,omitempty"`
|
||||
AudioS3 *SessionAudioS3Input `yaml:"audio_s3,omitempty"`
|
||||
SpeakersFile *string `yaml:"speakers_file,omitempty"`
|
||||
Extra map[string]any `yaml:",inline"`
|
||||
}{doc.Inputs.AudioDir, doc.Inputs.AudioFiles, doc.Inputs.AudioS3,
|
||||
inputValue("speakers_file", doc.Inputs.SpeakersFile),
|
||||
inputValue("autocorrect_file", doc.Inputs.AutocorrectFile),
|
||||
inputValue("spell_catalog_file", doc.Inputs.SpellCatalogFile), glossaryFields(result != nil, metadata, doc.Inputs.Setting, doc.Inputs.System, doc.Inputs.GlossaryFile, doc.Inputs.Glossary, true)}
|
||||
glossaryFields(result != nil, metadata, doc.Inputs.Setting, doc.Inputs.System, doc.Inputs.GlossaryFile, doc.Inputs.Glossary, true)}
|
||||
autocorrectPath := configkit.Keys("inputs", "autocorrect")
|
||||
if result != nil && metadata.IsExplicit(autocorrectPath) {
|
||||
if info, ok := metadata.Lookup(autocorrectPath); ok && info.Null {
|
||||
inputs.Extra["autocorrect"] = nil
|
||||
} else {
|
||||
inputs.Extra["autocorrect"] = doc.Inputs.Autocorrect
|
||||
}
|
||||
} else if result == nil && doc.Inputs.Autocorrect != nil {
|
||||
inputs.Extra["autocorrect"] = doc.Inputs.Autocorrect
|
||||
}
|
||||
value := struct {
|
||||
SessionID string `yaml:"session_id"`
|
||||
PreviousSessionID string `yaml:"previous_session_id,omitempty"`
|
||||
@@ -89,13 +110,6 @@ func RenderSessionDocument(cfg *Config, sessionID string) ([]byte, error) {
|
||||
return yaml.Marshal(value)
|
||||
}
|
||||
|
||||
func campaignMetadata(cfg *CampaignConfig) configkit.Metadata {
|
||||
if r := cfg.ConfigKitResult(); r != nil {
|
||||
return r.Metadata()
|
||||
}
|
||||
return configkit.Metadata{}
|
||||
}
|
||||
|
||||
func glossaryFields(hasResult bool, metadata configkit.Metadata, setting, system, file *string, glossary map[string]any, authoredOnly bool) map[string]any {
|
||||
result := map[string]any{}
|
||||
for _, field := range []struct {
|
||||
|
||||
Reference in New Issue
Block a user