buffgym: Code style vs linter

Pass redraw as parameter to Workout, instead of using global variable.
Make code to honor eslint linebreak-style (use Windows linebreak).
Change eslintrc indent rule to match code style.
Add missing semicolons.
master
Tuukka Ikkala 2020-05-23 22:41:18 +03:00
parent 0bacb71bbd
commit 9e69fd2e74
6 changed files with 269 additions and 267 deletions

View File

@ -15,7 +15,8 @@
"rules": { "rules": {
"indent": [ "indent": [
"error", "error",
2 2,
{ "SwitchCase": 1 }
], ],
"linebreak-style": [ "linebreak-style": [
"error", "error",

View File

@ -1,153 +1,153 @@
exports = class Exercise { exports = class Exercise {
constructor(params) { constructor(params) {
this.completed = false; this.completed = false;
this.sets = []; this.sets = [];
this.title = params.title; this.title = params.title;
this.weight = params.weight; this.weight = params.weight;
this.weightIncrement = params.weightIncrement; this.weightIncrement = params.weightIncrement;
this.unit = params.unit; this.unit = params.unit;
this.restPeriod = params.restPeriod; this.restPeriod = params.restPeriod;
this._originalRestPeriod = params.restPeriod; this._originalRestPeriod = params.restPeriod;
this._restTimeout = null; this._restTimeout = null;
this._restInterval = null; this._restInterval = null;
this._state = null; this._state = null;
} }
get humanTitle() { get humanTitle() {
return `${this.title} ${this.weight}${this.unit}`; return `${this.title} ${this.weight}${this.unit}`;
} }
get subTitle() { get subTitle() {
const totalSets = this.sets.length; const totalSets = this.sets.length;
const uncompletedSets = this.sets.filter((set) => !set.isCompleted()).length; const uncompletedSets = this.sets.filter((set) => !set.isCompleted()).length;
const currentSet = (totalSets - uncompletedSets) + 1; const currentSet = (totalSets - uncompletedSets) + 1;
return `Set ${currentSet} of ${totalSets}`; return `Set ${currentSet} of ${totalSets}`;
} }
decRestPeriod() { decRestPeriod() {
this.restPeriod--; this.restPeriod--;
} }
addSet(set) { addSet(set) {
this.sets.push(set); this.sets.push(set);
} }
currentSet() { currentSet() {
return this.sets.filter(set => !set.isCompleted())[0]; return this.sets.filter(set => !set.isCompleted())[0];
} }
isLastSet() { isLastSet() {
return this.sets.filter(set => !set.isCompleted()).length === 1; return this.sets.filter(set => !set.isCompleted()).length === 1;
} }
isCompleted() { isCompleted() {
return !!this.completed; return !!this.completed;
} }
canSetCompleted() { canSetCompleted() {
return this.sets.filter(set => set.isCompleted()).length === this.sets.length; return this.sets.filter(set => set.isCompleted()).length === this.sets.length;
} }
setCompleted() { setCompleted() {
if (!this.canSetCompleted()) throw "All sets must be completed"; if (!this.canSetCompleted()) throw "All sets must be completed";
if (this.canProgress()) this.weight += this.weightIncrement; if (this.canProgress()) this.weight += this.weightIncrement;
this.completed = true; this.completed = true;
} }
canProgress() { canProgress() {
let completedRepsTotalSum = 0; let completedRepsTotalSum = 0;
let targetRepsTotalSum = 0; let targetRepsTotalSum = 0;
this.sets.forEach(set => completedRepsTotalSum += set.reps); this.sets.forEach(set => completedRepsTotalSum += set.reps);
this.sets.forEach(set => targetRepsTotalSum += set.maxReps); this.sets.forEach(set => targetRepsTotalSum += set.maxReps);
return (targetRepsTotalSum - completedRepsTotalSum) === 0; return (targetRepsTotalSum - completedRepsTotalSum) === 0;
} }
startRestTimer(workout) { startRestTimer(workout) {
this._restTimeout = setTimeout(() => { this._restTimeout = setTimeout(() => {
this.next(workout); this.next(workout);
}, 1000 * this.restPeriod); }, 1000 * this.restPeriod);
this._restInterval = setInterval(() => { this._restInterval = setInterval(() => {
this.decRestPeriod(); this.decRestPeriod();
if (this.restPeriod < 0) { if (this.restPeriod < 0) {
this.resetRestTimer(); this.resetRestTimer();
this.next(); this.next();
return; return;
} }
workout.emit("redraw"); workout.emit("redraw");
}, 1000 ); }, 1000 );
} }
resetRestTimer() { resetRestTimer() {
clearTimeout(this._restTimeout); clearTimeout(this._restTimeout);
clearInterval(this._restInterval); clearInterval(this._restInterval);
this._restTimeout = null; this._restTimeout = null;
this._restInterval = null; this._restInterval = null;
this.restPeriod = this._originalRestPeriod; this.restPeriod = this._originalRestPeriod;
} }
isRestTimerRunning() { isRestTimerRunning() {
return this._restTimeout != null; return this._restTimeout != null;
} }
setupStartedButtons(workout) { setupStartedButtons(workout) {
clearWatch(); clearWatch();
setWatch(() => { setWatch(() => {
this.currentSet().incReps(); this.currentSet().incReps();
workout.emit("redraw"); workout.emit("redraw");
}, BTN1, {repeat: true}); }, BTN1, {repeat: true});
setWatch(workout.next.bind(workout), BTN2, {repeat: false}); setWatch(workout.next.bind(workout), BTN2, {repeat: false});
setWatch(() => { setWatch(() => {
this.currentSet().decReps(); this.currentSet().decReps();
workout.emit("redraw"); workout.emit("redraw");
}, BTN3, {repeat: true}); }, BTN3, {repeat: true});
} }
setupRestingButtons(workout) { setupRestingButtons(workout) {
clearWatch(); clearWatch();
setWatch(workout.next.bind(workout), BTN2, {repeat: false}); setWatch(workout.next.bind(workout), BTN2, {repeat: false});
} }
next(workout) { next(workout) {
const STARTED = 1; const STARTED = 1;
const RESTING = 2; const RESTING = 2;
const COMPLETED = 3; const COMPLETED = 3;
switch(this._state) { switch(this._state) {
case null: case null:
this._state = STARTED; this._state = STARTED;
this.setupStartedButtons(workout); this.setupStartedButtons(workout);
break; break;
case STARTED: case STARTED:
this._state = RESTING; this._state = RESTING;
this.startRestTimer(workout); this.startRestTimer(workout);
this.setupRestingButtons(workout); this.setupRestingButtons(workout);
break; break;
case RESTING: case RESTING:
this.resetRestTimer(); this.resetRestTimer();
this.currentSet().setCompleted(); this.currentSet().setCompleted();
if (this.canSetCompleted()) { if (this.canSetCompleted()) {
this._state = COMPLETED; this._state = COMPLETED;
this.setCompleted(); this.setCompleted();
} else { } else {
this._state = null; this._state = null;
} }
// As we are changing state and require it to be reprocessed // As we are changing state and require it to be reprocessed
// invoke the next step of workout // invoke the next step of workout
workout.next(); workout.next();
break; break;
default: default:
throw "Exercise: Attempting to move to an unknown state"; throw "Exercise: Attempting to move to an unknown state";
} }
workout.emit("redraw"); workout.emit("redraw");
} }
} };

View File

@ -1 +1 @@
require("heatshrink").decompress(atob("mEwxH+ACPI5AUSADAtB5vNGFQtBAIfNF95hoF4wwoF5AwmF5BhmXYbAEF/6QbF1QwIF04qB54ADAwIwoF4oRKBoIvsB4gvZ58kkgCDFxoxaF5wuHGDQcMF5IwXDZwLDGDmlDIWlkgJDSwIABCRAwPDQohCFgIABDQIOCFwYABr4RCCQIvQDYguEAAwtFF5owJDZAvHFw4vFOYQvKFAowMBxIvFMQwvPAB4wFUQ4vJGDYvUGC4vNdgyuEGDIsNFwYwGNAgAPExAvMGIdfTIovfTpYvrfRCOkZ44ugF44NGF05gUFyQvKGIoueGKIufGJ4uhG5oupGItfr4vvAAgvlGAQvt/wrEF9oEGF841IF9QGHX0oGIAD8kAAYJOFzwEBBQoMFACA=")) require("heatshrink").decompress(atob("mEwxH+ACPI5AUSADAtB5vNGFQtBAIfNF95hoF4wwoF5AwmF5BhmXYbAEF/6QbF1QwIF04qB54ADAwIwoF4oRKBoIvsB4gvZ58kkgCDFxoxaF5wuHGDQcMF5IwXDZwLDGDmlDIWlkgJDSwIABCRAwPDQohCFgIABDQIOCFwYABr4RCCQIvQDYguEAAwtFF5owJDZAvHFw4vFOYQvKFAowMBxIvFMQwvPAB4wFUQ4vJGDYvUGC4vNdgyuEGDIsNFwYwGNAgAPExAvMGIdfTIovfTpYvrfRCOkZ44ugF44NGF05gUFyQvKGIoueGKIufGJ4uhG5oupGItfr4vvAAgvlGAQvt/wrEF9oEGF841IF9QGHX0oGIAD8kAAYJOFzwEBBQoMFACA="));

View File

@ -1,28 +1,28 @@
exports = class Set { exports = class Set {
constructor(maxReps) { constructor(maxReps) {
this.completed = false; this.completed = false;
this.minReps = 0; this.minReps = 0;
this.reps = 0; this.reps = 0;
this.maxReps = maxReps; this.maxReps = maxReps;
} }
isCompleted() { isCompleted() {
return !!this.completed; return !!this.completed;
} }
setCompleted() { setCompleted() {
this.completed = true; this.completed = true;
} }
incReps() { incReps() {
if (this.completed) return; if (this.completed) return;
if (this.reps >= this.maxReps) return; if (this.reps >= this.maxReps) return;
this.reps++; this.reps++;
} }
decReps() { decReps() {
if (this.completed) return; if (this.completed) return;
if (this.reps <= this.minReps) return; if (this.reps <= this.minReps) return;
this.reps--; this.reps--;
} }
} };

View File

@ -1,83 +1,84 @@
exports = class Workout { exports = class Workout {
constructor(params) { constructor(params) {
this.title = params.title; this.title = params.title;
this.exercises = []; this.exercises = [];
this.completed = false; this.completed = false;
this.on("redraw", redraw.bind(null, this)); this.on("redraw", params.redraw.bind(null, this));
} }
addExercises(exercises) { addExercises(exercises) {
exercises.forEach(exercise => this.exercises.push(exercise)); exercises.forEach(exercise => this.exercises.push(exercise));
} }
currentExercise() { currentExercise() {
return this.exercises.filter(exercise => !exercise.isCompleted())[0]; return this.exercises.filter(exercise => !exercise.isCompleted())[0];
} }
canComplete() { canComplete() {
return this.exercises.filter(exercise => exercise.isCompleted()).length === this.exercises.length; return this.exercises.filter(exercise => exercise.isCompleted()).length === this.exercises.length;
} }
setCompleted() { setCompleted() {
if (!this.canComplete()) throw "All exercises must be completed"; if (!this.canComplete()) throw "All exercises must be completed";
this.completed = true; this.completed = true;
} }
isCompleted() { isCompleted() {
return !!this.completed; return !!this.completed;
} }
static fromJSON(workoutJSON) { static fromJSON(workoutJSON, redraw) {
const Set = require("buffgym-set.js"); const Set = require("buffgym-set.js");
const Exercise = require("buffgym-exercise.js"); const Exercise = require("buffgym-exercise.js");
const workout = new this({ const workout = new this({
title: workoutJSON.title, title: workoutJSON.title,
}); redraw: redraw,
const exercises = workoutJSON.exercises.map(exerciseJSON => { });
const exercise = new Exercise({ const exercises = workoutJSON.exercises.map(exerciseJSON => {
title: exerciseJSON.title, const exercise = new Exercise({
weight: exerciseJSON.weight, title: exerciseJSON.title,
weightIncrement: exerciseJSON.weightIncrement, weight: exerciseJSON.weight,
unit: exerciseJSON.unit, weightIncrement: exerciseJSON.weightIncrement,
restPeriod: exerciseJSON.restPeriod, unit: exerciseJSON.unit,
}); restPeriod: exerciseJSON.restPeriod,
exerciseJSON.sets.forEach(setJSON => { });
exercise.addSet(new Set(setJSON)); exerciseJSON.sets.forEach(setJSON => {
}); exercise.addSet(new Set(setJSON));
});
return exercise;
}); return exercise;
});
workout.addExercises(exercises);
workout.addExercises(exercises);
return workout;
} return workout;
}
toJSON() {
return { toJSON() {
title: this.title, return {
exercises: this.exercises.map(exercise => { title: this.title,
return { exercises: this.exercises.map(exercise => {
title: exercise.title, return {
weight: exercise.weight, title: exercise.title,
weightIncrement: exercise.weightIncrement, weight: exercise.weight,
unit: exercise.unit, weightIncrement: exercise.weightIncrement,
sets: exercise.sets.map(set => set.maxReps), unit: exercise.unit,
restPeriod: exercise.restPeriod, sets: exercise.sets.map(set => set.maxReps),
}; restPeriod: exercise.restPeriod,
}), };
}; }),
} };
}
// State machine
next() { // State machine
if (this.canComplete()) { next() {
this.setCompleted(); if (this.canComplete()) {
this.emit("redraw"); this.setCompleted();
return; this.emit("redraw");
} return;
}
// Call current exercise state machine
this.currentExercise().next(this); // Call current exercise state machine
} this.currentExercise().next(this);
} }
};

View File

@ -248,7 +248,7 @@ function getWorkoutIndex() {
function buildWorkout(fName) { function buildWorkout(fName) {
const Workout = require("buffgym-workout.js"); const Workout = require("buffgym-workout.js");
const workoutJSON = require("Storage").readJSON(fName); const workoutJSON = require("Storage").readJSON(fName);
const workout = Workout.fromJSON(workoutJSON); const workout = Workout.fromJSON(workoutJSON, redraw);
return workout; return workout;
} }