private List <AvailableSlot> GetAdditionalAvailableTableSlots(Graph graph, Tuple <GraphPath, GraphPath> path, RSATable table)
        {
            List <AvailableSlot> emptys = base.GetAvailableTableSlots(graph, path.Item2, table);

            foreach (DemandLinkPair savedMainPath in this.DemandSupplyMemory)
            {
                if (HasOverrideLink(path.Item1, savedMainPath.TuplePaths.Item1))
                {
                    continue;
                }

                foreach (GraphLink link in path.Item2.ToLinks(graph.Links))
                {
                    AvailableSlot availableSlot = new AvailableSlot
                    {
                        Link       = link,
                        Availables = table.Table[link.GetLinkId()].Where(r => r.Value.IsProtectionDemand && r.Value.Values.Count() == 1 && r.Value.Values.Contains(savedMainPath.Demand.Id.ToString())).Select(r => r.Key).ToList()
                    };

                    var reference = emptys.FirstOrDefault(r => r.Link.GetLinkId() == link.GetLinkId());

                    if (reference != null)
                    {
                        availableSlot.Availables.AddRange(reference.Availables);
                        emptys.Remove(reference);
                    }
                    emptys.Insert(0, availableSlot);
                }
            }

            return(emptys);
        }
예제 #2
0
 private static void CheckActiveBooster(ActiveBooster booster, AvailableSlot slot, ulong guildId,
                                        float multiplier,
                                        DateTime expirationDate)
 {
     booster.Slot.Id.Should().Be(slot.Id);
     booster.GuildId.Should().Be(guildId);
     booster.Multiplier.Should().Be(multiplier);
     booster.ExpirationDate.Should().Be(expirationDate);
 }
예제 #3
0
        protected virtual List <AvailableSlot> GetAvailableTableSlots(Graph graph, GraphPath path, RSATable table)
        {
            List <AvailableSlot> availableSlots = new List <AvailableSlot>();
            List <GraphLink>     pathLinks      = path.ToLinks(graph.Links);

            foreach (GraphLink link in pathLinks)
            {
                AvailableSlot element = new AvailableSlot();
                element.Link       = link;
                element.Availables = new List <int>(table.Table[link.GetLinkId()].Where(r => r.Value.Values.Count == 0).Select(r => r.Key).ToList());
                availableSlots.Add(element);
            }

            return(availableSlots);
        }
예제 #4
0
        private void ShowSlotInfo()
        {
            //Total slots
            string slot = this.cmbSlot.Text;
            string sql  = "select " + slot + " from Test_Hp where HpId = '" + this.HpId + "' and TestId = (select TestId from HpTest where TestName = '" + this.cmbTest.Text + "');";

            try
            {
                this.Dt = DataAccess.GetDataTable(sql);
            }
            catch
            {
                MessageBox.Show("Error: Something went wrong!");
            }

            if (this.Dt.Rows.Count > 0)
            {
                this.totalSlots.Text = this.Dt.Rows[0][slot].ToString();

                //Booked slots
                DateTime HpBook = DateTime.ParseExact(this.dtpBookHp.Text, "dddd , dd , MMMM , yyyy", null);    //Collecting time
                SetSlot();                                                                                      //Setting slot
                string sql2 = "select count(Slot) as Slot from HpBook where Slot = '" + this.Slot + "' and Time = '" + HpBook.Date + "' and HpId = '" + this.HpId + "' and TestId = '" + this.TestId + "'";

                try
                {
                    this.Dt = DataAccess.GetDataTable(sql2);

                    this.bookedSlots.Text = this.Dt.Rows[0]["Slot"].ToString();
                }
                catch
                {
                    MessageBox.Show("Error: Something went wrong!");
                }


                //Available slots
                int totalS  = Int16.Parse(this.totalSlots.Text);
                int bookedS = Int16.Parse(this.bookedSlots.Text);
                AvailableSlot            = totalS - bookedS;
                this.availableSlots.Text = AvailableSlot.ToString();
            }
            else
            {
                this.availableSlots.Text = "N/A";
            }
        }
