-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathtimeControlUtils.ts
More file actions
63 lines (54 loc) · 1.59 KB
/
Copy pathtimeControlUtils.ts
File metadata and controls
63 lines (54 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/**
* Utility functions for time control conversion between custom values and preset formats
*/
import { TimeControl, TimeControlOptions } from 'src/types'
export interface CustomTimeControl {
minutes: number
increment: number
}
/**
* Convert custom time control values to the closest preset TimeControl format
*/
export const customToPresetTimeControl = (
minutes: number,
increment: number,
): TimeControl => {
const customFormat = `${minutes}+${increment}`
// Check if it matches any existing preset
if (TimeControlOptions.includes(customFormat as TimeControl)) {
return customFormat as TimeControl
}
// For custom values that don't match presets, return the custom format
// The game logic will need to handle this appropriately
return customFormat as TimeControl
}
/**
* Parse a TimeControl string into custom time control values
*/
export const parseTimeControl = (
timeControl: TimeControl,
): CustomTimeControl => {
if (timeControl === 'unlimited') {
return { minutes: 0, increment: 0 }
}
const [minutesStr, incrementStr] = timeControl.split('+')
return {
minutes: parseInt(minutesStr, 10) || 0,
increment: parseInt(incrementStr, 10) || 0,
}
}
/**
* Check if a time control is a preset option
*/
export const isPresetTimeControl = (timeControl: TimeControl): boolean => {
return TimeControlOptions.includes(timeControl)
}
/**
* Get preset time control options with labels
*/
export const getPresetOptions = () => {
return TimeControlOptions.map((option) => ({
value: option,
label: option === 'unlimited' ? 'Unlimited' : option,
}))
}