Exemplo n.º 1
0
        public ActionResult AlgorithmTester(int? kindsMaxCount, int? restaurantsMaxCount, int? dishesMaxCount, int? usersMaxCount, int? maxOrdersPerUser)
        {
            if (!kindsMaxCount.HasValue || !restaurantsMaxCount.HasValue || !dishesMaxCount.HasValue || !usersMaxCount.HasValue || !maxOrdersPerUser.HasValue)
            {
                return Redirect("/Debug/AlgorithmTester/?kindsMaxCount=10&restaurantsMaxCount=50&dishesMaxCount=100&usersMaxCount=60&maxOrdersPerUser=4");
            }

            var random = new Random();
            var group = new Group()
            {
                Id = Guid.NewGuid(),
                Name = "Guinea Pigs"
            };
            var users = GenerateRandomUsers(group, random, usersMaxCount.Value);
            var restaurants = GenerateRestaurants(group, random, restaurantsMaxCount.Value);
            var kinds = GenerateKinds(group, random, kindsMaxCount.Value);
            var dishes = GenerateDishes(group, restaurants, kinds, random, dishesMaxCount.Value);
            var randomOrders = GenerateRandomOrders(random, users, dishes, maxOrdersPerUser.Value);
            var stopwatch = new Stopwatch();
            stopwatch.Start();
            var matches = new OrderMatchHandler(randomOrders).ProcessOrders();
            stopwatch.Stop();

            ViewBag.Time = stopwatch.Elapsed;
            ViewBag.Orders = randomOrders.Select(o => new OrderViewModel()
            {
                Dish = o.Dish.Name,
                DishId = o.Dish.Id,
                Kind = o.Dish.Kind.Name,
                KindId = o.Dish.Kind.Id,
                Id = o.Id,
                RestaurantId = o.Dish.Restaurant.Id,
                Restaurant = o.Dish.Restaurant.Name,
                IsHelpNeeded = o.IsHelpNeeded,
                IsOptional = false,
                UserEmail = o.CreatedBy.Email
            }).OrderBy(o => o.UserEmail).ToList();

            ViewBag.Matches = matches.Select(m => new MatchedOrderViewModel()
            {
                Captain = m.Captain.Email,
                RestaurantId = m.RestaurantId,
                Orders = m.MatchedOrders.Select(o => new OrderViewModel()
                {
                    Dish = o.Dish.Name,
                    DishId = o.Dish.Id,
                    Kind = o.Dish.Kind.Name,
                    KindId = o.Dish.Kind.Id,
                    Id = o.Id,
                    RestaurantId = o.Dish.Restaurant.Id,
                    Restaurant = o.Dish.Restaurant.Name,
                    IsHelpNeeded = o.IsHelpNeeded,
                    IsOptional = false,
                    UserEmail = o.CreatedBy.Email
                }).ToList()
            }).OrderBy(m => m.RestaurantId).ToList();

            return View();
        }
Exemplo n.º 2
0
        public async Task<IHttpActionResult> GetMatchesForPoll(Guid pollId)
        {
            var query = UnitOfWork.OrderRepository.GetForPoll(pollId, "Dish.Kind,CreatedBy");
            List<Order> orders = query.ToList();

            if (orders.Count > 0)
            {
                OrderMatchHandler matchHandler = new OrderMatchHandler(orders);
                matchHandler.ProcessOrders();

                // add not matched orders
                var matches = matchHandler.AddNotMatchedOrders();

                var viewModelQuery = matches.Select(b => new MatchedOrderViewModel()
                {
                    Captain = b.Captain != null ? b.Captain.Email : null,
                    Orders = b.MatchedOrders.Select(o => new OrderViewModel()
                    {
                        Id = o.Id,
                        RestaurantId = b.RestaurantId,
                        Dish = o.Dish.Name,
                        DishId = o.Dish.Id,
                        Kind = o.Dish.Kind.Name,
                        KindId = o.Dish.Kind.Id,
                        IsHelpNeeded = o.IsHelpNeeded,
                        Restaurant = !b.IsNotMatched ? o.Dish.Restaurant.Name : "Not matched",
                        UserEmail = o.CreatedBy.Email
                    }).ToList(),
                    RestaurantId = !b.IsNotMatched ? b.RestaurantId : new Guid()
                });

                var viewModelList = viewModelQuery.ToList();
                return Ok(viewModelList);
            }
            else
            {
                return Ok(new List<MatchedOrderViewModel>());
            }
        }
Exemplo n.º 3
0
        public async Task<ActionResult> Finish()
        {
            Poll poll = await UnitOfWork.PollRepository.GetCurrentForGroup(CurrentGroup, "CreatedBy").SingleAsync();

            if (poll.CreatedBy.Id != CurrentUser.Id)
            {
                return new HttpStatusCodeResult(HttpStatusCode.Unauthorized, "Unauthorized");
            }

            List<Order> orders = UnitOfWork.OrderRepository.GetForPoll(poll).ToList();
            List<OrderBasket> matches = new List<OrderBasket>();

            if (orders.Count > 0)
            {
                OrderMatchHandler matchHandler = new OrderMatchHandler(orders);
                matchHandler.ProcessOrders();

                // add not matched orders
                matches = matchHandler.AddNotMatchedOrders();
            }

            if (poll.IsFinished) // is it "second round"?
            {
                if (poll.IsResolved)
                {
                    return new HttpStatusCodeResult(HttpStatusCode.BadRequest, "Poll already closed");
                }

                // to prevent "holding up" the poll, second attempt is always resolving it
                poll.IsResolved = true;
                await ProcessResolvedPoll(orders, matches);
            }
            else
            {
                poll.IsFinished = true;

                // mark poll as resolved only when there are no non-matched orders
                poll.IsResolved = matches.Any(m => m.IsNotMatched == false && m.MatchedOrders.Count() == 1) == false;

                if (poll.IsResolved)
                {
                    await ProcessResolvedPoll(orders, matches);
                }
                else
                {
                    var singleOrders = matches.Where(o => o.MatchedOrders.Count() == 1 && o.IsNotMatched == false)
                        .SelectMany(o => o.MatchedOrders).ToList();

                    // if poll is not resolved, notify owners of not-matched orders
                    await SendFinalizeWarnings(singleOrders);
                }

            }
            await UnitOfWork.PollRepository.Update(poll);

            // TODO: make PollViewModel constructor that handles rewriting
            return Json(new PollViewModel()
            {
                Id = poll.Id,
                Group = CurrentGroup.Name,
                Orders = null, // i'm too lazy
                ETA = poll.ETA,
                FinishedAt = poll.FinishedAt,
                IsFinished = poll.IsFinished,
                IsResolved = poll.IsResolved,
                Remarks = poll.Remarks
            });
        }