updated deps, added golangci linter
This commit is contained in:
@@ -8,9 +8,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"git.harting.dev/csgowtf/csgowtfd/ent"
|
||||
"git.harting.dev/csgowtf/csgowtfd/ent/match"
|
||||
"git.harting.dev/csgowtf/csgowtfd/ent/matchplayer"
|
||||
"git.harting.dev/csgowtf/csgowtfd/ent/player"
|
||||
"git.harting.dev/csgowtf/csgowtfd/utils"
|
||||
"github.com/golang/geo/r2"
|
||||
"github.com/markus-wa/demoinfocs-golang/v3/pkg/demoinfocs"
|
||||
@@ -24,14 +22,13 @@ import (
|
||||
|
||||
type Demo struct {
|
||||
ShareCode string
|
||||
MatchId uint64
|
||||
Url string
|
||||
MatchID uint64
|
||||
URL string
|
||||
DecryptionKey []byte
|
||||
}
|
||||
|
||||
type DemoParser struct {
|
||||
demoQueue chan *Demo
|
||||
tempDir string
|
||||
db *ent.Client
|
||||
sprayTimeout int
|
||||
Done chan *Demo
|
||||
@@ -108,8 +105,8 @@ func (s *Sprays) Avg() (avg [][]float32) {
|
||||
return
|
||||
}
|
||||
|
||||
func (dp *DemoParser) Setup(db *ent.Client, worker int, sprayTimeout int) error {
|
||||
dp.demoQueue = make(chan *Demo, 1000)
|
||||
func (dp *DemoParser) Setup(db *ent.Client, worker, sprayTimeout int) error {
|
||||
dp.demoQueue = make(chan *Demo, 1000) //nolint:gomnd
|
||||
dp.db = db
|
||||
dp.sprayTimeout = sprayTimeout
|
||||
dp.Done = make(chan *Demo, worker)
|
||||
@@ -129,9 +126,12 @@ func (dp *DemoParser) ParseDemo(demo *Demo) error {
|
||||
}
|
||||
|
||||
func (d *Demo) download() (io.Reader, error) {
|
||||
log.Debugf("[DP] Downloading replay for %d", d.MatchId)
|
||||
log.Debugf("[DP] Downloading replay for %d", d.MatchID)
|
||||
|
||||
r, err := http.Get(d.Url)
|
||||
r, err := http.Get(d.URL)
|
||||
defer func(Body io.ReadCloser) {
|
||||
_ = Body.Close()
|
||||
}(r.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -142,26 +142,13 @@ func (d *Demo) download() (io.Reader, error) {
|
||||
return bzip2.NewReader(r.Body), nil
|
||||
}
|
||||
|
||||
func (dp *DemoParser) getDBPlayer(demo *Demo, demoPlayer *common.Player) (*ent.MatchPlayer, error) {
|
||||
tMatchPlayer, err := dp.db.MatchPlayer.Query().WithMatches(func(q *ent.MatchQuery) {
|
||||
q.Where(match.ID(demo.MatchId))
|
||||
}).WithPlayers(func(q *ent.PlayerQuery) {
|
||||
q.Where(player.ID(demoPlayer.SteamID64))
|
||||
}).Only(context.Background())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return tMatchPlayer, nil
|
||||
}
|
||||
|
||||
func (dp *DemoParser) MatchPlayerBySteamID(stats []*ent.MatchPlayer, steamId uint64) (*ent.MatchPlayer, error) {
|
||||
func (dp *DemoParser) MatchPlayerBySteamID(stats []*ent.MatchPlayer, steamID uint64) (*ent.MatchPlayer, error) {
|
||||
for _, tStats := range stats {
|
||||
tPLayer, err := tStats.Edges.PlayersOrErr()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to get stats from statList: %w", err)
|
||||
}
|
||||
if tPLayer.ID == steamId {
|
||||
if tPLayer.ID == steamID {
|
||||
return tStats, nil
|
||||
}
|
||||
}
|
||||
@@ -179,19 +166,14 @@ func setMatchPlayerColor(matchPlayer *ent.MatchPlayer, demoPlayer *common.Player
|
||||
switch color {
|
||||
case common.Yellow:
|
||||
matchPlayer.Color = matchplayer.ColorYellow
|
||||
break
|
||||
case common.Green:
|
||||
matchPlayer.Color = matchplayer.ColorGreen
|
||||
break
|
||||
case common.Purple:
|
||||
matchPlayer.Color = matchplayer.ColorPurple
|
||||
break
|
||||
case common.Blue:
|
||||
matchPlayer.Color = matchplayer.ColorBlue
|
||||
break
|
||||
case common.Orange:
|
||||
matchPlayer.Color = matchplayer.ColorOrange
|
||||
break
|
||||
case common.Grey:
|
||||
matchPlayer.Color = matchplayer.ColorGrey
|
||||
}
|
||||
@@ -200,7 +182,7 @@ func setMatchPlayerColor(matchPlayer *ent.MatchPlayer, demoPlayer *common.Player
|
||||
func (dp *DemoParser) parseWorker() {
|
||||
workloop:
|
||||
for demo := range dp.demoQueue {
|
||||
if demo.MatchId == 0 {
|
||||
if demo.MatchID == 0 {
|
||||
log.Warningf("[DP] can't parse match %s: no matchid found", demo.ShareCode)
|
||||
dp.Done <- demo
|
||||
continue
|
||||
@@ -214,17 +196,17 @@ workloop:
|
||||
return
|
||||
}
|
||||
|
||||
tMatch, err := tx.Match.Get(context.Background(), demo.MatchId)
|
||||
tMatch, err := tx.Match.Get(context.Background(), demo.MatchID)
|
||||
if err != nil {
|
||||
err = utils.Rollback(tx, err)
|
||||
log.Errorf("[DP] Unable to get match %d: %v", demo.MatchId, err)
|
||||
log.Errorf("[DP] Unable to get match %d: %v", demo.MatchID, err)
|
||||
dp.Done <- demo
|
||||
continue
|
||||
}
|
||||
|
||||
if tMatch.DemoParsed {
|
||||
err = utils.Rollback(tx, err)
|
||||
log.Infof("[DP] skipped already parsed %d", demo.MatchId)
|
||||
_ = utils.Rollback(tx, err)
|
||||
log.Infof("[DP] skipped already parsed %d", demo.MatchID)
|
||||
dp.Done <- demo
|
||||
continue
|
||||
}
|
||||
@@ -233,17 +215,17 @@ workloop:
|
||||
fDemo, err := demo.download()
|
||||
if err != nil {
|
||||
if errors.Is(err, DemoNotFoundError{}) {
|
||||
err = utils.Rollback(tx, err)
|
||||
_ = utils.Rollback(tx, err)
|
||||
if tMatch.Date.Before(time.Now().UTC().AddDate(0, 0, -30)) {
|
||||
log.Infof("[DP] demo expired for match %d", tMatch.ID)
|
||||
} else {
|
||||
log.Infof("[DP] demo 404 not found for match %d. Retrying later.", demo.MatchId)
|
||||
log.Infof("[DP] demo 404 not found for match %d. Retrying later.", demo.MatchID)
|
||||
}
|
||||
dp.Done <- demo
|
||||
continue
|
||||
} else {
|
||||
err = utils.Rollback(tx, err)
|
||||
log.Errorf("[DP] Unable to download demo for %d: %v", demo.MatchId, err)
|
||||
log.Errorf("[DP] Unable to download demo for %d: %v", demo.MatchID, err)
|
||||
dp.Done <- demo
|
||||
continue
|
||||
}
|
||||
@@ -253,7 +235,7 @@ workloop:
|
||||
tStats, err := tMatch.QueryStats().WithPlayers().All(context.Background())
|
||||
if err != nil {
|
||||
err = utils.Rollback(tx, err)
|
||||
log.Errorf("[DP] Failed to find players for match %d: %v", demo.MatchId, err)
|
||||
log.Errorf("[DP] Failed to find players for match %d: %v", demo.MatchID, err)
|
||||
dp.Done <- demo
|
||||
continue
|
||||
}
|
||||
@@ -274,7 +256,7 @@ workloop:
|
||||
spays := make([]*Sprays, 0)
|
||||
|
||||
cfg := demoinfocs.DefaultParserConfig
|
||||
if len(demo.DecryptionKey) == 16 {
|
||||
if len(demo.DecryptionKey) == 16 { //nolint:gomnd
|
||||
cfg.NetMessageDecryptionKey = demo.DecryptionKey
|
||||
}
|
||||
demoParser := demoinfocs.NewParserWithConfig(fDemo, cfg)
|
||||
@@ -484,7 +466,7 @@ workloop:
|
||||
Exec(context.Background())
|
||||
if err != nil {
|
||||
err = utils.Rollback(tx, err)
|
||||
log.Errorf("[DP] Unable to update match %d in database: %v", demo.MatchId, err)
|
||||
log.Errorf("[DP] Unable to update match %d in database: %v", demo.MatchID, err)
|
||||
dp.Done <- demo
|
||||
continue
|
||||
}
|
||||
@@ -522,7 +504,13 @@ workloop:
|
||||
}
|
||||
|
||||
for _, eqDmg := range eqMap[tMatchPlayer.PlayerStats] {
|
||||
err = tx.Weapon.Create().SetStat(nMatchPLayer).SetDmg(eqDmg.Dmg).SetVictim(eqDmg.To).SetHitGroup(eqDmg.HitGroup).SetEqType(eqDmg.Eq).Exec(context.Background())
|
||||
err = tx.Weapon.Create().
|
||||
SetStat(nMatchPLayer).
|
||||
SetDmg(eqDmg.Dmg).
|
||||
SetVictim(eqDmg.To).
|
||||
SetHitGroup(eqDmg.HitGroup).
|
||||
SetEqType(eqDmg.Eq).
|
||||
Exec(context.Background())
|
||||
if err != nil {
|
||||
err = utils.Rollback(tx, err)
|
||||
log.Errorf("[DP] Unable to create WeaponStat: %v", err)
|
||||
@@ -532,7 +520,13 @@ workloop:
|
||||
}
|
||||
|
||||
for _, eco := range ecoMap[tMatchPlayer.PlayerStats] {
|
||||
err := tx.RoundStats.Create().SetMatchPlayer(nMatchPLayer).SetRound(uint(eco.Round)).SetBank(uint(eco.Bank)).SetEquipment(uint(eco.EqV)).SetSpent(uint(eco.Spent)).Exec(context.Background())
|
||||
err := tx.RoundStats.Create().
|
||||
SetMatchPlayer(nMatchPLayer).
|
||||
SetRound(uint(eco.Round)).
|
||||
SetBank(uint(eco.Bank)).
|
||||
SetEquipment(uint(eco.EqV)).
|
||||
SetSpent(uint(eco.Spent)).
|
||||
Exec(context.Background())
|
||||
if err != nil {
|
||||
err = utils.Rollback(tx, err)
|
||||
log.Errorf("[DP] Unable to create RoundStat: %v", err)
|
||||
@@ -554,7 +548,11 @@ workloop:
|
||||
continue workloop
|
||||
}
|
||||
|
||||
err = tx.Spray.Create().SetMatchPlayers(nMatchPLayer).SetWeapon(spray.Weapon).SetSpray(sprayBuf.Bytes()).Exec(context.Background())
|
||||
err = tx.Spray.Create().
|
||||
SetMatchPlayers(nMatchPLayer).
|
||||
SetWeapon(spray.Weapon).
|
||||
SetSpray(sprayBuf.Bytes()).
|
||||
Exec(context.Background())
|
||||
if err != nil {
|
||||
err = utils.Rollback(tx, err)
|
||||
log.Warningf("[DP] Failure adding spray to database: %v", err)
|
||||
@@ -567,7 +565,11 @@ workloop:
|
||||
|
||||
var bulk []*ent.MessagesCreate
|
||||
for _, msg := range tMatchPlayer.Edges.Messages {
|
||||
bulk = append(bulk, tx.Messages.Create().SetMessage(msg.Message).SetAllChat(msg.AllChat).SetTick(msg.Tick).SetMatchPlayer(tMatchPlayer))
|
||||
bulk = append(bulk, tx.Messages.Create().
|
||||
SetMessage(msg.Message).
|
||||
SetAllChat(msg.AllChat).
|
||||
SetTick(msg.Tick).
|
||||
SetMatchPlayer(tMatchPlayer))
|
||||
}
|
||||
if len(bulk) > 0 {
|
||||
err = tx.Messages.CreateBulk(bulk...).Exec(context.Background())
|
||||
@@ -587,11 +589,11 @@ workloop:
|
||||
continue
|
||||
}
|
||||
|
||||
log.Infof("[DP] parsed match %d (took %s/%s)", demo.MatchId, downloadTime, time.Since(startTime))
|
||||
log.Infof("[DP] parsed match %d (took %s/%s)", demo.MatchID, downloadTime, time.Since(startTime))
|
||||
|
||||
err = demoParser.Close()
|
||||
if err != nil {
|
||||
log.Errorf("[DP] Unable close demo file for match %d: %v", demo.MatchId, err)
|
||||
log.Errorf("[DP] Unable close demo file for match %d: %v", demo.MatchID, err)
|
||||
}
|
||||
dp.Done <- demo
|
||||
}
|
||||
|
Reference in New Issue
Block a user