Ejemplo n.º 1
0
        static void MigrateTablePartial <T>(Stopwatch sw, Expression <Func <T, object> > getIdExpression, ref long rows, ref long totalTime, ISession pgSession, int limitFrom, int rowsToSelect)
        {
            T[] entities = null;

            sw.Restart();
            entities = pgSession.Query <T>().OrderBy(getIdExpression).Skip(limitFrom).Take(rowsToSelect).ToArray();
            sw.Stop();

            var getId = getIdExpression.Compile();

            Console.WriteLine($"Read total {typeof(T).Name}: {entities.Length} [{sw.ElapsedMilliseconds} ms]");
            totalTime += sw.ElapsedMilliseconds;

            using (var session = ModelEntities.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    sw.Restart();

                    for (var i = 0; i < entities.Length; i++)
                    {
                        session.Save(entities[i], getId(entities[i]));
                    }

                    transaction.Commit();

                    sw.Stop();
                }

                Console.WriteLine($"Migrated rows of {typeof(T).Name}: {entities.Length} [{sw.ElapsedMilliseconds} ms]");
                rows      += entities.Length;
                totalTime += sw.ElapsedMilliseconds;
            }
        }
Ejemplo n.º 2
0
 static Dictionary <PlayersKey, Players> ReadPlayers()
 {
     using (var session = ModelEntities.OpenSession())
     {
         return(session.Query <Players>()
                .ToDictionary(x => new PlayersKey {
             PlayerName = x.Playername, PokersiteId = x.PokersiteId
         },
                              new LambdaComparer <PlayersKey>((x, y) => x.PlayerName == y.PlayerName && x.PokersiteId == y.PokersiteId)));
     }
 }
        private void LoadPlayers(IEnumerable <string> players)
        {
            if (players == null)
            {
                return;
            }

            var playersToAdd = players.Where(x => !playerNamePlayerIdMap.ContainsKey(x)).ToArray();

            if (playersToAdd.Length > 0)
            {
                using (var session = ModelEntities.OpenSession())
                {
                    var playerNamePlayerIdToAdd = session.Query <Players>()
                                                  .Where(x => x.PokersiteId == (short)EnumPokerSites.PokerStars && playersToAdd.Contains(x.Playername))
                                                  .Select(x => new { x.Playername, x.PlayerId })
                                                  .ToArray();

                    playerNamePlayerIdToAdd.ForEach(x => playerNamePlayerIdMap.Add(x.Playername, x.PlayerId));
                }
            }
        }
Ejemplo n.º 4
0
        protected virtual void ProcessFastFoldNoticeGameReset(FastFoldImportDto fastFoldImportDto)
        {
            var noticeGameSnapshot = fastFoldImportDto.HandBuilder.GetNoticeRoomSnapShot(fastFoldImportDto.Package);

            if (noticeGameSnapshot == null ||
                fastFoldImportDto.NoticeResetGame.Players == null ||
                !long.TryParse(fastFoldImportDto.NoticeResetGame.GameId, out long handId))
            {
                LogProvider.Log.Error(Logger, $"Failed to get snapshot for {fastFoldImportDto.Package.UserId} of {fastFoldImportDto.Package.RoomId} (Fast Fold).");
                return;
            }

            // load players from db
            var playersToAdd = fastFoldImportDto.NoticeResetGame.Players
                               .Select(x => x.Playerid.ToString())
                               .Where(x => !fastFoldImportDto.PlayerNamePlayerIdMap.ContainsKey(x))
                               .ToArray();

            if (playersToAdd.Length > 0)
            {
                using (var session = ModelEntities.OpenSession())
                {
                    var playerNamePlayerIdToAdd = session.Query <Players>()
                                                  .Where(x => x.PokersiteId == (short)Site && playersToAdd.Contains(x.Playername))
                                                  .Select(x => new { x.Playername, x.PlayerId })
                                                  .ToArray();

                    playerNamePlayerIdToAdd.ForEach(x => fastFoldImportDto.PlayerNamePlayerIdMap.Add(x.Playername, x.PlayerId));
                }
            }

            var fastFoldRoomId = fastFoldImportDto.HandBuilder
                                 .GetFastFoldRoomByUser(fastFoldImportDto.Package.UserId);

            if (fastFoldRoomId == 0)
            {
                fastFoldRoomId = fastFoldImportDto.Package.RoomId;
            }

            var gameInfo = new GameInfo
            {
                Session      = $"{fastFoldRoomId}{fastFoldImportDto.Package.UserId}",
                WindowHandle = fastFoldImportDto.WindowHandle.ToInt32(),
                PokerSite    = Site,
                GameType     = Bovada.GameType.Holdem,
                TableType    = (EnumTableType)noticeGameSnapshot.Params.PlayerCountMax,
                GameFormat   = GameFormat.FastFold,
                GameNumber   = handId
            };

            // Initialize cache
            gameInfo.ResetPlayersCacheInfo();

            var players = new PlayerList(fastFoldImportDto.NoticeResetGame.Players.Select(x =>
                                                                                          new Player(x.Playerid.ToString(), 0, x.Seatid + 1)
            {
                PlayerId = fastFoldImportDto.PlayerNamePlayerIdMap.ContainsKey(x.Playerid.ToString()) ?
                           fastFoldImportDto.PlayerNamePlayerIdMap[x.Playerid.ToString()] : 0,
                PlayerNick = x.Name
            }));

            Player heroPlayer = null;

            foreach (var player in players)
            {
                if (player.PlayerId == 0)
                {
                    continue;
                }

                var isHero = false;

                if (player.PlayerName.Equals(fastFoldImportDto.Package.UserId.ToString()))
                {
                    heroPlayer = player;
                    isHero     = true;
                }

                var playerCollectionItem = new PlayerCollectionItem
                {
                    PlayerId  = player.PlayerId,
                    Name      = player.PlayerName,
                    PokerSite = Site
                };

                var playerCacheStatistic = fastFoldImportDto.ImporterSessionCacheService.GetPlayerStats(gameInfo.Session, playerCollectionItem, out bool exists);

                if (exists && playerCacheStatistic.IsHero)
                {
                    heroPlayer          = player;
                    gameInfo.GameFormat = playerCacheStatistic.GameFormat;
                    break;
                }
                else if (!exists && gameInfo.GameFormat == GameFormat.FastFold)
                {
                    var playerCacheInfo = new PlayerStatsSessionCacheInfo
                    {
                        Session    = gameInfo.Session,
                        GameFormat = gameInfo.GameFormat,
                        Player     = playerCollectionItem,
                        IsHero     = isHero,
                        Stats      = new Playerstatistic
                        {
                            SessionCode     = gameInfo.Session,
                            PokergametypeId = (short)HandHistories.Objects.GameDescription.GameType.NoLimitHoldem
                        }
                    };

                    if (playerCacheInfo.Stats.PokergametypeId != 0)
                    {
                        gameInfo.AddToPlayersCacheInfo(playerCacheInfo);
                    }
                }
            }

            PreparePlayerList(players,
                              noticeGameSnapshot.Params.PlayerCountMax,
                              heroPlayer != null ? heroPlayer.SeatNumber : 0);

            var importedArgs = new DataImportedEventArgs(players, gameInfo, heroPlayer, 0);

            eventAggregator.GetEvent <DataImportedEvent>().Publish(importedArgs);
        }
