added spray patterns

This commit is contained in:
2021-10-31 08:40:02 +01:00
parent 268793f0e5
commit 978232dd0a
66 changed files with 16805 additions and 11903 deletions

95
main.go
View File

@@ -1,14 +1,16 @@
package main
import (
"bytes"
"context"
"csgowtfd/csgo"
"csgowtfd/ent"
"csgowtfd/ent/match"
"csgowtfd/ent/matchplayer"
"csgowtfd/ent/migrate"
"csgowtfd/ent/player"
"csgowtfd/ent/stats"
"csgowtfd/utils"
"encoding/gob"
"entgo.io/ent/dialect"
"entgo.io/ent/dialect/sql"
"flag"
@@ -304,7 +306,12 @@ func getPlayer(w http.ResponseWriter, r *http.Request) {
GameBanDate: tPlayer.GameBanDate.Unix(),
VanityURL: tPlayer.VanityURLReal,
Tracked: tPlayer.AuthCode != "",
Matches: []*utils.MatchResponse{},
MatchStats: &utils.MatchStats{
Win: tPlayer.Wins,
Tie: tPlayer.Ties,
Loss: tPlayer.Looses,
},
Matches: []*utils.MatchResponse{},
}
var tMatches []*ent.Match
@@ -324,44 +331,6 @@ func getPlayer(w http.ResponseWriter, r *http.Request) {
return
}
metaStats := new(utils.MatchStats)
err = rdc.Get(context.Background(), fmt.Sprintf(utils.MatchMetaCacheKey, tPlayer.ID), &metaStats)
if err != nil {
wins, ties, losses, err := utils.GetMatchStats(tPlayer)
if err != nil {
log.Errorf("[GP] Error retrieving match-stats for player %s: %v", id, err)
w.WriteHeader(http.StatusInternalServerError)
return
}
response.MatchStats = &utils.MatchStats{
Win: wins,
Loss: losses,
Tie: ties,
}
err = rdc.Set(&cache.Item{
Ctx: context.Background(),
Key: fmt.Sprintf(utils.MatchMetaCacheKey, tPlayer.ID),
Value: response.MatchStats,
TTL: time.Hour * 24 * 30,
})
if err != nil {
log.Errorf("[GP] Failure saving to cache: %v", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
log.Debugf("[GP] Metastats for %d saved to cache", tPlayer.ID)
} else {
log.Debugf("[GP] Metastats for %d from cache", tPlayer.ID)
response.MatchStats = &utils.MatchStats{
Win: metaStats.Win,
Tie: metaStats.Tie,
Loss: metaStats.Loss,
}
}
for _, iMatch := range tMatches {
mResponse := &utils.MatchResponse{
MatchId: iMatch.ID,
@@ -377,10 +346,10 @@ func getPlayer(w http.ResponseWriter, r *http.Request) {
}
tStats, err := iMatch.QueryStats().Modify(func(s *sql.Selector) {
s.Select(stats.FieldTeamID, stats.FieldKills, stats.FieldDeaths, stats.FieldAssists, stats.FieldHeadshot,
stats.FieldMvp, stats.FieldScore, stats.FieldMk2, stats.FieldMk3, stats.FieldMk4, stats.FieldMk5,
stats.FieldRankOld, stats.FieldRankNew, stats.FieldDmgTeam, stats.FieldDmgEnemy)
s.Where(sql.EQ(s.C(stats.PlayersColumn), tPlayer.ID))
s.Select(matchplayer.FieldTeamID, matchplayer.FieldKills, matchplayer.FieldDeaths, matchplayer.FieldAssists, matchplayer.FieldHeadshot,
matchplayer.FieldMvp, matchplayer.FieldScore, matchplayer.FieldMk2, matchplayer.FieldMk3, matchplayer.FieldMk4, matchplayer.FieldMk5,
matchplayer.FieldRankOld, matchplayer.FieldRankNew, matchplayer.FieldDmgTeam, matchplayer.FieldDmgEnemy)
s.Where(sql.EQ(s.C(matchplayer.PlayersColumn), tPlayer.ID))
}).Only(context.Background())
if err != nil {
response.Matches = append(response.Matches, mResponse)
@@ -571,7 +540,7 @@ func getMatchRounds(w http.ResponseWriter, r *http.Request) {
return
}
tStats, err := db.Stats.Query().Where(stats.HasMatchesWith(match.ID(matchId))).All(context.Background())
tStats, err := db.MatchPlayer.Query().Where(matchplayer.HasMatchesWith(match.ID(matchId))).All(context.Background())
if err != nil {
log.Infof("[GMR] match %d not found: %+v", matchId, err)
w.WriteHeader(http.StatusNotFound)
@@ -618,7 +587,7 @@ func getMatchWeapons(w http.ResponseWriter, r *http.Request) {
return
}
tStats, err := db.Stats.Query().Where(stats.HasMatchesWith(match.ID(matchId))).All(context.Background())
tStats, err := db.MatchPlayer.Query().Where(matchplayer.HasMatchesWith(match.ID(matchId))).All(context.Background())
if err != nil {
log.Infof("[GMW] match %d not found: %+v", matchId, err)
w.WriteHeader(http.StatusNotFound)
@@ -626,11 +595,13 @@ func getMatchWeapons(w http.ResponseWriter, r *http.Request) {
}
mResponse := struct {
EquipmentMap map[int]string `json:"equipment_map,omitempty"`
Stats []map[string]map[string][][]int `json:"stats,omitempty"`
EquipmentMap map[int]string `json:"equipment_map,omitempty"`
Stats []map[string]map[string][][]int `json:"stats,omitempty"`
Spray []map[string]map[int][][]float32 `json:"spray,omitempty"`
}{
EquipmentMap: map[int]string{},
Stats: []map[string]map[string][][]int{},
Spray: []map[string]map[int][][]float32{},
}
for _, stat := range tStats {
@@ -644,7 +615,7 @@ func getMatchWeapons(w http.ResponseWriter, r *http.Request) {
playerId := strconv.FormatUint(stat.PlayerStats, 10)
for _, wr := range mWs {
if _, exists := mWr[strconv.FormatUint(stat.PlayerStats, 10)]; !exists {
if _, exists := mWr[playerId]; !exists {
mWr[playerId] = map[string][][]int{}
}
@@ -656,6 +627,32 @@ func getMatchWeapons(w http.ResponseWriter, r *http.Request) {
}
}
mResponse.Stats = append(mResponse.Stats, mWr)
mSprays, err := stat.QuerySpray().All(context.Background())
if err != nil {
log.Warningf("[GMW] Unable to get Sprays for player %d: %v", stat.PlayerStats, err)
continue
}
rSprays := map[string]map[int][][]float32{}
for _, spray := range mSprays {
if _, exists := rSprays[playerId]; !exists {
rSprays[playerId] = map[int][][]float32{}
}
bBuf := bytes.NewBuffer(spray.Spray)
dec := gob.NewDecoder(bBuf)
var dSpray [][]float32
err := dec.Decode(&dSpray)
if err != nil {
log.Warningf("[GMW] Unable to decode Sprays for player %d: %v", stat.PlayerStats, err)
continue
}
log.Debugf("%+v", dSpray)
rSprays[playerId][spray.Weapon] = dSpray
}
mResponse.Spray = append(mResponse.Spray, rSprays)
}
err = utils.SendJSON(mResponse, w)