Example #1
0
        public async Task <bool> CanAcceptAsync(int id)
        {
            await using var ctx = new CdClientContext();

            var mission = await ctx.MissionsTable.FirstAsync(m => m.Id == id);

            return(MissionParser.CheckPrerequiredMissions(
                       mission.PrereqMissionID,
                       GetCompletedMissions()
                       ));
        }
        // TODO: Improve
        private async Task SearchForNewAchievementsAsync <T>(MissionTaskType type, Lot lot, Func <T, Task> progress = null) where T : MissionTaskBase
        {
            await using var cdClient = new CdClientContext();

            //
            // Collect tasks which fits the requirements of this action.
            //

            var otherTasks = new List <MissionTasks>();

            // ReSharper disable once LoopCanBeConvertedToQuery
            foreach (var missionTask in cdClient.MissionTasksTable)
            {
                if (MissionParser.GetTargets(missionTask).Contains(lot))
                {
                    otherTasks.Add(missionTask);
                }
            }

            foreach (var task in otherTasks)
            {
                var mission = await cdClient.MissionsTable.FirstOrDefaultAsync(m => m.Id == task.Id);

                if (mission == default)
                {
                    continue;
                }

                //
                // Check if mission is an achievement and has a task of the correct type.
                //

                if (mission.OfferobjectID != -1 ||
                    mission.TargetobjectID != -1 ||
                    (mission.IsMission ?? true) ||
                    task.TaskType != (int)type)
                {
                    continue;
                }

                //
                // Get the mission on the character. If present.
                //

                var characterMission = MissionInstances.FirstOrDefault(m => m.MissionId == mission.Id);

                //
                // Check if the player could passably start this achievement.
                //

                if (characterMission != default)
                {
                    continue;
                }

                //
                // Check if player has the Prerequisites to start this achievement.
                //

                var hasPrerequisites = MissionParser.CheckPrerequiredMissions(
                    mission.PrereqMissionID,
                    GetCompletedMissions()
                    );

                if (!hasPrerequisites)
                {
                    continue;
                }

                //
                // Player can start achievement.
                //

                // Get Mission Id of new achievement.
                if (mission.Id == default)
                {
                    continue;
                }

                var missionId = mission.Id.Value;

                //
                // Setup new achievement.
                //

                var instance = new MissionInstance(GameObject as Player, missionId);

                MissionInstances.Add(instance);

                await instance.LoadAsync();

                // TODO: Silent?
                await instance.StartAsync();

                var activeTask = instance.Tasks.First(t => t.TaskId == task.Uid);

                if (progress != null)
                {
                    Detach(async() =>
                    {
                        await progress(activeTask as T);
                    });
                }
            }
        }
 /// <summary>
 /// Checks if the player can accept a mission based on whether it's repeatable, already started and if the
 /// requirements are met.
 /// </summary>
 /// <param name="mission"></param>
 /// <returns><c>true</c> if the player can accept this mission, <c>false</c> otherwise</returns>
 public bool CanAccept(MissionInstance mission) =>
 (mission.CanRepeat || !HasMission(mission.MissionId)) &&
 MissionParser.CheckPrerequiredMissions(mission.PrerequisiteMissions, AllMissions);
Example #4
0
        private async Task <bool> RequiredForNewAchievementsAsync(MissionTaskType type, Lot lot)
        {
            await using var cdClient = new CdClientContext();

            //
            // Collect tasks which fits the requirements of this action.
            //

            var otherTasks = new List <MissionTasks>();

            // ReSharper disable once LoopCanBeConvertedToQuery
            foreach (var missionTask in ClientCache.Tasks)
            {
                if (MissionParser.GetTargets(missionTask).Contains(lot))
                {
                    otherTasks.Add(missionTask);
                }
            }

            foreach (var task in otherTasks)
            {
                var mission = await cdClient.MissionsTable.FirstOrDefaultAsync(m => m.Id == task.Id);

                if (mission == default)
                {
                    continue;
                }

                //
                // Check if mission is an achievement and has a task of the correct type.
                //

                if (mission.OfferobjectID != -1 ||
                    mission.TargetobjectID != -1 ||
                    (mission.IsMission ?? true) ||
                    task.TaskType != (int)type)
                {
                    continue;
                }

                //
                // Get the mission on the character. If present.
                //

                MissionInstance characterMission;

                await Lock.WaitAsync();

                try
                {
                    characterMission = MissionInstances.FirstOrDefault(m => m.MissionId == mission.Id);
                }
                finally
                {
                    Lock.Release();
                }

                //
                // Check if the player could passably start this achievement.
                //

                if (characterMission != default)
                {
                    continue;
                }

                //
                // Check if player has the Prerequisites to start this achievement.
                //

                var hasPrerequisites = MissionParser.CheckPrerequiredMissions(
                    mission.PrereqMissionID,
                    GetCompletedMissions()
                    );

                if (hasPrerequisites)
                {
                    return(true);
                }
            }

            return(false);
        }
Example #5
0
 /// <summary>
 /// Updates the progress for this task, a notification is sent to the client
 /// </summary>
 /// <param name="value">The value to add to the progress</param>
 /// <example>
 /// If the player were to obtain a certain amount of collectibles, <c>value</c> might be the item lot here.
 /// </example>
 protected void AddProgress(float value)
 {
     if (this.Mission.Player.TryGetComponent <MissionInventoryComponent>(out var missionInventoryComponent) && !MissionParser.CheckPrerequiredMissions(this.Mission.PrerequisiteMissions, missionInventoryComponent.AllMissions))
     {
         return;
     }
     Progress.Add(value);
     MessageUpdateMissionTask();
 }