private static Event HandleDeliverCommand(DeliverCommand deliverCommand, Dictionary <Product, int> carriedProducts, Drone drone,
                                                  ref Coordinate droneLocation, ref long currentTurn, ref long carriedWeight)
        {
            var distance = droneLocation.CalcEucledianDistance(deliverCommand.Order.Location);

            currentTurn   += ((int)Math.Ceiling(distance)) + 1;
            droneLocation  = deliverCommand.Order.Location;
            carriedWeight -= deliverCommand.Product.Weight * deliverCommand.ProductCount;
            var newCount = carriedProducts.GetOrDefault(deliverCommand.Product, 0) - deliverCommand.ProductCount;

            if (newCount < 0)
            {
                throw new Exception(string.Format("Drone {0} attempted to deliver {1} products of type {2} which he doesn't have",
                                                  drone.Index, deliverCommand.ProductCount, deliverCommand.Product.Index));
            }

            carriedProducts[deliverCommand.Product] = newCount;

            var ev = new Event
            {
                Turn             = currentTurn,
                CurrentOrder     = deliverCommand.Order,
                ProductDelivered = deliverCommand.Product,
                DeliveredCount   = deliverCommand.ProductCount,
                Drone            = drone
            };

            return(ev);
        }
Example #2
0
 public void Handle(ICommandContext context, DeliverCommand command)
 {
     context.Get <StoreOrder>(command.AggregateRootId).Deliver(
         new Domain.Models.Stores.StoreOrders.ExpressInfo(
             command.ExpressName,
             command.ExpressCode,
             command.ExpressNumber));
 }
        public override DronesOutput GetResultFromReader(DronesInput input, TextReader reader)
        {
            var commands     = new List <CommandBase>();
            var commandCount = int.Parse(reader.ReadLine());

            for (int i = 0; i < commandCount; i++)
            {
                var line  = reader.ReadLine();
                var spl   = line.Split(' ');
                var drone = input.Drones[int.Parse(spl[0])];

                var others = spl.Skip(2).Select(int.Parse).ToList();

                CommandBase newCommand;
                switch (spl[1])
                {
                case "D":
                    newCommand = new DeliverCommand(drone, input.Orders[others[0]], input.Products[others[1]], others[2]);
                    break;

                case "W":
                    newCommand = new WaitCommand(drone, (uint)others[0]);
                    break;

                case "U":
                    newCommand = new UnloadCommand(drone, input.WareHouses[others[0]], input.Products[others[1]], others[2]);
                    break;

                case "L":
                    newCommand = new LoadCommand(drone, input.WareHouses[others[0]], input.Products[others[1]], others[2]);
                    break;

                default:
                    throw new ArgumentException(string.Format("Unknown command {0}", spl[1]));
                }

                commands.Add(newCommand);
            }

            return(new DronesOutput {
                Commands = commands
            });
        }
Example #4
0
        public async Task <BaseApiResponse> Deliver(DeliverRequest request)
        {
            request.CheckNotNull(nameof(request));

            var command = new DeliverCommand(request.ExpressName,
                                             request.ExpressCode,
                                             request.ExpressNumber)
            {
                AggregateRootId = request.Id
            };

            var result = await ExecuteCommandAsync(command);

            if (!result.IsSuccess())
            {
                return(new BaseApiResponse {
                    Code = 400, Message = "命令没有执行成功:{0}".FormatWith(result.GetErrorMessage())
                });
            }

            return(new BaseApiResponse());
        }
Example #5
0
        private IEnumerable <CommandBase> GetCommands(Drone d, WorkItem item, DronesInput input)
        {
            // For now, all will be load deliver heuristic.
            List <CommandBase> result = new List <CommandBase>();

            foreach (Warehouse w in input.WareHouses)
            {
                int itemCount;
                if (w.Products.TryGetValue(item.Item, out itemCount) && itemCount > 0)
                {
                    w.Products[item.Item] = itemCount - 1;
                    LoadCommand loadCmd = new LoadCommand(d, w, item.Item, /*productCount=*/ 1);
                    result.Add(loadCmd);
                    DeliverCommand deliverCommand = new DeliverCommand(d, item.ParentOrder, item.Item, /*productCount=*/ 1);
                    result.Add(deliverCommand);

                    break;
                }
            }

            return(result);
        }
Example #6
0
        public DronesOutput Solve(DronesInput input)
        {
            DronesOutput result = new DronesOutput();

            // TODO: populate m_RequestedItems according to input
            foreach (Order order in input.Orders)
            {
                foreach (Product product in order.WantedProducts)
                {
                    WorkItem workItem = new WorkItem(product, order.Location, order);
                    m_RequestedItems.Add(workItem);
                }
            }

            for (int t = 0; t < input.NumOfTurns; t++)
            {
                foreach (Drone d in input.Drones)
                {
                    d.TurnsUntilAvailable--;
                    if (d.TurnsUntilAvailable <= 0)
                    {
                        m_AvailableDrones.Add(d);
                    }

                    var cmd = d.Commands.FirstOrDefault();
                    if (cmd != null)
                    {
                        cmd.TurnsToComplete--;

                        if (cmd.TurnsToComplete <= 0)
                        {
                            d.Commands.RemoveAt(0);                              // remove this "first" cmd
                            DeliverCommand deliverCmd = cmd as DeliverCommand;
                            if (deliverCmd != null)
                            {
                                d.WeightLoad -= deliverCmd.Product.Weight * (uint)deliverCmd.ProductCount;
                            }
                        }
                    }
                }

                foreach (Drone d in m_AvailableDrones)
                {
                    for (int i = 0; i < m_RequestedItems.Count; i++)
                    {
                        if (input.MaxWeight - d.WeightLoad < m_RequestedItems [i].Item.Weight)
                        {
                            continue;
                        }

                        IEnumerable <CommandBase> cmds = GetCommands(d, m_RequestedItems[i], input);
                        d.Commands.AddRange(cmds);
                        result.Commands.AddRange(cmds);
                        foreach (CommandBase cmd in cmds)
                        {
                            d.TurnsUntilAvailable += cmd.TurnsToComplete;
                        }

                        d.WeightLoad += m_RequestedItems [i].Item.Weight;

                        // remove from list
                        m_RequestedItems.RemoveAt(i);
                        i--;                         // removed, hack

                        if (d.WeightLoad == input.MaxWeight)
                        {
                            break;                             // do not add new work to this drone for now
                        }
                    }
                }
            }

            return(result);
        }