refactored some functions, added demo download retry
This commit is contained in:
116
utils/utils.go
116
utils/utils.go
@@ -2,7 +2,6 @@ package utils
|
||||
|
||||
import (
|
||||
"context"
|
||||
"csgowtfd/csgo"
|
||||
"csgowtfd/ent"
|
||||
"csgowtfd/ent/match"
|
||||
"csgowtfd/ent/player"
|
||||
@@ -232,7 +231,9 @@ func getNextShareCode(lastCode string, apiKey string, authCode string, steamId u
|
||||
return "", fmt.Errorf("bad response from steam api (HTTP %d)", r.StatusCode)
|
||||
}
|
||||
|
||||
defer r.Body.Close()
|
||||
defer func(Body io.ReadCloser) {
|
||||
_ = Body.Close()
|
||||
}(r.Body)
|
||||
bJson, err := ioutil.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
return "", err
|
||||
@@ -319,104 +320,6 @@ func GetPlayerFromSteamID64(db *DBWithLock, steamID uint64, apiKey string, rl ra
|
||||
}
|
||||
}
|
||||
|
||||
func GCInfoParser(channel chan *csgo.Demo, dl *csgo.DemoMatchLoader, dp *csgo.DemoParser, db *DBWithLock, apiKey string, rl ratelimit.Limiter) {
|
||||
for {
|
||||
select {
|
||||
case demo := <-channel:
|
||||
if !dl.GCReady {
|
||||
time.Sleep(5 * time.Second)
|
||||
channel <- demo
|
||||
}
|
||||
|
||||
matchId, _, _, err := csgo.DecodeSharecode(demo.ShareCode)
|
||||
Check(err)
|
||||
if matchId == 0 {
|
||||
log.Warningf("Can't parse match with sharecode %s", demo.ShareCode)
|
||||
continue
|
||||
}
|
||||
|
||||
db.Lock.RLock()
|
||||
iMatch, err := db.Client.Match.Query().Where(match.ID(matchId)).Only(context.Background())
|
||||
db.Lock.RUnlock()
|
||||
if err != nil {
|
||||
switch e := err.(type) {
|
||||
case *ent.NotFoundError:
|
||||
break
|
||||
default:
|
||||
Check(e)
|
||||
}
|
||||
} else {
|
||||
if iMatch.DemoParsed == false && !iMatch.DemoExpired {
|
||||
log.Infof("Match %d is loaded, but not parsed. Try parsing.", demo.MatchId)
|
||||
demo.MatchId = matchId
|
||||
demo.Url = iMatch.ReplayURL
|
||||
dp.ParseDemo(demo)
|
||||
continue
|
||||
}
|
||||
|
||||
log.Debugf("Skipped match %d: already parsed", matchId)
|
||||
continue
|
||||
}
|
||||
|
||||
matchDetails, err := dl.GetMatchDetails(demo.ShareCode)
|
||||
Check(err)
|
||||
matchZero := matchDetails.GetMatches()[0]
|
||||
lastRound := matchZero.GetRoundstatsall()[len(matchZero.Roundstatsall)-1]
|
||||
var players []*ent.Player
|
||||
|
||||
for _, accountId := range lastRound.GetReservation().GetAccountIds() {
|
||||
tPlayer, err := GetPlayer(db, csgo.AccountId2SteamId(accountId), apiKey, rl)
|
||||
Check(err)
|
||||
players = append(players, tPlayer)
|
||||
}
|
||||
|
||||
demo.Url = lastRound.GetMap()
|
||||
demo.MatchId = matchZero.GetMatchid()
|
||||
|
||||
db.Lock.Lock()
|
||||
tMatch, err := db.Client.Match.Create().
|
||||
SetID(matchZero.GetMatchid()).
|
||||
AddPlayers(players...).
|
||||
SetDate(time.Unix(int64(matchZero.GetMatchtime()), 0).UTC()).
|
||||
SetMaxRounds(int(lastRound.GetMaxRounds())).
|
||||
SetDuration(int(lastRound.GetMatchDuration())).
|
||||
SetShareCode(demo.ShareCode).
|
||||
SetReplayURL(lastRound.GetMap()).
|
||||
SetScoreTeamA(int(lastRound.GetTeamScores()[0])).
|
||||
SetScoreTeamB(int(lastRound.GetTeamScores()[1])).
|
||||
SetMatchResult(int(lastRound.GetMatchResult())).
|
||||
Save(context.Background())
|
||||
db.Lock.Unlock()
|
||||
Check(err)
|
||||
|
||||
for i, mPlayer := range players {
|
||||
var teamId int
|
||||
if i > 4 {
|
||||
teamId = 2
|
||||
} else {
|
||||
teamId = 1
|
||||
}
|
||||
db.Lock.Lock()
|
||||
err := db.Client.Stats.Create().
|
||||
SetMatches(tMatch).
|
||||
SetPlayers(mPlayer).
|
||||
SetTeamID(teamId).
|
||||
SetKills(int(lastRound.GetKills()[i])).
|
||||
SetDeaths(int(lastRound.GetDeaths()[i])).
|
||||
SetAssists(int(lastRound.GetAssists()[i])).
|
||||
SetMvp(int(lastRound.GetMvps()[i])).
|
||||
SetScore(int(lastRound.GetScores()[i])).
|
||||
SetHeadshot(int(lastRound.GetEnemyHeadshots()[i])).
|
||||
Exec(context.Background())
|
||||
db.Lock.Unlock()
|
||||
Check(err)
|
||||
}
|
||||
|
||||
dp.ParseDemo(demo)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func SteamProfile2XML(id string, steamID64 uint64) (*CommunityXML, error) {
|
||||
var r *http.Response
|
||||
var err error
|
||||
@@ -425,10 +328,11 @@ func SteamProfile2XML(id string, steamID64 uint64) (*CommunityXML, error) {
|
||||
} else {
|
||||
r, err = http.Get(fmt.Sprintf(steamVanityURLEntry, id))
|
||||
}
|
||||
Check(err)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer func(Body io.ReadCloser) {
|
||||
err := Body.Close()
|
||||
Check(err)
|
||||
_ = Body.Close()
|
||||
}(r.Body)
|
||||
|
||||
body, err := ioutil.ReadAll(r.Body)
|
||||
@@ -480,9 +384,3 @@ func UpdatePlayerFromSteam(player *ent.Player, apiKey string, lock *sync.RWMutex
|
||||
|
||||
return tPlayer, nil
|
||||
}
|
||||
|
||||
func Check(e error) {
|
||||
if e != nil {
|
||||
panic(e)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user