64 lines
1.3 KiB
Go
64 lines
1.3 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
)
|
|
|
|
type Data struct {
|
|
Type string `json:"type"`
|
|
DisplayName string `json:"displayName"`
|
|
DeathType string `json:"deathType"`
|
|
KillerName string `json:"killerName"`
|
|
EntityName string `json:"entityName"`
|
|
NewLevel int `json:"newLevel"`
|
|
}
|
|
|
|
type httpHandler struct {
|
|
ctx context.Context
|
|
}
|
|
|
|
func newHttpHandler(ctx context.Context) *httpHandler {
|
|
return &httpHandler{
|
|
ctx: ctx,
|
|
}
|
|
}
|
|
|
|
func (h *httpHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
data := Data{}
|
|
|
|
err := json.NewDecoder(r.Body).Decode(&data)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
getLogger(h.ctx).Debugf("[http server] Data: %v", data)
|
|
|
|
switch data.Type {
|
|
case "join":
|
|
getCommandHandler(h.ctx).handle(&joinCommand{
|
|
name: data.DisplayName,
|
|
})
|
|
case "quit":
|
|
getCommandHandler(h.ctx).handle(&quitCommand{
|
|
name: data.DisplayName,
|
|
})
|
|
case "death":
|
|
getCommandHandler(h.ctx).handle(&deathCommand{
|
|
name: data.DisplayName,
|
|
deathType: data.DeathType,
|
|
})
|
|
case "playerKilledEntity":
|
|
getCommandHandler(h.ctx).handle(&killCommand{
|
|
name: data.DisplayName,
|
|
entity: data.EntityName,
|
|
})
|
|
case "playerLevelChange":
|
|
getCommandHandler(h.ctx).handle(&changeLevelCommand{
|
|
name: data.DisplayName,
|
|
newLevel: data.NewLevel,
|
|
})
|
|
}
|
|
}
|