1
0
forked from MTSR/mapserver
mapserver/web/minetest.go

96 lines
2.3 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
"net/http"
2019-02-15 20:37:16 +03:00
"github.com/sirupsen/logrus"
2019-01-29 09:53:59 +03:00
)
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 {
2019-02-15 20:37:16 +03:00
Id string `json:"id"`
PosInTrain float64 `json:"pos_in_train"`
Type string `json:"type"`
2019-02-15 12:51:56 +03:00
}
2019-07-25 16:02:48 +03:00
type Signal struct {
Pos GenericPos `json:"pos"`
Green bool `json:"green"`
}
2019-02-15 12:51:56 +03:00
type Train struct {
2019-02-24 22:06:20 +03:00
Pos GenericPos `json:"pos"`
Id string `json:"id"`
Wagons []*Wagon `json:"wagons"`
OffTrack bool `json:"off_track"`
Velocity float64 `json:"velocity"`
Line string `json:"line"`
TextOutside string `json:"text_outside"`
TextInside string `json:"text_inside"`
2019-02-15 12:51:56 +03:00
}
2019-04-24 17:21:04 +03:00
type Minecart struct {
Pos GenericPos `json:"pos"`
Speed GenericPos `json:"speed"`
2019-05-04 22:16:57 +03:00
Id float64 `json:"id"`
2019-04-24 17:21:04 +03:00
}
2019-02-03 15:08:57 +03:00
type Player struct {
2019-06-14 08:08:38 +03:00
Pos GenericPos `json:"pos"`
Velocity GenericPos `json:"velocity"`
Name string `json:"name"`
HP float64 `json:"hp"`
Breath float64 `json:"breath"`
Moderator bool `json:"moderator"`
RTT float64 `json:"rtt"`
ProtocolVersion float64 `json:"protocol_version"`
2019-02-03 15:08:57 +03:00
//TODO: stamina, skin, etc
}
type MinetestInfo struct {
2019-04-24 17:21:04 +03:00
MaxLag float64 `json:"max_lag"`
Players []*Player `json:"players"`
Trains []*Train `json:"trains"`
2019-07-25 16:02:48 +03:00
Signals []*Signal `json:"signals"`
2019-04-24 17:21:04 +03:00
Minecarts []*Minecart `json:"minecarts"`
Time float64 `json:"time"`
Uptime float64 `json:"uptime"`
2019-02-03 15:08:57 +03:00
}
var LastStats *MinetestInfo
2021-04-12 14:55:00 +03:00
func (api *Api) PostMinetestData(resp http.ResponseWriter, req *http.Request) {
if req.Header.Get("Authorization") != api.Context.Config.WebApi.SecretKey {
2019-02-03 20:04:01 +03:00
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()))
2019-02-15 20:37:16 +03:00
log.WithFields(logrus.Fields{"error": err}).Error("Json unmarshal")
2019-02-03 15:08:57 +03:00
return
}
2019-02-15 10:36:48 +03:00
mintestPlayers.Set(float64(len(data.Players)))
mintestMaxLag.Set(data.MaxLag)
LastStats = data
2021-04-12 14:55:00 +03:00
api.Context.WebEventbus.Emit("minetest-info", data)
2019-02-03 20:04:01 +03:00
2019-02-03 15:08:57 +03:00
json.NewEncoder(resp).Encode("stub")
2019-01-29 09:53:59 +03:00
}