133 lines
2.8 KiB
JavaScript
133 lines
2.8 KiB
JavaScript
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;
|
|
}
|
|
|
|
}
|