From 069d3c1c81d71042706372df153254487a765dfc Mon Sep 17 00:00:00 2001 From: historia <[not public]> Date: Mon, 29 Jun 2026 22:31:11 -0400 Subject: feat: wip web admin for mapping and crud --- internal/admin/api_players.go | 77 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 internal/admin/api_players.go (limited to 'internal/admin/api_players.go') diff --git a/internal/admin/api_players.go b/internal/admin/api_players.go new file mode 100644 index 0000000..2d0c09b --- /dev/null +++ b/internal/admin/api_players.go @@ -0,0 +1,77 @@ +package admin + +import ( + "net/http" + "os" + "path/filepath" + "strings" + + "gopkg.in/yaml.v3" + + "thehouseoficarus/internal/player" +) + +func (s *AdminServer) handlePlayers(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodGet { + http.Error(w, "method not allowed", http.StatusMethodNotAllowed) + return + } + + nameFilter := r.URL.Query().Get("name") + + if nameFilter != "" { + name, err := s.accountStore.FindCharacter(nameFilter) + if err != nil { + writeJSON(w, map[string]any{"error": "character not found: " + nameFilter}) + return + } + charPath := s.accountStore.CharPath(name) + data, err := os.ReadFile(charPath) + if err != nil { + writeJSON(w, map[string]any{"error": "read error: " + err.Error()}) + return + } + var p player.Player + if err := yaml.Unmarshal(data, &p); err != nil { + writeJSON(w, map[string]any{"error": "parse error: " + err.Error()}) + return + } + writeJSON(w, p) + return + } + + charsDir := filepath.Join(s.dataDir, "players", "characters") + entries, err := os.ReadDir(charsDir) + if err != nil { + writeJSON(w, map[string]any{"error": err.Error()}) + return + } + + type playerSummary struct { + Name string `json:"name"` + Level int `json:"level"` + Room int `json:"room"` + } + + var players []playerSummary + for _, e := range entries { + if e.IsDir() || filepath.Ext(e.Name()) != ".yaml" { + continue + } + rawName := strings.TrimSuffix(e.Name(), ".yaml") + data, err := os.ReadFile(filepath.Join(charsDir, e.Name())) + if err != nil { + continue + } + var p player.Player + if err := yaml.Unmarshal(data, &p); err != nil { + continue + } + players = append(players, playerSummary{ + Name: rawName, + Level: p.CombatLevel(), + Room: p.RoomID, + }) + } + writeJSON(w, players) +} -- cgit v1.2.3