aboutsummaryrefslogtreecommitdiff
path: root/internal/game/help.go
diff options
context:
space:
mode:
authorhistoria <[not public]>2026-06-09 05:59:20 -0400
committerhistoria <[not public]>2026-06-09 05:59:20 -0400
commit9eb5ab1818b6b19501fd7209db1987c9e06cc919 (patch)
tree76e046b0bf611dfe939a8c8f6507467de2e9e20f /internal/game/help.go
downloadthehouseoficarus-9eb5ab1818b6b19501fd7209db1987c9e06cc919.tar.gz
first commit
Diffstat (limited to 'internal/game/help.go')
-rw-r--r--internal/game/help.go88
1 files changed, 88 insertions, 0 deletions
diff --git a/internal/game/help.go b/internal/game/help.go
new file mode 100644
index 0000000..1e93dcf
--- /dev/null
+++ b/internal/game/help.go
@@ -0,0 +1,88 @@
+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 <command>' 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
+}