diff --git a/btc-UI/src/app/components/selection.service/selection.service.ts b/btc-UI/src/app/components/selection.service/selection.service.ts index 44176aa..064b023 100644 --- a/btc-UI/src/app/components/selection.service/selection.service.ts +++ b/btc-UI/src/app/components/selection.service/selection.service.ts @@ -26,6 +26,7 @@ export class SelectionService { private runnerCount: number = 12; private multiLegBaseRaceIdx: number = 0; + private currentPool: string | null = null; // Track current pool (e.g., 'trb1', 'jkp1') constructor(private sharedStateService: SharedStateService) { this.sharedStateService.runnerCount$.subscribe(count => { @@ -34,6 +35,12 @@ export class SelectionService { this.sharedStateService.sharedData$.subscribe(data => { if (data.type === 'selectedRace') { this.multiLegBaseRaceIdx = data.value; + } else if (data.type === 'multiLegPoolStart') { + this.currentPool = data.value.label; + this.multiLegBaseRaceIdx = data.value.baseRaceIdx; + } else if (data.type === 'multiLegPoolEnd') { + this.currentPool = null; + this.multiLegBaseRaceIdx = 0; } }); } @@ -165,14 +172,14 @@ export class SelectionService { const legCount = this.getLegCount(label); const legs = this.splitToLegs(numbers, legCount); - // Override for MJP to always start from race 1 - const baseIdx = label === 'MJP' ? 0 : this.multiLegBaseRaceIdx - 1; + // Use pool-specific race indices from currentPool + const baseIdx = this.currentPool === 'mjp1' ? 0 : this.multiLegBaseRaceIdx; const legCounts = legs.map((leg, idx) => { let count = 0; for (const item of leg) { if (item === 'F') { - count += this.getRunnerCountForLeg(baseIdx + 1, idx); + count += this.getRunnerCountForLeg(baseIdx, idx); } else if (typeof item === 'number') { count += 1; } @@ -257,52 +264,77 @@ export class SelectionService { private getRunnerCountForLeg(baseIdx: number, leg: number): number { const raceCardData = JSON.parse(localStorage.getItem('raceCardData') || '{}'); - const raceIdx = Number(baseIdx) - 1 + leg; - const race = raceCardData?.raceVenueRaces?.races?.[raceIdx] || []; - return Array.isArray(race) ? race.length : 12; + // Use pool-specific race index mapping + const raceIdx = this.getRaceForLeg(this.currentPool || '', leg) - 1; + const race = raceCardData?.raceVenueRaces?.races?.[raceIdx]; + console.log('getRunnerCountForLeg:', { pool: this.currentPool, leg, raceIdx, race }); // Enhanced debugging + if (!race) { + console.warn(`No race data found for pool ${this.currentPool}, raceIdx ${raceIdx}, defaulting to 12`); + return 12; + } + if (Array.isArray(race)) { + console.log(`Race data is array with length ${race.length}`); + return race.length; + } + if (race.runners && Array.isArray(race.runners)) { + console.log(`Race data has runners array with length ${race.runners.length}`); + return race.runners.length; + } + console.warn(`Invalid race data structure for pool ${this.currentPool}, raceIdx ${raceIdx}, defaulting to 12`); + return 12; } - - - private calculateTotal(data: SelectionData): number { - const temp = new BehaviorSubject(data); - const originalCurrent = this.currentRow; - this.currentRow = temp; - this.updatePartial(data); - const total = temp.value.total; - this.currentRow = originalCurrent; // Restore original - return total; -} - - - //-----------------ADDED THIS---------------------------------------------------// - createVirtualRowsFromWSP(): SelectionData[] { - const base = this.currentRow.value; - - if (!base.numbers.length || base.value <= 0) return []; - - return ['WIN', 'SHP', 'THP'].map(label => { - const newRow: SelectionData = { - ...base, - label, - total: 0 + private getRaceForLeg(poolName: string, leg: number): number { + const raceCardData = JSON.parse(localStorage.getItem('raceCardData') || '{}'); + const poolRaces = raceCardData?.raceVenueRaces?.pools?.[poolName] || []; + if (poolRaces.length > leg) { + return poolRaces[leg]; + } + // Fallback to default race mapping + const raceMap: { [key: string]: number[] } = { + 'mjp1': [1, 2, 3, 4], + 'jkp1': [3, 4, 5, 6, 7], + 'trb1': [2, 3, 4], + 'trb2': [5, 6, 7] }; - newRow.total = this.calculateTotal(newRow); - return newRow; - }); -} + return raceMap[poolName]?.[leg] || (this.multiLegBaseRaceIdx + leg); + } -setSelections(rows: SelectionData[]) { - this.selections.next(rows); -} + private calculateTotal(data: SelectionData): number { + const temp = new BehaviorSubject(data); + const originalCurrent = this.currentRow; + this.currentRow = temp; + this.updatePartial(data); + const total = temp.value.total; + this.currentRow = originalCurrent; // Restore original + return total; + } -getSelections(): SelectionData[] { - return this.selections.getValue(); -} + createVirtualRowsFromWSP(): SelectionData[] { + const base = this.currentRow.value; -getCurrentRow(): SelectionData { - return this.currentRow.value; -} + if (!base.numbers.length || base.value <= 0) return []; + return ['WIN', 'SHP', 'THP'].map(label => { + const newRow: SelectionData = { + ...base, + label, + total: 0 + }; + newRow.total = this.calculateTotal(newRow); + return newRow; + }); + } -} + setSelections(rows: SelectionData[]) { + this.selections.next(rows); + } + + getSelections(): SelectionData[] { + return this.selections.getValue(); + } + + getCurrentRow(): SelectionData { + return this.currentRow.value; + } +} \ No newline at end of file