Esempio n. 1
0
        public void QueueProcessing(double time)
        {
            foreach (var queue in _queues.Values)
            {
                var complectingUsers = new List <List <UserInQueue> >();
                var complectedUsers  = new List <List <UserInQueue> >();
                foreach (var user in queue.Queue)
                {
                    var complect = queue.Mode.BotsOnly ?
                                   null :
                                   complectingUsers.FirstOrDefault(x => !x.Any(complectingUser => complectingUser.UserId == user.UserId));
                    if (complect != null)
                    {
                        complect.Add(user);
                        if (complect.Count >= queue.Mode.MaxPlayers)
                        {
                            complectedUsers.Add(complect);
                            complectingUsers.RemoveAt(0);
                        }
                    }
                    else
                    {
                        complectingUsers.Add(new List <UserInQueue>()
                        {
                            user
                        });
                    }

                    user.Time += time;
                }

                if (_bots.Count() > 0 && queue.Mode.TimeTillBot.HasValue)
                {
                    foreach (var complectingUsersItem in complectingUsers)
                    {
                        if (complectingUsersItem.Average(x => x.Time) >= queue.Mode.TimeTillBot.Value)
                        {
                            while (complectingUsersItem.Count < queue.Mode.MaxPlayers)
                            {
                                var chosenBot = _botsRandom.Next(_bots.Count() * RandomModifier) % _bots.Count();
                                complectingUsersItem.Add(new UserInQueue()
                                {
                                    UserId = _bots.Skip(chosenBot).First().BotId
                                });
                            }

                            complectedUsers.Add(complectingUsersItem);
                        }
                    }
                }

                var allComplectedActors = complectedUsers.SelectMany(actor => actor).ToList();
                queue.Queue = queue.Queue.Where(x => !allComplectedActors.Contains(x)).ToHashSet();
                foreach (var complect in complectedUsers)
                {
                    Task.Run(async() => await _battleService.StartNewBattleAsync(queue.Mode, complect));
                }
            }
        }