Esempio n. 1
0
        /// <summary>
        /// Processes the request.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <returns></returns>
        public async Task <MissionCompletionResult> ProcessRequest(MissionRequest request)
        {
            if (String.IsNullOrEmpty(request.Proof.CreatedText))
            {
                request.StarsCount = 3;
                var recievedPoints =
                    await RewardsCalculator.UpdateUserAfterMissionCompletion(request, _missionRepository, _appCountersService);

                return(new MissionCompletionResult
                {
                    Points = recievedPoints,
                    StarsCount = request.StarsCount,
                    MissionCompletionStatus = MissionCompletionStatus.Success
                });
            }

            request.DeclineReason = "Увы. В следующий раз постарайся думать своей головой и не совершать глупости";
            await RewardsCalculator.UpdateUserAfterMissionDecline(request, _missionRepository, _appCountersService);

            return(new MissionCompletionResult
            {
                MissionCompletionStatus = MissionCompletionStatus.Fail,
                Description = request.DeclineReason
            });
        }
Esempio n. 2
0
        /// <summary>
        ///     Processes the request.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <returns>Task{MissionCompletionResult}.</returns>
        public async Task <MissionCompletionResult> ProcessRequest(MissionRequest request)
        {
            var homeCoordinate = request.Proof.Coordinates.First();

            request.User.HomeCoordinate = homeCoordinate;
            var homePlaceIdResult = await _placeIdService.GetUniquePlaceId(request.User.HomeCoordinate);

            if (homePlaceIdResult == null)
            {
                request.DeclineReason = "Невозможно опеределить идентификатор города. Возможно, ты находишься в пути. Попробуй еще раз позже.";
                return(new MissionCompletionResult
                {
                    MissionCompletionStatus = MissionCompletionStatus.IntermediateFail,
                    Description = request.DeclineReason
                });
            }

            request.User.CityShortName    = homePlaceIdResult.CityShortName;
            request.User.CountryShortName = homePlaceIdResult.CountryShortName;
            request.StarsCount            = 3;
            var recievedPoints = await RewardsCalculator.UpdateUserAfterMissionCompletion(request, _missionRepository);

            await _appCountersService.UserPassedFirstMission();

            return(new MissionCompletionResult
            {
                Points = recievedPoints,
                StarsCount = request.StarsCount,
                MissionCompletionStatus = MissionCompletionStatus.Success
            });
        }
