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"`
|
2022-12-13 21:40:26 +03:00
|
|
|
Yaw float64 `json:"yaw"`
|
2022-12-16 10:07:47 +03:00
|
|
|
Skin string `json:"skin"`
|
|
|
|
//TODO: stamina, armor, etc
|
2019-02-03 15:08:57 +03:00
|
|
|
}
|
|
|
|
|
2022-08-13 06:52:15 +03:00
|
|
|
type AirUtilsPlane struct {
|
|
|
|
Id string `json:"id"`
|
|
|
|
Entity string `json:"entity"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Pos GenericPos `json:"pos"`
|
|
|
|
Owner string `json:"owner"`
|
|
|
|
Driver string `json:"driver"`
|
|
|
|
Passenger string `json:"passenger"`
|
|
|
|
Color string `json:"color"`
|
|
|
|
Yaw float64 `json:"yaw"`
|
|
|
|
}
|
|
|
|
|
2019-02-03 15:08:57 +03:00
|
|
|
type MinetestInfo struct {
|
2022-08-13 06:52:15 +03:00
|
|
|
MaxLag float64 `json:"max_lag"`
|
|
|
|
Players []*Player `json:"players"`
|
|
|
|
Trains []*Train `json:"trains"`
|
|
|
|
Signals []*Signal `json:"signals"`
|
|
|
|
Minecarts []*Minecart `json:"minecarts"`
|
|
|
|
AirUtilsPlanes []*AirUtilsPlane `json:"airutils_planes"`
|
|
|
|
Time float64 `json:"time"`
|
|
|
|
Uptime float64 `json:"uptime"`
|
2019-02-03 15:08:57 +03:00
|
|
|
}
|
|
|
|
|
2020-03-23 17:07:11 +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)
|
|
|
|
|
2020-03-23 17:07:11 +03:00
|
|
|
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
|
|
|
}
|