Пример #1
0
 public Tournaments GetTournament(string tournamentId, string playerName, short pokersiteId)
 {
     using (var session = ModelEntities.OpenSession())
     {
         return(session.Query <Tournaments>().FirstOrDefault(x => x.Tourneynumber == tournamentId && x.Player.Playername == playerName && x.SiteId == pokersiteId));
     }
 }
Пример #2
0
        public IList <HandHistory> GetGames(IEnumerable <long> gameNumbers, short pokersiteId)
        {
            var handHistoryParserFactory = ServiceLocator.Current.GetInstance <IHandHistoryParserFactory>();

            var handHistoryParser = handHistoryParserFactory.GetFullHandHistoryParser((EnumPokerSites)pokersiteId);

            using (var session = ModelEntities.OpenSession())
            {
                List <HandHistory> historyList = new List <HandHistory>();

                Disjunction restriction = Restrictions.Disjunction();
                restriction.Add(Restrictions.Conjunction()
                                .Add(Restrictions.On <Handhistory>(x => x.Gamenumber).IsIn(gameNumbers.ToList()))
                                .Add(Restrictions.Where <Handhistory>(x => x.PokersiteId == pokersiteId)));

                var list = session.QueryOver <Handhistory>()
                           .Where(restriction)
                           .List();

                foreach (var history in list)
                {
                    var result = handHistoryParser.ParseFullHandHistory(history.HandhistoryVal);

                    if (result == null)
                    {
                        continue;
                    }

                    historyList.Add(result);
                }

                return(historyList);
            }
        }
Пример #3
0
 public IList <Handnotes> GetHandNotes(short pokersiteId)
 {
     using (var session = ModelEntities.OpenSession())
     {
         return(session.Query <Handnotes>().Where(x => x.PokersiteId == pokersiteId).ToList());
     }
 }
Пример #4
0
        public HandHistory GetGame(long gameNumber, short pokersiteId)
        {
            using (var session = ModelEntities.OpenSession())
            {
                var hh = session.Query <Handhistory>().FirstOrDefault(x => x.Gamenumber == gameNumber && x.PokersiteId == pokersiteId);

                if (hh == null)
                {
                    return(null);
                }

                var handHistoryParserFactory = ServiceLocator.Current.GetInstance <IHandHistoryParserFactory>();

                var handHistoryParser = handHistoryParserFactory.GetFullHandHistoryParser((EnumPokerSites)pokersiteId, hh.HandhistoryVal);

                var result = handHistoryParser.ParseFullHandHistory(hh.HandhistoryVal);

                if (result == null)
                {
                    return(null);
                }

                if (result.GameDescription != null)
                {
                    result.GameDescription.Site = (EnumPokerSites)pokersiteId;
                }

                return(result);
            }
        }
Пример #5
0
        public void DeletePlayerNotes(IEnumerable <Playernotes> playernotes)
        {
            if (playernotes == null || playernotes.Count() == 0)
            {
                return;
            }

            try
            {
                using (var session = ModelEntities.OpenSession())
                {
                    using (var transaction = session.BeginTransaction())
                    {
                        var playernotesIds = playernotes.Select(x => x.PlayerNoteId).Distinct().ToArray();

                        var playernotesToDelete = session.Query <Playernotes>()
                                                  .Where(x => playernotesIds.Contains(x.PlayerNoteId))
                                                  .ToArray();

                        playernotesToDelete.ForEach(x => session.Delete(x));

                        transaction.Commit();
                    }
                }
            }
            catch (Exception e)
            {
                LogProvider.Log.Error(this, "Could not delete player notes", e);
            }
        }
