updated deps, added golangci linter

This commit is contained in:
2022-11-20 18:11:52 +01:00
parent d10a134a34
commit 5b938f4d06
8 changed files with 231 additions and 154 deletions

View File

@@ -7,7 +7,6 @@ import (
"git.harting.dev/csgowtf/csgowtfd/utils"
"github.com/an0nfunc/go-steam/v3"
"github.com/an0nfunc/go-steam/v3/csgo/protocol/protobuf"
"github.com/an0nfunc/go-steam/v3/netutil"
"github.com/an0nfunc/go-steam/v3/protocol/gamecoordinator"
"github.com/an0nfunc/go-steam/v3/protocol/steamlang"
"github.com/go-redis/cache/v8"
@@ -34,9 +33,9 @@ type DemoMatchLoaderConfig struct {
AuthCode string
Sentry string
LoginKey string
Db *ent.Client
DB *ent.Client
Worker int
ApiKey string
APIKey string
RateLimit *rate.Limiter
Cache *cache.Cache
SprayTimeout int
@@ -48,7 +47,6 @@ type DemoMatchLoader struct {
GCReady bool
steamLogin *steam.LogOnDetails
matchRecv chan *protobuf.CMsgGCCStrike15V2_MatchList
cmList []*netutil.PortAddr
sentryFile string
loginKey string
db *ent.Client
@@ -63,18 +61,20 @@ type DemoMatchLoader struct {
LoggedIn bool
}
func AccountId2SteamId(accId uint32) uint64 {
return uint64(accId) + 76561197960265728
func AccountID2SteamID(accID uint32) uint64 {
return uint64(accID) + 76561197960265728 //nolint:gomnd
}
func SteamId2AccountId(steamId uint64) uint32 {
return uint32(steamId - 76561197960265728)
func SteamID2AccountID(steamID uint64) uint32 {
return uint32(steamID - 76561197960265728) //nolint:gomnd
}
func playerStatsFromRound(round *protobuf.CMsgGCCStrike15V2_MatchmakingServerRoundStats, p *ent.Player) (kills int32, deaths int32, assists int32, headshots int32, score int32, mvps int32) {
func playerStatsFromRound(round *protobuf.CMsgGCCStrike15V2_MatchmakingServerRoundStats, p *ent.Player) (kills int32,
deaths int32, assists int32, headshots int32, score int32, mvps int32) {
for i, acc := range round.GetReservation().GetAccountIds() {
if AccountId2SteamId(acc) == p.ID {
return round.GetKills()[i], round.GetDeaths()[i], round.GetAssists()[i], round.GetEnemyHeadshots()[i], round.GetScores()[i], round.GetMvps()[i]
if AccountID2SteamID(acc) == p.ID {
return round.GetKills()[i], round.GetDeaths()[i], round.GetAssists()[i], round.GetEnemyHeadshots()[i],
round.GetScores()[i], round.GetMvps()[i]
}
}
@@ -149,25 +149,23 @@ func (dml *DemoMatchLoader) getMatchDetails(sharecode string) (*protobuf.CMsgGCC
return nil, fmt.Errorf("gc not ready")
}
matchId, outcomeId, tokenId, err := DecodeSharecode(sharecode)
matchID, outcomeID, tokenID, err := DecodeSharecode(sharecode)
if err != nil {
return nil, err
}
err = dml.requestDemoInfo(matchId, outcomeId, uint32(tokenId))
err = dml.requestDemoInfo(matchID, outcomeID, uint32(tokenID))
if err != nil {
return nil, err
}
for {
select {
case matchDetails := <-dml.matchRecv:
if *matchDetails.Matches[0].Matchid == matchId {
return matchDetails, nil
} else {
dml.matchRecv <- matchDetails
}
for matchDetails := range dml.matchRecv {
if *matchDetails.Matches[0].Matchid == matchID {
return matchDetails, nil
} else {
dml.matchRecv <- matchDetails
}
}
return nil, err
}
func (dml *DemoMatchLoader) connectToSteam() error {
@@ -185,15 +183,15 @@ func (dml *DemoMatchLoader) connectToSteam() error {
func (dml *DemoMatchLoader) Setup(config *DemoMatchLoaderConfig) error {
dml.loginKey = config.LoginKey
dml.sentryFile = config.Sentry
dml.db = config.Db
dml.db = config.DB
dml.dp = &DemoParser{}
dml.parseMap = map[string]bool{}
dml.parseMapL = new(sync.RWMutex)
dml.cache = config.Cache
dml.connectFeedback = make(chan int, 10)
dml.connectFeedback = make(chan int, 10) //nolint:gomnd
dml.connectionWait = retry.WithCappedDuration(time.Minute*time.Duration(config.RetryTimeout), retry.NewExponential(time.Minute))
dml.connectionWaitTmpl = retry.WithCappedDuration(time.Minute*time.Duration(config.RetryTimeout), retry.NewExponential(time.Minute))
err := dml.dp.Setup(config.Db, config.Worker, config.SprayTimeout)
err := dml.dp.Setup(config.DB, config.Worker, config.SprayTimeout)
if err != nil {
return err
}
@@ -225,15 +223,15 @@ func (dml *DemoMatchLoader) Setup(config *DemoMatchLoaderConfig) error {
if err != nil {
return err
}
dml.matchRecv = make(chan *protobuf.CMsgGCCStrike15V2_MatchList, 1000)
dml.parseDemo = make(chan *Demo, 1000)
dml.matchRecv = make(chan *protobuf.CMsgGCCStrike15V2_MatchList, 1000) //nolint:gomnd
dml.parseDemo = make(chan *Demo, 1000) //nolint:gomnd
go dml.connectLoop()
go dml.steamEventHandler()
go dml.demoWorker()
for i := 0; i < config.Worker; i++ {
go dml.gcWorker(config.ApiKey, config.RateLimit)
go dml.gcWorker(config.APIKey, config.RateLimit)
}
dml.connectFeedback <- LoginFailed
@@ -258,28 +256,25 @@ func (dml *DemoMatchLoader) LoadDemo(demo *Demo) error {
}
func (dml *DemoMatchLoader) connectLoop() {
for {
select {
case res := <-dml.connectFeedback:
switch res {
case LoginFailed:
if sleep, ok := dml.connectionWait.Next(); !ok {
time.Sleep(sleep)
} else {
panic("retry should never be stop")
}
if !dml.LoggedIn {
log.Infof("[DL] Connecting to steam...")
err := dml.connectToSteam()
if err != nil {
log.Warningf("[DL] Error connecting to steam: %v", err)
}
}
case LoginSuccess:
log.Info("[DL] Steam login successfully restored")
dml.connectionWait = dml.connectionWaitTmpl
for res := range dml.connectFeedback {
switch res {
case LoginFailed:
if sleep, ok := dml.connectionWait.Next(); !ok {
time.Sleep(sleep)
} else {
panic("retry should never be stop")
}
if !dml.LoggedIn {
log.Infof("[DL] Connecting to steam...")
err := dml.connectToSteam()
if err != nil {
log.Warningf("[DL] Error connecting to steam: %v", err)
}
}
case LoginSuccess:
log.Info("[DL] Steam login successfully restored")
dml.connectionWait = dml.connectionWaitTmpl
}
}
}
@@ -355,16 +350,19 @@ func (dml *DemoMatchLoader) greetGC() {
}
}
func (dml *DemoMatchLoader) requestDemoInfo(matchId uint64, conclusionId uint64, tokenId uint32) error {
func (dml *DemoMatchLoader) requestDemoInfo(matchID, conclusionID uint64, tokenID uint32) error {
if !dml.GCReady {
return fmt.Errorf("gc not ready")
}
msg := protobuf.CMsgGCCStrike15V2_MatchListRequestFullGameInfo{Matchid: &matchId,
Outcomeid: &conclusionId,
Token: &tokenId}
msg := protobuf.CMsgGCCStrike15V2_MatchListRequestFullGameInfo{
Matchid: &matchID,
Outcomeid: &conclusionID,
Token: &tokenID,
}
dml.client.GC.Write(gamecoordinator.NewGCMsgProtobuf(APPID, uint32(protobuf.ECsgoGCMsg_k_EMsgGCCStrike15_v2_MatchListRequestFullGameInfo), &msg))
dml.client.GC.Write(gamecoordinator.NewGCMsgProtobuf(APPID,
uint32(protobuf.ECsgoGCMsg_k_EMsgGCCStrike15_v2_MatchListRequestFullGameInfo), &msg))
return nil
}
@@ -379,46 +377,46 @@ func (dml *DemoMatchLoader) handleDemo(demo *Demo, apiKey string, rl *rate.Limit
defer dml.unlockDemo(demo)
if !dml.GCReady {
log.Infof("[DL] Postponing match %d (%s): GC not ready", demo.MatchId, demo.ShareCode)
log.Infof("[DL] Postponing match %d (%s): GC not ready", demo.MatchID, demo.ShareCode)
time.Sleep(5 * time.Second)
dml.parseDemo <- demo
return nil
}
matchId, _, _, err := DecodeSharecode(demo.ShareCode)
if err != nil || matchId == 0 {
matchID, _, _, err := DecodeSharecode(demo.ShareCode)
if err != nil || matchID == 0 {
return fmt.Errorf("error decoding sharecode %s: %w", demo.ShareCode, err)
}
iMatch, err := dml.db.Match.Get(context.Background(), matchId)
iMatch, err := dml.db.Match.Get(context.Background(), matchID)
if err != nil && !ent.IsNotFound(err) {
return fmt.Errorf("error looking up match: %w", err)
} else if err == nil {
if iMatch.DemoParsed == false && iMatch.Date.After(time.Now().UTC().AddDate(0, 0, -30)) {
if !iMatch.DemoParsed && iMatch.Date.After(time.Now().UTC().AddDate(0, 0, -30)) {
log.Infof("[DL] Match %d is loaded, but not parsed. Try parsing.", iMatch.ID)
demo.MatchId = matchId
demo.Url = iMatch.ReplayURL
demo.MatchID = matchID
demo.URL = iMatch.ReplayURL
demo.DecryptionKey = iMatch.DecryptionKey
err := dml.dp.ParseDemo(demo)
if err != nil {
return fmt.Errorf("error parsing match %d: %w", demo.MatchId, err)
return fmt.Errorf("error parsing match %d: %w", demo.MatchID, err)
}
return nil
}
log.Infof("[DL] Skipped match %d: already loaded", matchId)
log.Infof("[DL] Skipped match %d: already loaded", matchID)
return nil
}
log.Infof("[DL] Requesting match %d from GC", matchId)
log.Infof("[DL] Requesting match %d from GC", matchID)
t := time.Now()
matchDetails, err := dml.getMatchDetails(demo.ShareCode)
if err != nil {
return fmt.Errorf("error getting match-details for %d: %w", demo.MatchId, err)
return fmt.Errorf("error getting match-details for %d: %w", demo.MatchID, err)
}
log.Infof("[DL] Recieved matchdetails for match %d (%s)", matchId, time.Since(t))
log.Infof("[DL] Received matchdetails for match %d (%s)", matchID, time.Since(t))
// init tx
tx, err := dml.db.Tx(context.Background())
@@ -430,17 +428,17 @@ func (dml *DemoMatchLoader) handleDemo(demo *Demo, apiKey string, rl *rate.Limit
lastRound := matchZero.GetRoundstatsall()[len(matchZero.Roundstatsall)-1]
var players []*ent.Player
for _, accountId := range lastRound.GetReservation().GetAccountIds() {
tPlayer, err := utils.Player(tx.Client(), AccountId2SteamId(accountId), apiKey, rl)
for _, accountID := range lastRound.GetReservation().GetAccountIds() {
tPlayer, err := utils.Player(tx.Client(), AccountID2SteamID(accountID), apiKey, rl)
if err != nil {
err = utils.Rollback(tx, err)
return fmt.Errorf("error getting player for steamid %d: %w", AccountId2SteamId(accountId), err)
return fmt.Errorf("error getting player for steamid %d: %w", AccountID2SteamID(accountID), err)
}
players = append(players, tPlayer)
}
demo.Url = lastRound.GetMap()
demo.MatchId = matchZero.GetMatchid()
demo.URL = lastRound.GetMap()
demo.MatchID = matchZero.GetMatchid()
demo.DecryptionKey = []byte(strings.ToUpper(fmt.Sprintf("%016x", matchZero.GetWatchablematchinfo().GetClDecryptdataKeyPub())))
tMatch, err := tx.Match.Create().
@@ -463,14 +461,14 @@ func (dml *DemoMatchLoader) handleDemo(demo *Demo, apiKey string, rl *rate.Limit
for i, mPlayer := range players {
var (
teamId int
teamID int
mk2, mk3, mk4, mk5 uint
)
if i > 4 {
teamId = 2
teamID = 2
} else {
teamId = 1
teamID = 1
}
var oldKills int32
@@ -494,7 +492,7 @@ func (dml *DemoMatchLoader) handleDemo(demo *Demo, apiKey string, rl *rate.Limit
err = tx.MatchPlayer.Create().
SetMatches(tMatch).
SetPlayers(mPlayer).
SetTeamID(teamId).
SetTeamID(teamID).
SetKills(int(kills)).
SetDeaths(int(deaths)).
SetAssists(int(assists)).
@@ -539,7 +537,7 @@ func (dml *DemoMatchLoader) handleDemo(demo *Demo, apiKey string, rl *rate.Limit
err = dml.dp.ParseDemo(demo)
if err != nil {
return fmt.Errorf("error queueing demo %d for parsing: %w", demo.MatchId, err)
return fmt.Errorf("error queueing demo %d for parsing: %w", demo.MatchID, err)
}
return nil
}