Random generation of workout plan
This commit is contained in:
		
							
								
								
									
										11
									
								
								App.js
									
									
									
									
									
								
							
							
						
						
									
										11
									
								
								App.js
									
									
									
									
									
								
							| @@ -1,9 +1,14 @@ | |||||||
| document.addEventListener('DOMContentLoaded', function() { | document.addEventListener('DOMContentLoaded', function() { | ||||||
|     fetch('routine.json') |     fetch('collection.json') | ||||||
|         .then(response => response.json()) |         .then(response => response.json()) | ||||||
|         .then(workout_json => { |         .then(collection_json => { | ||||||
|             // Use the JSON workout_json here |             // Generate Exercise plan | ||||||
|  |             const egen = new ExerciseGenerator(collection_json); | ||||||
|  |  | ||||||
|  |             // 2 sets of 4 exercises each, 3 reps per set | ||||||
|  |             let workout_json = egen.generate_build_and_burn(2, 4, 3); | ||||||
|             console.log(workout_json); |             console.log(workout_json); | ||||||
|  |  | ||||||
|             const wo = new Workout(document.getElementById("content-wrapper"), |             const wo = new Workout(document.getElementById("content-wrapper"), | ||||||
|                                    workout_json); |                                    workout_json); | ||||||
|             wo.render(); |             wo.render(); | ||||||
|   | |||||||
							
								
								
									
										132
									
								
								ExerciseGenerator.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										132
									
								
								ExerciseGenerator.js
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,132 @@ | |||||||
|  | class ExerciseGenerator { | ||||||
|  |  | ||||||
|  | // ---------- | ||||||
|  | constructor(collection) { | ||||||
|  |     this.collection = collection; | ||||||
|  |  | ||||||
|  |     // console.log(this.collection); | ||||||
|  |     this.used_exercise_ids = new Array; | ||||||
|  |  | ||||||
|  |     this.ex_nxt; | ||||||
|  |     this.ex_last = null; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | // ---------- | ||||||
|  | exercise_id_used(ex) { | ||||||
|  |     return (this.used_exercise_ids.filter(function(v) { return (v == ex.id) }).length > 0); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | // ---------- | ||||||
|  | exercise_related_to(ex) { | ||||||
|  |     let related = false; | ||||||
|  |     let gen = this; | ||||||
|  |  | ||||||
|  |     if (gen.ex_last == null) { | ||||||
|  |         return related; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     ex.focus.forEach(function(e) { | ||||||
|  |         related |= gen.ex_last.focus.includes(e); | ||||||
|  |     }); | ||||||
|  |  | ||||||
|  |     return related; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | // ---------- | ||||||
|  | get_next_exercise() { | ||||||
|  |     let scope = this.collection.exercises; | ||||||
|  |     let gen = this; | ||||||
|  |  | ||||||
|  |     scope = this.collection.exercises.filter(function(e) { | ||||||
|  |         let pick = true; | ||||||
|  |  | ||||||
|  |         // Pick if exercise is not already used | ||||||
|  |         pick &= (gen.exercise_id_used(e) == false); | ||||||
|  |  | ||||||
|  |         // Pick if nor related to the previous one | ||||||
|  |         pick &= (gen.exercise_related_to(e) == false); | ||||||
|  |  | ||||||
|  |         return pick | ||||||
|  |     }); | ||||||
|  |     gen.shuffle_exercise_array(scope); | ||||||
|  |     // gen.show_exercise_array(scope); | ||||||
|  |  | ||||||
|  |     return scope[0]; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | // ---------- | ||||||
|  | show_exercise_array(arr) { | ||||||
|  |     let gen = this; | ||||||
|  |  | ||||||
|  |     arr.map(function(it) { | ||||||
|  |         gen.show_exercise(it); | ||||||
|  |     }); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | // ---------- | ||||||
|  | show_exercise(ex) { | ||||||
|  |     console.log(`${ex.id} : ${ex.name} [` + ex.focus.join(", ") + ']'); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | // ---------- | ||||||
|  | // From answer on https://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array | ||||||
|  | shuffle_exercise_array(arr) { | ||||||
|  |     let currentIndex = arr.length; | ||||||
|  |  | ||||||
|  |     // While there remain elements to shuffle... | ||||||
|  |     while (currentIndex != 0) { | ||||||
|  |         // Pick a remaining element... | ||||||
|  |         let randomIndex = Math.floor(Math.random() * currentIndex); | ||||||
|  |         currentIndex--; | ||||||
|  |  | ||||||
|  |         // And swap it with the current element. | ||||||
|  |         [arr[currentIndex], arr[randomIndex]] = [ | ||||||
|  |             arr[randomIndex], arr[currentIndex]]; | ||||||
|  |     } | ||||||
|  | } | ||||||
|  |  | ||||||
|  | // ---------- | ||||||
|  | generate_set(size) { | ||||||
|  |     let set_exercises = Array(); | ||||||
|  |     let lim = size; | ||||||
|  |  | ||||||
|  |     this.ex_last = null; | ||||||
|  |  | ||||||
|  |     do { | ||||||
|  |         this.ex_nxt = this.get_next_exercise(); | ||||||
|  |         if (this.ex_nxt == null) { | ||||||
|  |             break; | ||||||
|  |         } | ||||||
|  |         this.used_exercise_ids.push(this.ex_nxt.id); | ||||||
|  |         set_exercises.push(this.ex_nxt); | ||||||
|  |         this.ex_last = this.ex_nxt; | ||||||
|  |         lim--; | ||||||
|  |     } while ((this.ex_nxt != null) && (lim > 0)); | ||||||
|  |  | ||||||
|  |     this.show_exercise_array(set_exercises); | ||||||
|  |     console.log(this.used_exercise_ids); | ||||||
|  |  | ||||||
|  |     return set_exercises; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | // ---------- | ||||||
|  | generate_build_and_burn(sets, exercises, reps) { | ||||||
|  |     let wo_plan = { | ||||||
|  |         "name": "Daily Build and Burn", | ||||||
|  |         "set": [ | ||||||
|  |         ] | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     for (let i = 1; i <= sets; i ++) { | ||||||
|  |         let set = { | ||||||
|  |             "name": `Set ${i}`, | ||||||
|  |             "count": reps, | ||||||
|  |             "exercise": this.generate_set(exercises) | ||||||
|  |         } | ||||||
|  |         wo_plan.set.push(set); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     return wo_plan; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | } | ||||||
							
								
								
									
										65
									
								
								collection.json
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										65
									
								
								collection.json
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,65 @@ | |||||||
|  | { | ||||||
|  |     "name": "Collection of exercises", | ||||||
|  |     "exercises": [ | ||||||
|  |         { | ||||||
|  |           "id": 1, | ||||||
|  |           "name": "Leg triads with dumbell", | ||||||
|  |           "count": 10, | ||||||
|  |           "pattern": "each side", | ||||||
|  |           "focus": ["legs"]}, | ||||||
|  |         { | ||||||
|  |           "id": 2, | ||||||
|  |           "name": "Forward lunge with dumbell twist", | ||||||
|  |           "count": 10, | ||||||
|  |           "pattern": "each side", | ||||||
|  |           "focus": ["legs"]}, | ||||||
|  |         { | ||||||
|  |           "id": 3, | ||||||
|  |           "name": "A and Y core on floor", | ||||||
|  |           "count": 10, | ||||||
|  |           "pattern": "each end", | ||||||
|  |           "focus": ["core"]}, | ||||||
|  |         { | ||||||
|  |           "id": 4, | ||||||
|  |           "name": "Bow and arrow pull with bands", | ||||||
|  |           "count": 10, | ||||||
|  |           "pattern": "each side", | ||||||
|  |           "focus": ["arms"]}, | ||||||
|  |         { | ||||||
|  |           "id": 5, | ||||||
|  |           "name": "Push ups", | ||||||
|  |           "count": 10, | ||||||
|  |           "pattern": "", | ||||||
|  |           "focus": ["arms", "chest"]}, | ||||||
|  |         { | ||||||
|  |           "id": 6, | ||||||
|  |           "name": "Bicep curls", | ||||||
|  |           "count": 10, | ||||||
|  |           "pattern": "each side", | ||||||
|  |           "focus": ["arms"]}, | ||||||
|  |         { | ||||||
|  |           "id": 7, | ||||||
|  |           "name": "Russian twists", | ||||||
|  |           "count": 10, | ||||||
|  |           "pattern": "", | ||||||
|  |           "focus": ["core"]}, | ||||||
|  |         { | ||||||
|  |           "id": 8, | ||||||
|  |           "name": "Chin up core", | ||||||
|  |           "count": 10, | ||||||
|  |           "pattern": "", | ||||||
|  |           "focus": ["arms", "core"]}, | ||||||
|  |         { | ||||||
|  |           "id": 9, | ||||||
|  |           "name": "Tricep curls on the floor", | ||||||
|  |           "count": 15, | ||||||
|  |           "pattern": "", | ||||||
|  |           "focus": ["arms"]}, | ||||||
|  |         { | ||||||
|  |           "id": 10, | ||||||
|  |           "name": "Leg curl on floor", | ||||||
|  |           "count": 10, | ||||||
|  |           "pattern": "each leg", | ||||||
|  |           "focus": ["legs"]} | ||||||
|  |     ] | ||||||
|  | } | ||||||
| @@ -10,6 +10,7 @@ | |||||||
|         <link href="https://fonts.googleapis.com/css2?family=Boldonse&family=Roboto:ital,wght@0,100..900;1,100..900&display=swap" rel="stylesheet"> |         <link href="https://fonts.googleapis.com/css2?family=Boldonse&family=Roboto:ital,wght@0,100..900;1,100..900&display=swap" rel="stylesheet"> | ||||||
|         <link rel="stylesheet" href="style.css" /> |         <link rel="stylesheet" href="style.css" /> | ||||||
|  |  | ||||||
|  |         <script type="text/javascript" src="ExerciseGenerator.js"></script> | ||||||
|         <script type="text/javascript" src="Workout.js"></script> |         <script type="text/javascript" src="Workout.js"></script> | ||||||
|         <script type="text/javascript" src="App.js"></script> |         <script type="text/javascript" src="App.js"></script> | ||||||
|  |  | ||||||
|   | |||||||
							
								
								
									
										39
									
								
								routine.json
									
									
									
									
									
								
							
							
						
						
									
										39
									
								
								routine.json
									
									
									
									
									
								
							| @@ -1,39 +0,0 @@ | |||||||
| { |  | ||||||
|     "name": "Daily Build and Burn", |  | ||||||
|     "set": [ |  | ||||||
|         { "name": "Build", |  | ||||||
|           "count": 3, |  | ||||||
|           "exercise": [ |  | ||||||
|             { "name": "Forward lunge with dumbell twist", |  | ||||||
|               "count": 10, |  | ||||||
|               "pattern": "each side"}, |  | ||||||
|             { "name": "A and Y core on floor", |  | ||||||
|               "count": 10, |  | ||||||
|               "pattern": "each end"}, |  | ||||||
|             { "name": "Bow and arrow pull with bands", |  | ||||||
|               "count": 10, |  | ||||||
|               "pattern": "each side"}, |  | ||||||
|             { "name": "Push ups", |  | ||||||
|               "count": 10, |  | ||||||
|               "pattern": ""} |  | ||||||
|             ] |  | ||||||
|         }, |  | ||||||
|         { "name": "Burn", |  | ||||||
|           "count": 3, |  | ||||||
|           "exercise": [ |  | ||||||
|             { "name": "Bicep curls", |  | ||||||
|               "count": 10, |  | ||||||
|               "pattern": "each side"}, |  | ||||||
|             { "name": "Russian twists", |  | ||||||
|               "count": 10, |  | ||||||
|               "pattern": ""}, |  | ||||||
|             { "name": "Chin up core", |  | ||||||
|               "count": 10, |  | ||||||
|               "pattern": ""}, |  | ||||||
|             { "name": "Tricep curls on the floor", |  | ||||||
|               "count": 15, |  | ||||||
|               "pattern": ""} |  | ||||||
|             ] |  | ||||||
|         } |  | ||||||
|     ] |  | ||||||
| } |  | ||||||
		Reference in New Issue
	
	Block a user