예제 #1
0
        /// <summary>
        ///     Declines the mission.
        /// </summary>
        /// <param name="userId">The user identifier.</param>
        /// <param name="missionId">The mission identifier.</param>
        /// <returns>
        ///     Task{OperationResult}.
        /// </returns>
        public async Task <MissionCompletionResult> DeclineMission(string userId, string missionId)
        {
            var user = await _userRepository.GetUser(userId);

            if (user == null)
            {
                return(new MissionCompletionResult(OperationResultStatus.Error, "User was not found"));
            }
            var currentMission = user.ActiveMissionIds.FirstOrDefault(m => m.MissionId == missionId);

            if (currentMission == null)
            {
                return(new MissionCompletionResult(OperationResultStatus.Error, "Mission was not found"));
            }

            var mission = await _missionRepository.GetMission(missionId);

            //Check if mission is unique and sensored
            var possibleUniqueMissionType = UniqueMissionsDictionary.GetMissionType(missionId);

            if (possibleUniqueMissionType == typeof(Censored))
            {
                return
                    (await
                     UniqueMissionProcessor.Process(
                         possibleUniqueMissionType,
                         new MissionRequest
                {
                    Mission = mission,
                    MissionId = mission.Id,
                    User = user,
                    UserId = user.Id,
                    Status = MissionRequestStatus.AutoApproval,
                    LastUpdateDate = DateTime.UtcNow,
                    Proof = new MissionProof()
                },
                         _missionRequestRepository,
                         _userRepository,
                         _ratingService));
            }

            var oldPoints = user.Points;

            await RewardsCalculator.UpdateUserAfterMissionDecline(
                new MissionRequest { User = user, Mission = mission },
                _missionRepository);

            var userUpdateResult = await _userRepository.UpdateUser(user);

            if (userUpdateResult.Status != OperationResultStatus.Error)
            {
                // new points should always have value
                // ReSharper disable once PossibleInvalidOperationException
                await _ratingService.UpdateUserRating(user, oldPoints, user.Points.Value);
            }
            return(userUpdateResult.Status == OperationResultStatus.Success
                       ? new MissionCompletionResult
            {
                MissionCompletionStatus = MissionCompletionStatus.Fail
            }
                       : MissionCompletionResult.FromOperationResult(userUpdateResult));
        }
예제 #2
0
        /// <summary>
        ///     Completes the mission.
        /// </summary>
        /// <param name="userId">The user id.</param>
        /// <param name="missionId">The mission id.</param>
        /// <param name="missionProof">The mission proof.</param>
        /// <returns>Task{MissionCompletionResult}.</returns>
        public async Task <MissionCompletionResult> CompleteMission(
            string userId,
            string missionId,
            MissionProof missionProof)
        {
            var user = await _userRepository.GetUser(userId);

            if (user == null)
            {
                return(new MissionCompletionResult(OperationResultStatus.Error, "User was not found"));
            }

            var currentMission = user.ActiveMissionIds.FirstOrDefault(m => m.MissionId == missionId);

            if (currentMission == null)
            {
                return(new MissionCompletionResult(OperationResultStatus.Error, "Mission was not found"));
            }
            var mission = await _missionRepository.GetMission(missionId);

            var missionRequest = new MissionRequest
            {
                Mission        = mission,
                User           = user,
                MissionId      = missionId,
                UserId         = userId,
                Proof          = missionProof,
                LastUpdateDate = DateTime.UtcNow,
                Status         = MissionRequestStatus.AutoApproval
            };
            var possibleUniqueMissionType = UniqueMissionsDictionary.GetMissionType(missionRequest.MissionId);

            if (possibleUniqueMissionType != null)
            {
                return
                    (await
                     UniqueMissionProcessor.Process(
                         possibleUniqueMissionType,
                         missionRequest,
                         _missionRequestRepository,
                         _userRepository,
                         _ratingService));
            }

            if (mission.ExecutionType == ExecutionType.PhotoCreation ||
                mission.ExecutionType == ExecutionType.TextCreation || mission.ExecutionType == ExecutionType.Video)
            {
                missionRequest.Status = MissionRequestStatus.NotChecked;
                var addResult = await _missionRequestRepository.AddMissionRequest(missionRequest);

                if (addResult.Status != OperationResultStatus.Success)
                {
                    return(MissionCompletionResult.FromOperationResult(addResult));
                }

                return(new MissionCompletionResult {
                    MissionCompletionStatus = MissionCompletionStatus.Waiting
                });
            }

            return(await ProcessAutoApprovedMissions(missionRequest));
        }