Пример #6
0
        public IEnumerable <PlayerNetWon> GetTopPlayersByNetWon(int top, IEnumerable <int> playersToExclude)
        {
            if (top < 1)
            {
                return(new List <PlayerNetWon>());
            }

            try
            {
                using (var session = ModelEntities.OpenStatelessSession())
                {
                    var playersToExcludeQuery = string.Join(",", playersToExclude);

                    var query = session.CreateSQLQuery($@"select hpt2.PlayerId, hpt2.Currency, sum(hpt2.NetWon) as NetWon from HandsPlayers hpt1 
                        join HandsPlayers hpt2 on hpt1.HandId = hpt2.HandId
                        where hpt1.PlayerId in ({playersToExcludeQuery})
                        group by hpt2.PlayerId, hpt2.Currency
                        order by NetWon desc limit 0, {top}");

                    query.SetResultTransformer(PlayerNetWonQuery.Transformer);

                    var result = query.List <PlayerNetWon>();

                    return(result);
                }
            }
            catch (Exception e)
            {
                LogProvider.Log.Error(this, $"Could not read top players by net won [{top}, {string.Join(";", playersToExclude)}]", e);
            }

            return(new List <PlayerNetWon>());
        }
Пример #7
0
        private List <IPlayer> GetPlayersListInternal()
        {
            try
            {
                List <IPlayer> players = new List <IPlayer>();

                using (var session = ModelEntities.OpenSession())
                {
                    players.AddRange(session.Query <Players>().ToArray().Select(x => new PlayerCollectionItem
                    {
                        PlayerId  = x.PlayerId,
                        PokerSite = (EnumPokerSites)x.PokersiteId,
                        Name      = x.Playername
                    }));

                    return(players);
                }
            }
            catch (Exception e)
            {
                LogProvider.Log.Error(this, "Couldn't get player list", e);
            }

            return(new List <IPlayer>());
        }
Пример #8
0
        public void SaveAlias(AliasCollectionItem aliasToSave)
        {
            using (var session = ModelEntities.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    var playersIds = aliasToSave.PlayersInAlias.Select(x => x.PlayerId).ToArray();

                    var players = session.Query <Players>().Where(x => playersIds.Contains(x.PlayerId)).ToArray();

                    var alias = new Aliases()
                    {
                        AliasId   = aliasToSave.PlayerId,
                        AliasName = aliasToSave.Name,
                        Players   = players
                    };

                    session.SaveOrUpdate(alias);

                    transaction.Commit();

                    if (aliasToSave.PlayerId == 0)
                    {
                        aliasToSave.PlayerId = alias.AliasId;
                    }
                }
            }
        }
Пример #9
0
 public Aliases GetAlias(string aliasName)
 {
     using (var session = ModelEntities.OpenSession())
     {
         return(session.Query <Aliases>().FirstOrDefault(x => x.AliasName == aliasName));
     }
 }
Пример #10
0
 public Players GetPlayer(string playerName, short pokersiteId)
 {
     using (var session = ModelEntities.OpenSession())
     {
         return(session.Query <Players>().FirstOrDefault(x => x.Playername == playerName && x.PokersiteId == pokersiteId));
     }
 }
Пример #11
0
 public Players GetPlayer(int playerId)
 {
     using (var session = ModelEntities.OpenSession())
     {
         return(session.Query <Players>().FirstOrDefault(x => x.PlayerId == playerId));
     }
 }
Пример #12
0
 public IEnumerable <ImportedFile> GetImportedFiles(IEnumerable <string> fileNames)
 {
     using (var session = ModelEntities.OpenSession())
     {
         return(GetImportedFiles(fileNames, session));
     }
 }
Пример #13
0
        private List <IPlayer> GetAliasesListInternal()
        {
            try
            {
                using (var session = ModelEntities.OpenSession())
                {
                    var aliasesEntities = session.Query <Aliases>().Fetch(x => x.Players).ToArray();

                    var aliases = aliasesEntities.Select(x => new AliasCollectionItem
                    {
                        PlayerId       = x.AliasId,
                        Name           = x.AliasName,
                        PlayersInAlias = new ObservableCollection <PlayerCollectionItem>(x.Players.Select(p =>
                                                                                                          new PlayerCollectionItem
                        {
                            PlayerId  = p.PlayerId,
                            PokerSite = (EnumPokerSites)p.PokersiteId,
                            Name      = p.Playername
                        }))
                    }).OfType <IPlayer>().ToList();

                    return(aliases);
                }
            }
            catch (Exception e)
            {
                LogProvider.Log.Error(this, "Couldn't get aliases list", e);
            }

            return(new List <IPlayer>());
        }
