From f96143757a7671d10eb3fedeb1ec6c99727c2eee Mon Sep 17 00:00:00 2001 From: Mahesh Asolkar Date: Sun, 30 Mar 2025 21:52:16 -0700 Subject: [PATCH] Random generation of workout plan --- App.js | 11 +++- ExerciseGenerator.js | 132 +++++++++++++++++++++++++++++++++++++++++++ collection.json | 65 +++++++++++++++++++++ index.html | 1 + routine.json | 39 ------------- 5 files changed, 206 insertions(+), 42 deletions(-) create mode 100644 ExerciseGenerator.js create mode 100644 collection.json delete mode 100644 routine.json diff --git a/App.js b/App.js index 7ef4b55..0d45841 100644 --- a/App.js +++ b/App.js @@ -1,9 +1,14 @@ document.addEventListener('DOMContentLoaded', function() { - fetch('routine.json') + fetch('collection.json') .then(response => response.json()) - .then(workout_json => { - // Use the JSON workout_json here + .then(collection_json => { + // 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); + const wo = new Workout(document.getElementById("content-wrapper"), workout_json); wo.render(); diff --git a/ExerciseGenerator.js b/ExerciseGenerator.js new file mode 100644 index 0000000..140ba44 --- /dev/null +++ b/ExerciseGenerator.js @@ -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; +} + +} diff --git a/collection.json b/collection.json new file mode 100644 index 0000000..4e630ee --- /dev/null +++ b/collection.json @@ -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"]} + ] +} diff --git a/index.html b/index.html index d03ec1f..ec1b053 100644 --- a/index.html +++ b/index.html @@ -10,6 +10,7 @@ + diff --git a/routine.json b/routine.json deleted file mode 100644 index 64d3db8..0000000 --- a/routine.json +++ /dev/null @@ -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": ""} - ] - } - ] -}