Ejemplo n.º 5
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="capturedFiles"></param>
        protected virtual void ImportCapturedFiles(ConcurrentDictionary <string, CapturedFile> capturedFiles, bool isHighestPriority)
        {
            if (cancellationTokenSource.IsCancellationRequested)
            {
                return;
            }

            Task.Run(() =>
            {
                while (!cancellationTokenSource.IsCancellationRequested)
                {
                    foreach (var capturedFile in capturedFiles)
                    {
                        if (!isHighestPriority)
                        {
                            importingResetEvent.WaitOne();
                        }
                        else
                        {
                            importingResetEvent.Reset();
                        }

                        if (cancellationTokenSource.IsCancellationRequested)
                        {
                            if (isHighestPriority)
                            {
                                importingResetEvent.Set();
                            }

                            return;
                        }

                        ProcessCapturedFile(capturedFile.Key, capturedFile.Value, isHighestPriority);

                        if (!capturedFile.Value.WasModified || capturedFile.Value.GameInfo == null)
                        {
                            continue;
                        }

                        // modify captured file
                        using (var session = ModelEntities.OpenSession())
                        {
                            lock (dbLocker)
                            {
                                try
                                {
                                    using (var transaction = session.BeginTransaction())
                                    {
                                        var existingImportedFile = dataService.GetImportedFiles(new[] { capturedFile.Key }, session).FirstOrDefault();

                                        if (existingImportedFile != null)
                                        {
                                            existingImportedFile.FileSize      = capturedFile.Value.ImportedFile.FileSize;
                                            existingImportedFile.LastWriteTime = capturedFile.Value.ImportedFile.LastWriteTime;
                                            capturedFile.Value.ImportedFile    = existingImportedFile;
                                        }

                                        session.Save(capturedFile.Value.ImportedFile);

                                        capturedFile.Value.WasModified = false;

                                        transaction.Commit();
                                    }
                                }
                                // ignore errors if transaction is locked too long
                                catch
                                {
                                }
                            }
                        }
                    }

                    // remove invalid files from the list of files
                    filesToSkip.ForEach(fileToSkip =>
                    {
                        if (!capturedFiles.ContainsKey(fileToSkip))
                        {
                            return;
                        }

                        if (capturedFiles.TryRemove(fileToSkip, out CapturedFile removedCapturedFile))
                        {
                            removedCapturedFile?.FileStream?.Close();
                        }
                    });

                    try
                    {
                        if (isHighestPriority)
                        {
                            importingResetEvent.Set();
                        }

                        Task.Delay(ReadingTimeout).Wait(cancellationTokenSource.Token);
                    }
                    catch (OperationCanceledException)
                    {
                    }
                }

                if (isHighestPriority)
                {
                    importingResetEvent.Set();
                }
            });
        }