diff options
| author | historia <[not public]> | 2026-06-29 22:31:11 -0400 |
|---|---|---|
| committer | historia <[not public]> | 2026-06-29 22:31:11 -0400 |
| commit | 069d3c1c81d71042706372df153254487a765dfc (patch) | |
| tree | d83a4a3954d2b8304f49d83e365f4c2b075b91eb /internal/admin/api_search.go | |
| parent | 6c5271fb085b4571a10f106391974c249cd84317 (diff) | |
| download | thehouseoficarus-069d3c1c81d71042706372df153254487a765dfc.tar.gz | |
feat: wip web admin for mapping and crud
Diffstat (limited to 'internal/admin/api_search.go')
| -rw-r--r-- | internal/admin/api_search.go | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/internal/admin/api_search.go b/internal/admin/api_search.go new file mode 100644 index 0000000..d47dc0e --- /dev/null +++ b/internal/admin/api_search.go @@ -0,0 +1,82 @@ +package admin + +import ( + "net/http" + "strconv" + "strings" +) + +func (s *AdminServer) handleSearch(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodGet { + http.Error(w, "method not allowed", http.StatusMethodNotAllowed) + return + } + + q := strings.ToLower(strings.TrimSpace(r.URL.Query().Get("q"))) + if q == "" { + writeJSON(w, map[string]any{"error": "missing query parameter 'q'"}) + return + } + + type searchResult struct { + ID string `json:"id"` + Name string `json:"name"` + } + + type searchResponse struct { + Rooms []searchResult `json:"rooms"` + Items []searchResult `json:"items"` + Mobs []searchResult `json:"mobs"` + Objects []searchResult `json:"objects"` + } + + var resp searchResponse + + roomIDs, _ := listYAMLFiles(s.dataDir, "rooms") + for _, idStr := range roomIDs { + roomID, err := strconv.Atoi(idStr) + if err != nil { + continue + } + room, err := s.world.LoadRoom(roomID) + if err != nil { + continue + } + if strings.Contains(strings.ToLower(room.Name), q) || strings.Contains(strings.ToLower(idStr), q) { + resp.Rooms = append(resp.Rooms, searchResult{ID: idStr, Name: room.Name}) + } + } + + for id := range s.itemStore.PathIndex() { + itemDef, err := s.itemStore.Load(id) + if err != nil { + continue + } + if strings.Contains(strings.ToLower(itemDef.Name), q) || strings.Contains(strings.ToLower(itemDef.ID), q) { + resp.Items = append(resp.Items, searchResult{ID: itemDef.ID, Name: itemDef.Name}) + } + } + + mobIDs, _ := listYAMLFiles(s.dataDir, "mobs") + for _, id := range mobIDs { + mobDef, err := s.mobStore.LoadDef(id) + if err != nil { + continue + } + if strings.Contains(strings.ToLower(mobDef.Name), q) || strings.Contains(strings.ToLower(mobDef.ID), q) { + resp.Mobs = append(resp.Mobs, searchResult{ID: mobDef.ID, Name: mobDef.Name}) + } + } + + for id := range s.objectStore.PathIndex() { + objDef, err := s.objectStore.Load(id) + if err != nil { + continue + } + if strings.Contains(strings.ToLower(objDef.Name), q) || strings.Contains(strings.ToLower(objDef.ID), q) { + resp.Objects = append(resp.Objects, searchResult{ID: objDef.ID, Name: objDef.Name}) + } + } + + writeJSON(w, resp) +} |
