示例#1
0
        private async Task ExecuteMission()
        {
            var mission     = default(Mission);
            var nextMission = default(Mission);

            lock (stackLock)
            {
                mission     = currentMission;
                nextMission = activeMissions.Any() ? activeMissions.First() : null;

                if (null != MissionUpdated)
                {
                    var state = new CommanderState(mission, nextMission);
                    MissionUpdated.Invoke(state);
                }
            }

            // step 1. check if there is a mission begin executed
            // check the mission timeout
            if (null != mission)
            {
                //Debug.WriteLine($"mission {mission.Id} {mission.Type} is running...");

                // check the result of mission and return if it is already completed.
                if (!mission.CheckIfCompleted())
                {
                    return;
                }

                lock (stackLock)
                {
                    // mission completed, put it into bucket.
                    completedMissions.Add(mission);
                    currentMission = null;

                    if (null != MissionUpdated)
                    {
                        var state = new CommanderState(null, nextMission);
                        MissionUpdated.Invoke(state);
                    }
                }
            }

            // step 2. pop a new mission
            lock (stackLock)
            {
                // no more mission, skip this cycle.
                if (!activeMissions.Any())
                {
                    return;
                }

                mission        = activeMissions.Dequeue();
                currentMission = mission;

                nextMission = activeMissions.Any() ? activeMissions.First() : null;

                if (null != MissionUpdated)
                {
                    var state = new CommanderState(mission, nextMission);
                    MissionUpdated.Invoke(state);
                }
            }

            try
            {
                mission.Start();
            }
            catch (Exception ex)
            {
                Debug.WriteLine($"Cmd Executor: ${ex.ToString()}"); Debugger.Break();
            }
        }
示例#2
0
 public CommanderState(Mission currentMission, Mission nextMission)
 {
     CurrentMission = currentMission;
     NextMission    = nextMission;
 }