Exemplo n.º 1
0
    public void VerifyLambdasAreCalled()
    {
        var directories     = new ConcurrentBag <CapturedDirectory>();
        var files           = new ConcurrentBag <CapturedFile>();
        var targetDirectory = Path.Combine(TestContext.CurrentContext.TestDirectory,
                                           "DirectorySnippetExtractor/VerifyLambdasAreCalled");
        var result    = new TestResult();
        var extractor = new DirectorySnippetExtractor(
            directoryFilter: path =>
        {
            var capture = new CapturedDirectory
            {
                Path = path
            };
            directories.Add(capture);
            return(true);
        },
            fileFilter: path =>
        {
            var capture = new CapturedFile
            {
                Path = path
            };
            files.Add(capture);
            return(true);
        },
            packageOrder: component => new List <string>()
            );

        extractor.ReadComponents(targetDirectory);
        result.Files       = files.OrderBy(file => file.Path).ToList();
        result.Directories = directories.OrderBy(file => file.Path).ToList();
        ObjectApprover.VerifyWithJson(result, Scrubber.Scrub);
    }
Exemplo n.º 2
0
        protected virtual void ProcessCapturedFile(string file, CapturedFile capturedFile, bool isHighestPriority)
        {
            try
            {
                var fs = capturedFile.FileStream;

                var handText = GetHandTextFromStream(fs, capturedFile.Encoding, file);

                if (string.IsNullOrEmpty(handText))
                {
                    return;
                }

                // update size and last write time
                capturedFile.ImportedFile.FileSize      = fs.Length;
                capturedFile.ImportedFile.LastWriteTime = File.GetLastWriteTimeUtc(capturedFile.ImportedFile.FileName);
                capturedFile.WasModified = true;

                // if file could not be parsed, mark it as invalid to prevent further processing
                if (capturedFile.GameInfo == null)
                {
                    if (!TryGetPokerSiteName(handText, out EnumPokerSites siteName) && !filesToSkip.Contains(file))
                    {
                        lock (filesToSkip)
                        {
                            filesToSkip.Add(file);
                        }

                        fs.Close();

                        LogProvider.Log.Warn($"Cannot find parser for hand: {handText} [{SiteString}]");
                        LogProvider.Log.Warn($"File '{file}' has bad format. Skipped. [{SiteString}]");

                        return;
                    }

                    var fileName = Path.GetFileNameWithoutExtension(file);

                    var gameInfo = new GameInfo
                    {
                        PokerSite       = siteName,
                        Session         = capturedFile.Session,
                        TournamentSpeed = ParserUtils.ParseNullableTournamentSpeed(fileName, null),
                        FileName        = fileName,
                        FullFileName    = file
                    };

                    LogProvider.Log.Info(this, $"Found '{file}' file. [{(isHighestPriority ? "High" : "Low")}] [{SiteString}]");

                    capturedFile.GameInfo = gameInfo;
                }

                ProcessHand(handText, capturedFile.GameInfo);
            }
            catch (Exception e)
            {
                LogProvider.Log.Error(this, $"Could not process file {file}. [{(isHighestPriority ? "High" : "Low")}] [{SiteString}]", e);
            }
        }
