fix : field working according to the rc

This commit is contained in:
karthik 2025-09-17 16:45:51 +05:30
parent d1b52dc64c
commit 7ebce2dbcd

View File

@ -24,13 +24,13 @@ export class SelectionService {
}); });
currentRow$ = this.currentRow.asObservable(); currentRow$ = this.currentRow.asObservable();
private runnerCount: number = 12; private runnerCount: number = 0; // Initialize to 0, will be updated dynamically
private multiLegBaseRaceIdx: number = 0; private multiLegBaseRaceIdx: number = 0;
private currentPool: string | null = null; // Track current pool (e.g., 'trb1', 'jkp1') private currentPool: string | null = null; // Track current pool (e.g., 'TBP1', 'MJP1')
constructor(private sharedStateService: SharedStateService) { constructor(private sharedStateService: SharedStateService) {
this.sharedStateService.runnerCount$.subscribe(count => { this.sharedStateService.runnerCount$.subscribe(count => {
this.runnerCount = count || 12; this.runnerCount = count || 0; // Use 0 as default to avoid invalid calculations
}); });
this.sharedStateService.sharedData$.subscribe(data => { this.sharedStateService.sharedData$.subscribe(data => {
if (data.type === 'selectedRace') { if (data.type === 'selectedRace') {
@ -45,6 +45,86 @@ export class SelectionService {
}); });
} }
// Fetch structuredRaceCard from localStorage (prefer rpinfo, fallback to raceCardData)
private getStructuredRaceCard(): any {
const rpinfo = this.safeGetJSON('rpinfo');
if (rpinfo && rpinfo.structuredRaceCard) {
return rpinfo.structuredRaceCard;
}
const rc = this.safeGetJSON('raceCardData');
return (rc && rc.structuredRaceCard) ? rc.structuredRaceCard : {};
}
private safeGetJSON(key: string): any | null {
try {
const raw = localStorage.getItem(key);
if (!raw) return null;
return JSON.parse(raw);
} catch {
return null;
}
}
// Get runner count for a specific race
private getRunnerCountForRace(raceNo: number): number {
const structuredRaceCard = this.getStructuredRaceCard();
const races = structuredRaceCard?.raceVenueRaces?.races || [];
const race = races.find((r: any) => Number(r?.raceNo) === Number(raceNo));
if (!race || !Array.isArray(race.horses)) {
console.warn(`No valid race data for raceNo ${raceNo}, defaulting to 0`);
return 0;
}
console.log(`Runner count for raceNo ${raceNo}: ${race.horses.length}`);
return race.horses.length;
}
// Get runner count for a specific leg in a multi-leg pool
private getRunnerCountForLeg(baseIdx: number, leg: number): number {
const structuredRaceCard = this.getStructuredRaceCard();
const raceNo = this.getRaceForLeg(this.currentPool || '', leg);
const races = structuredRaceCard?.raceVenueRaces?.races || [];
const race = races.find((r: any) => Number(r?.raceNo) === Number(raceNo));
if (!race || !Array.isArray(race.horses)) {
console.warn(`No valid race data for pool ${this.currentPool}, leg ${leg}, raceNo ${raceNo}, defaulting to 0`);
return 0;
}
console.log(`Runner count for pool ${this.currentPool}, leg ${leg}, raceNo ${raceNo}: ${race.horses.length}`);
return race.horses.length;
}
// Get race number for a specific leg in a multi-leg pool
private getRaceForLeg(poolName: string, leg: number): number {
const structuredRaceCard = this.getStructuredRaceCard();
const poolKey = this.normalizePoolNameToKey(poolName);
const poolRaces = structuredRaceCard?.pools?.[poolKey] || [];
if (poolRaces.length > leg) {
return poolRaces[leg];
}
const comboRaces = structuredRaceCard?.comboRaces?.[poolKey] || [];
if (comboRaces.length > leg) {
return comboRaces[leg];
}
// Fallback to sequential race numbers if pool data is unavailable
return this.multiLegBaseRaceIdx + leg;
}
private normalizePoolNameToKey(name: string): string {
const lower = name.toLowerCase();
const possibleKeys = lower.startsWith('tbp') || lower.startsWith('trb')
? ['TBP1', 'TBP2', 'TRB1', 'TRB2']
: lower.startsWith('mjp')
? ['MJP1', 'MJP2']
: lower.startsWith('jpp') || lower.startsWith('jkp')
? ['JPP1', 'JPP2', 'JKP1', 'JKP2']
: [];
const structuredRaceCard = this.getStructuredRaceCard();
const pools = structuredRaceCard?.pools || {};
for (const key of possibleKeys) {
if (key in pools) return key;
}
return name.toUpperCase();
}
updatePartial(update: Partial<SelectionData>) { updatePartial(update: Partial<SelectionData>) {
const current = this.currentRow.value; const current = this.currentRow.value;
const updated: SelectionData = { ...current, ...update }; const updated: SelectionData = { ...current, ...update };
@ -57,6 +137,25 @@ export class SelectionService {
if (value > 100) updated.value = 100; if (value > 100) updated.value = 100;
updated.total = 0; updated.total = 0;
// Dynamically fetch runner count based on context
let runnerCount = this.runnerCount;
if (numbers.includes('F')) {
if (['TBP', 'MJP', 'JPP'].includes(label)) {
const legCount = this.getLegCount(label);
const legs = this.splitToLegs(numbers, legCount);
runnerCount = legs.reduce((acc, leg, idx) => {
if (leg.includes('F')) {
return acc + this.getRunnerCountForLeg(this.multiLegBaseRaceIdx, idx);
}
return acc + leg.filter(n => typeof n === 'number').length;
}, 0);
} else {
// For single-race bets, use the current race's runner count
const selectedRace = this.sharedStateService.getSelectedRace();
runnerCount = this.getRunnerCountForRace(selectedRace);
}
}
if ((numbers.length > 0 || numbers.includes('F')) && value > 0 && label) { if ((numbers.length > 0 || numbers.includes('F')) && value > 0 && label) {
switch (label) { switch (label) {
case 'WNP': case 'WNP':
@ -66,7 +165,7 @@ export class SelectionService {
case 'SHW': case 'SHW':
case 'WSP': { case 'WSP': {
updated.total = numbers.includes('F') updated.total = numbers.includes('F')
? this.runnerCount * value * 10 ? runnerCount * value * 10
: numbers.filter(n => typeof n === 'number').length * value * 10; : numbers.filter(n => typeof n === 'number').length * value * 10;
break; break;
} }
@ -87,8 +186,8 @@ export class SelectionService {
} }
} }
} else { } else {
const group1Numbers = group1.includes('F') ? Array.from({ length: this.runnerCount }, (_, i) => i + 1) : group1.filter(n => typeof n === 'number') as number[]; const group1Numbers = group1.includes('F') ? Array.from({ length: runnerCount }, (_, i) => i + 1) : group1.filter(n => typeof n === 'number') as number[];
const group2Numbers = group2.includes('F') ? Array.from({ length: this.runnerCount }, (_, i) => i + 1) : group2.filter(n => typeof n === 'number') as number[]; const group2Numbers = group2.includes('F') ? Array.from({ length: runnerCount }, (_, i) => i + 1) : group2.filter(n => typeof n === 'number') as number[];
if (isBoxed) { if (isBoxed) {
const allNumbers = [...new Set([...group1Numbers, ...group2Numbers])]; const allNumbers = [...new Set([...group1Numbers, ...group2Numbers])];
combinations = this.calculatePermutations(allNumbers.length); combinations = this.calculatePermutations(allNumbers.length);
@ -126,8 +225,8 @@ export class SelectionService {
} }
combinations = pairSet.size; combinations = pairSet.size;
} else { } else {
const group1Numbers = group1.includes('F') ? Array.from({ length: this.runnerCount }, (_, i) => i + 1) : group1.filter(n => typeof n === 'number') as number[]; const group1Numbers = group1.includes('F') ? Array.from({ length: runnerCount }, (_, i) => i + 1) : group1.filter(n => typeof n === 'number') as number[];
const group2Numbers = group2.includes('F') ? Array.from({ length: this.runnerCount }, (_, i) => i + 1) : group2.filter(n => typeof n === 'number') as number[]; const group2Numbers = group2.includes('F') ? Array.from({ length: runnerCount }, (_, i) => i + 1) : group2.filter(n => typeof n === 'number') as number[];
if (isBoxed) { if (isBoxed) {
const allNumbers = [...new Set([...group1Numbers, ...group2Numbers])]; const allNumbers = [...new Set([...group1Numbers, ...group2Numbers])];
combinations = this.calculateCombinations(allNumbers.length); combinations = this.calculateCombinations(allNumbers.length);
@ -174,9 +273,9 @@ export class SelectionService {
} }
} }
} else { } else {
const group1Numbers = group1.includes('F') ? Array.from({ length: this.runnerCount }, (_, i) => i + 1) : group1.filter(n => typeof n === 'number') as number[]; const group1Numbers = group1.includes('F') ? Array.from({ length: runnerCount }, (_, i) => i + 1) : group1.filter(n => typeof n === 'number') as number[];
const group2Numbers = group2.includes('F') ? Array.from({ length: this.runnerCount }, (_, i) => i + 1) : group2.filter(n => typeof n === 'number') as number[]; const group2Numbers = group2.includes('F') ? Array.from({ length: runnerCount }, (_, i) => i + 1) : group2.filter(n => typeof n === 'number') as number[];
const group3Numbers = group3.includes('F') ? Array.from({ length: this.runnerCount }, (_, i) => i + 1) : group3.filter(n => typeof n === 'number') as number[]; const group3Numbers = group3.includes('F') ? Array.from({ length: runnerCount }, (_, i) => i + 1) : group3.filter(n => typeof n === 'number') as number[];
if (isBoxed) { if (isBoxed) {
const allNumbers = [...new Set([...group1Numbers, ...group2Numbers, ...group3Numbers])]; const allNumbers = [...new Set([...group1Numbers, ...group2Numbers, ...group3Numbers])];
combinations = this.calculatePermutationsN(allNumbers.length, 3); combinations = this.calculatePermutationsN(allNumbers.length, 3);
@ -204,14 +303,11 @@ export class SelectionService {
const legCount = this.getLegCount(label); const legCount = this.getLegCount(label);
const legs = this.splitToLegs(numbers, legCount); const legs = this.splitToLegs(numbers, legCount);
// Use pool-specific race indices from currentPool
const baseIdx = this.currentPool === 'mjp1' ? 0 : this.multiLegBaseRaceIdx;
const legCounts = legs.map((leg, idx) => { const legCounts = legs.map((leg, idx) => {
let count = 0; let count = 0;
for (const item of leg) { for (const item of leg) {
if (item === 'F') { if (item === 'F') {
count += this.getRunnerCountForLeg(baseIdx, idx); count += this.getRunnerCountForLeg(this.multiLegBaseRaceIdx, idx);
} else if (typeof item === 'number') { } else if (typeof item === 'number') {
count += 1; count += 1;
} }
@ -294,44 +390,6 @@ export class SelectionService {
} }
} }
private getRunnerCountForLeg(baseIdx: number, leg: number): number {
const raceCardData = JSON.parse(localStorage.getItem('raceCardData') || '{}');
// 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 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]
};
return raceMap[poolName]?.[leg] || (this.multiLegBaseRaceIdx + leg);
}
private calculateTotal(data: SelectionData): number { private calculateTotal(data: SelectionData): number {
const temp = new BehaviorSubject<SelectionData>(data); const temp = new BehaviorSubject<SelectionData>(data);
const originalCurrent = this.currentRow; const originalCurrent = this.currentRow;