Task GotCommand(CateringServiceCommand cmd) { Console.WriteLine($"Got new catering command from groundservice"); int countCars = HowManyCarsNeeded(cmd); for (int i = 1; i <= countCars; i++) //breaking the command on small commands for cars { var foodList = new List <Tuple <Food, int> >(); commands.Enqueue(new CateringServiceCommand() { PlaneId = cmd.PlaneId, PlaneLocationVertex = cmd.PlaneLocationVertex, FoodList = new List <Tuple <Food, int> > ( cmd.FoodList.Select(tuple => { var maxAmount = CateringCar.MaxFoodAmount.Find(t => t.Item1 == tuple.Item1).Item2; var dif = tuple.Item2 - maxAmount * i; if (dif <= 0 && dif > maxAmount * (-1)) { return(Tuple.Create(tuple.Item1, tuple.Item2)); } else { return(Tuple.Create(tuple.Item1, maxAmount)); } }) ) }); } var cde = new CountdownEvent(countCars); completionEvents.TryAdd(cmd.PlaneId, cde); foreach (var car in cars.Values) //break cars path home { if (car.IsGoingHome) { tokens[car.CarId].Cancel(); } } foreach (var ev in wakeEvents) //wake cars in garage { ev.Set(); } return(new Task(() => { cde.Wait(); completionEvents.Remove(cmd.PlaneId, out cde); mqClient.Send <ServiceCompletionMessage>(queuesTo[Component.GroundService], new ServiceCompletionMessage() { Component = Component.Catering, PlaneId = cmd.PlaneId }); })); }
int HowManyCarsNeeded(CateringServiceCommand cmd) { var cmdCat = (CateringServiceCommand)cmd; var count = 1; for (int i = 0; i < cmd.FoodList.Count; i++) { var maxAmout = CateringCar.MaxFoodAmount.Find (tuple => tuple.Item1 == cmdCat.FoodList[i].Item1).Item2; while (cmd.FoodList[i].Item2 > maxAmout * count) { count++; break; } } return(count); }