diff options
Diffstat (limited to 'src/state.ts')
-rw-r--r-- | src/state.ts | 80 |
1 files changed, 52 insertions, 28 deletions
diff --git a/src/state.ts b/src/state.ts index 3b390c5..a0348f0 100644 --- a/src/state.ts +++ b/src/state.ts @@ -1,6 +1,7 @@ import * as Config from 'config' export enum Step { + WarmUp, Prepare, Work, Rest, @@ -8,56 +9,79 @@ export enum Step { } export function prettyPrintStep(step: Step): string { + if (step === Step.WarmUp) + return 'Warm Up' if (step === Step.Prepare) - return "Prepare" + return 'Prepare' else if (step === Step.Work) - return "Work" + return 'Work' else if (step === Step.Rest) - return "Rest" + return 'Rest' else - return "End" + return 'End' } export interface State { step: Step, remaining: number, - tabata: number, - cycle: number, + info: string, + elapsed: number, } export function getAt(config: Config.Config, elapsed: number): State { + if (elapsed < config.warmup) { + return { + step: Step.WarmUp, + remaining: config.warmup - elapsed, + info: '', + elapsed + } + } + + const tabataElapsed = elapsed - config.warmup + const cycleDuration = config.work + config.rest const tabataDuration = config.prepare + (config.cycles * cycleDuration) - if (elapsed >= tabataDuration * config.tabatas) { + + if (tabataElapsed >= tabataDuration * config.tabatas) { return { step: Step.End, remaining: 0, - tabata: config.tabatas, - cycle: config.cycles, + info: '', + elapsed } + } + + const currentTabataElapsed = tabataElapsed % tabataDuration + let step, remaining + if (currentTabataElapsed < config.prepare) { + step = Step.Prepare + remaining = config.prepare - currentTabataElapsed } else { - const currentTabataElapsed = elapsed % tabataDuration - let step, remaining - if (currentTabataElapsed < config.prepare) { - step = Step.Prepare - remaining = config.prepare - currentTabataElapsed + const currentCycleElapsed = (currentTabataElapsed - config.prepare) % cycleDuration + if (currentCycleElapsed < config.work) { + step = Step.Work + remaining = config.work - currentCycleElapsed } else { - const currentCycleElapsed = (currentTabataElapsed - config.prepare) % cycleDuration - if (currentCycleElapsed < config.work) { - step = Step.Work - remaining = config.work - currentCycleElapsed - } else { - step = Step.Rest - remaining = config.work + config.rest - currentCycleElapsed - } + step = Step.Rest + remaining = config.work + config.rest - currentCycleElapsed } + } - const tabata = Math.floor(elapsed / tabataDuration) + 1 - const cycle = - currentTabataElapsed < config.prepare - ? 1 - : Math.floor((currentTabataElapsed - config.prepare) / cycleDuration) + 1 + const tabata = Math.floor(tabataElapsed / tabataDuration) + 1 + const cycle = + currentTabataElapsed < config.prepare + ? 1 + : Math.floor((currentTabataElapsed - config.prepare) / cycleDuration) + 1 + const info = stepCount(step, tabata, cycle) - return { step, remaining, tabata, cycle } + return { step, remaining, info, elapsed } +} + +function stepCount(step: Step, tabata: number, cycle: number): string { + if (step === Step.Work || step === Step.Rest) { + return `#${tabata.toString()}.${cycle.toString()}` + } else { + return `#${tabata.toString()}` } } |