let s = true; // Whether to start sequence
const r = 50; // Total number of steps
let x; // Number of recipe steps 1 -> r
let y; // Starting recipe step 1 -> r
let z; // Ending recipe step 1 -> r
let c; // Current recipe step 1 -> r
const t = 60; // Time duration in seconds
let d = false; // Whether steps are done
async function algorithm(options) {
return new Promise(resolve_entire_algorithm => {
const total_steps = options.r;
const number_of_steps = options.x;
const starting_step = options.y;
let ending_step = starting_step + number_of_steps;
while(ending_step > /*Math.min(starting_step,number_of_steps,*/total_steps/*)*/) ending_step -= 50;
if(!options.s) {
return {s:options.s,r:total_steps,x:number_of_steps,y:starting_step,z:ending_step,c:options.c,t:options.t,d:options.d};
};
let current_step = starting_step;
let time_duration = options.t;
let completed_all_steps = options.d;
async function complete_step() {
return new Promise(resolve => {
if(completed_all_steps) {
resolve(true);
return;
};
if(current_step > number_of_steps && ending_step < starting_step && ending_step < number_of_steps) c = 1;
if(current_step > total_steps) current_step -= total_steps;
if(current_step == ending_step) {
completed_all_steps = true;
};
console.log(current_step, ending_step, completed_all_steps);
setTimeout(() => {
resolve(complete_step);
current_step++;
}, time_duration*10);
});
};
async function listen(func) {
const result = await func();
if(result === true) {
current_step--;
resolve_entire_algorithm({
s:options.s,r:total_steps,x:number_of_steps,y:starting_step,z:ending_step,c:current_step,t:options.t,d:completed_all_steps
});
return;
} else if(typeof func == "function") {
console.log("Not completed (even if current_step==ending_step, you still have to wait until the current_step has expired. Takes (T) seconds per step, including the last step.)");
listen(func);
};
};
listen(complete_step);
});
};
const initial_settings = {s,r,x:30,y:30,z,c,t,d};
algorithm(initial_settings)
.then(output_settings => {
console.log("\n\n...\nAlgorithm finished.\n\n\nOriginal:\n",initial_settings, "\n\nResult:\n",output_settings);
});