Esempio n. 1
0
        /// <summary>
        /// Get or add session data to cache for specified session (must be used inside write lock)
        /// </summary>
        /// <param name="session">Session</param>
        /// <returns>Session data of specified session</returns>
        private SessionCacheData GetOrAddSessionData(SessionCreationInfo sessionCreationInfo)
        {
            Check.Require(!cacheLock.IsWriteLockHeld, "Write lock must be held before using this function");

            SessionCacheData sessionData = null;

            if (cachedData.ContainsKey(sessionCreationInfo.Session))
            {
                sessionData = cachedData[sessionCreationInfo.Session];
                sessionData.LastModified = DateTime.Now;
            }
            else
            {
                sessionData = new SessionCacheData
                {
                    PokerSiteId     = sessionCreationInfo.PokerSiteId,
                    IsTourney       = sessionCreationInfo.IsTourney,
                    PokerGameTypeId = sessionCreationInfo.PokerGameTypeId
                };

                cachedData.Add(sessionCreationInfo.Session, sessionData);
            }

            RemoveExpiredSessions();

            return(sessionData);
        }
Esempio n. 2
0
        /// <summary>
        /// Prepares data to update player stickers
        /// </summary>
        /// <param name="playersStickersCacheData"></param>
        /// <param name="sessionData"></param>
        /// <returns></returns>
        private List <PlayerStickersCacheDataInfo> PreparePlayerStickersCacheDataInfo(IEnumerable <PlayerStickersCacheData> playersStickersCacheData,
                                                                                      SessionCacheData sessionData)
        {
            var playersStickersCacheDataInfo = new List <PlayerStickersCacheDataInfo>();

            foreach (var playerStickersCacheData in playersStickersCacheData)
            {
                var layoutWasChanged = !sessionData.LayoutByPlayer.ContainsKey(playerStickersCacheData.Player) ||
                                       sessionData.LayoutByPlayer[playerStickersCacheData.Player] != playerStickersCacheData.Layout;

                if (layoutWasChanged)
                {
                    if (sessionData.StickersStatisticByPlayer.ContainsKey(playerStickersCacheData.Player))
                    {
                        sessionData.StickersStatisticByPlayer[playerStickersCacheData.Player]?.Clear();
                    }

                    if (sessionData.LayoutByPlayer.ContainsKey(playerStickersCacheData.Player))
                    {
                        sessionData.LayoutByPlayer[playerStickersCacheData.Player] = playerStickersCacheData.Layout;
                    }
                    else
                    {
                        sessionData.LayoutByPlayer.Add(playerStickersCacheData.Player, playerStickersCacheData.Layout);
                    }
                }

                Dictionary <string, HudStickerIndicators> playerStickersDictionary = null;

                if (sessionData.StickersStatisticByPlayer.ContainsKey(playerStickersCacheData.Player))
                {
                    playerStickersDictionary = sessionData.StickersStatisticByPlayer[playerStickersCacheData.Player];
                }
                else
                {
                    playerStickersDictionary = new Dictionary <string, HudStickerIndicators>();
                    sessionData.StickersStatisticByPlayer.Add(playerStickersCacheData.Player, playerStickersDictionary);
                }


                var playerStickersCacheDataInfo = new PlayerStickersCacheDataInfo
                {
                    PlayerStickersCacheData  = playerStickersCacheData,
                    PlayerStickersDictionary = playerStickersDictionary,
                    ReloadRequired           = layoutWasChanged
                };

                playersStickersCacheDataInfo.Add(playerStickersCacheDataInfo);
            }

            return(playersStickersCacheDataInfo);
        }
