diff --git a/main.go b/main.go index 367f387..29cd406 100644 --- a/main.go +++ b/main.go @@ -165,6 +165,42 @@ func housekeeping() { log.Warningf("[HK] Failure trying to parse match %d: %v", m.ID, err) } } + + // check for inconsistent matches + tMatchIDs, err := db.Match.Query().IDs(context.Background()) + + for _, mid := range tMatchIDs { + var v []struct { + ID int `json:"match_stats"` + Count int `json:"count"` + } + + err = db.MatchPlayer.Query().Where(matchplayer.MatchStats(mid)).GroupBy(matchplayer.FieldMatchStats).Aggregate(ent.Count()).Scan(context.Background(), &v) + if err != nil { + log.Warningf("[HK] Unable to query for matchplayers for match %d: %v", mid, err) + continue + } + + log.Debugf("%+v", v) + if v[0].Count < 10 { + log.Warningf("[HK] Found match without all players, try to reload it.") + tMatch, err := db.Match.Get(context.Background(), mid) + if err != nil { + log.Warningf("[HK] Unable to get match with id %d: %v", mid, err) + continue + } + err = utils.DeleteMatch(tMatch, db) + if err != nil { + log.Warningf("[HK] Unable to delete match with id %d: %v", mid, err) + continue + } + + err = demoLoader.LoadDemo(&csgo.Demo{ShareCode: tMatch.ShareCode}) + if err != nil { + log.Warningf("[HK] Unable to requeue match with id %d: %v", mid, err) + } + } + } } } diff --git a/utils/utils.go b/utils/utils.go index a58f4c6..350399f 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -6,6 +6,8 @@ import ( "csgowtfd/ent/match" "csgowtfd/ent/matchplayer" "csgowtfd/ent/player" + "csgowtfd/ent/roundstats" + "csgowtfd/ent/spray" "csgowtfd/ent/weapon" "encoding/json" "entgo.io/ent/dialect/sql" @@ -388,6 +390,36 @@ func GetMetaStats(dbPlayer *ent.Player) (*MetaStatsResponse, error) { return mResponse, nil } +func DeleteMatch(matchDel *ent.Match, db *ent.Client) error { + tMatchPlayer, err := matchDel.QueryStats().IDs(context.Background()) + if err != nil { + return err + } + + _, err = db.Spray.Delete().Where(spray.HasMatchPlayersWith(matchplayer.IDIn(tMatchPlayer...))).Exec(context.Background()) + if err != nil { + return err + } + _, err = db.Weapon.Delete().Where(weapon.HasStatWith(matchplayer.IDIn(tMatchPlayer...))).Exec(context.Background()) + if err != nil { + return err + } + _, err = db.RoundStats.Delete().Where(roundstats.HasMatchPlayerWith(matchplayer.IDIn(tMatchPlayer...))).Exec(context.Background()) + if err != nil { + return err + } + _, err = db.MatchPlayer.Delete().Where(matchplayer.IDIn(tMatchPlayer...)).Exec(context.Background()) + if err != nil { + return err + } + _, err = db.Match.Delete().Where(match.ID(matchDel.ID)).Exec(context.Background()) + if err != nil { + return err + } + + return nil +} + func GetWinLossTieForPlayer(dbPlayer *ent.Player) (wins int, looses int, ties int, err error) { var res []struct { MatchResult int `json:"match_result"`