Example #1
0
        public bool RegisterPlayersInternal(IEnumerable <Player> list)
        {
            var players = list.ToList();

            if (!HaveChanges(players, x => x.Num))
            {
                return(false);
            }


            var guids = players.Select(x => x.Guid).ToList();

            var playersInDb = _playerRepository.GetPlayers(guids);
            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       = player.LastIp,
                            Name     = player.Name,
                            PlayerId = player.Id,
                            ServerId = _serverId
                        });

                        player.Name   = p.Name;
                        player.LastIp = p.IP;

                        needUpdate = true;
                    }
                    if ((DateTime.UtcNow - player.LastSeen).TotalHours > 2)
                    {
                        player.LastSeen = DateTime.UtcNow;
                        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
                    };

                    playerToUpdate.Add(np);

                    historyToAdd.Add(new PlayerHistory
                    {
                        IP       = np.LastIp,
                        Name     = np.Name,
                        PlayerId = np.Id,
                        ServerId = _serverId
                    });
                }
            }

            _playerRepository.AddOrUpdate(playerToUpdate);
            _playerRepository.AddHistory(historyToAdd);


            return(true);
        }
Example #2
0
 public void AddHistory(List <PlayerHistory> histories)
 {
     _playerRepository.AddHistory(histories);
 }