Esempio n. 3
0
        /// <summary>
        /// Gets player session cache data using existing data or loading from DB
        /// </summary>
        /// <param name="cacheInfos"></param>
        /// <param name="sessionData"></param>
        /// <returns></returns>
        private Dictionary <PlayerStatsSessionCacheInfo, SessionCacheStatistic> GetSessionCacheStatistic(IEnumerable <PlayerStatsSessionCacheInfo> cacheInfos,
                                                                                                         SessionCacheData sessionData)
        {
            var playerSessionCacheStatistic = new Dictionary <PlayerStatsSessionCacheInfo, SessionCacheStatistic>();

            var playerSessionCacheStatisticTasks = new List <Task>();

            foreach (var cacheInfo in cacheInfos)
            {
                if (playerSessionCacheStatistic.ContainsKey(cacheInfo))
                {
                    continue;
                }

                SessionCacheStatistic sessionCacheStatistic = null;

                var isNewSessionCacheStatistic = true;

                // existing statistic
                if (sessionData.StatisticByPlayer.ContainsKey(cacheInfo.Player))
                {
                    sessionCacheStatistic      = sessionData.StatisticByPlayer[cacheInfo.Player];
                    isNewSessionCacheStatistic = false;
                }
                // hero starts empty session
                else if (cacheInfo.IsHero)
                {
                    sessionCacheStatistic = new SessionCacheStatistic
                    {
                        IsHero     = true,
                        GameFormat = cacheInfo.GameFormat
                    };

                    sessionCacheStatistic.PlayerData.InitializeHeatMaps(sessionData.HeatMapStats);
                }
                // try to check existing sessions for this player
                else
                {
                    var similarPlayerSession = cachedData.Values.FirstOrDefault(x => x.PokerSiteId == sessionData.PokerSiteId &&
                                                                                x.IsTourney == sessionData.IsTourney &&
                                                                                GameTypeUtils.CompareGameType((GameType)x.PokerGameTypeId, (GameType)sessionData.PokerGameTypeId) &&
                                                                                ((x.Filter == null && sessionData.Filter == null) || x.Filter.Equals(sessionData.Filter)) &&
                                                                                x.StatisticByPlayer.ContainsKey(cacheInfo.Player));

                    if (similarPlayerSession != null &&
                        similarPlayerSession.StatisticByPlayer[cacheInfo.Player].PlayerData.TotalHands >= TotalHandsToUseExistingSession)
                    {
                        sessionCacheStatistic = similarPlayerSession.StatisticByPlayer[cacheInfo.Player].Clone();
                    }
                }

                var needToReadStatistic = sessionCacheStatistic == null;

                if (needToReadStatistic)
                {
                    sessionCacheStatistic = new SessionCacheStatistic
                    {
                        GameFormat = cacheInfo.GameFormat
                    };
                }

                playerSessionCacheStatistic.Add(cacheInfo, sessionCacheStatistic);

                if (isNewSessionCacheStatistic)
                {
                    sessionData.StatisticByPlayer.Add(cacheInfo.Player, sessionCacheStatistic);
                }

                if (!needToReadStatistic)
                {
                    continue;
                }

                sessionCacheStatistic.PlayerData.InitializeHeatMaps(sessionData.HeatMapStats);

                // read data from statistic
                var taskToReadPlayerStats = Task.Run(() =>
                                                     playerStatisticRepository
                                                     .GetPlayerStatistic(cacheInfo.Player.PlayerId)
                                                     .Where(stat => (stat.PokersiteId == (short)cacheInfo.Player.PokerSite) &&
                                                            stat.IsTourney == cacheInfo.Stats.IsTourney &&
                                                            GameTypeUtils.CompareGameType((GameType)stat.PokergametypeId, (GameType)cacheInfo.Stats.PokergametypeId) &&
                                                            (sessionData.Filter == null || sessionData.Filter.Apply(stat)) &&
                                                            stat.GameNumber != cacheInfo.Stats.GameNumber)
                                                     .ForEach(stat => sessionCacheStatistic.PlayerData.AddStatistic(stat))
                                                     );

                playerSessionCacheStatisticTasks.Add(taskToReadPlayerStats);
            }

            Task.WhenAll(playerSessionCacheStatisticTasks).Wait();

            return(playerSessionCacheStatistic);
        }