From b74e9da1c18d9b190bd93e4856aa7ad6b78a8653 Mon Sep 17 00:00:00 2001 From: NatureFreshMilk Date: Fri, 18 Jan 2019 11:04:37 +0100 Subject: [PATCH] mapblock debugging --- app/config.go | 16 +++++++++--- mapblockparser/mapblock.go | 42 ++++++++++++++++---------------- mapserver.json | 5 +++- web/mapblock.go | 41 +++++++++++++++++++++++++++++++ web/serve.go | 6 ++++- web/tiles.go | 50 +++++++++++++++++++++----------------- 6 files changed, 112 insertions(+), 48 deletions(-) create mode 100644 web/mapblock.go diff --git a/app/config.go b/app/config.go index dc86147..0fc05b2 100644 --- a/app/config.go +++ b/app/config.go @@ -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) diff --git a/mapblockparser/mapblock.go b/mapblockparser/mapblock.go index 305cbc6..353dca9 100644 --- a/mapblockparser/mapblock.go +++ b/mapblockparser/mapblock.go @@ -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 -} diff --git a/mapserver.json b/mapserver.json index 5031bac..5530a27 100644 --- a/mapserver.json +++ b/mapserver.json @@ -1,5 +1,8 @@ { "port": 8080, "enableinitialrendering": false, - "webdev": true + "webdev": true, + "webapi": { + "enablemapblock": true + } } diff --git a/web/mapblock.go b/web/mapblock.go new file mode 100644 index 0000000..7ccac5c --- /dev/null +++ b/web/mapblock.go @@ -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) + + } +} diff --git a/web/serve.go b/web/serve.go index fbcdd97..eefdca5 100644 --- a/web/serve.go +++ b/web/serve.go @@ -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 { diff --git a/web/tiles.go b/web/tiles.go index 4ae38ef..d5f3df8 100644 --- a/web/tiles.go +++ b/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) + } }