|
| 1 | +import { RundownTTimer } from '@sofie-automation/corelib/dist/dataModel/RundownPlaylist' |
| 2 | + |
| 3 | +/** |
| 4 | + * Calculate the display diff for a T-Timer. |
| 5 | + * For countdown/timeOfDay: positive = time remaining, negative = overrun. |
| 6 | + * For freeRun: positive = elapsed time. |
| 7 | + */ |
| 8 | +export function calculateTTimerDiff(timer: RundownTTimer, now: number): number { |
| 9 | + if (!timer.state) { |
| 10 | + return 0 |
| 11 | + } |
| 12 | + |
| 13 | + // Get current time: either frozen duration or calculated from zeroTime |
| 14 | + const currentTime = timer.state.paused ? timer.state.duration : timer.state.zeroTime - now |
| 15 | + |
| 16 | + // Free run counts up, so negate to get positive elapsed time |
| 17 | + if (timer.mode?.type === 'freeRun') { |
| 18 | + return -currentTime |
| 19 | + } |
| 20 | + |
| 21 | + // Apply stopAtZero if configured |
| 22 | + if (timer.mode?.stopAtZero && currentTime < 0) { |
| 23 | + return 0 |
| 24 | + } |
| 25 | + |
| 26 | + return currentTime |
| 27 | +} |
| 28 | + |
| 29 | +/** |
| 30 | + * Calculate the over/under difference between the timer's current value |
| 31 | + * and its estimate. |
| 32 | + * |
| 33 | + * Positive = over (behind schedule, will reach anchor after timer hits zero) |
| 34 | + * Negative = under (ahead of schedule, will reach anchor before timer hits zero) |
| 35 | + * |
| 36 | + * Returns undefined if no estimate is available. |
| 37 | + */ |
| 38 | +export function calculateTTimerOverUnder(timer: RundownTTimer, now: number): number | undefined { |
| 39 | + if (!timer.state || !timer.estimateState) { |
| 40 | + return undefined |
| 41 | + } |
| 42 | + |
| 43 | + const duration = timer.state.paused ? timer.state.duration : timer.state.zeroTime - now |
| 44 | + const estimateDuration = timer.estimateState.paused |
| 45 | + ? timer.estimateState.duration |
| 46 | + : timer.estimateState.zeroTime - now |
| 47 | + |
| 48 | + return duration - estimateDuration |
| 49 | +} |
| 50 | + |
| 51 | +// TODO: remove this mock |
| 52 | +let mockTimer: RundownTTimer | undefined |
| 53 | + |
| 54 | +export function getDefaultTTimer(tTimers: [RundownTTimer, RundownTTimer, RundownTTimer]): RundownTTimer | undefined { |
| 55 | + const active = tTimers.find((t) => t.mode) |
| 56 | + if (active) return active |
| 57 | + |
| 58 | + if (!mockTimer) { |
| 59 | + const now = Date.now() |
| 60 | + mockTimer = { |
| 61 | + index: 0, |
| 62 | + label: 'MOCK TIMER', |
| 63 | + mode: { |
| 64 | + type: 'countdown', |
| 65 | + }, |
| 66 | + state: { |
| 67 | + zeroTime: now + 30 * 60 * 1000, |
| 68 | + duration: 0, |
| 69 | + paused: false, |
| 70 | + }, |
| 71 | + estimateState: { |
| 72 | + zeroTime: now + 25 * 60 * 1000, // Estimate was 25 mins -> we are 5 mins over |
| 73 | + duration: 0, |
| 74 | + paused: false, |
| 75 | + }, |
| 76 | + } as any |
| 77 | + } |
| 78 | + |
| 79 | + return mockTimer |
| 80 | +} |
0 commit comments