import from unified repo
This commit is contained in:
262
csgo/demo_loader.go
Normal file
262
csgo/demo_loader.go
Normal file
@@ -0,0 +1,262 @@
|
||||
package csgo
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"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"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"google.golang.org/protobuf/proto"
|
||||
"io/ioutil"
|
||||
"math/rand"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
//goland:noinspection SpellCheckingInspection
|
||||
const (
|
||||
SENTRYFILE = ".sentry"
|
||||
LOGINKEYFILE = ".login_key"
|
||||
SERVERLIST = ".server.json"
|
||||
APPID = 730
|
||||
)
|
||||
|
||||
type DemoMatchLoader struct {
|
||||
client *steam.Client
|
||||
GCReady bool
|
||||
steamLogin *steam.LogOnDetails
|
||||
matchRecv chan *protobuf.CMsgGCCStrike15V2_MatchList
|
||||
cmList []*netutil.PortAddr
|
||||
}
|
||||
|
||||
func AccountId2SteamId(accId uint32) uint64 {
|
||||
return uint64(accId) + 76561197960265728
|
||||
}
|
||||
|
||||
func SteamId2AccountId(steamId uint64) uint32 {
|
||||
return uint32(steamId - 76561197960265728)
|
||||
}
|
||||
|
||||
func (d *DemoMatchLoader) HandleGCPacket(pkg *gamecoordinator.GCPacket) {
|
||||
switch pkg.MsgType {
|
||||
case uint32(protobuf.EGCBaseClientMsg_k_EMsgGCClientWelcome):
|
||||
msg := &protobuf.CMsgClientWelcome{}
|
||||
err := proto.Unmarshal(pkg.Body, msg)
|
||||
if err != nil {
|
||||
log.Errorf("[DL] Unable to unmarshal event %v: %v", pkg.MsgType, err)
|
||||
}
|
||||
log.Debugf("[GC] Welcome: %+v", msg)
|
||||
d.GCReady = true
|
||||
case uint32(protobuf.EGCBaseClientMsg_k_EMsgGCClientConnectionStatus):
|
||||
msg := &protobuf.CMsgConnectionStatus{}
|
||||
err := proto.Unmarshal(pkg.Body, msg)
|
||||
if err != nil {
|
||||
log.Errorf("[DL] Unable to unmarshal event %v: %v", pkg.MsgType, err)
|
||||
}
|
||||
|
||||
log.Debugf("[GC] Status: %+v", msg)
|
||||
if msg.GetStatus() != protobuf.GCConnectionStatus_GCConnectionStatus_HAVE_SESSION {
|
||||
d.GCReady = false
|
||||
go d.greetGC()
|
||||
}
|
||||
case uint32(protobuf.ECsgoGCMsg_k_EMsgGCCStrike15_v2_GC2ClientGlobalStats):
|
||||
msg := &protobuf.GlobalStatistics{}
|
||||
err := proto.Unmarshal(pkg.Body, msg)
|
||||
if err != nil {
|
||||
log.Errorf("[DL] Unable to unmarshal event %v: %v", pkg.MsgType, err)
|
||||
}
|
||||
log.Debugf("[GC] Stats: %+v", msg)
|
||||
d.GCReady = true
|
||||
case uint32(protobuf.ECsgoGCMsg_k_EMsgGCCStrike15_v2_MatchList):
|
||||
msg := &protobuf.CMsgGCCStrike15V2_MatchList{}
|
||||
err := proto.Unmarshal(pkg.Body, msg)
|
||||
if err != nil {
|
||||
log.Errorf("[DL] Unable to unmarshal event %v: %v", pkg.MsgType, err)
|
||||
}
|
||||
d.matchRecv <- msg
|
||||
default:
|
||||
log.Debugf("[GC] Unhandled GC message: %+v", pkg)
|
||||
}
|
||||
}
|
||||
|
||||
func (d *DemoMatchLoader) GetMatchDetails(sharecode string) (*protobuf.CMsgGCCStrike15V2_MatchList, error) {
|
||||
if !d.GCReady {
|
||||
return nil, fmt.Errorf("gc not ready")
|
||||
}
|
||||
|
||||
matchId, outcomeId, tokenId, err := DecodeSharecode(sharecode)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = d.RequestDemoInfo(matchId, outcomeId, tokenId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for {
|
||||
select {
|
||||
case match := <-d.matchRecv:
|
||||
if *match.Matches[0].Matchid == matchId {
|
||||
return match, nil
|
||||
} else {
|
||||
d.matchRecv <- match
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (d *DemoMatchLoader) getRandomCM() *netutil.PortAddr {
|
||||
return d.cmList[rand.Intn(len(d.cmList))]
|
||||
}
|
||||
|
||||
func (d *DemoMatchLoader) connectToSteam() error {
|
||||
if d.cmList != nil {
|
||||
err := d.client.ConnectTo(d.getRandomCM())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
_, err := d.client.Connect()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *DemoMatchLoader) Setup(username string) error {
|
||||
d.steamLogin = new(steam.LogOnDetails)
|
||||
d.steamLogin.Username = username
|
||||
d.steamLogin.Password = os.Getenv("STEAM_PASSWORD")
|
||||
d.steamLogin.ShouldRememberPassword = true
|
||||
|
||||
if _, err := os.Stat(SENTRYFILE); err == nil {
|
||||
hash, err := ioutil.ReadFile(SENTRYFILE)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
d.steamLogin.SentryFileHash = hash
|
||||
}
|
||||
|
||||
if _, err := os.Stat(LOGINKEYFILE); err == nil {
|
||||
hash, err := ioutil.ReadFile(LOGINKEYFILE)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
d.steamLogin.LoginKey = string(hash)
|
||||
}
|
||||
|
||||
if _, err := os.Stat(SERVERLIST); err == nil {
|
||||
rawJson, err := ioutil.ReadFile(SERVERLIST)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = json.Unmarshal(rawJson, &d.cmList)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
d.client = steam.NewClient()
|
||||
d.matchRecv = make(chan *protobuf.CMsgGCCStrike15V2_MatchList, 500)
|
||||
|
||||
go d.connectLoop()
|
||||
go d.steamEventHandler()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d DemoMatchLoader) connectLoop() {
|
||||
for d.connectToSteam() != nil {
|
||||
log.Infof("Retrying connecting to steam")
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
}
|
||||
|
||||
func (d *DemoMatchLoader) steamEventHandler() {
|
||||
for event := range d.client.Events() {
|
||||
switch e := event.(type) {
|
||||
case *steam.ConnectedEvent:
|
||||
log.Debug("[DL] Connected!")
|
||||
d.client.Auth.LogOn(d.steamLogin)
|
||||
case *steam.ClientCMListEvent:
|
||||
log.Debug("[DL] CM list")
|
||||
serverJson, err := json.Marshal(e.Addresses)
|
||||
if err != nil {
|
||||
log.Errorf("[DL] Unable to marshal JSON: %v", err)
|
||||
}
|
||||
|
||||
err = ioutil.WriteFile(SERVERLIST, serverJson, os.ModePerm)
|
||||
if err != nil {
|
||||
log.Errorf("[DL] Unable to write server list: %v", err)
|
||||
}
|
||||
case *steam.MachineAuthUpdateEvent:
|
||||
log.Debug("[DL] Got sentry!")
|
||||
err := ioutil.WriteFile(SENTRYFILE, e.Hash, os.ModePerm)
|
||||
if err != nil {
|
||||
log.Errorf("[DL] Unable write sentry file: %v", err)
|
||||
}
|
||||
case *steam.LoggedOnEvent:
|
||||
log.Debug("[DL] Login successfully!")
|
||||
d.client.Social.SetPersonaState(steamlang.EPersonaState_Online)
|
||||
go d.SetPlaying()
|
||||
case *steam.LogOnFailedEvent:
|
||||
log.Warningf("[DL] Steam login denied: %+v", e)
|
||||
log.Warningf("[DL] Asking for auth code now, please provide on stdin.")
|
||||
scanner := bufio.NewScanner(os.Stdin)
|
||||
scanner.Scan()
|
||||
d.steamLogin.AuthCode = scanner.Text()
|
||||
case *steam.DisconnectedEvent:
|
||||
log.Warningf("Steam disconnected, trying to reconnect...")
|
||||
_, err := d.client.Connect()
|
||||
if err != nil {
|
||||
log.Errorf("[DL] Unable to reconnect to steam: %v", err)
|
||||
}
|
||||
case *steam.LoginKeyEvent:
|
||||
log.Debug("Got login_key!")
|
||||
err := ioutil.WriteFile(LOGINKEYFILE, []byte(e.LoginKey), os.ModePerm)
|
||||
if err != nil {
|
||||
log.Errorf("[DL] Unable write login_key: %v", err)
|
||||
}
|
||||
case steam.FatalErrorEvent:
|
||||
log.Debugf("[DL] Got FatalError %+v", e)
|
||||
case error:
|
||||
log.Fatalf("[DL] Got error %+v", e)
|
||||
default:
|
||||
log.Debugf("[DL] %T: %v", e, e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (d *DemoMatchLoader) SetPlaying() {
|
||||
d.client.GC.SetGamesPlayed(APPID)
|
||||
d.client.GC.RegisterPacketHandler(d)
|
||||
go d.greetGC()
|
||||
}
|
||||
|
||||
func (d *DemoMatchLoader) greetGC() {
|
||||
for !d.GCReady {
|
||||
log.Debugf("[DL] Sending GC greeting")
|
||||
msg := protobuf.CMsgClientHello{}
|
||||
d.client.GC.Write(gamecoordinator.NewGCMsgProtobuf(APPID, uint32(protobuf.EGCBaseClientMsg_k_EMsgGCClientHello), &msg))
|
||||
time.Sleep(500 * time.Millisecond)
|
||||
}
|
||||
}
|
||||
|
||||
func (d *DemoMatchLoader) RequestDemoInfo(matchId uint64, conclusionId uint64, tokenId uint32) error {
|
||||
if !d.GCReady {
|
||||
return fmt.Errorf("gc not ready")
|
||||
}
|
||||
|
||||
msg := protobuf.CMsgGCCStrike15V2_MatchListRequestFullGameInfo{Matchid: &matchId,
|
||||
Outcomeid: &conclusionId,
|
||||
Token: &tokenId}
|
||||
|
||||
d.client.GC.Write(gamecoordinator.NewGCMsgProtobuf(APPID, uint32(protobuf.ECsgoGCMsg_k_EMsgGCCStrike15_v2_MatchListRequestFullGameInfo), &msg))
|
||||
|
||||
return nil
|
||||
}
|
321
csgo/demo_parser.go
Normal file
321
csgo/demo_parser.go
Normal file
@@ -0,0 +1,321 @@
|
||||
package csgo
|
||||
|
||||
import (
|
||||
"compress/bzip2"
|
||||
"context"
|
||||
"csgowtfd/ent"
|
||||
"csgowtfd/ent/match"
|
||||
"csgowtfd/ent/player"
|
||||
"fmt"
|
||||
"github.com/markus-wa/demoinfocs-golang/v2/pkg/demoinfocs"
|
||||
"github.com/markus-wa/demoinfocs-golang/v2/pkg/demoinfocs/common"
|
||||
"github.com/markus-wa/demoinfocs-golang/v2/pkg/demoinfocs/events"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"io"
|
||||
"net/http"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Demo struct {
|
||||
ShareCode string
|
||||
MatchId uint64
|
||||
Url string
|
||||
Rank int
|
||||
Tickrate int
|
||||
File string
|
||||
}
|
||||
|
||||
type DemoParser struct {
|
||||
demoQueue chan *Demo
|
||||
tempDir string
|
||||
}
|
||||
|
||||
type DemoNotFoundError struct {
|
||||
error
|
||||
}
|
||||
|
||||
func (p *DemoParser) Setup(db *ent.Client, lock *sync.RWMutex) error {
|
||||
p.demoQueue = make(chan *Demo, 1000)
|
||||
go p.parseWorker(db, lock)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *DemoParser) ParseDemo(demo *Demo) error {
|
||||
select {
|
||||
case p.demoQueue <- demo:
|
||||
return nil
|
||||
default:
|
||||
return fmt.Errorf("queue full")
|
||||
}
|
||||
}
|
||||
|
||||
func (p *DemoParser) downloadReplay(demo *Demo) (io.Reader, error) {
|
||||
log.Debugf("[DP] Downloading replay for %d", demo.MatchId)
|
||||
|
||||
r, err := http.Get(demo.Url)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if r.StatusCode != http.StatusOK {
|
||||
return nil, DemoNotFoundError{fmt.Errorf("demo not found")}
|
||||
}
|
||||
return bzip2.NewReader(r.Body), nil
|
||||
|
||||
}
|
||||
|
||||
func (p *DemoParser) getDBPlayer(db *ent.Client, lock *sync.RWMutex, demo *Demo, demoPlayer *common.Player) (*ent.Stats, error) {
|
||||
lock.RLock()
|
||||
tMatchPlayer, err := db.Stats.Query().WithMatches(func(q *ent.MatchQuery) {
|
||||
q.Where(match.MatchID(demo.MatchId))
|
||||
}).WithPlayers(func(q *ent.PlayerQuery) {
|
||||
q.Where(player.Steamid(demoPlayer.SteamID64))
|
||||
}).Only(context.Background())
|
||||
lock.RUnlock()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return tMatchPlayer, nil
|
||||
}
|
||||
|
||||
func (p *DemoParser) getMatchPlayerBySteamID(stats []*ent.Stats, steamId uint64) *ent.Stats {
|
||||
for _, tStats := range stats {
|
||||
tPLayer, err := tStats.Edges.PlayersOrErr()
|
||||
if err != nil {
|
||||
log.Errorf("Unbale to get Stats from statList: %v", err)
|
||||
return nil
|
||||
}
|
||||
if tPLayer.Steamid == steamId {
|
||||
return tStats
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *DemoParser) parseWorker(db *ent.Client, lock *sync.RWMutex) {
|
||||
for {
|
||||
select {
|
||||
case demo := <-p.demoQueue:
|
||||
if demo.MatchId == 0 {
|
||||
log.Warningf("[DP] can't parse match %s: no matchid found", demo.ShareCode)
|
||||
continue
|
||||
}
|
||||
|
||||
lock.RLock()
|
||||
tMatch, err := db.Match.Query().Where(match.MatchID(demo.MatchId)).Only(context.Background())
|
||||
lock.RUnlock()
|
||||
if err != nil {
|
||||
log.Errorf("[DP] Unable to get match %d: %v", demo.MatchId, err)
|
||||
continue
|
||||
}
|
||||
|
||||
if tMatch.DemoParsed {
|
||||
log.Infof("[DP] skipped already parsed %d", demo.MatchId)
|
||||
continue
|
||||
}
|
||||
|
||||
startTime := time.Now()
|
||||
fDemo, err := p.downloadReplay(demo)
|
||||
if err != nil {
|
||||
switch e := err.(type) {
|
||||
case DemoNotFoundError:
|
||||
err := tMatch.Update().SetDemoExpired(true).Exec(context.Background())
|
||||
if err != nil {
|
||||
log.Errorf("[DP] Unable to set demo expire for match %d: %v", demo.MatchId, e)
|
||||
continue
|
||||
}
|
||||
log.Warningf("[DP] Demo already expired for %d", demo.MatchId)
|
||||
continue
|
||||
default:
|
||||
log.Warningf("[DP] Unable to download demo for %d: %v", demo.MatchId, e)
|
||||
continue
|
||||
}
|
||||
}
|
||||
downloadTime := time.Now().Sub(startTime)
|
||||
|
||||
lock.RLock()
|
||||
tStats, err := tMatch.QueryStats().WithPlayers().All(context.Background())
|
||||
lock.RUnlock()
|
||||
if err != nil {
|
||||
log.Errorf("[DP] Failed to find players for match %d: %v", demo.MatchId, err)
|
||||
continue
|
||||
}
|
||||
|
||||
killMap := make(map[uint64]int, 10)
|
||||
gameStarted := false
|
||||
demoParser := demoinfocs.NewParser(fDemo)
|
||||
|
||||
// onPlayerHurt
|
||||
demoParser.RegisterEventHandler(func(e events.PlayerHurt) {
|
||||
if e.Attacker == nil || e.Player == nil || !gameStarted {
|
||||
return
|
||||
}
|
||||
|
||||
tAttacker := p.getMatchPlayerBySteamID(tStats, e.Attacker.SteamID64)
|
||||
if e.Attacker.Team == e.Player.Team {
|
||||
tAttacker.Extended.Dmg.Team += e.HealthDamageTaken
|
||||
return
|
||||
} else {
|
||||
tAttacker.Extended.Dmg.Enemy += e.HealthDamageTaken
|
||||
}
|
||||
|
||||
switch e.Weapon.Type {
|
||||
case common.EqDecoy:
|
||||
tAttacker.Extended.Dmg.UD.Decoy += e.HealthDamageTaken
|
||||
case common.EqSmoke:
|
||||
tAttacker.Extended.Dmg.UD.Smoke += e.HealthDamageTaken
|
||||
case common.EqHE:
|
||||
tAttacker.Extended.Dmg.UD.HE += e.HealthDamageTaken
|
||||
case common.EqMolotov, common.EqIncendiary:
|
||||
tAttacker.Extended.Dmg.UD.Flames += e.HealthDamageTaken
|
||||
case common.EqFlash:
|
||||
tAttacker.Extended.Dmg.UD.Flash += e.HealthDamageTaken
|
||||
}
|
||||
|
||||
switch e.HitGroup {
|
||||
case events.HitGroupHead:
|
||||
tAttacker.Extended.Dmg.HitGroup.Head += e.HealthDamageTaken
|
||||
case events.HitGroupChest:
|
||||
tAttacker.Extended.Dmg.HitGroup.Chest += e.HealthDamageTaken
|
||||
case events.HitGroupStomach:
|
||||
tAttacker.Extended.Dmg.HitGroup.Stomach += e.HealthDamageTaken
|
||||
case events.HitGroupLeftArm:
|
||||
tAttacker.Extended.Dmg.HitGroup.LeftArm += e.HealthDamageTaken
|
||||
case events.HitGroupRightArm:
|
||||
tAttacker.Extended.Dmg.HitGroup.RightArm += e.HealthDamageTaken
|
||||
case events.HitGroupLeftLeg:
|
||||
tAttacker.Extended.Dmg.HitGroup.LeftLeg += e.HealthDamageTaken
|
||||
case events.HitGroupRightLeg:
|
||||
tAttacker.Extended.Dmg.HitGroup.RightLeg += e.HealthDamageTaken
|
||||
case events.HitGroupGear:
|
||||
tAttacker.Extended.Dmg.HitGroup.Gear += e.HealthDamageTaken
|
||||
}
|
||||
})
|
||||
|
||||
// onKill
|
||||
demoParser.RegisterEventHandler(func(e events.Kill) {
|
||||
|
||||
})
|
||||
|
||||
// onFreezeTimeEnd
|
||||
demoParser.RegisterEventHandler(func(e events.RoundFreezetimeEnd) {
|
||||
|
||||
})
|
||||
|
||||
// onRoundEnd
|
||||
demoParser.RegisterEventHandler(func(e events.RoundEnd) {
|
||||
if gameStarted {
|
||||
for _, IGP := range demoParser.GameState().Participants().Playing() {
|
||||
if IGP != nil && IGP.SteamID64 != 0 {
|
||||
killDiff := IGP.Kills() - killMap[IGP.SteamID64]
|
||||
tPlayer := p.getMatchPlayerBySteamID(tStats, IGP.SteamID64)
|
||||
|
||||
switch killDiff {
|
||||
case 2:
|
||||
tPlayer.Extended.MultiKills.Duo++
|
||||
case 3:
|
||||
tPlayer.Extended.MultiKills.Triple++
|
||||
case 4:
|
||||
tPlayer.Extended.MultiKills.Quad++
|
||||
case 5:
|
||||
tPlayer.Extended.MultiKills.Pent++
|
||||
}
|
||||
killMap[IGP.SteamID64] = IGP.Kills()
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
// onPlayerFlashed
|
||||
demoParser.RegisterEventHandler(func(e events.PlayerFlashed) {
|
||||
if e.Attacker == nil || e.Player == nil || !gameStarted {
|
||||
return
|
||||
}
|
||||
|
||||
tAttacker := p.getMatchPlayerBySteamID(tStats, e.Attacker.SteamID64)
|
||||
|
||||
// team flash
|
||||
if e.Attacker.Team == e.Player.Team && e.Attacker.SteamID64 != e.Player.SteamID64 {
|
||||
tAttacker.Extended.Flash.Total.Team++
|
||||
tAttacker.Extended.Flash.Duration.Team += e.Player.FlashDuration
|
||||
}
|
||||
|
||||
// own flash
|
||||
if e.Attacker.SteamID64 == e.Player.SteamID64 {
|
||||
tAttacker.Extended.Flash.Total.Self++
|
||||
tAttacker.Extended.Flash.Duration.Self += e.Player.FlashDuration
|
||||
}
|
||||
|
||||
// enemy flash
|
||||
if e.Attacker.Team != e.Player.Team {
|
||||
tAttacker.Extended.Flash.Total.Enemy++
|
||||
tAttacker.Extended.Flash.Duration.Enemy += e.Player.FlashDuration
|
||||
}
|
||||
})
|
||||
|
||||
// onMatchStart
|
||||
demoParser.RegisterEventHandler(func(e events.MatchStart) {
|
||||
gs := demoParser.GameState()
|
||||
gameStarted = true
|
||||
|
||||
for _, demoPlayer := range gs.Participants().Playing() {
|
||||
if demoPlayer != nil && demoPlayer.SteamID64 != 0 {
|
||||
tMatchPlayer := p.getMatchPlayerBySteamID(tStats, demoPlayer.SteamID64)
|
||||
|
||||
tMatchPlayer.Extended.Crosshair = demoPlayer.CrosshairCode()
|
||||
tMatchPlayer.Extended.Color = int(demoPlayer.Color())
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
// onMatchEnd?
|
||||
demoParser.RegisterEventHandler(func(e events.AnnouncementWinPanelMatch) {
|
||||
|
||||
})
|
||||
|
||||
// onRankUpdate
|
||||
demoParser.RegisterEventHandler(func(e events.RankUpdate) {
|
||||
if e.Player != nil && e.SteamID64() != 0 {
|
||||
tMatchPlayer := p.getMatchPlayerBySteamID(tStats, e.SteamID64())
|
||||
|
||||
tMatchPlayer.Extended.Rank.Old = e.RankOld
|
||||
tMatchPlayer.Extended.Rank.New = e.RankNew
|
||||
}
|
||||
})
|
||||
|
||||
err = demoParser.ParseToEnd()
|
||||
if err != nil {
|
||||
log.Errorf("[DP] Error parsing replay: %v", err)
|
||||
continue
|
||||
}
|
||||
|
||||
lock.Lock()
|
||||
err = tMatch.Update().SetMap(demoParser.Header().MapName).SetDemoParsed(true).Exec(context.Background())
|
||||
lock.Unlock()
|
||||
if err != nil {
|
||||
log.Errorf("[DP] Unable to update match %d in database: %v", demo.MatchId, err)
|
||||
continue
|
||||
}
|
||||
|
||||
for _, tMatchPlayer := range tStats {
|
||||
lock.Lock()
|
||||
err := tMatchPlayer.Update().SetExtended(tMatchPlayer.Extended).Exec(context.Background())
|
||||
lock.Unlock()
|
||||
if err != nil {
|
||||
log.Errorf("[DP] Unable to update player %d in database: %v", tMatchPlayer.Edges.Players.Steamid, err)
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
log.Infof("[DP] Parsed %d (took %s/%s)", demo.MatchId, downloadTime, time.Now().Sub(startTime))
|
||||
|
||||
err = demoParser.Close()
|
||||
if err != nil {
|
||||
log.Errorf("[DP] Unable close demo file for match %d: %v", demo.MatchId, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
57
csgo/sharecode.go
Normal file
57
csgo/sharecode.go
Normal file
@@ -0,0 +1,57 @@
|
||||
package csgo
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"math/big"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
//goland:noinspection SpellCheckingInspection
|
||||
var DICT = "ABCDEFGHJKLMNOPQRSTUVWXYZabcdefhijkmnopqrstuvwxyz23456789"
|
||||
var sharecodeRexEx = regexp.MustCompile("^CSGO(?:-?[\\w]{5}){5}$")
|
||||
|
||||
func DecodeSharecode(code string) (uint64, uint64, uint32, error) {
|
||||
if !sharecodeRexEx.MatchString(code) {
|
||||
return 0, 0, 0, fmt.Errorf("not a CSGO sharecode: %s", code)
|
||||
}
|
||||
|
||||
cleanCode := strings.ReplaceAll(code, "CSGO", "")
|
||||
cleanCode = strings.ReplaceAll(cleanCode, "-", "")
|
||||
|
||||
chars := ReverseString(strings.Split(cleanCode, ""))
|
||||
bigInt := new(big.Int)
|
||||
dictLenBig := big.NewInt(int64(len(DICT)))
|
||||
|
||||
for _, c := range chars {
|
||||
bigInt.Mul(bigInt, dictLenBig)
|
||||
bigInt.Add(bigInt, big.NewInt(int64(strings.Index(DICT, c))))
|
||||
}
|
||||
|
||||
bytes := make([]byte, 18)
|
||||
bigInt.FillBytes(bytes)
|
||||
|
||||
matchId := new(big.Int)
|
||||
matchId.SetString(hex.EncodeToString(Reverse(bytes[0:8])), 16)
|
||||
outcomeId := new(big.Int)
|
||||
outcomeId.SetString(hex.EncodeToString(Reverse(bytes[8:16])), 16)
|
||||
tokenId := new(big.Int)
|
||||
tokenId.SetString(hex.EncodeToString(Reverse(bytes[16:18])), 16)
|
||||
|
||||
return matchId.Uint64(), outcomeId.Uint64(), uint32(tokenId.Uint64()), nil
|
||||
}
|
||||
|
||||
func Reverse(numbers []byte) []byte {
|
||||
for i, j := 0, len(numbers)-1; i < j; i, j = i+1, j-1 {
|
||||
numbers[i], numbers[j] = numbers[j], numbers[i]
|
||||
}
|
||||
return numbers
|
||||
}
|
||||
|
||||
func ReverseString(numbers []string) []string {
|
||||
for i, j := 0, len(numbers)-1; i < j; i, j = i+1, j-1 {
|
||||
numbers[i], numbers[j] = numbers[j], numbers[i]
|
||||
}
|
||||
return numbers
|
||||
}
|
29
csgo/sharecode_test.go
Normal file
29
csgo/sharecode_test.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package csgo
|
||||
|
||||
import "testing"
|
||||
|
||||
//goland:noinspection SpellCheckingInspection
|
||||
func TestSharecode(t *testing.T) {
|
||||
eMatchId := uint64(3505575050994516382)
|
||||
eOutcomeId := uint64(3505581094013501947)
|
||||
eTokenId := uint32(12909)
|
||||
|
||||
matchId, outcomeId, tokenId, err := DecodeSharecode("CSGO-P9k3F-eVL9n-LZLXN-DrBGF-VKD7K")
|
||||
if err != nil {
|
||||
t.Log("error should be nil", err)
|
||||
t.Fail()
|
||||
}
|
||||
|
||||
if matchId != eMatchId {
|
||||
t.Logf("matchID should be %d, is %d", eMatchId, matchId)
|
||||
t.Fail()
|
||||
}
|
||||
if outcomeId != eOutcomeId {
|
||||
t.Logf("outcomeID should be %d, is %d", eOutcomeId, outcomeId)
|
||||
t.Fail()
|
||||
}
|
||||
if tokenId != eTokenId {
|
||||
t.Logf("tokenID should be %d, is %d", eTokenId, tokenId)
|
||||
t.Fail()
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user