mapserver/web/mapblock.go

37 lines
752 B
Go
Raw Permalink Normal View History

2019-01-18 13:04:37 +03:00
package web
import (
"encoding/json"
2023-12-29 18:00:11 +03:00
"mapserver/types"
2019-01-18 13:04:37 +03:00
"net/http"
"strconv"
"strings"
)
2021-04-12 14:55:00 +03:00
func (api *Api) GetMapBlockData(resp http.ResponseWriter, req *http.Request) {
2019-01-18 13:04:37 +03:00
str := strings.TrimPrefix(req.URL.Path, "/api/mapblock/")
parts := strings.Split(str, "/")
if len(parts) != 3 {
resp.WriteHeader(500)
resp.Write([]byte("wrong number of arguments"))
return
}
x, _ := strconv.Atoi(parts[0])
y, _ := strconv.Atoi(parts[1])
z, _ := strconv.Atoi(parts[2])
2023-12-29 18:00:11 +03:00
c := types.NewMapBlockCoords(x, y, z)
2021-04-12 14:55:00 +03:00
mb, err := api.Context.MapBlockAccessor.GetMapBlock(c)
2019-01-18 13:04:37 +03:00
if err != nil {
resp.WriteHeader(500)
resp.Write([]byte(err.Error()))
} else {
resp.Header().Add("content-type", "application/json")
json.NewEncoder(resp).Encode(mb)
}
}