예제 #5
0
        public async Task QueueShouldBeInCorrectOrder_WhenPopulatedFromDatabase([Range(2u, 50, 1)] uint count)
        {
            ulong guildId   = 6;
            ulong channelId = 7;
            var   slot      = new AvailableSlot
            {
                Id        = 69,
                GuildId   = guildId,
                ChannelId = channelId
            };
            var boosters = new List <QueuedBooster>();
            var random   = new Random();

            for (uint i = 0; i < count; i++)
            {
                boosters.Add(new QueuedBooster
                {
                    Id                = (int)i,
                    GuildId           = guildId,
                    Position          = i,
                    Multiplier        = (float)random.NextDouble() * 3f,
                    DurationInSeconds = random.Next(5, 500)
                });
            }

            _dbContext.ConfigureMockDbSet(x => x.XPActiveBoosters);
            _dbContext.ConfigureMockDbSet(x => x.XPQueuedBoosters, boosters);
            _dbContext.ConfigureMockDbSet(x => x.XPAvailableSlots, slot);

            await _appFixture.SendAsync(new PopulateBoosterServiceCommand());

            _activeBoosters.Should().NotBeNull();
            _queuedBoosters.Should().NotBeNull();
            _availableSlots.Should().NotBeNull();

            _queuedBoosters[guildId].Count.Should().Be(boosters.Count);

            foreach (var ogBooster in boosters)
            {
                var compareAgainstBooster = _queuedBoosters[guildId].Dequeue();

                CompareQueuedBoosters(ogBooster, compareAgainstBooster);
            }
        }
