added match meta stats

This commit is contained in:
2021-10-07 21:15:48 +02:00
parent ee161fd589
commit 62d8d16fc9
2 changed files with 77 additions and 13 deletions

45
main.go
View File

@@ -43,13 +43,18 @@ var (
)
type PlayerResponse struct {
SteamID64 uint64 `json:"steamid64,string"`
Name string `json:"name"`
Avatar string `json:"avatar"`
VAC bool `json:"vac"`
Tracked bool `json:"tracked"`
VanityURL string `json:"vanity_url,omitempty"`
Matches []*MatchResponse `json:"matches,omitempty"`
SteamID64 uint64 `json:"steamid64,string"`
Name string `json:"name"`
Avatar string `json:"avatar"`
VAC bool `json:"vac"`
Tracked bool `json:"tracked"`
VanityURL string `json:"vanity_url,omitempty"`
MatchStats struct {
Win int
Tie int
Loss int
} `json:"match_stats"`
Matches []*MatchResponse `json:"matches,omitempty"`
}
type MatchResponse struct {
@@ -163,9 +168,21 @@ func getPlayer(w http.ResponseWriter, r *http.Request) {
if err != nil {
log.Errorf("[GP] Unable to marshal JSON: %v", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
}
wins, ties, losses, err := utils.GetMatchStats(db.Client, db.Lock, tPlayer)
if err != nil {
log.Debugf("[GP] Error retrieving match-stats for player %s: %v", id, err)
w.WriteHeader(http.StatusInternalServerError)
return
}
response.MatchStats.Win = wins
response.MatchStats.Tie = ties
response.MatchStats.Loss = losses
for _, iMatch := range tMatches {
mResponse := &MatchResponse{
MatchId: iMatch.MatchID,
@@ -413,11 +430,13 @@ func main() {
// Define routes
router = mux.NewRouter().StrictSlash(true)
router.HandleFunc("/player/{id}", getPlayer).Methods("GET")
router.HandleFunc("/player/trackme", postPlayerTrackMe).Methods("POST")
router.HandleFunc("/match/parse/{sharecode}", getMatchParse).Methods("GET")
router.HandleFunc("/match/{id:[0-9]{19}}", getMatch).Methods("GET")
router.HandleFunc("/player/{id}", getPlayer).Methods(http.MethodGet, http.MethodOptions)
router.HandleFunc("/player/trackme", postPlayerTrackMe).Methods(http.MethodPost, http.MethodOptions)
router.HandleFunc("/match/parse/{sharecode}", getMatchParse).Methods(http.MethodGet, http.MethodOptions)
router.HandleFunc("/match/{id:[0-9]{19}}", getMatch).Methods(http.MethodGet, http.MethodOptions)
router.Use(mux.CORSMethodMiddleware(router))
loggedRouter := handlers.LoggingHandler(os.Stdout, router)
proxyRouter := handlers.ProxyHeaders(loggedRouter)
sockets := make([]net.Listener, 0)
@@ -429,7 +448,7 @@ func main() {
}
sockets = append(sockets, sL)
go func() {
_ = http.Serve(sL, loggedRouter)
_ = http.Serve(sL, proxyRouter)
}()
} else {
tL, err := net.Listen("tcp", fmt.Sprintf("%s:%d", l.Host, l.Port))
@@ -437,7 +456,7 @@ func main() {
log.Fatalf("Failure listing on %s:%d: %v", l.Host, l.Port, err)
}
go func() {
err = http.Serve(tL, loggedRouter)
err = http.Serve(tL, proxyRouter)
if err != nil {
log.Fatalf("Failure serving on %s:%d: %v", l.Host, l.Port, err)
}