522 lines
15 KiB
Go
522 lines
15 KiB
Go
// Package api provides the REST + WebSocket API for the bot dashboard.
|
|
//
|
|
// Endpoints:
|
|
// GET /api/status — bot status, current state, routine, stats
|
|
// GET /api/capture/frame — returns current frame as JPEG
|
|
// GET /api/capture/frame/annotated — frame with region overlays
|
|
// GET /api/capture/stats — capture performance stats
|
|
// POST /api/capture/upload — upload a screenshot for testing
|
|
// POST /api/capture/source — switch capture source
|
|
// GET /api/regions — list all defined regions
|
|
// GET /api/state — current detected game state + vitals
|
|
// GET /api/loot/rules — get current loot filter rules
|
|
// GET /api/routines — list available routines
|
|
// GET /api/pixel — get pixel color at coordinates
|
|
// GET /api/config — current config
|
|
// GET /api/plugins — list loaded plugins and info
|
|
// WS /api/ws — WebSocket for real-time updates
|
|
// GET / — serve dev dashboard
|
|
//
|
|
// The API is served by the bot process itself (single binary).
|
|
package api
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"image"
|
|
"image/color"
|
|
"image/draw"
|
|
"image/jpeg"
|
|
"io"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
"strconv"
|
|
"sync"
|
|
|
|
"git.cloonar.com/openclawd/iso-bot/pkg/engine"
|
|
"git.cloonar.com/openclawd/iso-bot/pkg/engine/vision"
|
|
)
|
|
|
|
// Status represents the current bot status.
|
|
type Status struct {
|
|
Running bool `json:"running"`
|
|
Paused bool `json:"paused"`
|
|
DevMode bool `json:"devMode"`
|
|
GameState string `json:"gameState"`
|
|
Routine string `json:"routine,omitempty"`
|
|
Phase string `json:"phase,omitempty"`
|
|
RunCount int `json:"runCount"`
|
|
ItemsFound int `json:"itemsFound"`
|
|
Uptime string `json:"uptime"`
|
|
CaptureFPS float64 `json:"captureFps"`
|
|
HealthPct float64 `json:"healthPct"`
|
|
ManaPct float64 `json:"manaPct"`
|
|
}
|
|
|
|
// PixelInfo represents RGB and HSV values at a pixel.
|
|
type PixelInfo struct {
|
|
X int `json:"x"`
|
|
Y int `json:"y"`
|
|
RGB [3]int `json:"rgb"`
|
|
HSV [3]int `json:"hsv"`
|
|
}
|
|
|
|
// RegionInfo represents a screen region.
|
|
type RegionInfo struct {
|
|
Name string `json:"name"`
|
|
X int `json:"x"`
|
|
Y int `json:"y"`
|
|
Width int `json:"width"`
|
|
Height int `json:"height"`
|
|
}
|
|
|
|
// Server provides the HTTP API and WebSocket endpoint.
|
|
type Server struct {
|
|
mu sync.RWMutex
|
|
engine *engine.Engine
|
|
addr string
|
|
mux *http.ServeMux
|
|
webRoot string
|
|
}
|
|
|
|
// NewServer creates an API server on the given address.
|
|
func NewServer(addr string, eng *engine.Engine, webRoot string) *Server {
|
|
s := &Server{
|
|
engine: eng,
|
|
addr: addr,
|
|
mux: http.NewServeMux(),
|
|
webRoot: webRoot,
|
|
}
|
|
s.registerRoutes()
|
|
return s
|
|
}
|
|
|
|
// Start begins serving the API.
|
|
func (s *Server) Start() error {
|
|
return http.ListenAndServe(s.addr, s.enableCORS(s.mux))
|
|
}
|
|
|
|
func (s *Server) registerRoutes() {
|
|
// API routes
|
|
s.mux.HandleFunc("GET /api/status", s.handleStatus)
|
|
s.mux.HandleFunc("GET /api/capture/frame", s.handleCaptureFrame)
|
|
s.mux.HandleFunc("GET /api/capture/frame/annotated", s.handleCaptureFrameAnnotated)
|
|
s.mux.HandleFunc("GET /api/capture/stats", s.handleCaptureStats)
|
|
s.mux.HandleFunc("POST /api/capture/upload", s.handleCaptureUpload)
|
|
s.mux.HandleFunc("POST /api/capture/source", s.handleCaptureSource)
|
|
s.mux.HandleFunc("GET /api/regions", s.handleRegions)
|
|
s.mux.HandleFunc("GET /api/state", s.handleState)
|
|
s.mux.HandleFunc("GET /api/loot/rules", s.handleLootRules)
|
|
s.mux.HandleFunc("GET /api/routines", s.handleRoutines)
|
|
s.mux.HandleFunc("GET /api/pixel", s.handlePixel)
|
|
s.mux.HandleFunc("GET /api/config", s.handleConfig)
|
|
s.mux.HandleFunc("GET /api/plugins", s.handlePlugins)
|
|
s.mux.HandleFunc("POST /api/start", s.handleStart)
|
|
s.mux.HandleFunc("POST /api/stop", s.handleStop)
|
|
s.mux.HandleFunc("POST /api/pause", s.handlePause)
|
|
|
|
// WebSocket endpoint
|
|
// s.mux.HandleFunc("/api/ws", s.handleWebSocket)
|
|
|
|
// Serve static files for dev dashboard
|
|
s.mux.Handle("/", http.FileServer(http.Dir(s.webRoot)))
|
|
}
|
|
|
|
func (s *Server) enableCORS(next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Access-Control-Allow-Origin", "*")
|
|
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
|
|
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization")
|
|
|
|
if r.Method == "OPTIONS" {
|
|
w.WriteHeader(http.StatusOK)
|
|
return
|
|
}
|
|
|
|
next.ServeHTTP(w, r)
|
|
})
|
|
}
|
|
|
|
func (s *Server) handleStatus(w http.ResponseWriter, r *http.Request) {
|
|
status := s.engine.Status()
|
|
|
|
response := Status{
|
|
Running: status.Running,
|
|
Paused: status.Paused,
|
|
DevMode: status.DevMode,
|
|
GameState: string(status.GameState),
|
|
Uptime: status.Uptime.String(),
|
|
CaptureFPS: status.CaptureStats.FPS,
|
|
HealthPct: status.Vitals.HealthPct,
|
|
ManaPct: status.Vitals.ManaPct,
|
|
ItemsFound: status.ItemsFound,
|
|
RunCount: status.RunCount,
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(response)
|
|
}
|
|
|
|
func (s *Server) handleCaptureFrame(w http.ResponseWriter, r *http.Request) {
|
|
frame := s.engine.CurrentFrame()
|
|
if frame == nil {
|
|
http.Error(w, "No frame available", http.StatusNoContent)
|
|
return
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "image/jpeg")
|
|
|
|
var buf bytes.Buffer
|
|
if err := jpeg.Encode(&buf, frame, &jpeg.Options{Quality: 85}); err != nil {
|
|
http.Error(w, "Failed to encode frame", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
w.Write(buf.Bytes())
|
|
}
|
|
|
|
func (s *Server) handleCaptureFrameAnnotated(w http.ResponseWriter, r *http.Request) {
|
|
frame := s.engine.CurrentFrame()
|
|
if frame == nil {
|
|
http.Error(w, "No frame available", http.StatusNoContent)
|
|
return
|
|
}
|
|
|
|
// Create a copy of the frame to draw on
|
|
bounds := frame.Bounds()
|
|
annotated := image.NewRGBA(bounds)
|
|
draw.Draw(annotated, bounds, frame, bounds.Min, draw.Src)
|
|
|
|
// Draw region overlays
|
|
regions := s.getRegions()
|
|
s.drawRegionOverlays(annotated, regions)
|
|
|
|
w.Header().Set("Content-Type", "image/jpeg")
|
|
|
|
var buf bytes.Buffer
|
|
if err := jpeg.Encode(&buf, annotated, &jpeg.Options{Quality: 85}); err != nil {
|
|
http.Error(w, "Failed to encode annotated frame", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
w.Write(buf.Bytes())
|
|
}
|
|
|
|
func (s *Server) handleCaptureStats(w http.ResponseWriter, r *http.Request) {
|
|
status := s.engine.Status()
|
|
stats := status.CaptureStats
|
|
|
|
response := map[string]interface{}{
|
|
"frameCount": stats.FrameCount,
|
|
"avgCaptureMs": stats.AvgCaptureMs,
|
|
"fps": stats.FPS,
|
|
"lastCapture": stats.LastCapture,
|
|
"backend": "file", // TODO: Get actual backend name
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(response)
|
|
}
|
|
|
|
func (s *Server) handleCaptureUpload(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != "POST" {
|
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
|
return
|
|
}
|
|
|
|
file, header, err := r.FormFile("file")
|
|
if err != nil {
|
|
http.Error(w, "Failed to read uploaded file", http.StatusBadRequest)
|
|
return
|
|
}
|
|
defer file.Close()
|
|
|
|
// Save to temporary directory
|
|
tempDir := "/tmp/iso-bot-uploads"
|
|
os.MkdirAll(tempDir, 0755)
|
|
|
|
filename := filepath.Join(tempDir, header.Filename)
|
|
out, err := os.Create(filename)
|
|
if err != nil {
|
|
http.Error(w, "Failed to save file", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
defer out.Close()
|
|
|
|
_, err = io.Copy(out, file)
|
|
if err != nil {
|
|
http.Error(w, "Failed to save file", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
response := map[string]string{
|
|
"filename": filename,
|
|
"message": "File uploaded successfully. Use /api/capture/source to switch to it.",
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(response)
|
|
}
|
|
|
|
func (s *Server) handleCaptureSource(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != "POST" {
|
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
|
return
|
|
}
|
|
|
|
var req struct {
|
|
Type string `json:"type"`
|
|
Config map[string]interface{} `json:"config"`
|
|
}
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
http.Error(w, "Invalid JSON", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
// TODO: Switch capture source
|
|
// This would require restarting the engine with a new source
|
|
response := map[string]string{
|
|
"message": "Capture source switching not implemented yet",
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(response)
|
|
}
|
|
|
|
func (s *Server) handleRegions(w http.ResponseWriter, r *http.Request) {
|
|
regions := s.getRegions()
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(regions)
|
|
}
|
|
|
|
func (s *Server) handleState(w http.ResponseWriter, r *http.Request) {
|
|
status := s.engine.Status()
|
|
|
|
response := map[string]interface{}{
|
|
"gameState": string(status.GameState),
|
|
"healthPct": status.Vitals.HealthPct,
|
|
"manaPct": status.Vitals.ManaPct,
|
|
"xpPct": status.Vitals.XPPct,
|
|
"frameCount": status.FrameCount,
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(response)
|
|
}
|
|
|
|
func (s *Server) handleLootRules(w http.ResponseWriter, r *http.Request) {
|
|
lootEngine := s.engine.LootEngine()
|
|
if lootEngine == nil {
|
|
response := map[string]interface{}{
|
|
"rules": []interface{}{},
|
|
"count": 0,
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(response)
|
|
return
|
|
}
|
|
|
|
response := map[string]interface{}{
|
|
"rules": lootEngine.Rules,
|
|
"count": len(lootEngine.Rules),
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(response)
|
|
}
|
|
|
|
func (s *Server) handleRoutines(w http.ResponseWriter, r *http.Request) {
|
|
routines := s.engine.GamePlugin().Routines()
|
|
|
|
response := make([]map[string]interface{}, 0)
|
|
for _, routine := range routines {
|
|
response = append(response, map[string]interface{}{
|
|
"name": routine.Name(),
|
|
"phase": routine.Phase(),
|
|
})
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(response)
|
|
}
|
|
|
|
func (s *Server) handlePixel(w http.ResponseWriter, r *http.Request) {
|
|
xStr := r.URL.Query().Get("x")
|
|
yStr := r.URL.Query().Get("y")
|
|
|
|
x, err := strconv.Atoi(xStr)
|
|
if err != nil {
|
|
http.Error(w, "Invalid x coordinate", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
y, err := strconv.Atoi(yStr)
|
|
if err != nil {
|
|
http.Error(w, "Invalid y coordinate", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
frame := s.engine.CurrentFrame()
|
|
if frame == nil {
|
|
http.Error(w, "No frame available", http.StatusNoContent)
|
|
return
|
|
}
|
|
|
|
bounds := frame.Bounds()
|
|
point := image.Point{X: x, Y: y}
|
|
if !point.In(bounds) {
|
|
http.Error(w, "Coordinates out of bounds", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
c := frame.At(x, y)
|
|
red, green, blue, _ := c.RGBA()
|
|
// Convert to 8-bit
|
|
red, green, blue = red>>8, green>>8, blue>>8
|
|
|
|
hsv := vision.RGBToHSV(c)
|
|
|
|
response := PixelInfo{
|
|
X: x,
|
|
Y: y,
|
|
RGB: [3]int{int(red), int(green), int(blue)},
|
|
HSV: [3]int{hsv.H, hsv.S, hsv.V},
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(response)
|
|
}
|
|
|
|
func (s *Server) handleConfig(w http.ResponseWriter, r *http.Request) {
|
|
// TODO: Return actual config
|
|
response := map[string]interface{}{
|
|
"game": s.engine.GamePlugin().Info().ID,
|
|
"devMode": s.engine.Status().DevMode,
|
|
"resolution": fmt.Sprintf("%dx%d", 1920, 1080), // TODO: Get actual resolution
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(response)
|
|
}
|
|
|
|
func (s *Server) handlePlugins(w http.ResponseWriter, r *http.Request) {
|
|
pluginInfo := s.engine.GamePlugin().Info()
|
|
|
|
response := []map[string]interface{}{
|
|
{
|
|
"id": pluginInfo.ID,
|
|
"name": pluginInfo.Name,
|
|
"version": pluginInfo.Version,
|
|
"description": pluginInfo.Description,
|
|
"resolution": fmt.Sprintf("%dx%d", pluginInfo.Resolution.X, pluginInfo.Resolution.Y),
|
|
},
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(response)
|
|
}
|
|
|
|
func (s *Server) handleStart(w http.ResponseWriter, r *http.Request) {
|
|
// TODO: Start the engine
|
|
response := map[string]string{
|
|
"message": "Start not implemented yet",
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(response)
|
|
}
|
|
|
|
func (s *Server) handleStop(w http.ResponseWriter, r *http.Request) {
|
|
s.engine.Stop()
|
|
response := map[string]string{
|
|
"message": "Engine stopped",
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(response)
|
|
}
|
|
|
|
func (s *Server) handlePause(w http.ResponseWriter, r *http.Request) {
|
|
var req struct {
|
|
Paused bool `json:"paused"`
|
|
}
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
http.Error(w, "Invalid JSON", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
s.engine.Pause(req.Paused)
|
|
|
|
action := "resumed"
|
|
if req.Paused {
|
|
action = "paused"
|
|
}
|
|
|
|
response := map[string]string{
|
|
"message": fmt.Sprintf("Engine %s", action),
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(response)
|
|
}
|
|
|
|
// Helper functions
|
|
|
|
func (s *Server) getRegions() []RegionInfo {
|
|
gameID := s.engine.GamePlugin().Info().ID
|
|
// TODO: Get actual current resolution
|
|
width, height := 1920, 1080
|
|
|
|
registry := s.engine.ResolutionRegistry()
|
|
profile, err := registry.Get(gameID, width, height)
|
|
if err != nil {
|
|
log.Printf("Failed to get resolution profile: %v", err)
|
|
return make([]RegionInfo, 0)
|
|
}
|
|
|
|
var regions []RegionInfo
|
|
for name, rect := range profile.Regions {
|
|
regions = append(regions, RegionInfo{
|
|
Name: name,
|
|
X: rect.Min.X,
|
|
Y: rect.Min.Y,
|
|
Width: rect.Dx(),
|
|
Height: rect.Dy(),
|
|
})
|
|
}
|
|
|
|
return regions
|
|
}
|
|
|
|
func (s *Server) drawRegionOverlays(img *image.RGBA, regions []RegionInfo) {
|
|
// Simple colored rectangles for region overlays
|
|
colors := []image.Uniform{
|
|
{C: color.RGBA{255, 0, 0, 128}}, // Red
|
|
{C: color.RGBA{0, 255, 0, 128}}, // Green
|
|
{C: color.RGBA{0, 0, 255, 128}}, // Blue
|
|
{C: color.RGBA{255, 255, 0, 128}}, // Yellow
|
|
{C: color.RGBA{255, 0, 255, 128}}, // Magenta
|
|
{C: color.RGBA{0, 255, 255, 128}}, // Cyan
|
|
}
|
|
|
|
for i, region := range regions {
|
|
color := &colors[i%len(colors)]
|
|
rect := image.Rect(region.X, region.Y, region.X+region.Width, region.Y+region.Height)
|
|
|
|
// Draw border (simple approach - just draw a few pixel wide border)
|
|
borderWidth := 2
|
|
|
|
// Top border
|
|
draw.Draw(img, image.Rect(rect.Min.X, rect.Min.Y, rect.Max.X, rect.Min.Y+borderWidth), color, image.Point{}, draw.Over)
|
|
// Bottom border
|
|
draw.Draw(img, image.Rect(rect.Min.X, rect.Max.Y-borderWidth, rect.Max.X, rect.Max.Y), color, image.Point{}, draw.Over)
|
|
// Left border
|
|
draw.Draw(img, image.Rect(rect.Min.X, rect.Min.Y, rect.Min.X+borderWidth, rect.Max.Y), color, image.Point{}, draw.Over)
|
|
// Right border
|
|
draw.Draw(img, image.Rect(rect.Max.X-borderWidth, rect.Min.Y, rect.Max.X, rect.Max.Y), color, image.Point{}, draw.Over)
|
|
}
|
|
}
|