public TrackingInstance(ILogger <TrackingInstance> logger, ITrackingConfigFactory trackingConfigFactory, ISentimentAnalysis sentiment, ITrackingManager manager, ITwitPersistency persistency)
        {
            if (trackingConfigFactory == null)
            {
                throw new ArgumentNullException(nameof(trackingConfigFactory));
            }

            this.sentiment   = sentiment ?? throw new ArgumentNullException(nameof(sentiment));
            this.manager     = manager ?? throw new ArgumentNullException(nameof(manager));
            this.persistency = persistency ?? throw new ArgumentNullException(nameof(persistency));
            this.logger      = logger ?? throw new ArgumentNullException(nameof(logger));
            Trackers         = trackingConfigFactory.GetTrackers();
            foreach (var tracker in Trackers.Where(item => !item.IsKeyword))
            {
                if (users.Contains(tracker.Keyword))
                {
                    logger.LogWarning("Keyword is already added {0}", tracker.Keyword);
                    continue;
                }

                users.Add(tracker.Keyword);
            }

            Languages = trackingConfigFactory.GetLanguages();
        }
        public async Task <int> SaveChanges()
        {
            var count      = 0;
            var allInserts = Trackers.Where(tracker => tracker.Value.State == State.Added).Select(tracker => tracker.Value).ToList();

            var groupedBulkInserts = allInserts.GroupBy(b => b.Type);

            foreach (var group in groupedBulkInserts)
            {
                var method = typeof(MongoContext).GetTypeInfo().GetMethod(nameof(InsertBatch), System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
                method = method.MakeGenericMethod(group.Key);
                var   task = (Task)method.Invoke(this, new[] { group });
                await task;
                count += group.Count();
            }


            var saveChangesMethod = typeof(MongoContext).GetTypeInfo().GetMethod(nameof(SaveChangesGeneric), System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);

            foreach (var trackerKeyValuePair in Trackers)
            {
                var genericSaveChangesMethod = saveChangesMethod.MakeGenericMethod(trackerKeyValuePair.Value.Type);
                var task = (Task <bool>)genericSaveChangesMethod.Invoke(this, new[] { trackerKeyValuePair.Value });
                if (await task)
                {
                    count++;
                }
            }

            return(count);
        }
        public async Task LoadRemoteAsync()
        {
            ChangeTracker.AutoDetectChangesEnabled = false;

            await Guilds
            .LoadAsync()
            .ConfigureAwait(false);

            await Channels
            .LoadAsync()
            .ConfigureAwait(false);

            await Games
            .LoadAsync()
            .ConfigureAwait(false);

            await Trackers
            .Where(t => t.State != TrackerState.Dead)
            .LoadAsync()
            .ConfigureAwait(false);

            await Races
            .Include(r => r.Entrants)
            .Include(r => r.Announcements)
            .Where(r => r.IsActive)
            .LoadAsync()
            .ConfigureAwait(false);
        }
        private void RequestPeers()
        {
            //считаем сколько нужно пиров
            int needPeersCount = normalPeersCount - Peers.Where(peer => peer.Status == PeerStatus.Connected).Count();

            if (needPeersCount <= 0)
            {
                return;
            }


            //сначала ищем пиры без трекера
            if (VotingsUser.PeerDiscovery)
            {
                try
                {
                    PeerDiscovery.DiscoverPeersAsync(PeerDiscovery.DiscoveryMethod.UDPBroadcast);
                }
                catch { }
            }



            //пиры подключенные прямо (сортируем по кол-ву запросов)
            var connectedPeersDirect = Peers.Where(peer => peer.Status == PeerStatus.Connected && peer.ConnectionMode == ConnectionMode.Direct)
                                       .OrderBy(peer => peer.PeersRequestsCount);

            //подключенные трекеры (сортируем по кол-ву запросов)
            var connectedTrackers = Trackers.Where(tracker => tracker.Status == TrackerStatus.Connected)
                                    .OrderBy(tracker => tracker.PeersRequestsCount);

            //пиры подключенные через трекер (сортируем по кол-ву запросов)
            var connectedPeersWithTracker = Peers.Where(peer => peer.Status == PeerStatus.Connected && peer.ConnectionMode == ConnectionMode.WithTracker)
                                            .OrderBy(peer => peer.PeersRequestsCount);

            //если есть подключенные пиры и трекеры
            if (connectedPeersDirect.Any() && connectedTrackers.Any())
            {
                var firstPeerDirect = connectedPeersDirect.First();
                var firstTracker    = connectedTrackers.First();

                //запрашиваем у первого прямого пира, если к нему было меньше запросов чем к первому трекеру
                if (firstPeerDirect.PeersRequestsCount <= firstTracker.PeersRequestsCount)
                {
                    firstPeerDirect.RequestPeers(needPeersCount);
                }
                //если есть подключенные через трекеры пиры
                else if (connectedPeersWithTracker.Any())
                {
                    var firstPeerWithTracker = connectedPeersWithTracker.First();

                    //запрашиваем у первого трекера, если к нему было меньше запросов чем к первому подключенному через трекер пиру
                    if (firstTracker.PeersRequestsCount <= firstPeerWithTracker.PeersRequestsCount)
                    {
                        firstTracker.RequestPeersFromTracker(needPeersCount);
                    }
                    else
                    {
                        firstPeerWithTracker.RequestPeers(needPeersCount);
                    }
                }
                else
                {
                    firstTracker.RequestPeersFromTracker(needPeersCount);
                }
            }
            else if (connectedPeersDirect.Any())
            {
                var firstPeerDirect = connectedPeersDirect.First();
                firstPeerDirect.RequestPeers(needPeersCount);
            }
            else if (connectedTrackers.Any())
            {
                var firstTracker = connectedTrackers.First();
                firstTracker.RequestPeersFromTracker(needPeersCount);
            }
        }
Exemple #5
0
    void LoadReplay(string name)
    {
        List <FixedQueue <StateDescriptor> > fieldStates;
        List <FixedQueue <StateDescriptor> > robotStates;
        Dictionary <string, List <FixedQueue <StateDescriptor> > > gamePieceStates;
        List <List <KeyValuePair <ContactDescriptor, int> > >      contacts;

        string simSelectedField;
        string simSelectedRobot;

        ReplayImporter.Read(name, out simSelectedField, out simSelectedRobot, out fieldStates, out robotStates, out gamePieceStates, out contacts);

        LoadField(simSelectedField);
        LoadRobot(simSelectedRobot);

        List <Tracker> robotTrackers = Trackers.Where(x => x.transform.parent.name.Equals("Robot")).ToList();
        List <Tracker> fieldTrackers = Trackers.Except(robotTrackers).ToList();

        int i = 0;

        foreach (Tracker t in fieldTrackers)
        {
            t.States = fieldStates[i];
            i++;
        }

        i = 0;

        foreach (Tracker t in robotTrackers)
        {
            t.States = robotStates[i];
            i++;
        }

        foreach (KeyValuePair <string, List <FixedQueue <StateDescriptor> > > k in gamePieceStates)
        {
            GameObject referenceObject = GameObject.Find(k.Key);

            if (referenceObject == null)
            {
                continue;
            }

            foreach (FixedQueue <StateDescriptor> f in k.Value)
            {
                GameObject currentPiece = UnityEngine.Object.Instantiate(referenceObject);
                currentPiece.name = "clone_" + k.Key;
                currentPiece.GetComponent <Tracker>().States = f;
            }
        }

        foreach (var c in contacts)
        {
            if (c != null)
            {
                List <ContactDescriptor> currentContacts = new List <ContactDescriptor>();

                foreach (var d in c)
                {
                    ContactDescriptor currentContact = d.Key;
                    currentContact.RobotBody = robotTrackers[d.Value].GetComponent <BRigidBody>();
                    currentContacts.Add(currentContact);
                }

                contactPoints.Add(currentContacts);
            }
            else
            {
                contactPoints.Add(null);
            }
        }
    }