private void Initialize() { timerPlayerList = new Timer((sender) => { UpdateOnlinePlayers(sender); }, this, 0, 30000); Players = new PlayersCollection(); Players.OnPlayerJoin = (player) => { if (player == null) { return; } var existing = OnlinePlayers.FirstOrDefault(p => p.NickId.Equals(player.NickId)); if (existing == null) { lock (lockOnlinePlayers) { OnlinePlayers.Add(player); } } else { existing.Country = player.Country; } }; Players.OnPlayerLeave = (player) => { if (player == null) { lock (lockOnlinePlayers ) { OnlinePlayers.RemoveAll(p => p.NickId.Equals(player.NickId)); } } }; GameObjects = new GameObjectsCollection(); AirFields = new AirFieldCollection(); CoalitionIndexes = new List <CoalitionIndex>(); }
private void UpdateOnlinePlayers(object sender) { bool playerListChanged = false; var server = sender as Server; if (sender == null || !server.IsRconConnected || Rcon == null) { return; } var newList = Rcon.GetPlayerList().With(list => list.Where(p => p != null)); if (newList == null) { return; } if (newList.Count() == 0) { lock (server.lockOnlinePlayers) OnlinePlayers.Clear(); if (OnPlayerListChange != null) { OnPlayerListChange(OnlinePlayers.ToList(), this); } return; } HashSet <Guid> onlineIds = new HashSet <Guid> (newList.Select(player => player.NickId)); lock (server.lockOnlinePlayers) { playerListChanged = OnlinePlayers.Count(p => !onlineIds.Contains(p.NickId)) > 0; OnlinePlayers.RemoveAll(player => !onlineIds.Contains(player.NickId)); foreach (var player in newList) { var existing = OnlinePlayers.FirstOrDefault(p => p.NickId.Equals(player.NickId)); if (existing == null) { OnlinePlayers.Add(player); playerListChanged = true; } else { if (!playerListChanged) { playerListChanged = existing.Ping != player.Ping || existing.Status != player.Status || existing.CoalitionIndex != player.CoalitionIndex; } existing.Ping = player.Ping; existing.Status = player.Status; existing.ClientId = player.ClientId; existing.CoalitionIndex = player.CoalitionIndex; } } if (playerListChanged && OnPlayerListChange != null) { OnPlayerListChange(OnlinePlayers.ToList(), this); } } }