Пример #14
0
        public void Store(Handnotes handnotes)
        {
            try
            {
                using (var session = ModelEntities.OpenSession())
                {
                    using (var transaction = session.BeginTransaction())
                    {
                        var existingHandNote = session
                                               .Query <Handnotes>()
                                               .FirstOrDefault(x => x.PokersiteId == handnotes.PokersiteId && x.Gamenumber == handnotes.Gamenumber);

                        if (existingHandNote != null)
                        {
                            existingHandNote.HandTag = handnotes.HandTag;
                            existingHandNote.Note    = handnotes.Note;
                        }
                        else
                        {
                            existingHandNote = handnotes;
                        }

                        session.SaveOrUpdate(existingHandNote);
                        transaction.Commit();
                    }
                }
            }
            catch (Exception e)
            {
                LogProvider.Log.Error(this, "Couldn't save hand notes", e);
            }
        }
Пример #15
0
        public virtual IEnumerable <Playerstatistic> GetAllPlayerStatistic(string playerName, short?pokersiteId)
        {
            try
            {
                using (var session = ModelEntities.OpenStatelessSession())
                {
                    var player = session.Query <Players>()
                                 .FirstOrDefault(x => x.Playername.Equals(playerName) && x.PokersiteId == pokersiteId);

                    if (player == null)
                    {
                        return(new List <Playerstatistic>());
                    }

                    var files = GetPlayerFiles(player.PlayerId);

                    var result = GetAllPlayerStatisticFromFiles(files, pokersiteId);

                    return(result);
                }
            }
            catch (Exception e)
            {
                LogProvider.Log.Error(this, $"Couldn't get player's {playerName} playerName.", e);
            }

            return(new List <Playerstatistic>());
        }
Пример #16
0
        public void DeleteTournament(string tournamentId, int pokerSiteId)
        {
            if (string.IsNullOrEmpty(tournamentId))
            {
                return;
            }

            try
            {
                LogProvider.Log.Info($"Deleting tournament #{tournamentId} [{(EnumPokerSites)pokerSiteId}]");

                // get all hands and players
                using (var session = ModelEntities.OpenSession())
                {
                    var tournaments = session
                                      .Query <Tournaments>()
                                      .Where(x => x.Tourneynumber == tournamentId && x.SiteId == pokerSiteId)
                                      .Fetch(x => x.Player)
                                      .ToList();

                    var handHistories = (from handHistory in session.Query <Handhistory>()
                                         where handHistory.Tourneynumber == tournamentId && handHistory.PokersiteId == pokerSiteId
                                         select new Handhistory
                    {
                        HandhistoryId = handHistory.HandhistoryId,
                        Gamenumber = handHistory.Gamenumber,
                        HandhistoryVal = string.Empty,
                        Handtimestamp = handHistory.Handtimestamp
                    }).ToList();

                    var playersHandHistories = tournaments
                                               .Select(x => x.Player.PlayerId)
                                               .Distinct()
                                               .ToDictionary(x => x, x => handHistories);

                    using (var transaction = session.BeginTransaction())
                    {
                        playerStatisticRepository.DeletePlayerStatistic(playersHandHistories);

                        tournaments.ForEach(tournament =>
                        {
                            tournament.Player.Tourneyhands -= playersHandHistories[tournament.Player.PlayerId].Count;
                            session.Update(tournament.Player);
                        });

                        handHistories.ForEach(x => session.Delete(x));
                        tournaments.ForEach(x => session.Delete(x));

                        transaction.Commit();
                    }
                }

                LogProvider.Log.Info($"Tournament #{tournamentId} [{(EnumPokerSites)pokerSiteId}] deleted.");
            }
            catch (Exception e)
            {
                LogProvider.Log.Error(this, $"Could not delete tournament ${tournamentId} [{(EnumPokerSites)pokerSiteId}].", e);
            }
        }
