aboutsummaryrefslogtreecommitdiff
path: root/internal/admin/api_players.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/admin/api_players.go')
-rw-r--r--internal/admin/api_players.go77
1 files changed, 77 insertions, 0 deletions
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)
+}