Пример #1
0
        public void Transfer(Warehouse location, Warehouse destination, int transferTime, IReadOnlyList <Cargo> cargos)
        {
            Location     = location;
            Destination  = destination;
            TransferTime = transferTime;
            Cargos       = cargos;

            if (cargos.Any())
            {
                var loadTask = new TransportTask("Load", LoadTime);
                if (0 < LoadTime)
                {
                    loadTask.OnStartedEvent += () => OnLoadEvent?.Invoke(new LoadEvent(this, location, LoadTime, cargos));
                }
                AddTask(loadTask);
            }

            var transferTask = new TransportTask("Transfer", transferTime);

            transferTask.OnStartedEvent   += () => OnDepartEvent?.Invoke(new DepartEvent(this, location, destination, cargos));
            transferTask.OnCompletedEvent += () =>
            {
                Location    = destination;
                Destination = null;
                OnArriveEvent?.Invoke(new ArriveEvent(this, destination, cargos));
            };
            AddTask(transferTask);

            if (cargos.Any())
            {
                var unloadTask = new TransportTask("Unload", UnloadTime);
                unloadTask.OnStartedEvent += () =>
                {
                    if (0 < UnloadTime)
                    {
                        OnUnloadEvent?.Invoke(new UnloadEvent(this, location, UnloadTime, cargos));
                    }
                };
                unloadTask.OnCompletedEvent += () =>
                {
                    destination.Cargos.AddRange(Cargos);
                    Cargos = new List <Cargo>();
                    Transfer(destination, location, transferTime, new List <Cargo>());
                };
                AddTask(unloadTask);
            }
        }
Пример #2
0
 private void AddTask(TransportTask task)
 {
     if (!_tasks.Any())
     {
         task.DoOnStarted();
         if (task.ExecutionTime == 0)
         {
             task.DoOnCompleted();
         }
         else
         {
             _tasks.Add(task);
         }
     }
     else
     {
         _tasks.Add(task);
     }
 }