forked from MTSR/mapserver
mapblock debugging
This commit is contained in:
parent
51bc900b00
commit
b74e9da1c1
@ -7,16 +7,26 @@ import (
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Port int `json:"port"`
|
||||
EnableInitialRendering bool `json:"enableinitialrendering"`
|
||||
Webdev bool `json:"webdev"`
|
||||
Port int `json:"port"`
|
||||
EnableInitialRendering bool `json:"enableinitialrendering"`
|
||||
Webdev bool `json:"webdev"`
|
||||
WebApi WebApiConfig `json:"webapi"`
|
||||
}
|
||||
|
||||
type WebApiConfig struct {
|
||||
EnableMapblock bool `json:"enablemapblock"`
|
||||
}
|
||||
|
||||
func ParseConfig(filename string) (*Config, error) {
|
||||
webapi := WebApiConfig{
|
||||
EnableMapblock: false,
|
||||
}
|
||||
|
||||
cfg := Config{
|
||||
Port: 8080,
|
||||
EnableInitialRendering: true,
|
||||
Webdev: false,
|
||||
WebApi: webapi,
|
||||
}
|
||||
|
||||
info, err := os.Stat(filename)
|
||||
|
@ -1,11 +1,27 @@
|
||||
package mapblockparser
|
||||
|
||||
type MapBlock struct {
|
||||
Version byte
|
||||
Underground bool
|
||||
Mapdata []byte
|
||||
Metadata Metadata
|
||||
BlockMapping map[int]string
|
||||
Version byte `json:"version"`
|
||||
Underground bool `json:"underground"`
|
||||
Mapdata []byte `json:"mapdata"`
|
||||
Metadata Metadata `json:"metadata"`
|
||||
BlockMapping map[int]string `json:"blockmapping"`
|
||||
}
|
||||
|
||||
type Metadata struct {
|
||||
Inventories map[int]map[string]*Inventory `json:"inventories"`
|
||||
Pairs map[int]map[string]string `json:"pairs"`
|
||||
}
|
||||
|
||||
type Item struct {
|
||||
Name string `json:"name"`
|
||||
Count int `json:"count"`
|
||||
Wear int `json:"wear"`
|
||||
}
|
||||
|
||||
type Inventory struct {
|
||||
Size int `json:"size"`
|
||||
Items []Item `json:"items"`
|
||||
}
|
||||
|
||||
func getNodePos(x, y, z int) int {
|
||||
@ -25,11 +41,6 @@ func NewMapblock() MapBlock {
|
||||
return mb
|
||||
}
|
||||
|
||||
type Metadata struct {
|
||||
Inventories map[int]map[string]*Inventory
|
||||
Pairs map[int]map[string]string
|
||||
}
|
||||
|
||||
func NewMetadata() Metadata {
|
||||
md := Metadata{}
|
||||
md.Inventories = make(map[int]map[string]*Inventory)
|
||||
@ -67,14 +78,3 @@ func (md *Metadata) GetInventory(pos int, name string) *Inventory {
|
||||
|
||||
return inv
|
||||
}
|
||||
|
||||
type Item struct {
|
||||
Name string
|
||||
Count int
|
||||
Wear int
|
||||
}
|
||||
|
||||
type Inventory struct {
|
||||
Size int
|
||||
Items []Item
|
||||
}
|
||||
|
@ -1,5 +1,8 @@
|
||||
{
|
||||
"port": 8080,
|
||||
"enableinitialrendering": false,
|
||||
"webdev": true
|
||||
"webdev": true,
|
||||
"webapi": {
|
||||
"enablemapblock": true
|
||||
}
|
||||
}
|
||||
|
41
web/mapblock.go
Normal file
41
web/mapblock.go
Normal file
@ -0,0 +1,41 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"mapserver/app"
|
||||
"mapserver/coords"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type MapblockHandler struct {
|
||||
ctx *app.App
|
||||
}
|
||||
|
||||
func (h *MapblockHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
|
||||
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])
|
||||
|
||||
c := coords.NewMapBlockCoords(x, y, z)
|
||||
mb, err := h.ctx.BlockAccessor.GetMapBlock(c)
|
||||
|
||||
if err != nil {
|
||||
resp.WriteHeader(500)
|
||||
resp.Write([]byte(err.Error()))
|
||||
|
||||
} else {
|
||||
resp.Header().Add("content-type", "application/json")
|
||||
json.NewEncoder(resp).Encode(mb)
|
||||
|
||||
}
|
||||
}
|
@ -18,7 +18,11 @@ func Serve(ctx *app.App) {
|
||||
mux := http.NewServeMux()
|
||||
|
||||
mux.Handle("/", http.FileServer(vfs.FS(ctx.Config.Webdev)))
|
||||
mux.Handle("/tile/", &Tiles{ctx: ctx})
|
||||
mux.Handle("/api/tile/", &Tiles{ctx: ctx})
|
||||
|
||||
if ctx.Config.WebApi.EnableMapblock {
|
||||
mux.Handle("/api/mapblock/", &MapblockHandler{ctx: ctx})
|
||||
}
|
||||
|
||||
err := http.ListenAndServe(":"+strconv.Itoa(ctx.Config.Port), mux)
|
||||
if err != nil {
|
||||
|
50
web/tiles.go
50
web/tiles.go
@ -1,34 +1,40 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"mapserver/app"
|
||||
"mapserver/coords"
|
||||
"net/http"
|
||||
"strings"
|
||||
"strconv"
|
||||
"mapserver/app"
|
||||
"mapserver/coords"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Tiles struct {
|
||||
ctx *app.App
|
||||
ctx *app.App
|
||||
}
|
||||
|
||||
func (t *Tiles) ServeHTTP(resp http.ResponseWriter, req *http.Request){
|
||||
str := strings.TrimPrefix(req.URL.Path, "/tile/")
|
||||
parts := strings.Split(str, "/")
|
||||
layerid, _ := strconv.Atoi(parts[0])
|
||||
x, _ := strconv.Atoi(parts[1])
|
||||
y, _ := strconv.Atoi(parts[2])
|
||||
zoom, _ := strconv.Atoi(parts[3])
|
||||
func (t *Tiles) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
|
||||
str := strings.TrimPrefix(req.URL.Path, "/api/tile/")
|
||||
parts := strings.Split(str, "/")
|
||||
if len(parts) != 4 {
|
||||
resp.WriteHeader(500)
|
||||
resp.Write([]byte("wrong number of arguments"))
|
||||
return
|
||||
}
|
||||
|
||||
c := coords.NewTileCoords(x,y,zoom,layerid)
|
||||
data, err := t.ctx.Tilerenderer.Render(c)
|
||||
layerid, _ := strconv.Atoi(parts[0])
|
||||
x, _ := strconv.Atoi(parts[1])
|
||||
y, _ := strconv.Atoi(parts[2])
|
||||
zoom, _ := strconv.Atoi(parts[3])
|
||||
|
||||
if err != nil {
|
||||
resp.WriteHeader(500)
|
||||
resp.Write([]byte(err.Error()))
|
||||
c := coords.NewTileCoords(x, y, zoom, layerid)
|
||||
data, err := t.ctx.Tilerenderer.Render(c)
|
||||
|
||||
} else {
|
||||
resp.Header().Add("content-type", "image/png")
|
||||
resp.Write(data)
|
||||
}
|
||||
if err != nil {
|
||||
resp.WriteHeader(500)
|
||||
resp.Write([]byte(err.Error()))
|
||||
|
||||
} else {
|
||||
resp.Header().Add("content-type", "image/png")
|
||||
resp.Write(data)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user