mapobjects in the web

This commit is contained in:
NatureFreshMilk 2019-01-28 10:42:07 +01:00
parent 64b1e56e21
commit 60d715d25f
3 changed files with 53 additions and 0 deletions

51
server/web/mapobjects.go Normal file
View File

@ -0,0 +1,51 @@
package web
import (
"mapserver/app"
"mapserver/coords"
"mapserver/mapobjectdb"
"encoding/json"
"net/http"
"strconv"
"strings"
)
type MapObjects struct {
ctx *app.App
}
func (t *MapObjects) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
str := strings.TrimPrefix(req.URL.Path, "/api/mapobjects/")
// x1/y1/z1/x2/y2/z2/type
parts := strings.Split(str, "/")
if len(parts) != 7 {
resp.WriteHeader(500)
resp.Write([]byte("wrong number of arguments"))
return
}
x1, _ := strconv.Atoi(parts[0])
y1, _ := strconv.Atoi(parts[1])
z1, _ := strconv.Atoi(parts[2])
x2, _ := strconv.Atoi(parts[3])
y2, _ := strconv.Atoi(parts[4])
z2, _ := strconv.Atoi(parts[5])
typeStr := parts[6]
q := mapobjectdb.SearchQuery{
Pos1: coords.NewMapBlockCoords(x1, y1, z1),
Pos2: coords.NewMapBlockCoords(x2, y2, z2),
Type: typeStr,
}
objects, err := t.ctx.Objectdb.GetMapData(q)
if err != nil {
resp.WriteHeader(500)
resp.Write([]byte(err.Error()))
} else {
resp.Header().Add("content-type", "application/json")
json.NewEncoder(resp).Encode(objects)
}
}

View File

@ -20,6 +20,7 @@ func Serve(ctx *app.App) {
mux.Handle("/", http.FileServer(vfs.FS(ctx.Config.Webdev))) mux.Handle("/", http.FileServer(vfs.FS(ctx.Config.Webdev)))
mux.Handle("/api/tile/", &Tiles{ctx: ctx}) mux.Handle("/api/tile/", &Tiles{ctx: ctx})
mux.Handle("/api/config", &ConfigHandler{ctx: ctx}) mux.Handle("/api/config", &ConfigHandler{ctx: ctx})
mux.Handle("/api/mapobjects", &MapObjects{ctx: ctx})
ws := NewWS(ctx) ws := NewWS(ctx)
mux.Handle("/api/ws", ws) mux.Handle("/api/ws", ws)

View File

@ -16,6 +16,7 @@ type Tiles struct {
func (t *Tiles) ServeHTTP(resp http.ResponseWriter, req *http.Request) { func (t *Tiles) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
str := strings.TrimPrefix(req.URL.Path, "/api/tile/") str := strings.TrimPrefix(req.URL.Path, "/api/tile/")
// {layerId}/x/y/zoom
parts := strings.Split(str, "/") parts := strings.Split(str, "/")
if len(parts) != 4 { if len(parts) != 4 {
resp.WriteHeader(500) resp.WriteHeader(500)