Пример #17
0
        public Handhistory GetHandHistory(long gameNumber, short pokersiteId)
        {
            using (var session = ModelEntities.OpenSession())
            {
                var hh = session.Query <Handhistory>().FirstOrDefault(x => x.Gamenumber == gameNumber && x.PokersiteId == pokersiteId);

                return(hh ?? null);
            }
        }
Пример #18
0
        public Handnotes GetHandNote(long gameNumber, short pokersiteId)
        {
            using (var session = ModelEntities.OpenSession())
            {
                var hn = session.Query <Handnotes>().FirstOrDefault(x => x.Gamenumber == gameNumber && x.PokersiteId == pokersiteId);

                return(hn);
            }
        }
Пример #19
0
 public IList <Gametypes> GetPlayerGameTypes(IEnumerable <int> playerIds)
 {
     using (var session = ModelEntities.OpenStatelessSession())
     {
         return(session.Query <PlayerGameInfo>()
                .Where(x => playerIds.Contains(x.PlayerId))
                .Select(x => x.GameInfo)
                .Distinct()
                .ToList());
     }
 }
Пример #20
0
        public IEnumerable <Playernotes> GetPlayerNotes(string playerName, short pokersiteId)
        {
            using (var session = ModelEntities.OpenSession())
            {
                var playerNotes = session.Query <Playernotes>()
                                  .Where(x => x.Player.Playername == playerName && x.PokersiteId == pokersiteId)
                                  .ToArray();

                return(playerNotes);
            }
        }
Пример #21
0
        public IEnumerable <Playernotes> GetPlayerNotes(int playerId)
        {
            using (var session = ModelEntities.OpenSession())
            {
                var playerNotes = session.Query <Playernotes>()
                                  .Where(x => x.PlayerId == playerId)
                                  .ToArray();

                return(playerNotes);
            }
        }
Пример #22
0
        public void RemoveAlias(AliasCollectionItem aliasToRemove)
        {
            using (var session = ModelEntities.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    var aliasEntity = session.Load <Aliases>(aliasToRemove.PlayerId);

                    session.Delete(aliasEntity);

                    transaction.Commit();
                }
            }
        }
Пример #23
0
 public void VacuumDatabase()
 {
     try
     {
         using (var session = ModelEntities.OpenStatelessSession())
         {
             session.CreateSQLQuery("vacuum").ExecuteUpdate();
         }
     }
     catch (Exception ex)
     {
         LogProvider.Log.Error(this, "Vacuuming failed.", ex);
     }
 }
Пример #24
0
 public void Store(Playernotes playernotes)
 {
     try
     {
         using (var session = ModelEntities.OpenSession())
         {
             using (var transaction = session.BeginTransaction())
             {
                 session.SaveOrUpdate(playernotes);
                 transaction.Commit();
             }
         }
     }
     catch (Exception e)
     {
         LogProvider.Log.Error(this, "Couldn't save player notes", e);
     }
 }
Пример #25
0
 public void Store(Tournaments tournament)
 {
     try
     {
         using (var session = ModelEntities.OpenSession())
         {
             using (var transaction = session.BeginTransaction())
             {
                 session.SaveOrUpdate(tournament);
                 transaction.Commit();
             }
         }
     }
     catch (Exception e)
     {
         LogProvider.Log.Error(this, "Couldn't save tournament", e);
     }
 }
Пример #26
0
        /// <summary>
        /// Gets tournaments list for the specified ids of players
        /// </summary>
        /// <param name="playerIds"></param>
        /// <returns></returns>
        public IList <Tournaments> GetPlayerTournaments(IEnumerable <int> playerIds)
        {
            if (playerIds.IsNullOrEmpty())
            {
                return(new List <Tournaments>());
            }

            try
            {
                using (var session = ModelEntities.OpenSession())
                {
                    return(session.Query <Tournaments>().Where(x => playerIds.Contains(x.Player.PlayerId)).Fetch(x => x.Player).ToList());
                }
            }
            catch (Exception ex)
            {
                LogProvider.Log.Error(this, "Could not read tournaments", ex);
            }

            return(new List <Tournaments>());
        }
