Exemplo n.º 1
0
        public async Task <List <UserTrainingProgram> > CalculateUserTrainingProgram(User user)
        {
            var body = new ChooseTrainingProgramBody()
            {
                User = user,
                AllTrainingPrograms = App.defaultTrainingPrograms
            };

            var response = await App.MobileService.InvokeApiAsync <ChooseTrainingProgramBody, List <UserTrainingProgram> >(
                "ChooseTrainingProgram",    // The name of the API
                body,                       // The body of the POST
                HttpMethod.Post,            // The HTTP Method
                null);

            return(response);
        }
        public List <UserTrainingProgram> PostAsync([FromBody] ChooseTrainingProgramBody body)
        {
            var userPreferredWorkoutDays = body.User.PreferredWorkoutDays.Split(';');

            //First, filter by preferred training program type
            var optionalDefaultProgramsByType = body.AllTrainingPrograms.Where(program => program.TrainingProgramType == body.User.PreferredTrainingProgramType);

            // Weight functions definitions
            TrainingProgramGoal[] goalsDistance = { TrainingProgramGoal.Speed, TrainingProgramGoal.Flexibility, TrainingProgramGoal.WeightLoss, TrainingProgramGoal.Fitness, TrainingProgramGoal.Strength, TrainingProgramGoal.Size };
            Difficulty[]          difficulties  = { Difficulty.Begginer, Difficulty.Intermediate, Difficulty.Expert };

            double firstPlaceGrade  = 0;
            double secondPlaceGrade = 0;
            double thirdPlaceGrade  = 0;

            DefaultTrainingProgram firstPlace  = null;
            DefaultTrainingProgram secondPlace = null;
            DefaultTrainingProgram thirdPlace  = null;

            // Calculate grades for all programs
            foreach (var program in body.AllTrainingPrograms)
            {
                double currentProgramGoalWeight = goalsDistance.ToList().FindIndex(goal => goal.Equals(program.TrainingProgramGoal));
                double userPreferredGoalWeight  = goalsDistance.ToList().FindIndex(goal => goal.Equals(body.User.PreferredTrainingGoal));

                double currentProgramDiffiucaltyWeight = difficulties.ToList().FindIndex(difficulty => difficulty.Equals(program.Difficulty));
                double userPreferredDiffiucaltyWeight  = difficulties.ToList().FindIndex(difficulty => difficulty.Equals(body.User.PreferredTrainingProgramDifficulty));

                double goalsGrade       = 1 - (Math.Abs(currentProgramGoalWeight - userPreferredGoalWeight) / goalsDistance.Count());
                double diffiucaltyGrade = 1 - (Math.Abs(currentProgramDiffiucaltyWeight - userPreferredDiffiucaltyWeight) / difficulties.Count());

                double totalProgramGrade = 6 * goalsGrade + diffiucaltyGrade;

                // Check if program's grade is bigger then the max grades
                if (totalProgramGrade > firstPlaceGrade)
                {
                    thirdPlaceGrade  = secondPlaceGrade;
                    secondPlaceGrade = firstPlaceGrade;
                    firstPlaceGrade  = totalProgramGrade;

                    thirdPlace  = secondPlace;
                    secondPlace = firstPlace;
                    firstPlace  = program;
                }
                else if (totalProgramGrade > secondPlaceGrade)
                {
                    thirdPlaceGrade  = secondPlaceGrade;
                    secondPlaceGrade = totalProgramGrade;

                    thirdPlace  = secondPlace;
                    secondPlace = program;
                }
                else if (totalProgramGrade > thirdPlaceGrade)
                {
                    thirdPlace      = program;
                    thirdPlaceGrade = totalProgramGrade;
                }
            }

            var optionalUserPrograms = new List <UserTrainingProgram>();

            var firstSuggestedProgram = CreateUserTrainingProgram(firstPlace, body.User);

            optionalUserPrograms.Add(firstSuggestedProgram);

            var secondSuggestedProgram = CreateUserTrainingProgram(secondPlace, body.User);

            optionalUserPrograms.Add(secondSuggestedProgram);

            var thirdSuggestedProgram = CreateUserTrainingProgram(thirdPlace, body.User);

            optionalUserPrograms.Add(thirdSuggestedProgram);


            return(optionalUserPrograms);
        }