added match meta stats
This commit is contained in:
31
main.go
31
main.go
@@ -49,6 +49,11 @@ type PlayerResponse struct {
|
||||
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"`
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
@@ -6,6 +6,7 @@ import (
|
||||
"csgowtfd/ent"
|
||||
"csgowtfd/ent/match"
|
||||
"csgowtfd/ent/player"
|
||||
"csgowtfd/ent/stats"
|
||||
"encoding/json"
|
||||
"encoding/xml"
|
||||
"fmt"
|
||||
@@ -98,6 +99,50 @@ func SendJSON(data interface{}, w http.ResponseWriter) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetMatchStats(db *ent.Client, lock *sync.RWMutex, player *ent.Player) (int, int, int, error) {
|
||||
var v struct {
|
||||
Wins int `json:"wins"`
|
||||
Ties int `json:"ties"`
|
||||
Total int `json:"total"`
|
||||
}
|
||||
err := player.QueryMatches().
|
||||
WithStats().
|
||||
Where(
|
||||
match.Or(
|
||||
match.And(
|
||||
match.HasStatsWith(stats.TeamIDEQ(1)),
|
||||
match.MatchResultEQ(1)),
|
||||
match.And(
|
||||
match.HasStatsWith(stats.TeamIDEQ(2)),
|
||||
match.MatchResultEQ(2))),
|
||||
).
|
||||
GroupBy(match.FieldMatchID).
|
||||
Aggregate(ent.As(ent.Count(), "wins")).
|
||||
Scan(context.Background(), &v)
|
||||
if err != nil {
|
||||
return 0, 0, 0, err
|
||||
}
|
||||
|
||||
err = player.QueryMatches().
|
||||
Where(match.MatchResultEQ(0)).
|
||||
GroupBy(match.FieldMatchID).
|
||||
Aggregate(ent.As(ent.Count(), "ties")).
|
||||
Scan(context.Background(), &v)
|
||||
if err != nil {
|
||||
return 0, 0, 0, err
|
||||
}
|
||||
|
||||
err = player.QueryMatches().
|
||||
GroupBy(match.FieldMatchID).
|
||||
Aggregate(ent.As(ent.Count(), "total")).
|
||||
Scan(context.Background(), &v)
|
||||
if err != nil {
|
||||
return 0, 0, 0, err
|
||||
}
|
||||
|
||||
return v.Wins, v.Ties, v.Total - v.Wins - v.Ties, nil
|
||||
}
|
||||
|
||||
func IsAuthCodeValid(player *ent.Player, lock *sync.RWMutex, apiKey string, shareCode string, authCode string, rl ratelimit.Limiter) (bool, error) {
|
||||
var tMatch *ent.Match
|
||||
var err error
|
||||
|
Reference in New Issue
Block a user