private void PlayerEventsOnPlayerUpdated(object sender, PlayerCache playerCache)
        {
            PrintScore(playerCache);
            PrintKillLabel(playerCache);
            PrintDeathLabel(playerCache);
            PrintSuicideLabel(playerCache);
            PrintTooHighKD(playerCache);

            PrintNewTopKiller(playerCache);
            PrintNewTopScorer(playerCache);
        }
 private IEnumerable<PlayerHistoryItem> GetHistory(PlayerCache playerCache, TimeSpan scoreReportDelay)
 {
     // Get player history
     var history = playerCache.CopyHistory();
     for (int i = history.Count() - 1; i > -1; i--)
     {
         var historyItem = history[i];
         var since = DateTime.Now.Subtract(historyItem.Time);
         if (since > scoreReportDelay)
             yield break;
         yield return historyItem;
     }
 }
        private void PrintSuicideLabel(PlayerCache playerCache)
        {
            // Print some simple kill stats to player and all when at certain threshold

            // Did player kill someone?
            if (playerCache.LastPlayerDelta.Score.Suicides < 1)
                return;

            _rconClient.SendMessageAll("KD: " + playerCache.Player.Name + " suicided.");
        }
        private void PrintTooHighKD(PlayerCache playerCache)
        {
            // Did player kill someone?
            if (playerCache.LastPlayerDelta.Score.Kills < 1)
                return;

            if (playerCache.Player.Score.KillDeathRatio >= _warnCheatAtKDRatio)
            {
                _rconClient.SendMessageAll("Warning: " + playerCache.Player.Name + " has very high kill-death ratio: " +
                                           playerCache.Player.Score.KillDeathRatio + "(" + playerCache.Player.Score.Kills + " kills / " + playerCache.Player.Score.Deaths + " deaths)");
                _rconClient.SendMessageAll("Warning: If " + playerCache.Player.Name +
                                           " is cheating consider a VIP kick vote. Type: /kick " + playerCache.Player.Name);
            }
        }
        private void PrintNewTopScorer(PlayerCache playerCache)
        {
            if (_lastTopScorer == null)
            {
                _lastTopScorer = playerCache.Player.Clone();
                //foreach (var player in _rconClient.PlayerListCommand.GetPlayers())
                //{
                //    if (_lastTopScorer == null || player.Score.Score > _lastTopScorer.Score.Score)
                //        _lastTopScorer = player;
                //}
            }

            if (_lastTopScorer.Name != playerCache.Player.Name && playerCache.Player.Score.Score > _lastTopScorer.Score.Score)
            //if (playerCache.Player.Score.Score > _lastTopScorer.Score.Score)
            {
                if (_canReportTopScoreAndKill && playerCache.Player.Score.Score > _minimumScoreToReportTopScorer)
                {
                    if (!playerCache.Settings.StopSpam)
                    {

                        _rconClient.SendMessagePlayer(playerCache.Player,
                                                      "KD: You just replaced " + _lastTopScorer.Name +
                                                      " as top scorer this round.");
                    }
                    var other = playerEvents.GetPlayerCache(_lastTopScorer);
                    if (other != null && !other.Settings.StopSpam)
                    {
                        _rconClient.SendMessagePlayer(_lastTopScorer,
                                                      "KD: You just got replaced as top scorer by " +
                                                      playerCache.Player.Name +
                                                      ".");
                    }
                }
                _lastTopScorer = playerCache.Player.Clone();
            }
        }
        private void PrintScore(PlayerCache playerCache)
        {
            // Print some simple kill stats to player and all when at certain threshold
            if (playerCache.LastPlayerDelta.Score.Score < 1)
                return;
            var lastReportedTime = DateTime.Now.Subtract(playerCache.LastScoreReportedTime);
            // Waiting since last report?
            if (lastReportedTime < _scoreReportDelay)
                return;

            var historyItems = new List<PlayerHistoryItem>(GetHistory(playerCache, _scoreReportHistory));
            if (historyItems.Count() == 0)
                return;
            DateTime oldestUpdate = historyItems.Last().Player.LastUpdate;
            //DateTime newestUpdate = historyItems.Last().Player.LastUpdate;
            var timeSinceOldest = DateTime.Now.Subtract(oldestUpdate);
            //var timeSinceNewest = DateTime.Now.Subtract(newestUpdate);

            int score = 0;
            foreach (var historyItem in historyItems)
            {
                score += historyItem.PlayerDelta.Score.Score;
                //if (score > _minimumScoreToReport)
                //    return;
            }
            var oldestSec = (int)timeSinceOldest.TotalSeconds;
            // Message player
            if (!playerCache.Settings.StopSpam && score > _minimumScoreToReport && score > playerCache.LastScoreReported - lastReportedTime.TotalSeconds)
            {
                //var tmpScore = playerCache.LastScoreReported;
                playerCache.LastScoreReported = score;
                playerCache.LastScoreReportedTime = DateTime.Now;
                //score -= tmpScore;

                if ((int)timeSinceOldest.TotalSeconds != 0)
                    _rconClient.SendMessagePlayer(playerCache.Player,
                                              "KD: Score last " + (int)_scoreReportHistory.TotalSeconds + " seconds: " + score);
            }
        }
        private void PrintKillLabel(PlayerCache playerCache)
        {
            // Print some simple kill stats to player and all when at certain threshold

            // Did player kill someone?
            if (playerCache.LastPlayerDelta.Score.Kills < 1)
                return;

            var historyItems = new List<PlayerHistoryItem>(GetHistory(playerCache, _killPeriodLookbehind));
            if (historyItems.Count() == 0)
                return;

            if (_lookingForFirstKill)
            {
                _lookingForFirstKill = false;
                _rconClient.SendMessageAll("KD: First kill: " + playerCache.Player.Name + " in " + _rconClient.ServerInfoCommand.ServerInfo.ElapsedRoundTime + " seconds.");
            }

            int kills = 0;
            DateTime oldestUpdate = default(DateTime);
            PlayerHistoryItem lastWithKill = null;
            foreach (var historyItem in historyItems)
            {
                kills += historyItem.PlayerDelta.Score.Kills;
                if (historyItem.PlayerDelta.Score.Kills > 0)
                    lastWithKill = historyItem;
            }
            // Last kill watch
            if (lastWithKill != null)
            {
                oldestUpdate = lastWithKill.Player.LastUpdate;
                _lastKills.Enqueue(lastWithKill);
            }
            // Last kill queue length limit
            while (_lastKills.Count > _lastkillsLength)
            {
                _lastKills.Dequeue();
            }

            var timeSinceOldest = DateTime.Now.Subtract(oldestUpdate);

            // Message player
            if (kills >= _reportKillsToPersonAt && !playerCache.Settings.StopSpam)
                _rconClient.SendMessagePlayer(playerCache.Player, "KD: Last " + (int)timeSinceOldest.TotalSeconds + " seconds: " + kills + " kills.");
            if (kills >= _reportKillsToAllAt)
                _rconClient.SendMessageAll("KD: " + playerCache.Player.Name + " has " + kills + " kills in just " + (int)timeSinceOldest.TotalSeconds + " seconds.");
        }
        private void PrintDeathLabel(PlayerCache playerCache)
        {
            // Print some simple Death stats to player and all when at certain threshold

            // Did player Death someone?
            if (playerCache.LastPlayerDelta.Score.Deaths < 1)
                return;

            var historyItems = new List<PlayerHistoryItem>(GetHistory(playerCache, _deathPeriodLookbehind));
            if (historyItems.Count() == 0)
                return;

            if (_lookingForFirstDeath)
            {
                _lookingForFirstDeath = false;
                _rconClient.SendMessageAll("KD: First Death: " + playerCache.Player.Name);
            }

            int deaths = 0;
            DateTime oldestUpdate = default(DateTime);
            PlayerHistoryItem lastWithDeath = null;
            foreach (var historyItem in historyItems)
            {
                deaths += historyItem.PlayerDelta.Score.Deaths;
                if (historyItem.PlayerDelta.Score.Deaths > 0)
                    lastWithDeath = historyItem;
            }
            // Last Death watch
            if (lastWithDeath != null)
            {
                oldestUpdate = lastWithDeath.Player.LastUpdate;
                _lastDeaths.Enqueue(lastWithDeath);
            }
            // Last Death queue length limit
            while (_lastDeaths.Count > _lastDeathsLength)
            {
                _lastDeaths.Dequeue();
            }

            var timeSinceOldest = DateTime.Now.Subtract(oldestUpdate);

            // Message player
            if (deaths > 1 && !playerCache.Settings.StopSpam)
            {

                _rconClient.SendMessagePlayer(playerCache.Player,
                                              "KD: Last " + (int)timeSinceOldest.TotalSeconds + " seconds: " + deaths +
                                              " deaths.");
            }
            if (deaths > 3)
                _rconClient.SendMessageAll("KD: " + playerCache.Player.Name + " has " + deaths + " deaths in just " + (int)timeSinceOldest.TotalSeconds + " seconds.");
        }
        private void PlayerListCommandOnPlayerJoined(object sender, Player player)
        {
            lock (_players)
            {
                // Add player to our cache

                //var pcs = (from pc in _playerStorage.PlayerCaches
                //          where pc.Player.Name == player.Name
                //          select pc).ToList<PlayerCache>();
                //PlayerCache dPlayer;
                //if (pcs.Count != 0)
                //{
                //    dPlayer = pcs.First();
                //    dPlayer.Player = player;
                //}
                //else
                //{
                //    // Create PlayerCache in DB and save
                var dPlayer = new PlayerCache();
                dPlayer.Player = player;
                //_playerStorage.PlayerCaches.Add(dPlayer);
                //try
                //{
                //    _playerStorage.SaveChanges();
                //}
                //catch (Exception exception)
                //{
                //    Debug.WriteLine(exception.ToString());
                //}
                //}

                _players.Add(player, dPlayer);
            }
        }
 protected virtual void OnPlayerUpdated(PlayerCache playerCache)
 {
     PlayerUpdatedDelegate handler = PlayerUpdated;
     if (handler != null) handler(this, playerCache);
 }
 private void PlayerEventsOnPlayerUpdated(object sender, PlayerCache playercache)
 {
     var name = playercache.Player.Name;
     if (playercache.LastPlayerDelta.Score.Kills > 0)
         SendToTrackers(playercache.Player, string.Format("KD: {0} kills +{1}. Total: {2}", name, playercache.LastPlayerDelta.Score.Kills, playercache.Player.Score.Kills));
     if (playercache.LastPlayerDelta.Score.Deaths > 0)
         SendToTrackers(playercache.Player, string.Format("KD: {0} deaths +{1}. Total: {2}", name, playercache.LastPlayerDelta.Score.Deaths, playercache.Player.Score.Deaths));
     if (playercache.LastPlayerDelta.Score.Suicides > 0)
         SendToTrackers(playercache.Player, string.Format("KD: {0} suicides +{1}. Total: {2}", name, playercache.LastPlayerDelta.Score.Suicides, playercache.Player.Score.Suicides));
 }