fixed match win/loss/ties
This commit is contained in:
@@ -9,6 +9,7 @@ import (
|
||||
"csgowtfd/ent/stats"
|
||||
"encoding/json"
|
||||
"encoding/xml"
|
||||
"entgo.io/ent/dialect/sql"
|
||||
"fmt"
|
||||
"github.com/Philipp15b/go-steamapi"
|
||||
log "github.com/sirupsen/logrus"
|
||||
@@ -99,52 +100,51 @@ func SendJSON(data interface{}, w http.ResponseWriter) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetMatchStats(db *ent.Client, lock *sync.RWMutex, player *ent.Player) (int, int, int, error) {
|
||||
var v []struct {
|
||||
Wins int `json:"wins"`
|
||||
Ties int `json:"ties"`
|
||||
Total int `json:"total"`
|
||||
func GetMatchStats(db *ent.Client, lock *sync.RWMutex, dbPlayer *ent.Player) (int, int, int, error) {
|
||||
var res []struct {
|
||||
MatchResult int `json:"match_result"`
|
||||
Count int `json:"count"`
|
||||
}
|
||||
err := player.QueryMatches().
|
||||
WithStats().
|
||||
Where(
|
||||
match.Or(
|
||||
match.And(
|
||||
match.HasStatsWith(stats.TeamIDEQ(1)),
|
||||
match.MatchResultEQ(1)),
|
||||
match.And(
|
||||
match.HasStatsWith(stats.TeamIDEQ(2)),
|
||||
match.MatchResultEQ(2))),
|
||||
).
|
||||
GroupBy(match.FieldMatchID).
|
||||
Aggregate(ent.As(ent.Count(), "wins")).
|
||||
Scan(context.Background(), &v)
|
||||
|
||||
lock.RLock()
|
||||
err := dbPlayer.QueryMatches().GroupBy(match.FieldMatchResult).Aggregate(func(s *sql.Selector) string {
|
||||
sT := sql.Table(stats.Table)
|
||||
s.Join(sT).On(s.C(match.FieldID), sT.C(stats.MatchesColumn))
|
||||
s.Where(sql.And(sql.Or(sql.ColumnsEQ(match.FieldMatchResult, stats.FieldTeamID), sql.EQ(s.C(match.FieldMatchResult), 0)), sql.EQ(sT.C(stats.PlayersColumn), dbPlayer.ID)))
|
||||
return sql.Count("*")
|
||||
}).Scan(context.Background(), &res)
|
||||
lock.RUnlock()
|
||||
if err != nil {
|
||||
return 0, 0, 0, err
|
||||
}
|
||||
|
||||
err = player.QueryMatches().
|
||||
Where(match.MatchResultEQ(0)).
|
||||
GroupBy(match.FieldMatchID).
|
||||
Aggregate(ent.As(ent.Count(), "ties")).
|
||||
Scan(context.Background(), &v)
|
||||
lock.RLock()
|
||||
total, err := dbPlayer.QueryMatches().Modify(func(s *sql.Selector) {
|
||||
s.Select("COUNT(*)")
|
||||
}).Int(context.Background())
|
||||
lock.RUnlock()
|
||||
if err != nil {
|
||||
return 0, 0, 0, err
|
||||
}
|
||||
|
||||
err = player.QueryMatches().
|
||||
GroupBy(match.FieldMatchID).
|
||||
Aggregate(ent.As(ent.Count(), "total")).
|
||||
Scan(context.Background(), &v)
|
||||
if err != nil {
|
||||
return 0, 0, 0, err
|
||||
}
|
||||
|
||||
if len(v) < 1 {
|
||||
if len(res) < 1 {
|
||||
return 0, 0, 0, nil
|
||||
}
|
||||
var (
|
||||
wins int
|
||||
ties int
|
||||
)
|
||||
|
||||
return v[0].Wins, v[0].Ties, v[0].Total - v[0].Wins - v[0].Ties, nil
|
||||
for _, r := range res {
|
||||
switch r.MatchResult {
|
||||
case 0:
|
||||
ties = r.Count
|
||||
case 1, 2:
|
||||
wins += r.Count
|
||||
}
|
||||
}
|
||||
|
||||
return wins, ties, total - wins - ties, nil
|
||||
}
|
||||
|
||||
func IsAuthCodeValid(player *ent.Player, lock *sync.RWMutex, apiKey string, shareCode string, authCode string, rl ratelimit.Limiter) (bool, error) {
|
||||
@@ -158,13 +158,13 @@ func IsAuthCodeValid(player *ent.Player, lock *sync.RWMutex, apiKey string, shar
|
||||
return false, err
|
||||
}
|
||||
|
||||
_, err := getNextShareCode(tMatch.ShareCode, apiKey, authCode, player.Steamid, rl)
|
||||
_, err := getNextShareCode(tMatch.ShareCode, apiKey, authCode, player.ID, rl)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return true, nil
|
||||
} else {
|
||||
_, err := getNextShareCode(shareCode, apiKey, authCode, player.Steamid, rl)
|
||||
_, err := getNextShareCode(shareCode, apiKey, authCode, player.ID, rl)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
@@ -180,14 +180,14 @@ func GetNewShareCodesForPlayer(player *ent.Player, lock *sync.RWMutex, apiKey st
|
||||
return nil, err
|
||||
}
|
||||
var rCodes []string
|
||||
newShareCode, err := getNextShareCode(tMatch.ShareCode, apiKey, player.AuthCode, player.Steamid, rl)
|
||||
newShareCode, err := getNextShareCode(tMatch.ShareCode, apiKey, player.AuthCode, player.ID, rl)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for newShareCode != "n/a" {
|
||||
rCodes = append(rCodes, newShareCode)
|
||||
newShareCode, err = getNextShareCode(rCodes[len(rCodes)-1], apiKey, player.AuthCode, player.Steamid, rl)
|
||||
newShareCode, err = getNextShareCode(rCodes[len(rCodes)-1], apiKey, player.AuthCode, player.ID, rl)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -278,7 +278,7 @@ func GetPlayerFromVanityURL(db *DBWithLock, id string) (*ent.Player, error) {
|
||||
}
|
||||
|
||||
db.Lock.Lock()
|
||||
nPlayer, err := db.Client.Player.Create().SetSteamid(profile.SteamID64).SetVanityURL(strings.ToLower(profile.VanityURL)).SetVac(profile.VacBanned).SetAvatarURL(profile.AvatarURL).SetName(profile.ProfileName).Save(context.Background())
|
||||
nPlayer, err := db.Client.Player.Create().SetID(profile.SteamID64).SetVanityURL(strings.ToLower(profile.VanityURL)).SetVac(profile.VacBanned).SetAvatarURL(profile.AvatarURL).SetName(profile.ProfileName).Save(context.Background())
|
||||
db.Lock.Unlock()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -289,13 +289,13 @@ func GetPlayerFromVanityURL(db *DBWithLock, id string) (*ent.Player, error) {
|
||||
|
||||
func GetPlayerFromSteamID64(db *DBWithLock, steamID uint64, apiKey string, rl ratelimit.Limiter) (*ent.Player, error) {
|
||||
db.Lock.RLock()
|
||||
tPlayer, err := db.Client.Player.Query().Where(player.Steamid(steamID)).Only(context.Background())
|
||||
tPlayer, err := db.Client.Player.Query().Where(player.ID(steamID)).Only(context.Background())
|
||||
db.Lock.RUnlock()
|
||||
if err == nil {
|
||||
return tPlayer, nil
|
||||
} else {
|
||||
db.Lock.Lock()
|
||||
nPlayer, err := db.Client.Player.Create().SetSteamid(steamID).Save(context.Background())
|
||||
nPlayer, err := db.Client.Player.Create().SetID(steamID).Save(context.Background())
|
||||
db.Lock.Unlock()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -326,7 +326,7 @@ func GCInfoParser(channel chan *csgo.Demo, dl *csgo.DemoMatchLoader, dp *csgo.De
|
||||
}
|
||||
|
||||
db.Lock.RLock()
|
||||
iMatch, err := db.Client.Match.Query().Where(match.MatchID(matchId)).Only(context.Background())
|
||||
iMatch, err := db.Client.Match.Query().Where(match.ID(matchId)).Only(context.Background())
|
||||
db.Lock.RUnlock()
|
||||
if err != nil {
|
||||
switch e := err.(type) {
|
||||
@@ -365,7 +365,7 @@ func GCInfoParser(channel chan *csgo.Demo, dl *csgo.DemoMatchLoader, dp *csgo.De
|
||||
|
||||
db.Lock.Lock()
|
||||
tMatch, err := db.Client.Match.Create().
|
||||
SetMatchID(matchZero.GetMatchid()).
|
||||
SetID(matchZero.GetMatchid()).
|
||||
AddPlayers(players...).
|
||||
SetDate(time.Unix(int64(matchZero.GetMatchtime()), 0).UTC()).
|
||||
SetMaxRounds(int(lastRound.GetMaxRounds())).
|
||||
@@ -435,7 +435,7 @@ func SteamProfile2XML(id string, steamID64 uint64) (*CommunityXML, error) {
|
||||
}
|
||||
|
||||
func UpdatePlayerFromSteam(player *ent.Player, apiKey string, lock *sync.RWMutex, rl ratelimit.Limiter) (*ent.Player, error) {
|
||||
profile, err := SteamProfile2XML("", player.Steamid)
|
||||
profile, err := SteamProfile2XML("", player.ID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
Reference in New Issue
Block a user