added avg rank to matchresponse

This commit is contained in:
2021-11-15 19:11:38 +01:00
parent ecfb099a08
commit 95c93f738b
2 changed files with 29 additions and 2 deletions

30
main.go
View File

@@ -668,7 +668,7 @@ func getMatches(w http.ResponseWriter, r *http.Request) {
if t != "" { if t != "" {
unixOffset, err := strconv.ParseInt(t, 10, 64) unixOffset, err := strconv.ParseInt(t, 10, 64)
if err != nil { if err != nil {
log.Infof("[GP] offset not an int: %v", err) log.Infof("[GMS] offset not an int: %v", err)
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
return return
} }
@@ -689,7 +689,7 @@ func getMatches(w http.ResponseWriter, r *http.Request) {
log.Debug("[GMS] No matches found") log.Debug("[GMS] No matches found")
err := utils.SendJSON(mResponse, w) err := utils.SendJSON(mResponse, w)
if err != nil { if err != nil {
log.Errorf("[GP] Unable to marshal JSON: %v", err) log.Errorf("[GMS] Unable to marshal JSON: %v", err)
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
return return
} }
@@ -697,6 +697,18 @@ func getMatches(w http.ResponseWriter, r *http.Request) {
} }
for _, iMatch := range tMatches { for _, iMatch := range tMatches {
var v []struct {
Avg float64 `json:"avg"`
MatchID uint64 `json:"match_stats"`
}
err := iMatch.QueryStats().GroupBy(matchplayer.MatchesColumn).Aggregate(ent.Mean(matchplayer.FieldRankOld)).Scan(context.Background(), &v)
if err != nil || len(v) == 0 {
log.Errorf("[GMS] Unable to calc avg rank for match %d: %v", iMatch.ID, err)
w.WriteHeader(http.StatusInternalServerError)
return
}
mResponse = append(mResponse, &utils.MatchResponse{ mResponse = append(mResponse, &utils.MatchResponse{
MatchId: iMatch.ID, MatchId: iMatch.ID,
Map: iMatch.Map, Map: iMatch.Map,
@@ -708,6 +720,7 @@ func getMatches(w http.ResponseWriter, r *http.Request) {
Parsed: iMatch.DemoParsed, Parsed: iMatch.DemoParsed,
VAC: iMatch.VacPresent, VAC: iMatch.VacPresent,
GameBan: iMatch.GamebanPresent, GameBan: iMatch.GamebanPresent,
AvgRank: v[0].Avg,
}) })
} }
@@ -740,6 +753,18 @@ func getMatch(w http.ResponseWriter, r *http.Request) {
return return
} }
var v []struct {
Avg float64 `json:"avg"`
MatchID uint64 `json:"match_stats"`
}
err = tMatch.QueryStats().GroupBy(matchplayer.MatchesColumn).Aggregate(ent.Mean(matchplayer.FieldRankOld)).Scan(context.Background(), &v)
if err != nil || len(v) == 0 {
log.Errorf("[GM] Unable to calc avg rank for match %d: %v", tMatch.ID, err)
w.WriteHeader(http.StatusInternalServerError)
return
}
mResponse := &utils.MatchResponse{ mResponse := &utils.MatchResponse{
MatchId: tMatch.ID, MatchId: tMatch.ID,
ShareCode: tMatch.ShareCode, ShareCode: tMatch.ShareCode,
@@ -751,6 +776,7 @@ func getMatch(w http.ResponseWriter, r *http.Request) {
MaxRounds: tMatch.MaxRounds, MaxRounds: tMatch.MaxRounds,
Parsed: tMatch.DemoParsed, Parsed: tMatch.DemoParsed,
Stats: []*utils.StatsResponse{}, Stats: []*utils.StatsResponse{},
AvgRank: v[0].Avg,
} }
if tMatch.Date.After(time.Now().AddDate(0, 0, -1*conf.Csgowtfd.DemosExpire)) { if tMatch.Date.After(time.Now().AddDate(0, 0, -1*conf.Csgowtfd.DemosExpire)) {

View File

@@ -183,6 +183,7 @@ type MatchResponse struct {
VAC bool `json:"vac"` VAC bool `json:"vac"`
GameBan bool `json:"game_ban"` GameBan bool `json:"game_ban"`
Stats interface{} `json:"stats,omitempty"` Stats interface{} `json:"stats,omitempty"`
AvgRank float64 `json:"avg_rank,omitempty"`
} }
type ( type (