Esempio n. 3
0
        /// <summary>
        /// Processes the request.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <returns>Task{MissionCompletionResult}.</returns>
        public async Task <MissionCompletionResult> ProcessRequest(MissionRequest request)
        {
            var userAnswer = request.Proof.CreatedText.ToLower();

            if ((userAnswer.Contains("мам") && !userAnswer.Contains("друг")) ||
                (userAnswer.Contains("друг") && !userAnswer.Contains("мам")))
            {
                request.StarsCount = 1;
            }
            else if (userAnswer.Contains("мам") && userAnswer.Contains("друг") || userAnswer.Contains("обоим") ||
                     userAnswer.Contains("двоим"))
            {
                request.StarsCount = 3;
            }
            else
            {
                request.DeclineReason = "Это неправильный выбор.";
                await RewardsCalculator.UpdateUserAfterMissionDecline(request, _missionRepository);

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

            var recievedPoints = await RewardsCalculator.UpdateUserAfterMissionCompletion(request, _missionRepository);

            return(new MissionCompletionResult
            {
                Points = recievedPoints,
                StarsCount = request.StarsCount,
                MissionCompletionStatus = MissionCompletionStatus.Success
            });
        }
Esempio n. 4
0
        /// <summary>
        /// Processes the request.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <returns>Task{MissionCompletionResult}.</returns>
        public async Task <MissionCompletionResult> ProcessRequest(MissionRequest request)
        {
            var baseCoordinates = request.Proof.Coordinates.ToArray();

            if (!ValidBaseCoordinates(baseCoordinates, request.User.HomeCoordinate))
            {
                request.DeclineReason = "Координаты базы ошибочны";
                await RewardsCalculator.UpdateUserAfterMissionDecline(request, _missionRepository);

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

            request.User.BaseNorthCoordinate = baseCoordinates[0];
            request.User.BaseEastCoordinate  = baseCoordinates[1];
            request.User.BaseSouthCoordinate = baseCoordinates[2];
            request.User.BaseWestCoordinate  = baseCoordinates[3];
            request.StarsCount = 3;
            var recievedPoints = await RewardsCalculator.UpdateUserAfterMissionCompletion(request, _missionRepository);

            return(new MissionCompletionResult
            {
                Points = recievedPoints,
                StarsCount = request.StarsCount,
                MissionCompletionStatus = MissionCompletionStatus.Success
            });
        }
Esempio n. 5
0
        /// <summary>
        /// Processes the request.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <returns>Task{MissionCompletionResult}.</returns>
        public async Task <MissionCompletionResult> ProcessRequest(MissionRequest request)
        {
            request.Mission.TriesFor1Star  = 7;
            request.Mission.TriesFor2Stars = 4;
            request.Mission.TriesFor3Stars = 2;

            if (request.Proof.Coordinates == null || !request.Proof.Coordinates.Any())
            {
                return(new MissionCompletionResult(OperationResultStatus.Error, "Not enough data"));
            }

            var    oupostCoordinate = request.Proof.Coordinates.First();
            double distanceError;

            if (!CheckOutpost(oupostCoordinate, request.User, out distanceError))
            {
                return(await MissionRequestService.ProcessIncorrectTry(request, _missionRepository, _missionRequestRepository));
            }
            await MissionRequestService.SetStarsAccordingToTries(request, _missionRequestRepository);

            request.User.OutpostCoordinate = oupostCoordinate;

            var recievedPoints = await RewardsCalculator.UpdateUserAfterMissionCompletion(
                request,
                _missionRepository);

            return(new MissionCompletionResult
            {
                Points = recievedPoints,
                StarsCount = request.StarsCount,
                MissionCompletionStatus = MissionCompletionStatus.Success
            });
        }
Esempio n. 6
0
        private async Task <MissionCompletionResult> ProcessTemporaryCommonPlace(
            MissionRequest request,
            string commonPlaceAlias,
            GeoCoordinate passedCoordinate)
        {
            var result =
                await _commonPlacesRepository.AddCommonPlace(request.UserId, commonPlaceAlias, passedCoordinate);

            if (result.Status != OperationResultStatus.Success)
            {
                return(new MissionCompletionResult(
                           OperationResultStatus.Error,
                           "Невозможно добавить временное общее место"));
            }

            request.StarsCount = 3;
            var recievedPoints =
                await
                RewardsCalculator.UpdateUserAfterMissionCompletion(request, _missionRepository, _appCountersService);

            var requestResult = await _missionRequestRepository.AddMissionRequest(request);

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

            return(new MissionCompletionResult
            {
                Points = recievedPoints,
                StarsCount = request.StarsCount,
                MissionCompletionStatus = MissionCompletionStatus.Success
            });
        }
Esempio n. 7
0
        private async Task <MissionCompletionResult> ProcessCommonPlace(MissionRequest request)
        {
            var commonPlaceAlias = request.Mission.CommonPlaceAlias;
            var passedCoordinate = request.Proof.Coordinates.First();

            if (request.User.HomeCoordinate.GetDistanceTo(passedCoordinate) < Core.Constants.GameConstants.Mission.CommonPlaceMinDistanceFromHome)
            {
                var incorrectResult = await ProcessIncorrectTry(request, _missionRepository, _missionRequestRepository);

                if (incorrectResult.MissionCompletionStatus == MissionCompletionStatus.IntermediateFail)
                {
                    incorrectResult.Description = "StillHome";
                }
                return(incorrectResult);
            }

            var realCommonPlace = await _commonPlacesRepository.GetCommonPlaceByAlias(request.UserId, commonPlaceAlias);

            if (realCommonPlace == null)
            {
                return(await ProcessTemporaryCommonPlace(request, commonPlaceAlias, passedCoordinate));
            }

            var distance = passedCoordinate.GetDistanceTo(realCommonPlace.Coordinate);

            if (distance > request.Mission.AccuracyRadius)
            {
                var incorrectResult = await ProcessIncorrectTry(request, _missionRepository, _missionRequestRepository);

                if (incorrectResult.MissionCompletionStatus == MissionCompletionStatus.IntermediateFail)
                {
                    incorrectResult.Description = distance > request.Mission.AccuracyRadius * 2 ? "Incorrect" : "IsNear";
                }
                return(incorrectResult);
            }

            await SetStarsAccordingToTries(request, _missionRequestRepository);

            var completePoints =
                await
                RewardsCalculator.UpdateUserAfterMissionCompletion(request, _missionRepository, _appCountersService);

            var finalResult = new MissionCompletionResult
            {
                Points     = completePoints,
                StarsCount = request.StarsCount,
                MissionCompletionStatus = MissionCompletionStatus.Success
            };

            var requestResult = await _missionRequestRepository.AddMissionRequest(request);

            return(requestResult.Status != OperationResultStatus.Success
                       ? MissionCompletionResult.FromOperationResult(requestResult)
                       : finalResult);
        }
Esempio n. 8
0
        /// <summary>
        /// Processes the request.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <returns>Task{MissionCompletionResult}.</returns>
        public async Task <MissionCompletionResult> ProcessRequest(MissionRequest request)
        {
            request.StarsCount = 3;
            var recievedPoints = await RewardsCalculator.UpdateUserAfterMissionCompletion(request, _missionRepository);

            return(new MissionCompletionResult
            {
                Points = recievedPoints,
                StarsCount = request.StarsCount,
                MissionCompletionStatus = MissionCompletionStatus.Success
            });
        }
Esempio n. 9
0
        /// <summary>
        ///     Processes the request.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <returns>Task{MissionCompletionResult}.</returns>
        public async Task <MissionCompletionResult> ProcessRequest(MissionRequest request)
        {
            var users = await _userRepository.GetUsers(new QueryOptions <User> {
                Filter = u => u.Id != request.UserId, Select = u => u.HomeCoordinate
            });

            var           coordinates           = users.Select(u => u.HomeCoordinate).Where(coord => coord != null);
            var           passedCoordinate      = request.Proof.Coordinates.First();
            GeoCoordinate friendsBaseCoordinate = null;

            foreach (var geoCoordinate in coordinates)
            {
                var distance = passedCoordinate.GetDistanceTo(geoCoordinate);

                if (distance <= 100)
                {
                    request.StarsCount = 1;
                    if (distance <= 50)
                    {
                        request.StarsCount = 2;
                    }
                    if (distance <= 10)
                    {
                        request.StarsCount = 3;
                    }
                    friendsBaseCoordinate = geoCoordinate;
                    break;
                }
            }

            if (friendsBaseCoordinate == null)
            {
                request.DeclineReason = "Мы не смогли найти ни одного пользователя с таким командным пунктом";
                await RewardsCalculator.UpdateUserAfterMissionDecline(request, _missionRepository, _appCountersService);

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

            var recievedPoints =
                await
                RewardsCalculator.UpdateUserAfterMissionCompletion(request, _missionRepository, _appCountersService);

            return(new MissionCompletionResult
            {
                Points = recievedPoints,
                StarsCount = request.StarsCount,
                MissionCompletionStatus = MissionCompletionStatus.Success
            });
        }
Esempio n. 10
0
        private async Task <RightAnswerMissionCompletionResult> ProcessRightAnswer(MissionRequest request)
        {
            var userAnswers = request.Proof.CreatedText.Trim().SplitStringByDelimiter();

            if (!userAnswers.AnyValues())
            {
                return(new RightAnswerMissionCompletionResult(OperationResultStatus.Error, "Ответ не может быть пустым"));
            }

            var           rightAnswers = AnswerModelHelper.SplitAnswer(request.Mission.CorrectAnswers);
            List <string> userAnswerStatuses;
            var           invalidAnswersCount = ParseUserAnswers(rightAnswers, userAnswers, request.Mission.ExactAnswer ?? false, out userAnswerStatuses);
            var           rightAnswersCount   = userAnswers.Count - invalidAnswersCount;

            if (rightAnswersCount == 0 || rightAnswersCount < request.Mission.AnswersCount)
            {
                var result = await ProcessIncorrectTry(request, _missionRepository, _missionRequestRepository);

                var rightAnswerResult = RightAnswerMissionCompletionResult.FromMissionCompletionResult(result);
                request.DeclineReason            = userAnswerStatuses.JoinToString();
                rightAnswerResult.AnswerStatuses = userAnswerStatuses;

                return(rightAnswerResult);
            }

            await SetStarsAccordingToTries(request, _missionRequestRepository);

            var recievedPoints =
                await
                RewardsCalculator.UpdateUserAfterMissionCompletion(request, _missionRepository, _appCountersService);

            var requestResult = await _missionRequestRepository.AddMissionRequest(request);

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

            return(new RightAnswerMissionCompletionResult
            {
                Points = recievedPoints,
                StarsCount = request.StarsCount,
                MissionCompletionStatus = MissionCompletionStatus.Success
            });
        }
Esempio n. 11
0
        /// <summary>
        ///     The approve request.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <param name="starsCount">The stars count.</param>
        /// <returns>
        ///     The <see cref="Task" />.
        /// </returns>
        public async Task <OperationResult> ApproveRequest(string id, int starsCount)
        {
            var missionRequest = await _missionRequestRepository.GetMissionRequest(id);

            if (missionRequest == null || missionRequest.Status != MissionRequestStatus.NotChecked)
            {
                return(OperationResult.NotFound);
            }

            missionRequest.Status     = MissionRequestStatus.Approved;
            missionRequest.StarsCount = (byte)starsCount;

            var result = await _missionRequestRepository.UpdateMissionRequest(missionRequest);

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

            var oldUserPoints = missionRequest.User.Points;
            await
            RewardsCalculator.UpdateUserAfterMissionCompletion(
                missionRequest,
                _missionRepository,
                _appCountersService);

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

            var finalResult = await _userRepository.UpdateUser(missionRequest.User);

            if (finalResult.Status != OperationResultStatus.Error)
            {
                // new points should always have value
                // ReSharper disable once PossibleInvalidOperationException
                await _ratingService.UpdateUserRating(missionRequest.User, oldUserPoints, missionRequest.User.Points.Value);
            }

            await _notificationService.ApproveMissionNotify(missionRequest.UserId, missionRequest.Mission.Name);

            return(finalResult);
        }
Esempio n. 12
0
        /// <summary>
        /// Processes the request.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <returns>Task{MissionCompletionResult}.</returns>
        public async Task <MissionCompletionResult> ProcessRequest(MissionRequest request)
        {
            request.Mission.TriesFor1Star  = 10;
            request.Mission.TriesFor2Stars = 6;
            request.Mission.TriesFor3Stars = 2;

            if (request.Proof.Coordinates == null || !request.Proof.Coordinates.Any())
            {
                return(new MissionCompletionResult(OperationResultStatus.Error, "Not enough coordinates"));
            }
            var  radarCoordinate = request.Proof.Coordinates.First();
            var  borders         = GetBorders(request.User.HomeCoordinate, radarCoordinate);
            bool isNear;

            if (!CheckValidRadar(radarCoordinate, borders, request.User, out isNear))
            {
                var incorrectResult = await MissionRequestService.ProcessIncorrectTry(request, _missionRepository, _missionRequestRepository); if (incorrectResult.MissionCompletionStatus == MissionCompletionStatus.IntermediateFail)
                {
                    incorrectResult.Description = isNear ? "Incorrect" : "IsNear";
                }
                return(incorrectResult);
            }

            await MissionRequestService.SetStarsAccordingToTries(request, _missionRequestRepository);

            request.User.RadarCoordinate          = radarCoordinate;
            request.User.Selected2BaseCoordinates = borders;

            var recievedPoints = await RewardsCalculator.UpdateUserAfterMissionCompletion(
                request,
                _missionRepository);

            return(new MissionCompletionResult
            {
                Points = recievedPoints,
                StarsCount = request.StarsCount,
                MissionCompletionStatus = MissionCompletionStatus.Success
            });
        }
Esempio n. 13
0
        /// <summary>
        ///     Processes the request.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <returns>Task{MissionCompletionResult}.</returns>
        public async Task <MissionCompletionResult> ProcessRequest(MissionRequest request)
        {
            ushort amount;

            if (!ushort.TryParse(request.Proof.CreatedText, out amount))
            {
                return(new MissionCompletionResult(OperationResultStatus.Error, "Amount is not a number"));
            }
            if (amount > 180 || amount < 10)
            {
                request.DeclineReason = "Не верим!";
                await RewardsCalculator.UpdateUserAfterMissionDecline(request, _missionRepository);

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

            var dateOfBirth = request.User.DateOfBirth ?? DateTime.UtcNow;

            request.StarsCount = GetStarsBasingOnSexAndAge(
                (DateTime.UtcNow - dateOfBirth).TotalDays / 365,
                request.User.Sex ?? Sex.NotSet,
                amount);

            var recievedPoints = await RewardsCalculator.UpdateUserAfterMissionCompletion(request, _missionRepository);

            return(new MissionCompletionResult
            {
                Points = recievedPoints,
                StarsCount = request.StarsCount,
                MissionCompletionStatus = MissionCompletionStatus.Success
            });
        }
Esempio n. 14
0
        /// <summary>
        /// Processes the request.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <returns>Task{MissionCompletionResult}.</returns>
        public async Task <MissionCompletionResult> ProcessRequest(MissionRequest request)
        {
            if (request.Proof.NumberOfTries < 3)
            {
                request.StarsCount = 3;
            }
            else if (request.Proof.NumberOfTries <= 5)
            {
                request.StarsCount = 2;
            }
            else
            {
                request.StarsCount = 1;
            }

            var recievedPoints = await RewardsCalculator.UpdateUserAfterMissionCompletion(request, _missionRepository);

            return(new MissionCompletionResult
            {
                Points = recievedPoints,
                StarsCount = request.StarsCount,
                MissionCompletionStatus = MissionCompletionStatus.Success
            });
        }
Esempio n. 15
0
        private async Task <MissionCompletionResult> ProcessGeoCoordinate(MissionRequest request)
        {
            SetStarsAccordingToTime(request);
            var missionFailed  = request.StarsCount == 0;
            int?recievedPoints = null;

            if (missionFailed)
            {
                await RewardsCalculator.UpdateUserAfterMissionDecline(request, _missionRepository, _appCountersService);
            }
            else
            {
                recievedPoints = await RewardsCalculator.UpdateUserAfterMissionCompletion(
                    request,
                    _missionRepository,
                    _appCountersService);
            }


            var requestResult = await _missionRequestRepository.AddMissionRequest(request);

            if (requestResult.Status == OperationResultStatus.Success)
            {
                return(new MissionCompletionResult
                {
                    Points = recievedPoints,
                    StarsCount = request.StarsCount,
                    MissionCompletionStatus =
                        missionFailed
                                       ? MissionCompletionStatus.Fail
                                       : MissionCompletionStatus.Success
                });
            }

            return(MissionCompletionResult.FromOperationResult(requestResult));
        }