Example #1
0
        private ReplayCsvRecord ToCsvRecord(File <IReplay> replay)
        {
            // you lack abstractions, this is almost all information that should just readily be available on a `Replay` type
            // actually you have the ReplayDecorator class, maybe you should use that one??
            var gameType             = replay.Content.GameType;
            var players              = new IPlayer[12];
            var playersOrderedByTeam = replay.Content.Players
                                       .GroupBy(p => p.ForceIdentifier)
                                       .OrderBy(p => p.Key);

            int counter = 0;

            foreach (var team in playersOrderedByTeam)
            {
                foreach (var player in team)
                {
                    players[counter] = player;
                    counter++;
                }
            }

            var map      = replay.Content.ReplayMap.MapName;
            var duration = TimeSpan.FromSeconds(Math.Round(replay.Content.FrameCount / Constants.FastestFPS));
            var date     = replay.Content.Timestamp.ToString("yy-MM-dd", CultureInfo.InvariantCulture);
            var fileName = Path.GetFileNameWithoutExtension(replay.FilePath);
            var path     = replay.FilePath;

            var replayDecorator = ReplayDecorator.Create(replay);

            var record = new ReplayCsvRecord
            {
                GameType   = gameType.ToString(),
                GameFormat = replayDecorator.GameFormat(),
                Matchup    = replayDecorator.Matchup(),
                Player1    = players[0]?.Name,
                Player2    = players[1]?.Name,
                Player3    = players[2]?.Name,
                Player4    = players[3]?.Name,
                Player5    = players[4]?.Name,
                Player6    = players[5]?.Name,
                Player7    = players[6]?.Name,
                Player8    = players[7]?.Name,
                Player9    = players[8]?.Name,
                Player10   = players[9]?.Name,
                Player11   = players[10]?.Name,
                Player12   = players[11]?.Name,
                Map        = map,
                Duration   = duration.TotalSeconds + "s",
                Date       = date,
                FileName   = fileName,
                Path       = path
            };

            return(record);
        }
