Esempio n. 1
0
        public static async Task <MissionCompletionResult> ProcessIncorrectTry(
            MissionRequest request,
            IMissionRepository missionRepository,
            IMissionRequestRepository missionRequestRepository)
        {
            Expression <Func <MissionRequest, bool> > filter =
                a => a.MissionId == request.MissionId && a.UserId == request.UserId;
            var previousRequiests =
                await missionRequestRepository.GetMissionRequests(new QueryOptions <MissionRequest> {
                Filter = filter
            });

            var tryCount = previousRequiests.Count + 1;

            if (tryCount >= request.Mission.TriesFor1Star)
            {
                request.DeclineReason = "Увы, достигнуто максимальное количество попыток.";
                await RewardsCalculator.UpdateUserAfterMissionDecline(request, missionRepository);

                await missionRequestRepository.AddMissionRequest(request);

                return(new MissionCompletionResult
                {
                    MissionCompletionStatus = MissionCompletionStatus.Fail,
                    Description = request.DeclineReason
                });
            }

            var rejectResult = new MissionCompletionResult
            {
                MissionCompletionStatus =
                    MissionCompletionStatus.IntermediateFail,
                TryCount = tryCount
            };

            var failedRequesResult = await missionRequestRepository.AddMissionRequest(request);

            return(failedRequesResult.Status != OperationResultStatus.Success
                       ? MissionCompletionResult.FromOperationResult(failedRequesResult)
                       : rejectResult);
        }
Esempio n. 2
0
        internal static async Task <MissionCompletionResult> Process(
            Type uniqueMissionType,
            MissionRequest missionRequest,
            IMissionRequestRepository missionRequestRepository,
            IUserRepository userRepository,
            RatingService ratingService)
        {
            var oldUserPoints = missionRequest.User.Points;
            var uniqueMission = GetUniqueMission(uniqueMissionType);
            var result        = await uniqueMission.ProcessRequest(missionRequest);

            if (result.Status != OperationResultStatus.Success)
            {
                return(result);
            }

            var requestResult = await missionRequestRepository.AddMissionRequest(missionRequest);

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

            var userUpdateResult = await userRepository.UpdateUser(missionRequest.User);

            //after any mission completion user should be updated
            if (userUpdateResult.Status != OperationResultStatus.Error)
            {
                if (!missionRequest.User.Points.HasValue)
                {
                    throw new Exception($"Points are null for userId {missionRequest.UserId}, mission {missionRequest.MissionId}, status {userUpdateResult.Status}");
                }
                await ratingService.UpdateUserRating(missionRequest.User, oldUserPoints, missionRequest.User.Points.Value);
            }

            return(userUpdateResult.Status == OperationResultStatus.Success
                       ? result
                       : MissionCompletionResult.FromOperationResult(userUpdateResult));
        }
Esempio n. 3
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));
        }