示例#1
0
        public void LoadMatchFiles()
        {
            var matchFiles = MatchDirectory.GetAllMatchFiles();

            if (matchFiles.Count() > testSynchronizer.Limit)
            {
                matchFiles = matchFiles.Take((int)testSynchronizer.Limit);
            }

            // Start the counter at how many files we already have
            int matchFileCount = matchFiles.Count();

            testSynchronizer.Count = matchFileCount;
            Console.WriteLine("Match Files Cached: {0}", matchFileCount);

            // Load match files
            matchFiles.AsParallel().WithDegreeOfParallelism(8).ForAll(filename =>
            {
                MatchDetail match = MatchDirectory.LoadMatch(filename);
                if (match == null || match.Timeline == null)
                {
                    // Match file has an error, delete the cached match file
                    File.Delete(filename);
                }
                else
                {
                    ConsumeMatchDetailBlock.Post(match);
                }
            });

            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("Finished loading match files");
            Console.ResetColor();

            //MatchFileBufferBlock.Complete();
        }
示例#2
0
        /// <summary>
        /// Process a match.
        /// </summary>
        private async Task <MatchDetail> ConsumeMatch(MatchSummary match)
        {
            long matchId = match.MatchId;

            // Try to mark the match as being processed
            if (!TryLockMatch(matchId))
            {
                // Another thread has already started processing this match, we don't need to do anything
                return(null);
            }

            // Get the disk path and version of the match
            string      matchPath    = MatchDirectory.GetMatchPath(match);
            RiotVersion matchVersion = new RiotVersion(match.MatchVersion);

            // Check if the match version is older than the current realm version
            if (!StaticDataStore.Version.IsSamePatch(matchVersion))
            {
                TryUnlockMatch(matchId);
                return(null);
            }

            // Check if the match exists on disk
            if (MatchDirectory.MatchFileExists(match))
            {
                // Match file loading will handle this
                TryUnlockMatch(matchId);
                return(null);
            }

            // <TEST> download limiting
            long count = Interlocked.Read(ref testSynchronizer.Count);

            Interlocked.Increment(ref testSynchronizer.Count);
            if (count >= testSynchronizer.Limit)
            {
                return(null);
            }
            // </TEST>

            MatchDetail matchData = null;

            int retriesLeft = 3;

            while (retriesLeft > 0)
            {
                try
                {
                    // Get the match with full timeline data
                    matchData = api.GetMatch(match.Region, matchId, true);

                    // Verify the match
                    if (matchData == null)
                    {
                        throw new RiotSharpException("Null match: " + matchId);
                    }

                    if (matchData.Timeline == null)
                    {
                        throw new RiotSharpException("Null match timeline: " + matchId);
                    }

                    // Save it to disk
                    MatchDirectory.SaveMatch(matchData);

                    // Success, don't retry anymore
                    retriesLeft = 0;

                    Console.WriteLine(count);
                }
                catch (RiotSharpException ex)
                {
                    if (ex.IsRetryable())
                    {
                        --retriesLeft;
                    }
                    else
                    {
                        retriesLeft = 0;
                    }

                    // Don't do anything with the exception yet
                    // TODO: log exceptions

                    Console.ForegroundColor = retriesLeft > 0 ? ConsoleColor.Yellow : ConsoleColor.Red;
                    Console.WriteLine("{0}: Match Error ({1} retries left): {2}", count, retriesLeft, ex.Message);
                    Console.ResetColor();
                }
            }

            // Remove the match from current downloads
            TryUnlockMatch(matchId);

            return(matchData);
        }