Пример #1
0
        public IActionResult Position([FromBody] PositionDTO position)
        {
            if (position == null)
            {
                throw new ArgumentNullException(nameof(position));
            }
            if (!positionRepository.AddPosition(position))
            {
                return(BadRequest("Position couldn't be saved"));
            }
            foreach (KeyValuePair <string, DateTime> pair in new Dictionary <string, DateTime>(CesiumHub.ClientsActive))
            {
                if (pair.Value != DateTime.MinValue && (DateTime.Now - pair.Value).TotalMinutes > 10)
                {
                    CesiumHub.ClientsActive[pair.Key] = DateTime.MinValue;
                    CesiumHub.ClientsConnections.Remove(pair.Key);
                    wsContext.Clients.All.SendAsync("InactiveUser", pair.Key);
                }
            }
            if (CesiumHub.ClientsActive.GetValueOrDefault(position.Name, DateTime.MinValue) == DateTime.MinValue)
            {
                CesiumHub.ClientsActive[position.Name] = position.Time;
                wsContext.Clients.All.SendAsync("NewActiveUser", position.Name);
            }

            var nearArtifacts = artifactRepository.GetWonArtifacts(position.Longitude, position.Latitude, position.Time);

            if (nearArtifacts.Count > 0)
            {
                foreach (var artifact in nearArtifacts)
                {
                    wsContext.Clients.All.SendAsync("ArtifactWon", artifact.Id.ToString());
                }
                artifactRepository.GivePoints(nearArtifacts.Count, position.Name);
            }


            wsContext.Clients.Group(position.Name).SendAsync("Position", position.Name, position.Latitude, position.Longitude);
            return(Ok());
        }