Pseudocode
I. API Endpoint /assign_badges/
Method : POST
Parameters : user_id, workout_log_id, request_type
Usage request_type = 'workout': - When user clicks Start Workout Button, We need to make a entry in workout_log table with status started (Required Development).
Usage request_type = 'signup': - When new user signup in the app for the first time we send the API request signup.
Usage request_type = 'meal': - When new user personalize thier meal plan.
Usage request_type = 'exercise': - When new user completes certain exercise.
Usage request_type = 'game': - When new user plays certain games.
1. For request_type = 'workout'
In Background it starts to process the below processes,
1.1. Process 1: Workout Validation
- Retrieve Workout Time:
- Query A: Retrieve the
workout_timefrom theexercisestable. - Query B: Retrieve the
user_workout_timefrom theusers_workout_logtable. - Validation: Compare the results from Query A and Query B to ensure that
workout_time(fromexercises) matchesuser_workout_time(fromusers_workout_log). This ensures that the total workout time recorded matches the user's recorded progress.
- Query A: Retrieve the
1.2. Process 2: Badge Assignment Checks
If Process 1 is validated as true, proceed with the following checks:
1.2.1. Condition Check 1:
-
Requirement: Ensure the user has completed 3 or fewer workouts.
-
Actions:
- Method get_completed_workout_count() - Retrieves the
users_workout_logtable to count the number of workout completions for the user. Verify that this count is less than or equal to 3. - Method get_today_completed_workouts() - Retrieves the
users_workout_logtable to count the number of workout completions for the user in today. - Method get_today_workout_time() - Retrieves the total workout duration logged by the user for the current day from the
users_workout_logtable. - Method get_yesterday_workout_time() - Retrieves the total workout duration logged by the user for the previous day from the
users_workout_logtable. - Method badge_already_assigned() - Checks wheather the requested badge already assigned for the user.
- Method get_completed_workout_count() - Retrieves the
-
1.2.1.1. Loop Checks for Badge Assignment:
- Loop 1: If the count of completed exercises is 1
i.e get_completed_workout_count = 1 and not badge_already_assigned:- Check if the
C-CMB-01-003badge is not assigned previously. - If not assigned the badge, Reward the badge
C-CMB-01-003and end the loop, the call the functioncreate_badge_assignment().
- Check if the
- Loop 2: If the count of completed exercises is 2
i.e. get_completed_workout_count = 2:- Condition A: Verify that the
Today Completed workout = 1and badgeC-CMB-02-004is not assigned previouslyi.e today_completed_workouts == 1 and not badge_already_assigned. - If not assigned the badge, Reward Badge
C-CMB-02-004and end the loop, the call the functioncreate_badge_assignment(). - Condition B: Verify that the
Today Completed workout = 2and badgeC-CMB-03-005is not assigned previouslyi.e today_completed_workouts == 2 and not badge_already_assigned. - If not assigned the badge, Reward Badge
C-CMB-03-005and end the loop, the call the functioncreate_badge_assignment(). - Condition C: Check if the user's workout time today is greater than yesterday's workout time and the badge
C-MAB-04-006not been assigned beforei.e today_workout_time > yesterday_workout_time and not badge_already_assigned. - If not assigned the badge, Reward the badge
C-MAB-04-006and end the loop, the call the functioncreate_badge_assignment().
- Condition A: Verify that the
- Loop 3: If the count of completed exercises is 3
i.e. get_completed_workout_count = 3:- Condition A: Verify that the
Today Completed workout = 3and badgeC-CMB-05-007is not assigned previouslyi.e today_completed_workouts == 3 and not badge_already_assigned. - If not assigned the badge, Reward the badge
C-CMB-05-007and end the loop, the call the functioncreate_badge_assignment(). - Condition B: Check if the user's workout time today is greater than yesterday's workout time and the badge
C-MAB-04-006not been assigned beforei.e today_workout_time > yesterday_workout_time and not badge_already_assigned. - If not assigned the badge, Reward the badge
C-MAB-04-006and end the loop, the call the functioncreate_badge_assignment().
- Condition A: Verify that the
- Loop 1: If the count of completed exercises is 1
1.2.2. Condition Check 2:
-
Requirement: Ensure the user has completed at least 7 workouts and has completed at least one program.
-
Action:
- Method get_completed_workout_count() - Retrieves the
users_workout_logtable to count the number of workout completions for the user. Verify that this count is greater than or equal to 7. - Method get_programs_completed_count() - Checks the
exercise_workoutstable to check if at least one program has a status of "completed" for the respective user. - Method calculate_workout_completion_percentage() - Calculates the total completed workout of respective program and return its percentage
- Method meal_completed() - Checks table
user_meal_logand returns that user meal completion status. - Method are_workouts_at_same_time - Checks that All workout completed in same time with maximum 30 mins - 1 hr differences
- Method get_completed_workout_count() - Retrieves the
-
1.2.2.1. Loop Checks for Badge Assignment:
- Loop 1: If the percentage of completed workout is 80% (Eg. 8/10 completed workouts)
i.e percentage_completed == 80 and not badge_already_assigned:- Check if the
E-MB-01-012badge is not assigned previously. - If not assigned the badge, Reward the badge
E-MB-01-012and end the loop, the call the functioncreate_badge_assignment().
- Check if the
- Loop 2: If the percentage of completed workout is 90% (i.e 9/10 completed workouts)
i.e percentage_completed == 90:- Condition A: Verify that the
90% completition of workoutandCompletition of mealand badgeF-MAB-01is not assigned previouslyi.e meal_completed and not badge_already_assigned. - If not assigned the badge, Reward Badge
F-MAB-01and end the loop, the call the functioncreate_badge_assignment(). - Condition B: Verify that the
90% completition of workoutandAll workout completed in same time with maximum 30 mins - 1 hr differencesand badgeE-MEB-02-013is not assigned previouslyi.e same_time_workouts and not badge_already_assigned. - If not assigned the badge, Reward Badge
E-MEB-02-013and end the loop, the call the functioncreate_badge_assignment(). - Condition C: Verify that the
90% completition of workoutand badgeE-MEB-03-014is not assigned previouslyi.e not badge_already_assigned. - If not assigned the badge, Reward Badge
E-MEB-03-014and end the loop, the call the functioncreate_badge_assignment().
- Condition A: Verify that the
- Loop 3: If the percentage of completed workout is 100% (i.e 10/10 completed workouts)
i.e percentage_completed == 100:- Condition A: Verify that the
100% completition of workoutandCompletition of mealand badgeF-MAB-02is not assigned previouslyi.e meal_completed and not badge_already_assigned. - If not assigned the badge, Reward Badge
F-MAB-02and end the loop, the call the functioncreate_badge_assignment(). - Condition B: Verify that the
100% completition of workoutandAll workout completed in same time with maximum 30 mins - 1 hr differencesand badgeE-EHB-05-016is not assigned previouslyi.e same_time_workouts and not badge_already_assigned. - If not assigned the badge, Reward Badge
E-EHB-05-016and end the loop, the call the functioncreate_badge_assignment(). - Condition C: Verify that the
100% completition of workoutand badgeE-SB-04-015is not assigned previouslyi.e not badge_already_assigned. - If not assigned the badge, Reward Badge
E-SB-04-015and end the loop, the call the functioncreate_badge_assignment().
- Condition A: Verify that the
- Loop 1: If the percentage of completed workout is 80% (Eg. 8/10 completed workouts)
1.2.3. Condition Check 3:
-
Requirement: Ensure the user has completed at least 14 workouts and has completed at least two programs.
-
Action:
- Method get_completed_workout_count() - Retrieves the
users_workout_logtable to count the number of workout completions for the user. Verify that this count is greater than or equal to 14. - Method get_programs_completed_count() - Checks the
exercise_workoutstable to check if at least two program has a status of "completed" for the respective user. - Method calculate_workout_completion_percentage() - Calculates the total completed workout of respective program and return its percentage.
- Method are_workouts_at_same_time - Checks that All workout completed in same time with maximum 30 mins - 1 hr differences
- Method get_completed_workout_count() - Retrieves the
-
1.2.3.1. Loop Checks for Badge Assignment:
- Loop 1: If the percentage of completed workout is 100% (Eg. 10/10 completed workouts):
- Condition A: Verify that the
100% completition of workoutandAll workout completed in same time with maximum 30 mins - 1 hr differencesand badgeH-EHB-01-021is not assigned previouslyi.e percentage_completed == 100 and same_time_workouts and not badge_already_assigned. - If not assigned the badge, Reward Badge
H-EHB-01-021and end the loop, the call the functioncreate_badge_assignment(). - Condition B: Verify that the
100% completition of workoutand badgeH-EHB-02-022is not assigned previouslyi.e not badge_already_assigned. - If not assigned the badge, Reward Badge
H-EHB-02-022and end the loop, the call the functioncreate_badge_assignment().
- Condition A: Verify that the
- Loop 1: If the percentage of completed workout is 100% (Eg. 10/10 completed workouts):
1.2.4. Condition Check 4:
-
Requirement: Ensure the user has completed more than 21 workouts and has completed at least three programs.
-
Action:
- Method get_completed_workout_count() - Retrieves the
users_workout_logtable to count the number of workout completions for the user. Verify that this count is greater than or equal to 21. - Method get_programs_completed_count() - Checks the
exercise_workoutstable to check if at least three program has a status of "completed" for the respective user. - Method meal_completed() - Checks table
user_meal_logand returns that user meal completion status. - Method get_consecutive_workout_days() - Checks table
users_workout_logand returns total consecutive days.
- Method get_completed_workout_count() - Retrieves the
-
1.2.4.1. Loop Checks for Badge Assignment:
- Loop 1: If the workout was completed in consecutive days:
- Condition A: Verify that the user has
21 Consecutive Daysof workout completion and badgeI-SB-01-023is not assigned previouslyi.e. consecutive_days >= 21 and not badge_already_assigned. - If not assigned the badge, Reward Badge
I-SB-01-023and end the loop, the call the functioncreate_badge_assignment(). - Condition B: Verify that the user has
30 Consecutive Daysof workout completion and badgeI-SB-02-024is not assigned previouslyi.e. consecutive_days >= 30 and not badge_already_assigned. - If not assigned the badge, Reward Badge
I-SB-02-024and end the loop, the call the functioncreate_badge_assignment(). - Condition C: Verify that the user has
90 Consecutive Daysof workout completion and badgeI-SB-03-025is not assigned previouslyi.e. consecutive_days >= 90 and not badge_already_assigned. - If not assigned the badge, Reward Badge
I-SB-03-025and end the loop, the call the functioncreate_badge_assignment(). - Condition D: Verify that the user has
180 Consecutive Daysof workout completion and badgeI-SB-04-026is not assigned previouslyi.e. consecutive_days >= 180 and not badge_already_assigned. - If not assigned the badge, Reward Badge
I-SB-04-026and end the loop, the call the functioncreate_badge_assignment(). - Condition E: Verify that the user has
180 Consecutive Daysof workout completion andUser completes his mealsand badgeI-SB-05-027is not assigned previouslyi.e. consecutive_days >= 21 and meal_completed_not badge_already_assigned. - If not assigned the badge, Reward Badge
I-SB-05-027and end the loop, the call the functioncreate_badge_assignment().
- Condition A: Verify that the user has
- Loop 1: If the workout was completed in consecutive days:
Method create_badge_assignment()
- Create a queue in RabbitMQ and send the message
'badge_queue_{user_id}_{workoutlog_id}' - consumer.py listens the message and once message received, call the
reward_badge() - reward_badge() - Method create a badge for user in
user_badgeswith statusinitated
2. For request_type = 'signup'
Conditions of assigning badge
Actions :
- Method badge_already_assigned() - Checks wheather the requested badge already assigned for the user.
- Method is_new_user() - Check user has entry in
auth_userstable, returns value if new user. - Method reward_badge() - To create a badge for user in
user_badges
Condition :
- When
is_new_user() and not badge_already_assigned() - Reward Badge
A-CM-01-001and end the loop and return the badges
3. For request_type = 'meal'
Conditions of assigning badge
Actions :
- Method badge_already_assigned() - Checks wheather the requested badge already assigned for the user.
- Method meal_personalized_with_nutrition() - Returns True, When user personalize a meal the we check table
user_meal_logthat their request contains more nutrition preferences. - Method meal_personalized_twice() - Returns True, When user personalize a meal more than twice in a day.
Condition :
- Loop Checks for Badge Assignment:
- Loop 1: If the user personalize a meal:
-
Condition A: Verify that the user personalize their meal with high nutrition preferences
i.e. meal_personalized_with_nutrition() and not badge_already_assigned() -
If not assigned the badge, Reward Badge
G-NCB-01-019and end the loop, the call the functionreward_badge(). -
Condition B: Verify that the user personalize their meal with more than twice in a day
i.e. meal_personalized_twice() and not badge_already_assigned() -
If not assigned the badge, Reward Badge
G-NCB-02-020and end the loop, the call the functionreward_badge().
-
- Loop 1: If the user personalize a meal:
4. For request_type = 'game'
Conditions of assigning badge
Actions :
- Method badge_already_assigned() - Checks wheather the requested badge already assigned for the user.
Condition :
- Condition A: Check If the user completes 2048 game and badge not assigned, Reward Badge
D-NCB-03-010and the call the functionreward_badge(). - Condition B: Check If the user completes Hangman game and badge not assigned, Reward Badge
D-NCB-04-011and the call the functionreward_badge().
II. API Endpoint /validate_badge_after_workout_completed/
Method : POST
Parameters : user_id, workout_log_id, user_workout_time
Usage : When user clicks Completed Workout Button, then call the API /validate_badge_after_workout_completed/
Step 1:
Update the workout_log table by setting the user_workout_time and changing the status from initiated to completed.
Step 2:
Invoke the validate_workout_time() method to verify that the user_workout_time matches the exercise_time, confirming the validity of the workout.
Step 3:
If the workout is valid, check the user_badges table for any badges associated with the user_id and workout_log_id. If a badge is found, return it in the API response. If no badge is found, delete the corresponding entry in the user_badges table.