48 lines
1.6 KiB
TypeScript
48 lines
1.6 KiB
TypeScript
/**
|
||
* Returns the number of handicap strokes a player receives on a given hole.
|
||
* WHS allocation: distribute course_handicap strokes across holes by stroke index.
|
||
*/
|
||
export function strokesReceived(
|
||
courseHandicap: number,
|
||
strokeIndex: number,
|
||
holesCount: number,
|
||
): number {
|
||
if (courseHandicap <= 0) return 0
|
||
const fullStrokes = Math.floor(courseHandicap / holesCount)
|
||
const extraStrokes = courseHandicap % holesCount
|
||
return fullStrokes + (strokeIndex <= extraStrokes ? 1 : 0)
|
||
}
|
||
|
||
/**
|
||
* Net score on a hole (gross strokes minus strokes received).
|
||
*/
|
||
export function netStrokes(
|
||
gross: number,
|
||
courseHandicap: number,
|
||
strokeIndex: number,
|
||
holesCount: number,
|
||
): number {
|
||
return gross - strokesReceived(courseHandicap, strokeIndex, holesCount)
|
||
}
|
||
|
||
/**
|
||
* Score vs par as a display string: "−2", "E", "+1", "+3"
|
||
*/
|
||
export function formatVsPar(diff: number): string {
|
||
if (diff === 0) return 'E'
|
||
return diff > 0 ? `+${diff}` : `${diff}`
|
||
}
|
||
|
||
/**
|
||
* CSS class for a gross score vs par (standard golf scorecard conventions).
|
||
*/
|
||
export function scoreColorClass(vsPar: number | null): string {
|
||
if (vsPar === null) return ''
|
||
if (vsPar <= -2) return 'bg-yellow-300 text-yellow-900 rounded-full' // Eagle or better
|
||
if (vsPar === -1) return 'bg-red-500 text-white rounded-full' // Birdie
|
||
if (vsPar === 0) return '' // Par
|
||
if (vsPar === 1) return 'border border-border rounded-sm' // Bogey
|
||
if (vsPar === 2) return 'border-2 border-border rounded-sm' // Double
|
||
return 'border-2 border-destructive rounded-sm text-destructive' // Triple+
|
||
}
|