Commit 070015dc authored by Kegan's avatar Kegan
Browse files

Completed start api

parent cff7888a
package client
import (
"encoding/json"
"io"
"net/http"
"strings"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/sirupsen/logrus"
"gitlab.desolationredux.com/DayZ/DayZBR-Mod/Service/database/database"
"gitlab.desolationredux.com/DayZ/DayZBR-Mod/Service/database/dbhelpers"
"gitlab.desolationredux.com/DayZ/DayZBR-Mod/Service/discord-bot/discord"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
)
type Config struct {
......@@ -83,5 +89,101 @@ func NewClient(log *logrus.Entry, cfg *Config) *Client {
}
func (c *Client) Ping(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "pong")
}
func (c *Client) Start(w http.ResponseWriter, r *http.Request) {
ip := r.Header.Get("X-Forwarded-For")
req, err := ParseStartRequest(r.Body)
if err != nil {
c.Log.Errorln("Failed to parse OnStart request", err)
json.NewEncoder(w).Encode(buildErrorResponse("invalid request"))
return
}
if len(req.SteamId) != 17 {
if len(req.SteamId) > 24 {
c.Log.Errorln("invalid steamid. too long")
} else {
c.Log.Errorln("invalid steamid", req.SteamId)
}
json.NewEncoder(w).Encode(buildErrorResponse("invalid request"))
return
}
//FindPlayer by steam id
plr, err := c.Db.GetPlayerBySteamID(req.SteamId)
if err != nil {
if err != mongo.ErrNoDocuments {
c.Log.Errorln("Failed to find player in db", err)
json.NewEncoder(w).Encode(buildErrorResponse("invalid request"))
return
}
//player not found? insert
plr, err = c.Db.InsertPlayer(&database.BRPlayer{
Name: req.Name,
SteamId: req.SteamId,
MatchIds: make([]primitive.ObjectID, 0),
Rating: 1000,
Ips: []string{ip},
Purchases: make([]string, 0),
})
if err != nil {
c.Log.Errorln("Failed to insert player into db", err)
json.NewEncoder(w).Encode(buildErrorResponse("invalid request"))
return
}
} else {
//update player ip if necessary
found_ip := false
for _, dbip := range plr.Ips {
if dbip == ip {
found_ip = true
}
}
if !found_ip || strings.Trim(strings.ToLower(plr.Name), " \t\n\rr") != strings.Trim(strings.ToLower(req.Name), " \t\n\r") {
plr.Ips = append(plr.Ips, ip)
plr.Name = req.Name
plr, err = c.Db.UpdatePlayer(plr)
if err != nil {
c.Log.Errorln("Failed to update player ip", err)
json.NewEncoder(w).Encode(buildErrorResponse("invalid request"))
return
}
}
}
regions, err := dbhelpers.GetAllRegions(c.Db)
if err != nil {
c.Log.Errorln("Failed to get all regions", err)
json.NewEncoder(w).Encode(buildErrorResponse("invalid request"))
return
}
json.NewEncoder(w).Encode(buildSuccessResponse(&StartResponse{
Player: *plr,
Region: struct {
Regions []string "json:\"regions\""
}{
Regions: regions,
},
}))
}
func (c *Client) Matchmake(w http.ResponseWriter, r *http.Request) {
req, err := ParseMatchMakeRequest(r.Body)
if err != nil {
c.Log.Errorln("Failed to parse OnStart request", err)
json.NewEncoder(w).Encode(buildErrorResponse("invalid request"))
return
}
}
func (c *Client) GetPlayer(w http.ResponseWriter, r *http.Request) {
}
func (c *Client) GetMatch(w http.ResponseWriter, r *http.Request) {
}
func (c *Client) GetServer(w http.ResponseWriter, r *http.Request) {
}
package client
import (
"encoding/json"
"io"
)
type StartRequest struct {
SteamId string `json:"steamid"`
Name string `json:"name"`
}
type MatchMakeRequest struct {
PlayerId string `json:"id"`
Region string `json:"region"`
Version string `json:"version"`
}
func ParseStartRequest(r io.ReadCloser) (*StartRequest, error) {
var request StartRequest
err := json.NewDecoder(r).Decode(&request)
if err != nil {
return nil, err
}
return &request, nil
}
func ParseMatchMakeRequest(r io.ReadCloser) (*MatchMakeRequest, error) {
var request MatchMakeRequest
err := json.NewDecoder(r).Decode(&request)
if err != nil {
return nil, err
}
return &request, nil
}
package client
import "gitlab.desolationredux.com/DayZ/DayZBR-Mod/Service/database/database"
type GenericResponse struct {
Success int `json:"success"`
Error string `json:"error"`
Data interface{} `json:"data"`
}
type MatchmakeResponse struct {
Wait uint8 `json:"wait"`
Server database.BRServer `json:"server"`
}
type StartResponse struct {
Player database.BRPlayer `json:"player"`
Region struct {
Regions []string `json:"regions"`
} `json:"region"`
}
func buildSuccessResponse(data interface{}) *GenericResponse {
return &GenericResponse{
Success: 1,
Error: "",
Data: data,
}
}
func buildErrorResponse(message string) *GenericResponse {
return &GenericResponse{
Success: 0,
Error: message,
Data: nil,
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment