mapserver/server/web/minetest.go

56 lines
1.1 KiB
Go
Raw Normal View History

2019-01-29 09:53:59 +03:00
package web
import (
2019-01-29 20:00:00 +03:00
"encoding/json"
2019-01-29 09:53:59 +03:00
"mapserver/app"
"net/http"
)
2019-02-03 15:08:57 +03:00
type PlayerPos struct {
X float64 `json:"x"`
Y float64 `json:"y"`
Z float64 `json:"z"`
}
type Player struct {
Pos PlayerPos `json:"pos"`
Name string `json:"name"`
HP int `json:"hp"`
Breath int `json:"breath"`
//TODO: stamina, skin, etc
}
type MinetestInfo struct {
MaxLag float64 `json:"max_lag"`
Players []Player `json:"players"`
Time float64 `json:"time"`
Uptime float64 `json:"uptime"`
}
2019-01-29 09:53:59 +03:00
type Minetest struct {
ctx *app.App
}
2019-02-03 20:04:01 +03:00
func (this *Minetest) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
if req.Header.Get("Authorization") != this.ctx.Config.WebApi.SecretKey {
resp.WriteHeader(403)
resp.Write([]byte("invalid key!"))
return
}
2019-01-29 20:00:00 +03:00
resp.Header().Add("content-type", "application/json")
2019-02-03 15:08:57 +03:00
data := &MinetestInfo{}
err := json.NewDecoder(req.Body).Decode(data)
2019-01-29 09:53:59 +03:00
2019-02-03 15:08:57 +03:00
if err != nil {
resp.WriteHeader(500)
resp.Write([]byte(err.Error()))
return
}
2019-02-03 20:04:01 +03:00
this.ctx.WebEventbus.Emit("minetest-info", data)
2019-02-03 15:08:57 +03:00
json.NewEncoder(resp).Encode("stub")
2019-01-29 09:53:59 +03:00
}