Files
ainovel-clients/state/world_rules.go
voocel 27bd85ef90 init
2026-03-07 21:25:55 +08:00

59 lines
1.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package state
import (
"fmt"
"os"
"strings"
"github.com/voocel/ainovel-cli/domain"
)
// SaveWorldRules 全量写入 world_rules.json + world_rules.md。
func (s *Store) SaveWorldRules(rules []domain.WorldRule) error {
if err := s.writeJSON("world_rules.json", rules); err != nil {
return err
}
return s.writeMarkdown("world_rules.md", renderWorldRules(rules))
}
// LoadWorldRules 读取世界规则。
func (s *Store) LoadWorldRules() ([]domain.WorldRule, error) {
var rules []domain.WorldRule
if err := s.readJSON("world_rules.json", &rules); err != nil {
if os.IsNotExist(err) {
return nil, nil
}
return nil, err
}
return rules, nil
}
func renderWorldRules(rules []domain.WorldRule) string {
grouped := make(map[string][]domain.WorldRule)
var order []string
for _, r := range rules {
cat := r.Category
if cat == "" {
cat = "other"
}
if _, exists := grouped[cat]; !exists {
order = append(order, cat)
}
grouped[cat] = append(grouped[cat], r)
}
var b strings.Builder
b.WriteString("# 世界观规则\n\n")
for _, cat := range order {
fmt.Fprintf(&b, "## %s\n\n", cat)
for _, r := range grouped[cat] {
fmt.Fprintf(&b, "- **规则**%s\n", r.Rule)
if r.Boundary != "" {
fmt.Fprintf(&b, " - 边界:%s\n", r.Boundary)
}
}
b.WriteString("\n")
}
return b.String()
}