Example #2
0
        //TODO if you want you could incorporate preview into the regular sort methods, but it's annoying to adjust
        public IDictionary <string, List <File <IReplay> > > PreviewSort(List <string> replaysThrowingExceptions, BackgroundWorker worker_ReplaySorter, int currentCriteria, int numberOfCriteria, int currentPositionNested = 0, int numberOfPositions = 0)
        {
            IDictionary <string, List <File <IReplay> > > DirectoryFileReplay = new Dictionary <string, List <File <IReplay> > >();

            MatchUpEqualityComparer MatchUpEq = new MatchUpEqualityComparer();
            IDictionary <IDictionary <int, IDictionary <RaceType, int> >, IList <File <IReplay> > > ReplayMatchUp = new Dictionary <IDictionary <int, IDictionary <RaceType, int> >, IList <File <IReplay> > >(MatchUpEq);

            var counter    = 0;
            var maxCounter = Sorter.ListReplays.Count;

            foreach (var replay in Sorter.ListReplays)
            {
                try
                {
                    if (SortCriteriaParameters.ValidGameTypes[replay.Content.GameType] == true)
                    {
                        var wrappedReplay = ReplayDecorator.Create(replay);
                        var matchup       = wrappedReplay.GetReplayItem(Tuple.Create(CustomReplayNameSyntax.Matchup, string.Empty), ++counter, maxCounter);
                        IDictionary <int, IDictionary <RaceType, int> > EncodedMatchUp = new Dictionary <int, IDictionary <RaceType, int> >();
                        int team = 1;
                        foreach (var raceCombination in GetTeamRaces(matchup))
                        {
                            var RaceFrequency = EncodeRacesFrequency(raceCombination);
                            EncodedMatchUp.Add(new KeyValuePair <int, IDictionary <RaceType, int> >(team, RaceFrequency));
                            team++;
                        }
                        // Teams Team = new Teams(replay.Content);
                        // MatchUp MatchUp = new MatchUp(replay.Content, Team);
                        // // int => team
                        // IDictionary<int, IDictionary<RaceType, int>> EncodedMatchUp = new Dictionary<int, IDictionary<RaceType, int>>();
                        // int team = 1;
                        // foreach (var RaceCombination in MatchUp.TeamRaces)
                        // {
                        //     var RaceFrequency = EncodeRacesFrequency(RaceCombination);
                        //     EncodedMatchUp.Add(new KeyValuePair<int, IDictionary<RaceType, int>>(team, RaceFrequency));
                        //     team++;
                        // }
                        if (!ReplayMatchUp.ContainsKey(EncodedMatchUp))
                        {
                            ReplayMatchUp.Add(new KeyValuePair <IDictionary <int, IDictionary <RaceType, int> >, IList <File <IReplay> > >(EncodedMatchUp, new List <File <IReplay> > {
                                replay
                            }));
                        }
                        else
                        {
                            ReplayMatchUp[EncodedMatchUp].Add(replay);
                        }
                    }
                }
                catch (Exception ex)
                {
                    ErrorLogger.GetInstance()?.LogError($"{DateTime.Now} - SortOnMatchUp exception: Encoding matchups, file: {replay.OriginalFilePath}", ex: ex);
                }
            }

            string sortDirectory = Sorter.CurrentDirectory;

            if (!(IsNested && !Sorter.GenerateIntermediateFolders))
            {
                if (IsNested)
                {
                    sortDirectory = Sorter.CurrentDirectory + @"\" + SortCriteria;
                }
                else
                {
                    sortDirectory = Sorter.CurrentDirectory + @"\" + string.Join(",", Sorter.CriteriaStringOrder);
                }
                sortDirectory = FileHandler.AdjustName(sortDirectory, true);
            }

            int currentPosition    = 0;
            int progressPercentage = 0;

            //TODO if there are no matchups to sort... this will screw with the progress reporting...
            foreach (var matchup in ReplayMatchUp.Keys)
            {
                var MatchUpName = MatchUpToString(matchup);

                if (string.IsNullOrWhiteSpace(MatchUpName))
                {
                    MatchUpName = "NoPlayers";
                }

                var FileReplays = new List <File <IReplay> >();
                DirectoryFileReplay.Add(new KeyValuePair <string, List <File <IReplay> > >(sortDirectory + @"\" + MatchUpName, FileReplays));
                var MatchUpReplays = ReplayMatchUp[matchup];

                foreach (var replay in MatchUpReplays)
                {
                    if (worker_ReplaySorter.CancellationPending == true)
                    {
                        return(null);
                    }
                    currentPosition++;
                    if (IsNested == false)
                    {
                        progressPercentage = Convert.ToInt32(((double)currentPosition / Sorter.ListReplays.Count) * 1 / numberOfCriteria * 100);
                    }
                    else
                    {
                        progressPercentage  = Convert.ToInt32((((double)currentPosition / Sorter.ListReplays.Count) * 1 / numberOfPositions * 100 + ((currentPositionNested - 1) * 100 / numberOfPositions)) * ((double)1 / numberOfCriteria));
                        progressPercentage += (currentCriteria - 1) * 100 / numberOfCriteria;
                    }
                    worker_ReplaySorter.ReportProgress(progressPercentage, $"sorting on matchup... {replay.FilePath}");
                    try
                    {
                        if (IsNested == false)
                        {
                            ReplayHandler.CopyReplay(replay, sortDirectory, MatchUpName, KeepOriginalReplayNames, Sorter.CustomReplayFormat, true);
                        }
                        else
                        {
                            ReplayHandler.MoveReplay(replay, sortDirectory, MatchUpName, true, null, true);
                        }

                        FileReplays.Add(replay);
                    }
                    catch (Exception ex)
                    {
                        replaysThrowingExceptions.Add(replay.OriginalFilePath);
                        ErrorLogger.GetInstance()?.LogError($"{DateTime.Now} - SortOnMatchUp Exception", ex: ex);
                    }
                }
            }

            //... This is necessary in case you skip replays because of non-matching matchups
            if (IsNested == false)
            {
                progressPercentage = Convert.ToInt32(1.0 / numberOfCriteria * 100);
            }
            else
            {
                progressPercentage  = Convert.ToInt32((1.0 / numberOfPositions * 100 + ((currentPositionNested - 1) * 100 / numberOfPositions)) * ((double)1 / numberOfCriteria));
                progressPercentage += (currentCriteria - 1) * 100 / numberOfCriteria;
            }
            worker_ReplaySorter.ReportProgress(progressPercentage);
            return(DirectoryFileReplay);
        }
Example #3
0
        public IDictionary <string, List <File <IReplay> > > Sort(List <string> replaysThrowingExceptions)
        {
            // Dictionary<directory, dictionary<file, replay>>
            IDictionary <string, List <File <IReplay> > > DirectoryFileReplay = new Dictionary <string, List <File <IReplay> > >();

            // get all matchups from the replays
            // allow the ignoring of specific game types

            MatchUpEqualityComparer MatchUpEq = new MatchUpEqualityComparer();
            IDictionary <IDictionary <int, IDictionary <RaceType, int> >, IList <File <IReplay> > > ReplayMatchUps = new Dictionary <IDictionary <int, IDictionary <RaceType, int> >, IList <File <IReplay> > >(MatchUpEq);

            int counter    = 0;
            var maxCounter = Sorter.ListReplays.Count;

            foreach (var replay in Sorter.ListReplays)
            {
                try
                {
                    if (SortCriteriaParameters.ValidGameTypes[replay.Content.GameType] == true)
                    {
                        var wrappedReplay = ReplayDecorator.Create(replay);
                        var matchup       = wrappedReplay.GetReplayItem(Tuple.Create(CustomReplayNameSyntax.Matchup, string.Empty), ++counter, maxCounter);
                        IDictionary <int, IDictionary <RaceType, int> > EncodedMatchUp = new Dictionary <int, IDictionary <RaceType, int> >();
                        int team = 1;
                        foreach (var raceCombination in GetTeamRaces(matchup))
                        {
                            var RaceFrequency = EncodeRacesFrequency(raceCombination);
                            EncodedMatchUp.Add(new KeyValuePair <int, IDictionary <RaceType, int> >(team, RaceFrequency));
                            team++;
                        }

                        // Teams Team = new Teams(replay.Content);
                        // MatchUp MatchUp = new MatchUp(replay.Content, Team);
                        // // int => team
                        // IDictionary<int, IDictionary<RaceType, int>> EncodedMatchUp = new Dictionary<int, IDictionary<RaceType, int>>();
                        // int team = 1;
                        // foreach (var RaceCombination in MatchUp.TeamRaces)
                        // {
                        //     var RaceFrequency = EncodeRacesFrequency(RaceCombination);
                        //     EncodedMatchUp.Add(new KeyValuePair<int, IDictionary<RaceType, int>>(team, RaceFrequency));
                        //     team++;
                        // }
                        if (!ReplayMatchUps.ContainsKey(EncodedMatchUp))
                        {
                            ReplayMatchUps.Add(new KeyValuePair <IDictionary <int, IDictionary <RaceType, int> >, IList <File <IReplay> > >(EncodedMatchUp, new List <File <IReplay> > {
                                replay
                            }));
                        }
                        else
                        {
                            ReplayMatchUps[EncodedMatchUp].Add(replay);
                        }
                    }
                }
                catch (Exception ex)
                {
                    ErrorLogger.GetInstance()?.LogError($"{DateTime.Now} - SortOnMatchUp exception: Encoding matchups, file: {replay.OriginalFilePath}", ex: ex);
                }
            }

            string sortDirectory = Sorter.CurrentDirectory;

            if (!(IsNested && !Sorter.GenerateIntermediateFolders))
            {
                if (IsNested)
                {
                    sortDirectory = Sorter.CurrentDirectory + @"\" + SortCriteria;
                }
                else
                {
                    sortDirectory = Sorter.CurrentDirectory + @"\" + string.Join(",", Sorter.CriteriaStringOrder);
                }
                sortDirectory = FileHandler.CreateDirectory(sortDirectory);
            }

            foreach (var matchup in /*MatchUps.Distinct(MatchUpEq)*/ ReplayMatchUps.Keys)
            {
                // make directory per matchup
                var MatchUpName = MatchUpToString(matchup);

                if (string.IsNullOrWhiteSpace(MatchUpName))
                {
                    MatchUpName = "NoPlayers";
                }

                Directory.CreateDirectory(sortDirectory + @"\" + MatchUpName);

                var FileReplays = new List <File <IReplay> >();
                DirectoryFileReplay.Add(new KeyValuePair <string, List <File <IReplay> > >(sortDirectory + @"\" + MatchUpName, FileReplays));

                // write all associated replays to this directory
                var MatchUpReplays = ReplayMatchUps[matchup];
                foreach (var replay in MatchUpReplays)
                {
                    try
                    {
                        if (IsNested == false)
                        {
                            ReplayHandler.CopyReplay(replay, sortDirectory, MatchUpName, KeepOriginalReplayNames, Sorter.CustomReplayFormat);
                        }
                        else
                        {
                            ReplayHandler.MoveReplay(replay, sortDirectory, MatchUpName, true, null);
                        }

                        FileReplays.Add(replay);
                    }
                    catch (Exception ex)
                    {
                        replaysThrowingExceptions.Add(replay.OriginalFilePath);
                        ErrorLogger.GetInstance()?.LogError($"{DateTime.Now} - SortOnMatchUp Exception", ex: ex);
                    }
                }
            }
            return(DirectoryFileReplay);
        }