예제 #6
0
        public async Task ShouldActivateBooster_WhenSlotExists()
        {
            var   now        = DateTime.Now;
            ulong guildId    = 6;
            ulong channelId  = 7;
            var   multiplier = 0.5f;
            var   duration   = TimeSpan.FromSeconds(5);
            var   slot       = new AvailableSlot
            {
                Id        = 69,
                GuildId   = guildId,
                ChannelId = channelId
            };

            var activeBoosters = new Dictionary <ulong, List <ActiveBooster> >
            {
                { guildId, new List <ActiveBooster>() }
            };
            var availableSlots = new Dictionary <ulong, List <AvailableSlot> >
            {
                { guildId, new List <AvailableSlot> {
                      slot
                  } }
            };

            _dbContext.ConfigureMockDbSet(x => x.XPActiveBoosters);
            _dbContext.ConfigureMockDbSet(x => x.XPAvailableSlots, slot);
            _boosterService.ActiveBoosters.Returns(activeBoosters);
            _boosterService.AvailableSlots.Returns(availableSlots);

            _dateTime.Now.Returns(now);

            var command = new ActivateBoosterCommand
            {
                GuildId    = guildId,
                Multiplier = multiplier,
                Duration   = duration,
                Slot       = slot
            };

            await _appFixture.SendAsync(command);

            await _dbContext.Received(1).SaveChangesAsync(default);
예제 #7
0
        public async Task OtherCollectionsShouldNotBeEmpty_WhenGuildExists(bool withSlot)
        {
            ulong guildId   = 6;
            ulong channelId = 7;
            var   slot      = new AvailableSlot
            {
                Id        = 69,
                GuildId   = guildId,
                ChannelId = channelId
            };

            _dbContext.ConfigureMockDbSet(x => x.XPActiveBoosters);
            _dbContext.ConfigureMockDbSet(x => x.XPQueuedBoosters);
            if (withSlot)
            {
                _dbContext.ConfigureMockDbSet(x => x.XPAvailableSlots, slot);
            }
            else
            {
                _dbContext.ConfigureMockDbSet(x => x.XPAvailableSlots);
            }

            _discordGuildService.GetAllCurrentGuildIds().Returns(new[] { guildId });

            await _appFixture.SendAsync(new PopulateBoosterServiceCommand());

            _activeBoosters.Should().NotBeNull();
            _queuedBoosters.Should().NotBeNull();
            _availableSlots.Should().NotBeNull();

            if (withSlot)
            {
                _availableSlots[guildId].First().GuildId.Should().Be(guildId);
                _availableSlots[guildId].First().ChannelId.Should().Be(channelId);
            }
            else
            {
                _availableSlots[guildId].Should().NotBeNull().And.BeEmpty();
            }

            _activeBoosters[guildId].Should().NotBeNull().And.BeEmpty();
            _queuedBoosters[guildId].Should().NotBeNull().And.BeEmpty();
        }
예제 #8
0
        public async Task ActiveBoostersShouldBeCorrect_WhenPopulatedFromDatabase()
        {
            ulong guildId        = 6;
            ulong channelId      = 7;
            ulong otherChannelId = 8;
            var   multiplier     = 0.5f;
            var   expirationDate = DateTime.Now.ToUniversalTime().AddMinutes(5);
            var   slot           = new AvailableSlot
            {
                Id        = 69,
                GuildId   = guildId,
                ChannelId = channelId
            };
            var otherSlot = new AvailableSlot
            {
                Id        = 70,
                GuildId   = guildId,
                ChannelId = otherChannelId
            };

            var booster = new ActiveBooster
            {
                GuildId        = guildId,
                Slot           = slot,
                Multiplier     = multiplier,
                ExpirationDate = expirationDate
            };

            _dbContext.ConfigureMockDbSet(x => x.XPActiveBoosters, booster);
            _dbContext.ConfigureMockDbSet(x => x.XPQueuedBoosters);
            _dbContext.ConfigureMockDbSet(x => x.XPAvailableSlots, new[] { slot, otherSlot });

            await _appFixture.SendAsync(new PopulateBoosterServiceCommand());

            _activeBoosters.Should().NotBeNull().And.NotBeEmpty();
            _queuedBoosters.Should().NotBeNull();
            _availableSlots.Should().NotBeNull();

            var boosterToCheck = _activeBoosters[guildId].First();

            CheckActiveBooster(boosterToCheck, slot, guildId, multiplier, expirationDate);
        }
예제 #9
0
        public async Task <Unit> Handle(AddSlotCommand request, CancellationToken cancellationToken)
        {
            var slot = new AvailableSlot
            {
                GuildId   = request.GuildId,
                ChannelId = request.ChannelId
            };

            _context.XPAvailableSlots.Add(slot);
            await _context.SaveChangesAsync(cancellationToken);

            _boosterService.AvailableSlots.TryAdd(request.GuildId, new List <AvailableSlot>());
            _boosterService.AvailableSlots[request.GuildId].Add(slot);

            _boosterService.QueuedBoosters.TryAdd(request.GuildId, new Queue <QueuedBooster>());
            _boosterService.ActiveBoosters.TryAdd(request.GuildId, new List <ActiveBooster>());

            await _discordChannelService.RenameChannel(request.GuildId, request.ChannelId, "-");

            return(Unit.Value);
        }
        public async Task ShouldDeactivateBoosters_IfBoostersAreExpired()
        {
            ulong guildId   = 6;
            ulong channelId = 7;
            var   now       = DateTime.Now;
            var   slot      = new AvailableSlot
            {
                GuildId   = guildId,
                ChannelId = channelId
            };

            var sub = new List <ActiveBooster>
            {
                new()
                {
                    GuildId        = guildId,
                    ExpirationDate = now.ToUniversalTime().Subtract(TimeSpan.FromSeconds(5)),
                    Multiplier     = 6.9f,
                    Slot           = slot
                }
            };
            var activeBoosters = new Dictionary <ulong, List <ActiveBooster> >
            {
                { guildId, sub }
            };
            var queuedBoosters = new Dictionary <ulong, Queue <QueuedBooster> >
            {
                { guildId, new Queue <QueuedBooster>() }
            };
            var availableSlots = new Dictionary <ulong, List <AvailableSlot> >
            {
                {
                    guildId, new List <AvailableSlot>
                    {
                        slot
                    }
                }
            };

            _boosterService.ActiveBoosters.Returns(activeBoosters);
            _boosterService.QueuedBoosters.Returns(queuedBoosters);
            _boosterService.AvailableSlots.Returns(availableSlots);

            _dbContext.ConfigureMockDbSet(x => x.XPActiveBoosters);
            _dbContext.ConfigureMockDbSet(x => x.XPQueuedBoosters);
            _dbContext.ConfigureMockDbSet(x => x.XPAvailableSlots);

            ((IQueryable <QueuedBooster>)_dbContext.XPQueuedBoosters).Expression.Returns(new List <QueuedBooster>()
                                                                                         .AsQueryable().Where(x => false).Expression);
            ((IQueryable <ActiveBooster>)_dbContext.XPActiveBoosters).Expression.Returns(new List <ActiveBooster>()
                                                                                         .AsQueryable().Where(x => false).Expression);

            _dateTime.Now.Returns(now);

            await _sussy.Handle(new UpdateBoosterCycleCommand { GuildId = guildId }, CancellationToken.None);

            await _dbContext.XPActiveBoosters.Received().DeleteAsync();

            _boosterService.ActiveBoosters[guildId].Count.Should().Be(0);

            await _discordChannelService.Received().RenameChannel(guildId, channelId, "-");
        }
        public async Task ShouldActivateQueuedBooster_IfSlotBecomesAvailable()
        {
            ulong guildId   = 6;
            ulong channelId = 7;
            var   now       = DateTime.Now.ToUniversalTime();
            var   slot      = new AvailableSlot
            {
                GuildId   = guildId,
                ChannelId = channelId
            };
            var sub = new List <ActiveBooster>
            {
                new()
                {
                    GuildId        = guildId,
                    ExpirationDate = now.Subtract(TimeSpan.FromSeconds(5)),
                    Multiplier     = 6.9f,
                    Slot           = slot
                }
            };
            var queued = new QueuedBooster
            {
                GuildId           = guildId,
                Multiplier        = 5.0f,
                DurationInSeconds = 600
            };

            var activeBoosters = new Dictionary <ulong, List <ActiveBooster> >
            {
                { guildId, sub }
            };
            var queuedBoosters = new Dictionary <ulong, Queue <QueuedBooster> >
            {
                { guildId, new Queue <QueuedBooster>(new[] { queued }) }
            };
            var availableSlots = new Dictionary <ulong, List <AvailableSlot> >
            {
                {
                    guildId, new List <AvailableSlot>
                    {
                        slot
                    }
                }
            };

            _boosterService.ActiveBoosters.Returns(activeBoosters);
            _boosterService.QueuedBoosters.Returns(queuedBoosters);
            _boosterService.AvailableSlots.Returns(availableSlots);

            _dbContext.ConfigureMockDbSet(x => x.XPActiveBoosters);
            _dbContext.ConfigureMockDbSet(x => x.XPQueuedBoosters);
            _dbContext.ConfigureMockDbSet(x => x.XPAvailableSlots);

            ((IQueryable <QueuedBooster>)_dbContext.XPQueuedBoosters).Expression.Returns(new List <QueuedBooster>()
                                                                                         .AsQueryable().Where(x => false).Expression);
            ((IQueryable <ActiveBooster>)_dbContext.XPActiveBoosters).Expression.Returns(new List <ActiveBooster>()
                                                                                         .AsQueryable().Where(x => false).Expression);

            _dateTime.Now.Returns(now);

            await _sussy.Handle(new UpdateBoosterCycleCommand { GuildId = guildId }, CancellationToken.None);

            await _dbContext.XPActiveBoosters.Received().DeleteAsync();

            _boosterService.ActiveBoosters[guildId].Count.Should().Be(0);

            await _mediator.Received().Send(Arg.Is <ActivateBoosterCommand>(c =>
                                                                            c.GuildId == guildId &&
                                                                            Math.Abs(c.Multiplier - queued.Multiplier) < 0.01f &&
                                                                            c.Duration == TimeSpan.FromSeconds(queued.DurationInSeconds)
                                                                            ));

            await _mediator.Received(1).Send(Arg.Is <SaveQueueCommand>(c =>
                                                                       c.GuildId == guildId
                                                                       ));
        }
    }
 public void Add(AvailableSlot slot)
 {
     _available.Add(slot);
 }
