aboutsummaryrefslogtreecommitdiff
path: root/internal/admin/api_players.go
blob: f7a0b875544cbc0c84b95009a738be31f267176d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
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
	}

	charsDir := filepath.Join(s.dataDir, "players", "characters")
	entries, err := os.ReadDir(charsDir)
	if err != nil {
		writeJSON(w, map[string]any{"error": err.Error()})
		return
	}

	charToAccount := s.buildCharToAccountMap()

	type playerSummary struct {
		Name       string `json:"name"`
		Level      int    `json:"level"`
		TotalLevel int    `json:"total_level"`
		Room       int    `json:"room"`
		Account    string `json:"account"`
	}

	var players []playerSummary
	for _, e := range entries {
		if e.IsDir() || filepath.Ext(e.Name()) != ".yaml" {
			continue
		}
		rawName := strings.TrimSuffix(e.Name(), ".yaml")
		p, err := s.accountStore.LoadCharacter(rawName)
		if err != nil {
			continue
		}
		players = append(players, playerSummary{
			Name:       rawName,
			Level:      p.CombatLevel(),
			TotalLevel: p.TotalLevel(),
			Room:       p.RoomID,
			Account:    charToAccount[rawName],
		})
	}
	writeJSON(w, players)
}

func (s *AdminServer) buildCharToAccountMap() map[string]string {
	m := make(map[string]string)
	names, err := s.accountStore.ListAccountNames()
	if err != nil {
		return m
	}
	for _, name := range names {
		acc, err := s.accountStore.LoadAccount(name)
		if err != nil {
			continue
		}
		for _, charName := range acc.Characters {
			m[charName] = acc.Name
		}
	}
	return m
}

func (s *AdminServer) handlePlayerByID(w http.ResponseWriter, r *http.Request) {
	path := strings.TrimPrefix(r.URL.Path, "/api/players/")
	if path == "" {
		http.Error(w, `{"error":"missing player id"}`, http.StatusBadRequest)
		return
	}

	if r.Method != http.MethodGet {
		http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
		return
	}

	if strings.HasPrefix(path, "account/") {
		s.getAccount(w, r, strings.TrimPrefix(path, "account/"))
		return
	}

	s.getCharacter(w, r, path)
}

func (s *AdminServer) getCharacter(w http.ResponseWriter, r *http.Request, name string) {
	canonical, err := s.accountStore.FindCharacter(name)
	if err != nil {
		writeJSONError(w, "character not found: "+name, http.StatusNotFound)
		return
	}

	charPath := s.accountStore.CharPath(canonical)
	data, err := os.ReadFile(charPath)
	if err != nil {
		writeJSONError(w, "read error: "+err.Error(), http.StatusInternalServerError)
		return
	}

	m, err := yamlToMap(data)
	if err != nil {
		writeJSONError(w, "parse error: "+err.Error(), http.StatusInternalServerError)
		return
	}

	var p player.Player
	if err := yaml.Unmarshal(data, &p); err == nil {
		m["combat_level"] = p.CombatLevel()
		m["total_level"] = p.TotalLevel()
		m["max_hp"] = p.MaxHP()
		skillLevels := make(map[string]int)
		for _, skill := range player.AllSkills {
			skillLevels[string(skill)] = p.Level(skill)
		}
		m["skill_levels"] = skillLevels

		charToAccount := s.buildCharToAccountMap()
		if acc, ok := charToAccount[canonical]; ok {
			m["account"] = acc
		}
	}

	m["_path"] = charPath
	m["_raw"] = string(data)
	writeJSON(w, m)
}

func (s *AdminServer) getAccount(w http.ResponseWriter, r *http.Request, name string) {
	canonical, err := s.accountStore.FindAccount(name)
	if err != nil {
		writeJSONError(w, "account not found: "+name, http.StatusNotFound)
		return
	}

	accPath := s.accountStore.AccountPath(canonical)
	data, err := os.ReadFile(accPath)
	if err != nil {
		writeJSONError(w, "read error: "+err.Error(), http.StatusInternalServerError)
		return
	}

	m, err := yamlToMap(data)
	if err != nil {
		writeJSONError(w, "parse error: "+err.Error(), http.StatusInternalServerError)
		return
	}
	delete(m, "password_hash")

	m["_path"] = accPath
	m["_raw"] = string(data)
	writeJSON(w, m)
}