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-15 12:51:56 +03:00
|
|
|
type GenericPos struct {
|
2019-02-03 15:08:57 +03:00
|
|
|
X float64 `json:"x"`
|
|
|
|
Y float64 `json:"y"`
|
|
|
|
Z float64 `json:"z"`
|
|
|
|
}
|
|
|
|
|
2019-02-15 12:51:56 +03:00
|
|
|
type Wagon struct {
|
|
|
|
Id int `json:"id"`
|
|
|
|
PosInTrain int `json:"pos_in_train"`
|
|
|
|
Type string `json:"type"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Train struct {
|
|
|
|
Pos GenericPos `json:"pos"`
|
|
|
|
Id int `json:"id"`
|
|
|
|
Wagons []*Wagon `json:"wagons"`
|
|
|
|
OffTrack bool `json:"off_track"`
|
|
|
|
Velocity int `json:"velocity"`
|
|
|
|
}
|
|
|
|
|
2019-02-03 15:08:57 +03:00
|
|
|
type Player struct {
|
2019-02-15 12:51:56 +03:00
|
|
|
Pos GenericPos `json:"pos"`
|
|
|
|
Velocity GenericPos `json:"velocity"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
HP int `json:"hp"`
|
|
|
|
Breath int `json:"breath"`
|
2019-02-03 15:08:57 +03:00
|
|
|
//TODO: stamina, skin, etc
|
|
|
|
}
|
|
|
|
|
|
|
|
type MinetestInfo struct {
|
2019-02-15 12:51:56 +03:00
|
|
|
MaxLag float64 `json:"max_lag"`
|
|
|
|
Players []*Player `json:"players"`
|
|
|
|
Trains []*Train `json:"trains"`
|
|
|
|
Time float64 `json:"time"`
|
|
|
|
Uptime float64 `json:"uptime"`
|
2019-02-03 15:08:57 +03:00
|
|
|
}
|
|
|
|
|
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-15 10:36:48 +03:00
|
|
|
mintestPlayers.Set(float64(len(data.Players)))
|
|
|
|
mintestMaxLag.Set(data.MaxLag)
|
|
|
|
|
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
|
|
|
}
|