예제 #13
0
        public async Task QueueShouldBeInCorrectOrder_WhenMultipleGuildsArePresent([Range(20u, 30, 1)] uint count)
        {
            ulong guildId      = 6;
            ulong otherGuildId = 7;
            ulong channelId    = 7;
            var   slot         = new AvailableSlot
            {
                Id        = 69,
                GuildId   = guildId,
                ChannelId = channelId
            };
            var otherSlot = new AvailableSlot
            {
                Id        = 69,
                GuildId   = guildId,
                ChannelId = channelId
            };
            var boosters      = new List <QueuedBooster>();
            var otherBoosters = new List <QueuedBooster>();
            var random        = new Random();

            for (uint i = 0; i < count; i++)
            {
                boosters.Add(new QueuedBooster
                {
                    Id                = (int)i,
                    GuildId           = guildId,
                    Position          = i,
                    Multiplier        = (float)random.NextDouble() * 3f,
                    DurationInSeconds = random.Next(5, 500)
                });
            }
            for (uint i = 0; i < count + random.Next(-10, 10); i++)
            {
                otherBoosters.Add(new QueuedBooster
                {
                    Id                = (int)i,
                    GuildId           = otherGuildId,
                    Position          = i,
                    Multiplier        = (float)random.NextDouble() * 3f,
                    DurationInSeconds = random.Next(5, 500)
                });
            }

            _dbContext.ConfigureMockDbSet(x => x.XPActiveBoosters);
            _dbContext.ConfigureMockDbSet(x => x.XPQueuedBoosters, boosters.Union(otherBoosters));
            _dbContext.ConfigureMockDbSet(x => x.XPAvailableSlots, new[] { slot, otherSlot });

            _discordGuildService.GetAllCurrentGuildIds().Returns(new[] { guildId, otherGuildId });

            await _appFixture.SendAsync(new PopulateBoosterServiceCommand());

            _activeBoosters.Should().NotBeNull();
            _queuedBoosters.Should().NotBeNull();
            _availableSlots.Should().NotBeNull();

            _queuedBoosters[guildId].Count.Should().Be(boosters.Count);
            _queuedBoosters[otherGuildId].Count.Should().Be(otherBoosters.Count);

            foreach (var ogBooster in boosters)
            {
                var compareAgainstBooster = _queuedBoosters[guildId].Dequeue();

                CompareQueuedBoosters(ogBooster, compareAgainstBooster);
            }

            foreach (var ogBooster in otherBoosters)
            {
                var compareAgainstBooster = _queuedBoosters[otherGuildId].Dequeue();

                CompareQueuedBoosters(ogBooster, compareAgainstBooster);
            }
        }
예제 #14
0
        public static CalendarTimeSlot?GetNextAvailableSlot(Dictionary <string, List <AvailableSlot> > availableSlots, AvailableSlot currentSlot, string currentCalendar, int depth = 0)
        {
            if (depth >= availableSlots.Keys.Count)
            {
                return(null);
            }

            var currentCal = CalendarTraversal.GetNextCalendar(availableSlots.Keys.ToList(), currentCalendar);

            var nextSlot = availableSlots[currentCal].FirstOrDefault(s => s.IsAvailable == true && s.Start == currentSlot.Start && s.End == currentSlot.End);

            if (nextSlot == null)
            {
                return(GetNextAvailableSlot(availableSlots, currentSlot, currentCal, depth + 1));
            }
            else
            {
                return(new CalendarTimeSlot(nextSlot.Start, nextSlot.End, currentCal));
            }
        }