package game import ( "fmt" "os" "path/filepath" "strings" "thirdcollapse/internal/net" "gopkg.in/yaml.v3" ) type HelpDef struct { Name string `yaml:"name"` Category string `yaml:"category"` Content string `yaml:"description"` } func LoadHelp(dataDir string) ([]HelpDef, error) { dir := filepath.Join(dataDir, "help") entries, err := os.ReadDir(dir) if err != nil { return nil, err } var helps []HelpDef for _, entry := range entries { if entry.IsDir() || filepath.Ext(entry.Name()) != ".yaml" { continue } data, err := os.ReadFile(filepath.Join(dir, entry.Name())) if err != nil { continue } var h HelpDef if err := yaml.Unmarshal(data, &h); err != nil { continue } helps = append(helps, h) } return helps, nil } func (g *Game) doHelp(sess *net.Session, topic string) { helps, err := LoadHelp(g.dataDir) if err != nil || len(helps) == 0 { sess.WriteLine("No help available.") return } if topic == "" { // Group by category cats := make(map[string][]HelpDef) var catOrder []string for _, h := range helps { if _, ok := cats[h.Category]; !ok { catOrder = append(catOrder, h.Category) } cats[h.Category] = append(cats[h.Category], h) } sess.WriteLine("\nCommands:") for _, cat := range catOrder { sess.WriteLine(fmt.Sprintf("\n %s:", cat)) for _, h := range cats[cat] { sess.WriteLine(fmt.Sprintf(" %-12s - %s", h.Name, firstLine(h.Content))) } } sess.WriteLine("\n Use 'help ' for details.") return } topic = strings.ToLower(topic) for _, h := range helps { if strings.ToLower(h.Name) == topic { sess.WriteLine(fmt.Sprintf("\n %s", h.Content)) return } } sess.WriteLine(fmt.Sprintf("No help found for '%s'.", topic)) } func firstLine(s string) string { if idx := strings.Index(s, "\n"); idx >= 0 { return s[:idx] } return s }