aboutsummaryrefslogtreecommitdiff
path: root/internal/admin/api_rooms.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/admin/api_rooms.go')
-rw-r--r--internal/admin/api_rooms.go70
1 files changed, 25 insertions, 45 deletions
diff --git a/internal/admin/api_rooms.go b/internal/admin/api_rooms.go
index b60aefc..a477cc3 100644
--- a/internal/admin/api_rooms.go
+++ b/internal/admin/api_rooms.go
@@ -106,7 +106,7 @@ func (s *AdminServer) handleRooms(w http.ResponseWriter, r *http.Request) {
})
}
- exits[string(opp)] = float64(*linkFrom)
+ exits[string(opp)] = *linkFrom
}
}
@@ -161,7 +161,7 @@ func (s *AdminServer) handleRoomByID(w http.ResponseWriter, r *http.Request) {
return
}
m["id"] = id
- writeJSON(w, map[string]any{"room": m, "path": path, "file": path, "raw": string(data)})
+ writeJSON(w, map[string]any{"room": m, "path": path, "file": path})
case http.MethodPut:
path, ok := s.world.GetRoomPath(id)
@@ -298,45 +298,27 @@ func (s *AdminServer) handleRoomDirs(w http.ResponseWriter, r *http.Request) {
return
}
base := filepath.Join(s.dataDir, "rooms")
- entries, err := os.ReadDir(base)
- if err != nil {
- writeJSON(w, []string{""})
- return
- }
dirs := []string{""}
- for _, e := range entries {
- if !e.IsDir() {
- continue
- }
- if hasYAMLFiles(filepath.Join(base, e.Name())) {
- dirs = append(dirs, e.Name())
- }
- subEntries, err := os.ReadDir(filepath.Join(base, e.Name()))
- if err != nil {
- continue
- }
- for _, se := range subEntries {
- if se.IsDir() && hasYAMLFiles(filepath.Join(base, e.Name(), se.Name())) {
- dirs = append(dirs, e.Name()+"/"+se.Name())
+ filepath.WalkDir(base, func(path string, d os.DirEntry, err error) error {
+ if err != nil || !d.IsDir() || path == base {
+ return nil
+ }
+ entries, rdErr := os.ReadDir(path)
+ if rdErr != nil {
+ return nil
+ }
+ for _, e := range entries {
+ if !e.IsDir() && filepath.Ext(e.Name()) == ".yaml" {
+ rel, _ := filepath.Rel(base, path)
+ dirs = append(dirs, rel)
+ return nil
}
}
- }
+ return nil
+ })
writeJSON(w, dirs)
}
-func hasYAMLFiles(dir string) bool {
- entries, err := os.ReadDir(dir)
- if err != nil {
- return false
- }
- for _, e := range entries {
- if !e.IsDir() && filepath.Ext(e.Name()) == ".yaml" {
- return true
- }
- }
- return false
-}
-
func (s *AdminServer) handleRoomLink(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodPost:
@@ -377,10 +359,14 @@ func (s *AdminServer) handleRoomLink(w http.ResponseWriter, r *http.Request) {
cA, okA := grid.Coord[body.From]
cB, okB := grid.Coord[body.To]
- if !okA || !okB || cA[2] != cB[2] {
- writeJSON(w, map[string]any{"error": "rooms must be on same z-level"})
- return
- }
+ if !okA || !okB {
+ writeJSON(w, map[string]any{"error": "one or both rooms are not reachable from the seed room"})
+ return
+ }
+ if cA[2] != cB[2] {
+ writeJSON(w, map[string]any{"error": "rooms must be on same z-level"})
+ return
+ }
dx, dy := cB[0]-cA[0], cB[1]-cA[1]
for d, delta := range world.DirectionDeltas3D {
if delta[0] == dx && delta[1] == dy && delta[2] == 0 {
@@ -510,12 +496,6 @@ func (s *AdminServer) handleRoomLink(w http.ResponseWriter, r *http.Request) {
writeJSON(w, map[string]any{"error": "both rooms not found"})
return
}
- if (errA != nil && roomA == nil) {
- roomA = nil
- }
- if (errB != nil && roomB == nil) {
- roomB = nil
- }
var dir, oppDir world.ExitDir
if roomA != nil {