more linter fixes

This commit is contained in:
2022-11-20 18:31:09 +01:00
parent 5b938f4d06
commit 64fd38709b

134
main.go
View File

@@ -105,7 +105,7 @@ func housekeeping() {
Where( Where(
match.And( match.And(
match.HasPlayersWith(player.ID(bp.ID)), match.HasPlayersWith(player.ID(bp.ID)),
match.DateLTE(bp.GameBanDate.AddDate(0, 0, 30)), match.DateLTE(bp.GameBanDate.AddDate(0, 0, 30)), //nolint:gomnd
)). )).
SetGamebanPresent(true).Exec(context.Background()) SetGamebanPresent(true).Exec(context.Background())
if err != nil { if err != nil {
@@ -117,7 +117,7 @@ func housekeeping() {
Where( Where(
match.And( match.And(
match.HasPlayersWith(player.ID(bp.ID)), match.HasPlayersWith(player.ID(bp.ID)),
match.DateLTE(bp.VacDate.AddDate(0, 0, 30)), match.DateLTE(bp.VacDate.AddDate(0, 0, 30)), //nolint:gomnd
)).SetVacPresent(true).Exec(context.Background()) )).SetVacPresent(true).Exec(context.Background())
if err != nil { if err != nil {
log.Warningf("[HK] Unable to set gameban/vac for match: %v", err) log.Warningf("[HK] Unable to set gameban/vac for match: %v", err)
@@ -152,6 +152,9 @@ func housekeeping() {
// check for inconsistent matches // check for inconsistent matches
tMatchIDs, err := db.Match.Query().IDs(context.Background()) tMatchIDs, err := db.Match.Query().IDs(context.Background())
if err != nil {
log.Warningf("[HK] Failure trying to get matches: %v", err)
}
for _, mid := range tMatchIDs { for _, mid := range tMatchIDs {
var v []struct { var v []struct {
ID int `json:"match_stats"` ID int `json:"match_stats"`
@@ -168,7 +171,7 @@ func housekeeping() {
continue continue
} }
if v[0].Count < 10 { if v[0].Count < 10 { //nolint:gomnd
log.Warningf("[HK] Found match without all players, try to reload it.") log.Warningf("[HK] Found match without all players, try to reload it.")
tMatch, err := db.Match.Get(context.Background(), mid) tMatch, err := db.Match.Get(context.Background(), mid)
if err != nil { if err != nil {
@@ -269,7 +272,7 @@ func getPlayerMeta(c *gin.Context) {
limit = 4 limit = 4
} }
if limit > 10 { if limit > 10 { //nolint:gomnd
log.Infof("[GPM] limit out of bounds: %d", limit) log.Infof("[GPM] limit out of bounds: %d", limit)
c.Status(http.StatusBadRequest) c.Status(http.StatusBadRequest)
return return
@@ -326,26 +329,28 @@ func getPlayerMeta(c *gin.Context) {
for _, p := range append(metaStats.BestMates, metaStats.MostMates...) { for _, p := range append(metaStats.BestMates, metaStats.MostMates...) {
if p.Player.Name == "" { if p.Player.Name == "" {
tP, err := utils.Player(db, p.Player.SteamID64, conf.Steam.APIKey, nil) continue
if err != nil { }
log.Warningf("[GPM] Failure getting player: %v", err)
c.Status(http.StatusInternalServerError)
return
}
p.Player.Avatar = tP.Avatar
p.Player.Name = tP.Name
p.Player.VAC = !tP.VacDate.IsZero()
p.Player.Tracked = tP.AuthCode != ""
p.Player.GameBan = !tP.GameBanDate.IsZero()
p.Player.VanityURL = tP.VanityURLReal
if !tP.GameBanDate.IsZero() { tP, err := utils.Player(db, p.Player.SteamID64, conf.Steam.APIKey, nil)
p.Player.GameBanDate = tP.GameBanDate.Unix() if err != nil {
} log.Warningf("[GPM] Failure getting player: %v", err)
c.Status(http.StatusInternalServerError)
return
}
p.Player.Avatar = tP.Avatar
p.Player.Name = tP.Name
p.Player.VAC = !tP.VacDate.IsZero()
p.Player.Tracked = tP.AuthCode != ""
p.Player.GameBan = !tP.GameBanDate.IsZero()
p.Player.VanityURL = tP.VanityURLReal
if !tP.VacDate.IsZero() { if !tP.GameBanDate.IsZero() {
p.Player.VACDate = tP.VacDate.Unix() p.Player.GameBanDate = tP.GameBanDate.Unix()
} }
if !tP.VacDate.IsZero() {
p.Player.VACDate = tP.VacDate.Unix()
} }
} }
@@ -353,7 +358,6 @@ func getPlayerMeta(c *gin.Context) {
} }
func getPlayer(c *gin.Context) { func getPlayer(c *gin.Context) {
id := c.Param("id") id := c.Param("id")
t := c.Param("time") t := c.Param("time")
@@ -396,9 +400,10 @@ func getPlayer(c *gin.Context) {
var tMatches []*ent.Match var tMatches []*ent.Match
if !offsetTime.IsZero() { if !offsetTime.IsZero() {
tMatches, err = tPlayer.QueryMatches().Where(match.DateLT(offsetTime)).Order(ent.Desc(match.FieldDate)).Limit(10).All(context.Background()) tMatches, err = tPlayer.QueryMatches().
Where(match.DateLT(offsetTime)).Order(ent.Desc(match.FieldDate)).Limit(10).All(context.Background()) //nolint:gomnd
} else { } else {
tMatches, err = tPlayer.QueryMatches().Order(ent.Desc(match.FieldDate)).Limit(10).All(context.Background()) tMatches, err = tPlayer.QueryMatches().Order(ent.Desc(match.FieldDate)).Limit(10).All(context.Background()) //nolint:gomnd
} }
if err != nil || len(tMatches) == 0 { if err != nil || len(tMatches) == 0 {
log.Debugf("[GP] No matches found for player %s", id) log.Debugf("[GP] No matches found for player %s", id)
@@ -598,16 +603,16 @@ func getMatchRounds(c *gin.Context) {
return return
} }
matchId, err := strconv.ParseUint(id, 10, 64) matchID, err := strconv.ParseUint(id, 10, 64)
if err != nil { if err != nil {
log.Infof("[GMR] Error parsing matchID %s: %v", id, err) log.Infof("[GMR] Error parsing matchID %s: %v", id, err)
c.Status(http.StatusBadRequest) c.Status(http.StatusBadRequest)
return return
} }
tStats, err := db.MatchPlayer.Query().Where(matchplayer.HasMatchesWith(match.ID(matchId))).All(context.Background()) tStats, err := db.MatchPlayer.Query().Where(matchplayer.HasMatchesWith(match.ID(matchID))).All(context.Background())
if err != nil { if err != nil {
log.Infof("[GMR] match %d not found: %+v", matchId, err) log.Infof("[GMR] match %d not found: %+v", matchID, err)
c.Status(http.StatusNotFound) c.Status(http.StatusNotFound)
return return
} }
@@ -665,7 +670,7 @@ func getMatchChat(c *gin.Context) {
} }
} }
matchId, err := strconv.ParseUint(id, 10, 64) matchID, err := strconv.ParseUint(id, 10, 64)
if err != nil { if err != nil {
log.Infof("[GMC] Error parsing matchID %s: %v", id, err) log.Infof("[GMC] Error parsing matchID %s: %v", id, err)
c.Status(http.StatusBadRequest) c.Status(http.StatusBadRequest)
@@ -674,11 +679,14 @@ func getMatchChat(c *gin.Context) {
resp := map[string][]*utils.ChatResponse{} resp := map[string][]*utils.ChatResponse{}
if translate { if translate {
err = rdc.Get(context.Background(), fmt.Sprintf(utils.MatchChatCacheKey, matchId, lang.String()), &resp) err = rdc.Get(context.Background(), fmt.Sprintf(utils.MatchChatCacheKey, matchID, lang.String()), &resp)
if err != nil { if err != nil {
tStats, err := db.Messages.Query().Where(messages.HasMatchPlayerWith(matchplayer.HasMatchesWith(match.ID(matchId)))).WithMatchPlayer().All(context.Background()) tStats, err := db.Messages.Query().
Where(messages.HasMatchPlayerWith(matchplayer.HasMatchesWith(match.ID(matchID)))).
WithMatchPlayer().
All(context.Background())
if err != nil { if err != nil {
log.Infof("[GMC] match %d not found: %+v", matchId, err) log.Infof("[GMC] match %d not found: %+v", matchID, err)
c.Status(http.StatusNotFound) c.Status(http.StatusNotFound)
return return
} }
@@ -721,7 +729,7 @@ func getMatchChat(c *gin.Context) {
err = rdc.Set(&cache.Item{ err = rdc.Set(&cache.Item{
Ctx: context.Background(), Ctx: context.Background(),
Key: fmt.Sprintf(utils.MatchChatCacheKey, matchId, lang.String()), Key: fmt.Sprintf(utils.MatchChatCacheKey, matchID, lang.String()),
Value: resp, Value: resp,
TTL: time.Hour * 24 * 30, TTL: time.Hour * 24 * 30,
}) })
@@ -732,9 +740,12 @@ func getMatchChat(c *gin.Context) {
} }
} }
} else { } else {
tStats, err := db.Messages.Query().Where(messages.HasMatchPlayerWith(matchplayer.HasMatchesWith(match.ID(matchId)))).WithMatchPlayer().All(context.Background()) tStats, err := db.Messages.Query().
Where(messages.HasMatchPlayerWith(matchplayer.HasMatchesWith(match.ID(matchID)))).
WithMatchPlayer().
All(context.Background())
if err != nil { if err != nil {
log.Infof("[GMC] match %d not found: %+v", matchId, err) log.Infof("[GMC] match %d not found: %+v", matchID, err)
c.Status(http.StatusNotFound) c.Status(http.StatusNotFound)
return return
} }
@@ -764,16 +775,16 @@ func getMatchWeapons(c *gin.Context) {
return return
} }
matchId, err := strconv.ParseUint(id, 10, 64) matchID, err := strconv.ParseUint(id, 10, 64)
if err != nil { if err != nil {
log.Infof("[GMW] Error parsing matchID %s: %v", id, err) log.Infof("[GMW] Error parsing matchID %s: %v", id, err)
c.Status(http.StatusBadRequest) c.Status(http.StatusBadRequest)
return return
} }
tStats, err := db.MatchPlayer.Query().Where(matchplayer.HasMatchesWith(match.ID(matchId))).All(context.Background()) tStats, err := db.MatchPlayer.Query().Where(matchplayer.HasMatchesWith(match.ID(matchID))).All(context.Background())
if err != nil { if err != nil {
log.Infof("[GMW] match %d not found: %+v", matchId, err) log.Infof("[GMW] match %d not found: %+v", matchID, err)
c.Status(http.StatusNotFound) c.Status(http.StatusNotFound)
return return
} }
@@ -796,15 +807,15 @@ func getMatchWeapons(c *gin.Context) {
} }
mWr := map[string]map[string][][]int{} mWr := map[string]map[string][][]int{}
playerId := strconv.FormatUint(stat.PlayerStats, 10) playerID := strconv.FormatUint(stat.PlayerStats, 10)
for _, wr := range mWs { for _, wr := range mWs {
if _, exists := mWr[playerId]; !exists { if _, exists := mWr[playerID]; !exists {
mWr[playerId] = map[string][][]int{} mWr[playerID] = map[string][][]int{}
} }
victim := strconv.FormatUint(wr.Victim, 10) victim := strconv.FormatUint(wr.Victim, 10)
mWr[playerId][victim] = append(mWr[playerId][victim], []int{wr.EqType, wr.HitGroup, int(wr.Dmg)}) mWr[playerID][victim] = append(mWr[playerID][victim], []int{wr.EqType, wr.HitGroup, int(wr.Dmg)})
if _, exist := mResponse.EquipmentMap[wr.EqType]; !exist { if _, exist := mResponse.EquipmentMap[wr.EqType]; !exist {
mResponse.EquipmentMap[wr.EqType] = common.EquipmentType(wr.EqType).String() mResponse.EquipmentMap[wr.EqType] = common.EquipmentType(wr.EqType).String()
@@ -820,8 +831,8 @@ func getMatchWeapons(c *gin.Context) {
rSprays := map[string]map[int][][]float32{} rSprays := map[string]map[int][][]float32{}
for _, spray := range mSprays { for _, spray := range mSprays {
if _, exists := rSprays[playerId]; !exists { if _, exists := rSprays[playerID]; !exists {
rSprays[playerId] = map[int][][]float32{} rSprays[playerID] = map[int][][]float32{}
} }
bBuf := bytes.NewBuffer(spray.Spray) bBuf := bytes.NewBuffer(spray.Spray)
@@ -834,7 +845,7 @@ func getMatchWeapons(c *gin.Context) {
} }
log.Debugf("%+v", dSpray) log.Debugf("%+v", dSpray)
rSprays[playerId][spray.Weapon] = dSpray rSprays[playerID][spray.Weapon] = dSpray
} }
mResponse.Spray = append(mResponse.Spray, rSprays) mResponse.Spray = append(mResponse.Spray, rSprays)
} }
@@ -862,9 +873,10 @@ func getMatches(c *gin.Context) {
var err error var err error
var tMatches []*ent.Match var tMatches []*ent.Match
if !offsetTime.IsZero() { if !offsetTime.IsZero() {
tMatches, err = db.Match.Query().Where(match.DateLT(offsetTime)).Order(ent.Desc(match.FieldDate)).Limit(20).All(context.Background()) tMatches, err = db.Match.Query().
Where(match.DateLT(offsetTime)).Order(ent.Desc(match.FieldDate)).Limit(20).All(context.Background()) //nolint:gomnd
} else { } else {
tMatches, err = db.Match.Query().Order(ent.Desc(match.FieldDate)).Limit(20).All(context.Background()) tMatches, err = db.Match.Query().Order(ent.Desc(match.FieldDate)).Limit(20).All(context.Background()) //nolint:gomnd
} }
if err != nil || len(tMatches) == 0 { if err != nil || len(tMatches) == 0 {
log.Debug("[GMS] No matches found") log.Debug("[GMS] No matches found")
@@ -879,7 +891,11 @@ func getMatches(c *gin.Context) {
} }
avgRank := 0.0 avgRank := 0.0
err := iMatch.QueryStats().Where(matchplayer.RankOldNEQ(0)).GroupBy(matchplayer.MatchesColumn).Aggregate(ent.Mean(matchplayer.FieldRankOld)).Scan(context.Background(), &v) err := iMatch.QueryStats().
Where(matchplayer.RankOldNEQ(0)).
GroupBy(matchplayer.MatchesColumn).
Aggregate(ent.Mean(matchplayer.FieldRankOld)).
Scan(context.Background(), &v)
if err != nil || len(v) == 0 { if err != nil || len(v) == 0 {
log.Debugf("[GMS] Unable to calc avg rank for match %d: %v", iMatch.ID, err) log.Debugf("[GMS] Unable to calc avg rank for match %d: %v", iMatch.ID, err)
avgRank = 0.0 avgRank = 0.0
@@ -914,16 +930,16 @@ func getMatch(c *gin.Context) {
return return
} }
matchId, err := strconv.ParseUint(id, 10, 64) matchID, err := strconv.ParseUint(id, 10, 64)
if err != nil { if err != nil {
log.Infof("[GM] Unable to parse matchID %s: %v", id, err) log.Infof("[GM] Unable to parse matchID %s: %v", id, err)
c.Status(http.StatusBadRequest) c.Status(http.StatusBadRequest)
return return
} }
tMatch, err := db.Match.Query().Where(match.ID(matchId)).Only(context.Background()) tMatch, err := db.Match.Query().Where(match.ID(matchID)).Only(context.Background())
if err != nil { if err != nil {
log.Infof("[GM] match %d not found: %v", matchId, err) log.Infof("[GM] match %d not found: %v", matchID, err)
c.Status(http.StatusNotFound) c.Status(http.StatusNotFound)
return return
} }
@@ -934,7 +950,11 @@ func getMatch(c *gin.Context) {
} }
avgRank := 0.0 avgRank := 0.0
err = tMatch.QueryStats().Where(matchplayer.RankOldNEQ(0)).GroupBy(matchplayer.MatchesColumn).Aggregate(ent.Mean(matchplayer.FieldRankOld)).Scan(context.Background(), &v) err = tMatch.QueryStats().
Where(matchplayer.RankOldNEQ(0)).
GroupBy(matchplayer.MatchesColumn).
Aggregate(ent.Mean(matchplayer.FieldRankOld)).
Scan(context.Background(), &v)
if err != nil || len(v) == 0 { if err != nil || len(v) == 0 {
log.Debugf("[GM] Unable to calc avg rank for match %d: %v", tMatch.ID, err) log.Debugf("[GM] Unable to calc avg rank for match %d: %v", tMatch.ID, err)
avgRank = 0 avgRank = 0
@@ -1106,10 +1126,10 @@ func main() {
rdc = cache.New(&cache.Options{ rdc = cache.New(&cache.Options{
Redis: rdb, Redis: rdb,
LocalCache: cache.NewTinyLFU(1000, time.Minute), LocalCache: cache.NewTinyLFU(1000, time.Minute), //nolint:gomnd
}) })
rL = rate.NewLimiter(rate.Limit(conf.Steam.RatePerSecond), 100) rL = rate.NewLimiter(rate.Limit(conf.Steam.RatePerSecond), 100) //nolint:gomnd
// setup GC // setup GC
err = demoLoader.Setup(&csgo.DemoMatchLoaderConfig{ err = demoLoader.Setup(&csgo.DemoMatchLoaderConfig{
@@ -1135,7 +1155,7 @@ func main() {
r := gin.New() r := gin.New()
r.Use(gin.LoggerWithFormatter(func(param gin.LogFormatterParams) string { r.Use(gin.LoggerWithFormatter(func(param gin.LogFormatterParams) string {
return fmt.Sprintf("%s - - \"%s %s %s\" %d %d \"%s\" \"%s\" %s %s\n", return fmt.Sprintf("%s - - \"%s %s %s\" %d %d %q %q %s %s\n",
utils.RealIP(&param.Request.Header, param.Request.RemoteAddr), utils.RealIP(&param.Request.Header, param.Request.RemoteAddr),
param.Method, param.Method,
param.Path, param.Path,
@@ -1192,7 +1212,7 @@ func main() {
log.Infof("Listening on %s:%d", l.Host, l.Port) log.Infof("Listening on %s:%d", l.Host, l.Port)
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))
if err != nil { if err != nil {
log.Fatalf("Failure listing on %s:%d: %v", l.Host, l.Port, err) log.Panicf("Failure listing on %s:%d: %v", l.Host, l.Port, err)
} }
go func() { go func() {
srv := &http.Server{ srv := &http.Server{
@@ -1217,7 +1237,7 @@ killLoop:
case <-reloadSignals: case <-reloadSignals:
confStr, err := os.ReadFile(*configFlag) confStr, err := os.ReadFile(*configFlag)
if err != nil { if err != nil {
log.Fatalf("Unable to open config: %v", err) log.Panicf("Unable to open config: %v", err)
} }
err = yaml.Unmarshal(confStr, &conf) err = yaml.Unmarshal(confStr, &conf)