added match meta stats
This commit is contained in:
45
main.go
45
main.go
@@ -43,13 +43,18 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type PlayerResponse struct {
|
type PlayerResponse struct {
|
||||||
SteamID64 uint64 `json:"steamid64,string"`
|
SteamID64 uint64 `json:"steamid64,string"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Avatar string `json:"avatar"`
|
Avatar string `json:"avatar"`
|
||||||
VAC bool `json:"vac"`
|
VAC bool `json:"vac"`
|
||||||
Tracked bool `json:"tracked"`
|
Tracked bool `json:"tracked"`
|
||||||
VanityURL string `json:"vanity_url,omitempty"`
|
VanityURL string `json:"vanity_url,omitempty"`
|
||||||
Matches []*MatchResponse `json:"matches,omitempty"`
|
MatchStats struct {
|
||||||
|
Win int
|
||||||
|
Tie int
|
||||||
|
Loss int
|
||||||
|
} `json:"match_stats"`
|
||||||
|
Matches []*MatchResponse `json:"matches,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type MatchResponse struct {
|
type MatchResponse struct {
|
||||||
@@ -163,9 +168,21 @@ func getPlayer(w http.ResponseWriter, r *http.Request) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("[GP] Unable to marshal JSON: %v", err)
|
log.Errorf("[GP] Unable to marshal JSON: %v", err)
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
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 {
|
for _, iMatch := range tMatches {
|
||||||
mResponse := &MatchResponse{
|
mResponse := &MatchResponse{
|
||||||
MatchId: iMatch.MatchID,
|
MatchId: iMatch.MatchID,
|
||||||
@@ -413,11 +430,13 @@ func main() {
|
|||||||
|
|
||||||
// Define routes
|
// Define routes
|
||||||
router = mux.NewRouter().StrictSlash(true)
|
router = mux.NewRouter().StrictSlash(true)
|
||||||
router.HandleFunc("/player/{id}", getPlayer).Methods("GET")
|
router.HandleFunc("/player/{id}", getPlayer).Methods(http.MethodGet, http.MethodOptions)
|
||||||
router.HandleFunc("/player/trackme", postPlayerTrackMe).Methods("POST")
|
router.HandleFunc("/player/trackme", postPlayerTrackMe).Methods(http.MethodPost, http.MethodOptions)
|
||||||
router.HandleFunc("/match/parse/{sharecode}", getMatchParse).Methods("GET")
|
router.HandleFunc("/match/parse/{sharecode}", getMatchParse).Methods(http.MethodGet, http.MethodOptions)
|
||||||
router.HandleFunc("/match/{id:[0-9]{19}}", getMatch).Methods("GET")
|
router.HandleFunc("/match/{id:[0-9]{19}}", getMatch).Methods(http.MethodGet, http.MethodOptions)
|
||||||
|
router.Use(mux.CORSMethodMiddleware(router))
|
||||||
loggedRouter := handlers.LoggingHandler(os.Stdout, router)
|
loggedRouter := handlers.LoggingHandler(os.Stdout, router)
|
||||||
|
proxyRouter := handlers.ProxyHeaders(loggedRouter)
|
||||||
|
|
||||||
sockets := make([]net.Listener, 0)
|
sockets := make([]net.Listener, 0)
|
||||||
|
|
||||||
@@ -429,7 +448,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
sockets = append(sockets, sL)
|
sockets = append(sockets, sL)
|
||||||
go func() {
|
go func() {
|
||||||
_ = http.Serve(sL, loggedRouter)
|
_ = http.Serve(sL, proxyRouter)
|
||||||
}()
|
}()
|
||||||
} else {
|
} else {
|
||||||
tL, err := net.Listen("tcp", fmt.Sprintf("%s:%d", l.Host, l.Port))
|
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)
|
log.Fatalf("Failure listing on %s:%d: %v", l.Host, l.Port, err)
|
||||||
}
|
}
|
||||||
go func() {
|
go func() {
|
||||||
err = http.Serve(tL, loggedRouter)
|
err = http.Serve(tL, proxyRouter)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Failure serving on %s:%d: %v", l.Host, l.Port, err)
|
log.Fatalf("Failure serving on %s:%d: %v", l.Host, l.Port, err)
|
||||||
}
|
}
|
||||||
|
@@ -6,6 +6,7 @@ import (
|
|||||||
"csgowtfd/ent"
|
"csgowtfd/ent"
|
||||||
"csgowtfd/ent/match"
|
"csgowtfd/ent/match"
|
||||||
"csgowtfd/ent/player"
|
"csgowtfd/ent/player"
|
||||||
|
"csgowtfd/ent/stats"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"encoding/xml"
|
"encoding/xml"
|
||||||
"fmt"
|
"fmt"
|
||||||
@@ -98,6 +99,50 @@ func SendJSON(data interface{}, w http.ResponseWriter) error {
|
|||||||
return nil
|
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) {
|
func IsAuthCodeValid(player *ent.Player, lock *sync.RWMutex, apiKey string, shareCode string, authCode string, rl ratelimit.Limiter) (bool, error) {
|
||||||
var tMatch *ent.Match
|
var tMatch *ent.Match
|
||||||
var err error
|
var err error
|
||||||
|
Reference in New Issue
Block a user