diff --git a/main.go b/main.go index a3f3823..3f9ab3c 100644 --- a/main.go +++ b/main.go @@ -668,7 +668,7 @@ func getMatches(w http.ResponseWriter, r *http.Request) { if t != "" { unixOffset, err := strconv.ParseInt(t, 10, 64) 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) return } @@ -689,7 +689,7 @@ func getMatches(w http.ResponseWriter, r *http.Request) { log.Debug("[GMS] No matches found") err := utils.SendJSON(mResponse, w) 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) return } @@ -697,6 +697,18 @@ func getMatches(w http.ResponseWriter, r *http.Request) { } 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{ MatchId: iMatch.ID, Map: iMatch.Map, @@ -708,6 +720,7 @@ func getMatches(w http.ResponseWriter, r *http.Request) { Parsed: iMatch.DemoParsed, VAC: iMatch.VacPresent, GameBan: iMatch.GamebanPresent, + AvgRank: v[0].Avg, }) } @@ -740,6 +753,18 @@ func getMatch(w http.ResponseWriter, r *http.Request) { 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{ MatchId: tMatch.ID, ShareCode: tMatch.ShareCode, @@ -751,6 +776,7 @@ func getMatch(w http.ResponseWriter, r *http.Request) { MaxRounds: tMatch.MaxRounds, Parsed: tMatch.DemoParsed, Stats: []*utils.StatsResponse{}, + AvgRank: v[0].Avg, } if tMatch.Date.After(time.Now().AddDate(0, 0, -1*conf.Csgowtfd.DemosExpire)) { diff --git a/utils/utils.go b/utils/utils.go index 8790bdc..34ebe01 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -183,6 +183,7 @@ type MatchResponse struct { VAC bool `json:"vac"` GameBan bool `json:"game_ban"` Stats interface{} `json:"stats,omitempty"` + AvgRank float64 `json:"avg_rank,omitempty"` } type (