diff options
Diffstat (limited to 'src/state.ts')
-rw-r--r-- | src/state.ts | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/src/state.ts b/src/state.ts new file mode 100644 index 0000000..3b390c5 --- /dev/null +++ b/src/state.ts @@ -0,0 +1,63 @@ +import * as Config from 'config' + +export enum Step { + Prepare, + Work, + Rest, + End, +} + +export function prettyPrintStep(step: Step): string { + if (step === Step.Prepare) + return "Prepare" + else if (step === Step.Work) + return "Work" + else if (step === Step.Rest) + return "Rest" + else + return "End" +} + +export interface State { + step: Step, + remaining: number, + tabata: number, + cycle: number, +} + +export function getAt(config: Config.Config, elapsed: number): State { + const cycleDuration = config.work + config.rest + const tabataDuration = config.prepare + (config.cycles * cycleDuration) + if (elapsed >= tabataDuration * config.tabatas) { + return { + step: Step.End, + remaining: 0, + tabata: config.tabatas, + cycle: config.cycles, + } + } else { + const currentTabataElapsed = elapsed % tabataDuration + let step, remaining + if (currentTabataElapsed < config.prepare) { + step = Step.Prepare + remaining = config.prepare - currentTabataElapsed + } 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 + } + } + + const tabata = Math.floor(elapsed / tabataDuration) + 1 + const cycle = + currentTabataElapsed < config.prepare + ? 1 + : Math.floor((currentTabataElapsed - config.prepare) / cycleDuration) + 1 + + return { step, remaining, tabata, cycle } + } +} |