private async Task <bool> RegisterPlayersInternal(IEnumerable <Player> list) { var players = list.ToList(); if (!HaveChanges(players, x => x.Num)) { return(false); } var prevoius = new HashSet <string>(_previousState.Select(x => x.Guid).Distinct()); _previousState = players; var guids = players.Select(x => x.Guid).ToList(); var playersInDb = (await _playerRepository.GetPlayersAsync(guids)).ToArray(); var dbGuids = playersInDb.Select(x => x.GUID).ToList(); var historyToAdd = new List <PlayerHistory>(); var playerToUpdate = new List <PlayerDto>(); foreach (var player in playersInDb) { var p = players.FirstOrDefault(x => x.Guid == player.GUID); if (p != null) { var needUpdate = false; if ((player.Name != p.Name) || (player.LastIp != p.IP)) { historyToAdd.Add(new PlayerHistory { IP = p.IP, Name = p.Name, PlayerId = player.Id, ServerId = _serverId }); player.Name = p.Name; player.LastIp = p.IP; needUpdate = true; } if (prevoius.Contains(player.GUID) == false) { player.LastSeen = DateTime.UtcNow; needUpdate = true; } if (string.IsNullOrEmpty(player.SteamId)) { player.SteamId = _steamService.GetSteamId(player.GUID)?.ToString(); needUpdate = true; } if (needUpdate) { playerToUpdate.Add(player); } } } var newplayers = players.Where(x => !dbGuids.Contains(x.Guid)).ToList(); if (newplayers.Any()) { foreach (var p in newplayers) { var np = new Arma3BEClient.Libs.ModelCompact.Player { GUID = p.Guid, Name = p.Name, Id = Guid.NewGuid(), LastIp = p.IP, SteamId = _steamService.GetSteamId(p.Guid)?.ToString() }; playerToUpdate.Add(np); historyToAdd.Add(new PlayerHistory { IP = np.LastIp, Name = np.Name, PlayerId = np.Id, ServerId = _serverId }); } } await _playerRepository.AddOrUpdateAsync(playerToUpdate); await _playerRepository.AddHistoryAsync(historyToAdd); return(true); }