Exemplo n.º 3
0
        // Import data from PS HH
        protected override void DoImport()
        {
            importingResetEvent.Reset();

            var isActualTaskRunning    = false;
            var isNotActualTaskRunning = false;

            while (!cancellationTokenSource.IsCancellationRequested)
            {
                try
                {
                    var settings     = ServiceLocator.Current.GetInstance <ISettingsService>().GetSettings();
                    var siteSettings = settings.SiteSettings.SitesModelList?.FirstOrDefault(x => x.PokerSite == Site);

                    if (siteSettings != null && !siteSettings.Enabled)
                    {
                        Stop();
                        continue;
                    }

                    var handHistoryFolders = GetHandHistoryFolders(siteSettings);

                    IsAdvancedLogEnabled = settings.GeneralSettings.IsAdvancedLoggingEnabled;

                    // detect all *.txt files in directories
                    var handHistoryFiles = handHistoryFolders
                                           .Where(x => x.Exists)
                                           .SelectMany(x => x.GetFiles(HandHistoryFilter, SearchOption.AllDirectories))
                                           .DistinctBy(x => x.FullName.ToLower())
                                           .ToArray();

                    var capturedFiles = actualCapturedFiles.Select(x => x.Value).Concat(notActualCapturedFiles.Select(x => x.Value)).ToArray();

                    var newlyDetectedHandHistories = (from handHistoryFile in handHistoryFiles
                                                      join capturedFile in capturedFiles on handHistoryFile.FullName equals capturedFile.ImportedFile.FileName into capturedFileGrouped
                                                      from item in capturedFileGrouped.DefaultIfEmpty()
                                                      where item == null && !filesToSkip.Contains(handHistoryFile.FullName)
                                                      select handHistoryFile).ToArray();

                    // check if files were processed
                    if (newlyDetectedHandHistories.Length > 0)
                    {
                        var newlyDetectedHandHistoriesNames = newlyDetectedHandHistories.Select(x => x.FullName).ToArray();

                        var processedFiles = dataService.GetImportedFiles(newlyDetectedHandHistoriesNames);

                        var processedFilesToSkip = (from processedFile in processedFiles
                                                    join newlyDetectedHandHistory in newlyDetectedHandHistories on
                                                    new
                        {
                            FileName = processedFile.FileName,
                            FileSize = processedFile.FileSize,
                            LastWrite = DateTime.SpecifyKind(processedFile.LastWriteTime, DateTimeKind.Utc)
                        } equals
                                                    new
                        {
                            FileName = newlyDetectedHandHistory.FullName,
                            FileSize = newlyDetectedHandHistory.Length,
                            LastWrite = new DateTime(newlyDetectedHandHistory.LastWriteTimeUtc.Year,
                                                     newlyDetectedHandHistory.LastWriteTimeUtc.Month,
                                                     newlyDetectedHandHistory.LastWriteTimeUtc.Day,
                                                     newlyDetectedHandHistory.LastWriteTimeUtc.Hour,
                                                     newlyDetectedHandHistory.LastWriteTimeUtc.Minute,
                                                     newlyDetectedHandHistory.LastWriteTimeUtc.Second,
                                                     newlyDetectedHandHistory.LastWriteTimeUtc.Kind)
                        }
                                                    select newlyDetectedHandHistory).ToArray();

                        if (processedFilesToSkip.Length > 0)
                        {
                            newlyDetectedHandHistories = newlyDetectedHandHistories
                                                         .Except(processedFilesToSkip, new LambdaComparer <FileInfo>((x, y) => x.FullName == y.FullName))
                                                         .ToArray();
                        }
                    }

                    // add new files and lock them
                    foreach (var hh in newlyDetectedHandHistories)
                    {
                        if (cancellationTokenSource.IsCancellationRequested)
                        {
                            break;
                        }

                        if (!actualCapturedFiles.ContainsKey(hh.FullName) &&
                            !notActualCapturedFiles.ContainsKey(hh.FullName))
                        {
                            var fs = File.Open(hh.FullName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                            fs.Seek(0, SeekOrigin.Begin);

                            Encoding encoding;

                            try
                            {
                                encoding = EncodingDetector.DetectTextFileEncoding(fs, 0x10000) ?? Encoding.UTF8;
                            }
                            catch (Exception ex)
                            {
                                LogProvider.Log.Error(this, $"Couldn't detect encoding of {hh.FullName}. UTF8 will be used. [{SiteString}]", ex);
                                encoding = Encoding.UTF8;
                            }

                            fs.Seek(0, SeekOrigin.Begin);

                            var capturedFile = new CapturedFile
                            {
                                FileStream   = fs,
                                Session      = GetSessionForFile(hh.FullName),
                                Encoding     = encoding,
                                ImportedFile = new ImportedFile
                                {
                                    FileName = hh.FullName
                                }
                            };

                            var lastWriteTime = File.GetLastWriteTimeUtc(hh.FullName);

                            if (lastWriteTime > DateTime.UtcNow.AddSeconds(-ActualFileLifeTime))
                            {
                                actualCapturedFiles.TryAdd(hh.FullName, capturedFile);
                            }
                            else
                            {
                                notActualCapturedFiles.TryAdd(hh.FullName, capturedFile);
                            }
                        }
                    }

                    if (!isActualTaskRunning)
                    {
                        ImportCapturedFiles(actualCapturedFiles, true);
                        isActualTaskRunning = true;
                    }

                    if (!isNotActualTaskRunning)
                    {
                        ImportCapturedFiles(notActualCapturedFiles, false);
                        isNotActualTaskRunning = true;
                    }

                    if (!cancellationTokenSource.IsCancellationRequested)
                    {
                        try
                        {
                            Task.Delay(ReadingTimeout).Wait(cancellationTokenSource.Token);
                        }
                        catch (OperationCanceledException)
                        {
                        }
                    }
                }
                catch (Exception e)
                {
                    LogProvider.Log.Error(this, string.Format("{0} auto-import failed", SiteString), e);
                }
            }

            Clean();

            RaiseProcessStopped();
        }