Пример #27
0
        public virtual IDictionary <string, T> GetPlayersIndicators <T>(string[] playerNames, short?pokersiteId)
            where T : Indicators, IThreadSafeIndicators
        {
            try
            {
                Players[] players;

                // get players ids
                using (var session = ModelEntities.OpenStatelessSession())
                {
                    players = session.Query <Players>()
                              .Where(x => playerNames.Contains(x.Playername) && x.PokersiteId == pokersiteId)
                              .ToArray();
                }

                var playersStatFiles = (from player in players
                                        from file in GetPlayerFiles(player.PlayerId)
                                        select new PlayerStatFile {
                    Player = player, File = file
                }).ToArray();



                var maxThreads = Environment.ProcessorCount + 1;

                var runningTasks = new List <Task>();

                var fileQueue = GetPlayerStatFileQueue(playersStatFiles);

                var playersIndicators = (from playersStatFile in playersStatFiles
                                         group playersStatFile by playersStatFile.Player.Playername into grouped
                                         where grouped.Any()
                                         let indicators = Activator.CreateInstance <T>()
                                                          select new { grouped.Key, Indicators = indicators }).ToDictionary(x => x.Key, x => x.Indicators);

                while (fileQueue.Count > 0)
                {
                    while (runningTasks.Count < maxThreads && fileQueue.Count > 0)
                    {
                        var playerStatFile = fileQueue.Dequeue();

                        runningTasks.Add(Task.Run(() =>
                        {
                            var rwLock = GetLock(playerStatFile.File);

                            rwLock.EnterUpgradeableReadLock();

                            try
                            {
                                RestoreBackupFile(playerStatFile.File, rwLock);

                                using (var sr = new StreamReaderWrapper(playerStatFile.File))
                                {
                                    string line = null;

                                    while (sr != null && ((line = sr.ReadLine()) != null))
                                    {
                                        if (!TryParsePlayerStatistic(line, playerStatFile.File, out Playerstatistic stat) || stat == null)
                                        {
                                            continue;
                                        }

                                        var indicators = playersIndicators[playerStatFile.Player.Playername];

                                        indicators.AddStatistic(stat);
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                                LogProvider.Log.Error(this, $"Failed to build indicators for {string.Join(", ", playerNames)} by reading '{playerStatFile.File}' [{pokersiteId}]", ex);
                            }
                            finally
                            {
                                rwLock.ExitUpgradeableReadLock();
                            }
                        }));
                    }

                    var completedTask = Task.WhenAny(runningTasks).Result;

                    runningTasks.Remove(completedTask);
                }

                Task.WhenAll(runningTasks).Wait();

                return(playersIndicators);
            }
            catch (Exception e)
            {
                LogProvider.Log.Error(this, $"Could not build indicators for {string.Join(", ", playerNames)} [{pokersiteId}]", e);
            }

            return(null);
        }
Пример #28
0
        public IPlayer GetActivePlayer()
        {
            IPlayer activePlayer = new PlayerCollectionItem();

            string dataPath = StringFormatter.GetActivePlayerFilePath();

            if (File.Exists(dataPath))
            {
                var splittedResult = File.ReadAllText(dataPath).Split(new string[] { Environment.NewLine }, StringSplitOptions.None);

                if (splittedResult.Length < 2)
                {
                    try
                    {
                        using (var session = ModelEntities.OpenSession())
                        {
                            var alias = session.Query <Aliases>().Fetch(x => x.Players).FirstOrDefault(x => x.AliasName.Equals(splittedResult[0]));

                            if (alias != null)
                            {
                                activePlayer = new AliasCollectionItem
                                {
                                    PlayerId       = alias.AliasId,
                                    Name           = alias.AliasName,
                                    PlayersInAlias = new ObservableCollection <PlayerCollectionItem>(alias.Players.Select(p =>
                                                                                                                          new PlayerCollectionItem
                                    {
                                        PlayerId  = p.PlayerId,
                                        PokerSite = (EnumPokerSites)p.PokersiteId,
                                        Name      = p.Playername
                                    }))
                                };
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        LogProvider.Log.Error(this, "Couldn't get active alias", e);
                    }

                    return(activePlayer);
                }

                if (!short.TryParse(splittedResult[1], out short pokerSiteId))
                {
                    return(activePlayer);
                }

                try
                {
                    using (var session = ModelEntities.OpenSession())
                    {
                        var player = session.Query <Players>().FirstOrDefault(x => x.Playername.Equals(splittedResult[0]) && x.PokersiteId == pokerSiteId);

                        if (player != null)
                        {
                            activePlayer = new PlayerCollectionItem {
                                PlayerId = player.PlayerId, Name = player.Playername, PokerSite = (EnumPokerSites)player.PokersiteId
                            };
                        }
                    }
                }
                catch (Exception e)
                {
                    LogProvider.Log.Error(this, "Couldn't get active player", e);
                }
            }

            return(activePlayer);
        }
Пример #29
0
        public void DeleteHandHistory(long handNumber, int pokerSiteId)
        {
            LogProvider.Log.Info($"Deleting hand #{handNumber} [{(EnumPokerSites)pokerSiteId}]");

            try
            {
                var handHistory = GetGame(handNumber, (short)pokerSiteId);

                if (handHistory == null)
                {
                    LogProvider.Log.Warn(this, $"Hand {handNumber} has not been found in db. So it can't be deleted.");
                    return;
                }

                var allPlayers = GetPlayersList().Where(x => x.PokerSite == (EnumPokerSites)pokerSiteId);

                var players = (from player in allPlayers
                               join hhPlayer in handHistory.Players on player.Name equals hhPlayer.PlayerName
                               select player).ToArray();

                var opponentReportService       = ServiceLocator.Current.GetInstance <IOpponentReportService>();
                var opponentReportResetRequired = players.Any(x => opponentReportService.IsPlayerInReport(x.PlayerId));

                using (var session = ModelEntities.OpenStatelessSession())
                {
                    using (var transaction = session.BeginTransaction())
                    {
                        var hh = session.Query <Handhistory>().FirstOrDefault(x => x.Gamenumber == handNumber && x.PokersiteId == pokerSiteId);

                        if (hh == null)
                        {
                            LogProvider.Log.Info(this, $"Hand [{handNumber}, {(EnumPokerSites)pokerSiteId}] could not be found in db.");
                            return;
                        }

                        var playerIds = players.Select(x => x.PlayerId).Distinct().ToArray();

                        if (playerIds.Length > 0)
                        {
                            var playersEntities = session.Query <Players>().Where(x => playerIds.Contains(x.PlayerId)).ToArray();

                            playersEntities.ForEach(p =>
                            {
                                if (handHistory.GameDescription.IsTournament)
                                {
                                    p.Tourneyhands--;
                                }
                                else
                                {
                                    p.Cashhands--;
                                }

                                session.Update(p);
                            });
                        }

                        if (hh != null)
                        {
                            session.Delete(hh);
                        }

                        var playerHandsToDelete = playerIds.ToDictionary(x => x, x => new List <Handhistory> {
                            hh
                        });

                        playerStatisticRepository.DeletePlayerStatistic(playerHandsToDelete);

                        transaction.Commit();
                    }
                }

                if (opponentReportResetRequired)
                {
                    opponentReportService.Reset();
                }

                LogProvider.Log.Info($"Hand #{handNumber} [{(EnumPokerSites)pokerSiteId}] deleted.");
            }
            catch (Exception ex)
            {
                LogProvider.Log.Error(this, $"Could not delete hand {handNumber} {(EnumPokerSites)pokerSiteId}", ex);
            }
        }