overtime-live-trading-utils 2.1.36 → 2.1.37-rc.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. package/.circleci/config.yml +32 -32
  2. package/.prettierrc +9 -9
  3. package/codecov.yml +20 -20
  4. package/index.ts +26 -26
  5. package/jest.config.ts +16 -16
  6. package/main.js +1 -1
  7. package/package.json +30 -30
  8. package/src/constants/common.ts +8 -7
  9. package/src/constants/errors.ts +7 -6
  10. package/src/constants/sports.ts +78 -78
  11. package/src/enums/sports.ts +109 -109
  12. package/src/tests/mock/MockLeagueMap.ts +200 -170
  13. package/src/tests/mock/MockOpticOddsEvents.ts +662 -662
  14. package/src/tests/mock/MockOpticSoccer.ts +9864 -9378
  15. package/src/tests/mock/MockSoccerRedis.ts +2308 -2308
  16. package/src/tests/unit/bookmakers.test.ts +148 -79
  17. package/src/tests/unit/markets.test.ts +176 -156
  18. package/src/tests/unit/odds.test.ts +103 -92
  19. package/src/tests/unit/resolution.test.ts +1488 -1488
  20. package/src/tests/unit/sports.test.ts +58 -58
  21. package/src/tests/unit/spread.test.ts +144 -131
  22. package/src/tests/utils/helper.ts +10 -0
  23. package/src/types/bookmakers.ts +7 -0
  24. package/src/types/missing-types.d.ts +2 -2
  25. package/src/types/odds.ts +80 -61
  26. package/src/types/resolution.ts +96 -96
  27. package/src/types/sports.ts +22 -19
  28. package/src/utils/bookmakers.ts +315 -159
  29. package/src/utils/constraints.ts +210 -210
  30. package/src/utils/gameMatching.ts +81 -81
  31. package/src/utils/markets.ts +119 -119
  32. package/src/utils/odds.ts +947 -918
  33. package/src/utils/opticOdds.ts +71 -71
  34. package/src/utils/resolution.ts +319 -319
  35. package/src/utils/sportPeriodMapping.ts +36 -36
  36. package/src/utils/sports.ts +51 -51
  37. package/src/utils/spread.ts +97 -97
  38. package/tsconfig.json +16 -16
  39. package/webpack.config.js +24 -24
  40. package/CLAUDE.md +0 -84
  41. package/resolution_live_markets.md +0 -356
package/src/utils/odds.ts CHANGED
@@ -1,918 +1,947 @@
1
- import * as oddslib from 'oddslib';
2
- import { MarketType, MarketTypeMap } from 'overtime-utils';
3
- import { DRAW, MIN_ODDS_FOR_DIFF_CHECKING, MONEYLINE_TYPE_ID, ZERO } from '../constants/common';
4
- import { NO_MARKETS_FOR_LEAGUE_ID } from '../constants/errors';
5
- import { MoneylineTypes } from '../enums/sports';
6
- import { HomeAwayTeams, Odds, OddsObject } from '../types/odds';
7
- import { ChildMarket, LeagueConfigInfo } from '../types/sports';
8
- import { checkOddsFromBookmakers } from './bookmakers';
9
- import { getLeagueInfo } from './sports';
10
- import { adjustSpreadOnOdds, getSpreadData } from './spread';
11
-
12
- /**
13
- * Converts a given odds value from one format to another.
14
- * Specifically, it converts from 'moneyline' to 'impliedProbability', handling special cases.
15
- *
16
- * @param {Number} odds - The odds value to convert.
17
- * @returns {Number} The converted odds value.
18
- */
19
- export const convertOddsToImpl = (odds: number): number => {
20
- return odds === ZERO ? 0 : getOddsFromTo('decimal', 'impliedProbability', odds);
21
- };
22
-
23
- /**
24
- * Converts odds from one format to another.
25
- * @param {String} from - The original odds format.
26
- * @param {String} to - The target odds format.
27
- * @param {Number} input - The odds value.
28
- * @returns {Number} The converted odds.
29
- */
30
- export const getOddsFromTo = (from: string, to: string, input: number): number => {
31
- try {
32
- return oddslib.from(from, input).to(to);
33
- } catch (error) {
34
- return 0;
35
- }
36
- };
37
-
38
- /**
39
- * Filters the odds array to find entries matching the specified market name and bookmaker.
40
- *
41
- * @param {Array} oddsArray - The array of odds objects.
42
- * @param {string} marketName - The market name to filter by.
43
- * @param {Array} liveOddsProviders - Odds providers for live odds
44
- * @param {Object} commonData - The common data object.
45
- * @param {boolean} isTwoPositionalSport - Indicates if the sport is a two positional sport.,
46
- * @returns {Map} The filtered map for odds per provider.
47
- */
48
- export const filterOddsByMarketNameTeamNameBookmaker = (
49
- oddsArray: Odds,
50
- marketName: MoneylineTypes,
51
- liveOddsProviders: any[],
52
- commonData: HomeAwayTeams,
53
- isTwoPositionalSport: boolean
54
- ) => {
55
- const linesMap = new Map<any, any>();
56
- liveOddsProviders.forEach((oddsProvider) => {
57
- let homeOdds = 0;
58
- const homeTeamOddsObject = oddsArray.filter((odd) => {
59
- return (
60
- odd &&
61
- odd.marketName.toLowerCase() === marketName.toLowerCase() &&
62
- odd.sportsBookName.toLowerCase() == oddsProvider.toLowerCase() &&
63
- odd.selection.toLowerCase() === commonData.homeTeam.toLowerCase()
64
- );
65
- });
66
- if (homeTeamOddsObject.length !== 0) {
67
- homeOdds = homeTeamOddsObject[0].price;
68
- }
69
-
70
- let awayOdds = 0;
71
- const awayTeamOddsObject = oddsArray.filter(
72
- (odd) =>
73
- odd &&
74
- odd.marketName.toLowerCase() === marketName.toLowerCase() &&
75
- odd.sportsBookName.toLowerCase() == oddsProvider.toLowerCase() &&
76
- odd.selection.toLowerCase() === commonData.awayTeam.toLowerCase()
77
- );
78
-
79
- if (awayTeamOddsObject.length !== 0) {
80
- awayOdds = awayTeamOddsObject[0].price;
81
- }
82
-
83
- let drawOdds = 0;
84
- if (!isTwoPositionalSport) {
85
- const drawOddsObject = oddsArray.filter(
86
- (odd) =>
87
- odd &&
88
- odd.marketName.toLowerCase() === marketName.toLowerCase() &&
89
- odd.sportsBookName.toLowerCase() == oddsProvider.toLowerCase() &&
90
- odd.selection.toLowerCase() === DRAW.toLowerCase()
91
- );
92
-
93
- if (drawOddsObject.length !== 0) {
94
- drawOdds = drawOddsObject[0].price;
95
- }
96
- }
97
-
98
- linesMap.set(oddsProvider.toLowerCase(), {
99
- homeOdds: homeOdds,
100
- awayOdds: awayOdds,
101
- drawOdds: drawOdds,
102
- });
103
- });
104
- return linesMap;
105
- };
106
-
107
- /**
108
- * Retrieves the parent odds for the given event.
109
- *
110
- * @param {boolean} isTwoPositionalSport - Indicates if the sport is a two positional sport.
111
- * @param {Array} sportSpreadData - Spread data specific to the sport.
112
- * @param {Array} liveOddsProviders - Odds providers for live odds
113
- * @param {Object} oddsApiObject - Odds data from the API.
114
- * @param {String} sportId - Sport ID API.
115
- * @param {Number} defaultSpreadForLiveMarkets - Default spread for live markets,
116
- * @param {Number} maxPercentageDiffBetwenOdds - Maximum allowed percentage difference between same position odds from different providers
117
- * @returns {Array} The parent odds for the event [homeOdds, awayOdds, drawOdds].
118
- */
119
- export const getParentOdds = (
120
- isTwoPositionalSport: boolean,
121
- sportSpreadData: any[],
122
- liveOddsProviders: any[],
123
- oddsApiObject: OddsObject,
124
- sportId: string,
125
- defaultSpreadForLiveMarkets: number,
126
- maxPercentageDiffBetwenOdds: number
127
- ) => {
128
- const commonData = { homeTeam: oddsApiObject.homeTeam, awayTeam: oddsApiObject.awayTeam };
129
-
130
- // EXTRACTING ODDS FROM THE RESPONSE PER MARKET NAME AND BOOKMAKER
131
- const moneylineOddsMap = filterOddsByMarketNameTeamNameBookmaker(
132
- oddsApiObject.odds,
133
- MoneylineTypes.MONEYLINE,
134
- liveOddsProviders,
135
- commonData,
136
- isTwoPositionalSport
137
- );
138
-
139
- // CHECKING AND COMPARING ODDS FOR THE GIVEN BOOKMAKERS
140
- const oddsObject = checkOddsFromBookmakers(
141
- moneylineOddsMap,
142
- liveOddsProviders,
143
- isTwoPositionalSport,
144
- maxPercentageDiffBetwenOdds,
145
- MIN_ODDS_FOR_DIFF_CHECKING
146
- );
147
-
148
- if (oddsObject.errorMessage) {
149
- return {
150
- odds: isTwoPositionalSport ? [0, 0] : [0, 0, 0],
151
- errorMessage: oddsObject.errorMessage,
152
- };
153
- }
154
- const primaryBookmakerOdds = isTwoPositionalSport
155
- ? [oddsObject.homeOdds, oddsObject.awayOdds]
156
- : [oddsObject.homeOdds, oddsObject.awayOdds, oddsObject.drawOdds];
157
-
158
- let parentOdds = primaryBookmakerOdds.map((odd) => convertOddsToImpl(odd));
159
- const spreadData = getSpreadData(sportSpreadData, sportId, MONEYLINE_TYPE_ID, defaultSpreadForLiveMarkets);
160
-
161
- if (spreadData !== null) {
162
- parentOdds = adjustSpreadOnOdds(parentOdds, spreadData.minSpread, spreadData.targetSpread);
163
- } else {
164
- // Use min spread by sport if available, otherwise use default min spread
165
- parentOdds = adjustSpreadOnOdds(parentOdds, defaultSpreadForLiveMarkets, 0);
166
- }
167
- return { odds: parentOdds };
168
- };
169
-
170
- /**
171
- * Creates child markets based on the given parameters.
172
- *
173
- * @param {Object} leagueId - leagueId AKA sportId
174
- * @param {Array} spreadDataForSport - Spread data for sport.
175
- * @param {Object} apiResponseWithOdds - API response from the provider
176
- * @param {Array} liveOddsProviders - Odds providers for live odds
177
- * @param {Number} defaultSpreadForLiveMarkets - Default spread for live markets
178
- * @param {Boolean} leagueMap - League Map info
179
- * @returns {Array} The child markets.
180
- */
181
- export const createChildMarkets: (
182
- apiResponseWithOdds: OddsObject,
183
- spreadDataForSport: any,
184
- leagueId: number,
185
- liveOddsProviders: any,
186
- defaultSpreadForLiveMarkets: any,
187
- leagueMap: any
188
- ) => ChildMarket[] = (
189
- apiResponseWithOdds,
190
- spreadDataForSport,
191
- leagueId,
192
- liveOddsProviders,
193
- defaultSpreadForLiveMarkets,
194
- leagueMap
195
- ) => {
196
- const [spreadOdds, totalOdds, moneylineOdds, correctScoreOdds, doubleChanceOdds, ggOdds, childMarkets]: any[] = [
197
- [],
198
- [],
199
- [],
200
- [],
201
- [],
202
- [],
203
- [],
204
- ];
205
- const leagueInfo = getLeagueInfo(leagueId, leagueMap);
206
- const commonData = {
207
- homeTeam: apiResponseWithOdds.homeTeam,
208
- awayTeam: apiResponseWithOdds.awayTeam,
209
- };
210
-
211
- if (leagueInfo.length > 0) {
212
- // TODO ADD ODDS COMPARISON BETWEEN BOOKMAKERS
213
- const allChildOdds = filterOddsByMarketNameBookmaker(
214
- apiResponseWithOdds.odds,
215
- leagueInfo,
216
- liveOddsProviders[0]
217
- );
218
-
219
- allChildOdds.forEach((odd) => {
220
- if (odd.type === 'Total') {
221
- if (Math.abs(Number(odd.points) % 1) === 0.5) totalOdds.push(odd);
222
- } else if (odd.type === 'Spread') {
223
- if (Math.abs(Number(odd.points) % 1) === 0.5) spreadOdds.push(odd);
224
- } else if (odd.type === 'Moneyline') {
225
- moneylineOdds.push(odd);
226
- } else if (odd.type === 'Correct Score') {
227
- correctScoreOdds.push(odd);
228
- } else if (odd.type === 'Double Chance') {
229
- doubleChanceOdds.push(odd);
230
- } else if (odd.type === 'Both Teams To Score') {
231
- ggOdds.push(odd);
232
- }
233
- });
234
-
235
- const homeAwayFormattedOdds = [
236
- ...groupAndFormatSpreadOdds(spreadOdds, commonData),
237
- ...groupAndFormatTotalOdds(totalOdds, commonData),
238
- ...groupAndFormatMoneylineOdds(moneylineOdds, commonData),
239
- ...groupAndFormatGGOdds(ggOdds),
240
- ...groupAndFormatDoubleChanceOdds(doubleChanceOdds, commonData),
241
- ];
242
- const otherFormattedOdds = [...groupAndFormatCorrectScoreOdds(correctScoreOdds, commonData)];
243
-
244
- const homeAwayOddsWithSpreadAdjusted = adjustSpreadOnChildOdds(
245
- homeAwayFormattedOdds,
246
- spreadDataForSport,
247
- defaultSpreadForLiveMarkets
248
- );
249
-
250
- homeAwayOddsWithSpreadAdjusted.forEach((data) => {
251
- const childMarket = {
252
- leagueId: Number(data.sportId),
253
- typeId: Number(data.typeId),
254
- type: MarketTypeMap[data.typeId as MarketType]?.key || '',
255
- line: Number(data.line || 0),
256
- odds: data.odds,
257
- };
258
- const leagueInfoByTypeId = leagueInfo.find((league) => Number(league.typeId) === Number(data.typeId));
259
- const minOdds = leagueInfoByTypeId?.minOdds;
260
- const maxOdds = leagueInfoByTypeId?.maxOdds;
261
- if (
262
- !(
263
- minOdds &&
264
- maxOdds &&
265
- (data.odds[0] >= minOdds ||
266
- data.odds[0] <= maxOdds ||
267
- data.odds[1] >= minOdds ||
268
- data.odds[1] <= maxOdds)
269
- )
270
- ) {
271
- childMarkets.push(childMarket);
272
- }
273
- });
274
-
275
- otherFormattedOdds.forEach((data) => {
276
- const leagueInfoByTypeId = leagueInfo.find((league) => Number(league.typeId) === Number(data.typeId));
277
- const minOdds = leagueInfoByTypeId?.minOdds;
278
- const maxOdds = leagueInfoByTypeId?.maxOdds;
279
-
280
- const childMarket = {
281
- leagueId: Number(data.sportId),
282
- typeId: Number(data.typeId),
283
- type: MarketTypeMap[data.typeId as MarketType]?.key || '',
284
- line: Number(data.line || 0),
285
- odds: data.odds.map((odd: any) => {
286
- const impliedOdds = convertOddsToImpl(odd) || ZERO;
287
- return !minOdds || !maxOdds || impliedOdds >= minOdds || impliedOdds <= maxOdds ? 0 : impliedOdds;
288
- }),
289
- positionNames: data.positionNames,
290
- };
291
- childMarkets.push(childMarket);
292
- });
293
- } else {
294
- console.warn(`${NO_MARKETS_FOR_LEAGUE_ID}: ${Number(leagueId)}`);
295
- }
296
-
297
- return childMarkets;
298
- };
299
-
300
- /**
301
- * Filters the odds array to find entries matching the specified market name.
302
- *
303
- * @param {Array} oddsArray - The array of odds objects.
304
- * @param {string} leagueInfos - The market names to filter by.
305
- * @param {string} oddsProvider - The main odds provider to filter by.
306
- * @returns {Array} The filtered odds array.
307
- */
308
- export const filterOddsByMarketNameBookmaker = (
309
- oddsArray: Odds,
310
- leagueInfos: LeagueConfigInfo[],
311
- oddsProvider: string
312
- ): any[] => {
313
- const allChildMarketsTypes = leagueInfos
314
- .filter(
315
- (leagueInfo) =>
316
- leagueInfo.marketName.toLowerCase() !== MoneylineTypes.MONEYLINE.toLowerCase() &&
317
- leagueInfo.enabled === 'true'
318
- )
319
- .map((leagueInfo) => leagueInfo.marketName.toLowerCase());
320
- return oddsArray
321
- .filter(
322
- (odd) =>
323
- allChildMarketsTypes.includes(odd.marketName.toLowerCase()) &&
324
- odd.sportsBookName.toLowerCase() == oddsProvider.toLowerCase()
325
- )
326
- .map((odd) => {
327
- return {
328
- ...odd,
329
- ...leagueInfos.find(
330
- (leagueInfo) => leagueInfo.marketName.toLowerCase() === odd.marketName.toLowerCase()
331
- ), // using .find() for team totals means that we will always assign 10017 as typeID at this point
332
- };
333
- });
334
- };
335
-
336
- /**
337
- * Groups spread odds by their lines and formats the result.
338
- *
339
- * @param {Array} oddsArray - The input array of odds objects.
340
- * @param {Object} commonData - The common data object containing homeTeam information.
341
- * @returns {Array} The grouped and formatted spread odds.
342
- */
343
- export const groupAndFormatSpreadOdds = (oddsArray: any[], commonData: HomeAwayTeams) => {
344
- // Group odds by their selection points and selection
345
- const groupedOdds = oddsArray.reduce((acc: any, odd: any) => {
346
- const { points, marketName, price, selection, typeId, sportId, type } = odd;
347
- const isHomeTeam = selection === commonData.homeTeam;
348
-
349
- const key = `${marketName}_${isHomeTeam ? points : -points}`;
350
-
351
- if (!acc[key]) {
352
- acc[key] = { home: null, away: null, typeId: null, sportId: null };
353
- }
354
-
355
- if (isHomeTeam) {
356
- acc[key].home = price;
357
- } else {
358
- acc[key].away = price;
359
- }
360
-
361
- acc[key].typeId = typeId;
362
- acc[key].type = type;
363
- acc[key].sportId = sportId;
364
-
365
- return acc;
366
- }, {}) as any;
367
- // Format the grouped odds into the desired output
368
- const formattedOdds = Object.entries(groupedOdds as any).reduce((acc: any, [key, value]) => {
369
- const [_marketName, lineFloat] = key.split('_');
370
- const line = parseFloat(lineFloat);
371
- if ((value as any).home !== null && (value as any).away !== null) {
372
- acc.push({
373
- line: line as any,
374
- odds: [(value as any).home, (value as any).away],
375
- typeId: (value as any).typeId,
376
- sportId: (value as any).sportId,
377
- type: (value as any).type,
378
- });
379
- }
380
- return acc;
381
- }, []);
382
-
383
- return formattedOdds;
384
- };
385
-
386
- /**
387
- * Groups odds by selection and points over/under.
388
- *
389
- * @param {Array} oddsArray - The array of odds objects.
390
- * @returns {Object} The grouped odds.
391
- */
392
- export const groupAndFormatTotalOdds = (oddsArray: any[], commonData: HomeAwayTeams) => {
393
- // Group odds by their selection points and selection
394
- const groupedOdds = oddsArray.reduce((acc, odd) => {
395
- if (odd) {
396
- const key = `${odd.marketName}_${odd.selection}_${odd.points}`;
397
- if (!acc[key]) {
398
- acc[key] = { over: null, under: null };
399
- }
400
- if (odd.selectionLine === 'over') {
401
- acc[key].over = odd.price;
402
- } else if (odd.selectionLine === 'under') {
403
- acc[key].under = odd.price;
404
- }
405
-
406
- acc[key].typeId = odd.typeId;
407
- acc[key].type = odd.type;
408
- acc[key].sportId = odd.sportId;
409
- }
410
-
411
- return acc;
412
- }, {});
413
-
414
- // Format the grouped odds into the desired output
415
- const formattedOdds = Object.entries(groupedOdds as any).reduce((acc: any, [key, value]) => {
416
- const [_marketName, selection, selectionLine] = key.split('_');
417
- const line = parseFloat(selectionLine);
418
-
419
- // if we have away team in total odds we know the market is team total and we need to increase typeId by one.
420
- // if this is false typeId is already mapped correctly
421
- const isAwayTeam = selection === commonData.awayTeam;
422
- if ((value as any).over !== null && (value as any).under !== null) {
423
- acc.push({
424
- line: line as any,
425
- odds: [(value as any).over, (value as any).under],
426
- typeId: !isAwayTeam ? (value as any).typeId : Number((value as any).typeId) + 1,
427
- sportId: (value as any).sportId,
428
- type: (value as any).type,
429
- });
430
- }
431
- return acc;
432
- }, []);
433
-
434
- return formattedOdds;
435
- };
436
-
437
- /**
438
- * Groups moneyline odds by their lines and formats the result.
439
- *
440
- * @param {Array} oddsArray - The input array of odds objects.
441
- * @param {Object} commonData - The common data object containing homeTeam information.
442
- * @returns {Array} The grouped and formatted moneyline odds.
443
- */
444
- export const groupAndFormatMoneylineOdds = (oddsArray: any[], commonData: HomeAwayTeams) => {
445
- // Group odds by their selection points and selection
446
- const groupedOdds = oddsArray.reduce((acc: any, odd: any) => {
447
- const { price, selection, typeId, sportId, type } = odd;
448
- const key = typeId;
449
-
450
- if (!acc[key]) {
451
- acc[key] = { home: null, away: null, draw: null, typeId: null, sportId: null };
452
- }
453
-
454
- if (selection.toLowerCase() === commonData.homeTeam.toLowerCase()) acc[key].home = price;
455
- else if (selection.toLowerCase() === commonData.awayTeam.toLowerCase()) acc[key].away = price;
456
- else if (selection.toLowerCase() === DRAW.toLowerCase()) acc[key].draw = price;
457
-
458
- acc[key].typeId = typeId;
459
- acc[key].type = type;
460
- acc[key].sportId = sportId;
461
-
462
- return acc;
463
- }, {}) as any;
464
-
465
- // Format the grouped odds into the desired output
466
- const formattedOdds = Object.entries(groupedOdds as any).reduce((acc: any, [_key, value]) => {
467
- if ((value as any).home !== null && (value as any).away !== null) {
468
- acc.push({
469
- odds: (value as any).draw
470
- ? [(value as any).home, (value as any).away, (value as any).draw]
471
- : [(value as any).home, (value as any).away],
472
- typeId: (value as any).typeId,
473
- sportId: (value as any).sportId,
474
- type: (value as any).type,
475
- });
476
- }
477
- return acc;
478
- }, []);
479
-
480
- return formattedOdds;
481
- };
482
-
483
- /**w
484
- * Groups GG (Both Teams to Score) odds by their lines and formats the result.
485
- *
486
- * @param {Array} oddsArray - The input array of odds objects.
487
- * @returns {Array} The grouped and formatted moneyline odds.
488
- */
489
- export const groupAndFormatGGOdds = (oddsArray: any[]) => {
490
- const groupedOdds = oddsArray.reduce((acc: any, odd: any) => {
491
- const { price, selection, typeId, sportId, type } = odd;
492
- const key = typeId;
493
-
494
- if (!acc[key]) {
495
- acc[key] = { home: null, away: null, draw: null, typeId: null, sportId: null };
496
- }
497
-
498
- if (selection.toLowerCase() === 'yes') acc[key].home = price;
499
- else if (selection.toLowerCase() === 'no') acc[key].away = price;
500
-
501
- acc[key].typeId = typeId;
502
- acc[key].type = type;
503
- acc[key].sportId = sportId;
504
-
505
- return acc;
506
- }, {}) as any;
507
-
508
- // Format the grouped odds into the desired output
509
- const formattedOdds = Object.entries(groupedOdds as any).reduce((acc: any, [_key, value]) => {
510
- if ((value as any).home !== null && (value as any).away !== null) {
511
- acc.push({
512
- odds: (value as any).draw
513
- ? [(value as any).home, (value as any).away, (value as any).draw]
514
- : [(value as any).home, (value as any).away],
515
- typeId: (value as any).typeId,
516
- sportId: (value as any).sportId,
517
- type: (value as any).type,
518
- });
519
- }
520
- return acc;
521
- }, []);
522
-
523
- return formattedOdds;
524
- };
525
-
526
- /**
527
- * Normalize a score line like "A:B" to a map-friendly key "A_B" with
528
- * numeric normalization (removing any leading zeros).
529
- *
530
- * Behavior:
531
- * - Accepts optional whitespace around the colon (e.g., "03 : 10").
532
- * - Parses both sides as numbers to strip leading zeros:
533
- * "13:09" → "13_9", "13:00" → "13_0", "03:10" → "3_10".
534
- * - If the input does NOT match "<digits> : <digits>", falls back to a simple
535
- * string replace of ":" with "_" without numeric parsing.
536
- *
537
- * @param {string|number} line
538
- * A score string (e.g., "13:09") or any value coercible to string.
539
- *
540
- * @returns {string}
541
- * The normalized key (e.g., "13_9"). If not a digit:digit pattern, returns
542
- * the string with ":" replaced by "_".
543
- */
544
- function normalizeScoreLine(line: any) {
545
- const m = String(line).match(/(\d+)\s*:\s*(\d+)/);
546
- return m ? `${Number(m[1])}_${Number(m[2])}` : String(line).replace(':', '_');
547
- }
548
-
549
- /**
550
- * Groups correct score odds by their lines and formats the result.
551
- *
552
- * @param {Array} oddsArray - The input array of odds objects.
553
- * @param {Object} commonData - The common data object containing homeTeam and awayTeam information.
554
- * @returns {Array} The grouped and formatted correct score odds.
555
- */
556
- export const groupAndFormatCorrectScoreOdds = (oddsArray: any[], commonData: HomeAwayTeams): any[] => {
557
- const homeTeamKey = commonData.homeTeam.toLowerCase().replace(/\s+/g, '_');
558
- const awayTeamKey = commonData.awayTeam.toLowerCase().replace(/\s+/g, '_');
559
-
560
- // Group odds by typeId first
561
- const oddsByTypeId = oddsArray.reduce((acc: any, odd: any) => {
562
- const typeId = odd.typeId || 10100;
563
- if (!acc[typeId]) {
564
- acc[typeId] = [];
565
- }
566
- acc[typeId].push(odd);
567
- return acc;
568
- }, {});
569
-
570
- // Create market objects for each unique typeId
571
- const marketObjects: any[] = [];
572
-
573
- Object.entries(oddsByTypeId).forEach(([typeId, odds]: [string, any]) => {
574
- const oddsMap = {
575
- draw_0_0: 0,
576
- draw_1_1: 0,
577
- draw_2_2: 0,
578
- draw_3_3: 0,
579
- draw_4_4: 0,
580
- [`${homeTeamKey}_1_0`]: 0,
581
- [`${homeTeamKey}_2_0`]: 0,
582
- [`${homeTeamKey}_2_1`]: 0,
583
- [`${homeTeamKey}_3_0`]: 0,
584
- [`${homeTeamKey}_3_1`]: 0,
585
- [`${homeTeamKey}_3_2`]: 0,
586
- [`${homeTeamKey}_4_0`]: 0,
587
- [`${homeTeamKey}_4_1`]: 0,
588
- [`${homeTeamKey}_4_2`]: 0,
589
- [`${homeTeamKey}_4_3`]: 0,
590
- [`${awayTeamKey}_1_0`]: 0,
591
- [`${awayTeamKey}_2_0`]: 0,
592
- [`${awayTeamKey}_2_1`]: 0,
593
- [`${awayTeamKey}_3_0`]: 0,
594
- [`${awayTeamKey}_3_1`]: 0,
595
- [`${awayTeamKey}_3_2`]: 0,
596
- [`${awayTeamKey}_4_0`]: 0,
597
- [`${awayTeamKey}_4_1`]: 0,
598
- [`${awayTeamKey}_4_2`]: 0,
599
- [`${awayTeamKey}_4_3`]: 0,
600
- draw_5_5: 0,
601
- draw_6_6: 0,
602
- draw_7_7: 0,
603
- draw_8_8: 0,
604
- draw_9_9: 0,
605
- draw_10_10: 0,
606
- draw_11_11: 0,
607
- draw_12_12: 0,
608
- draw_13_13: 0,
609
- draw_14_14: 0,
610
- [`${homeTeamKey}_5_0`]: 0,
611
- [`${homeTeamKey}_5_1`]: 0,
612
- [`${homeTeamKey}_5_2`]: 0,
613
- [`${homeTeamKey}_5_3`]: 0,
614
- [`${homeTeamKey}_5_4`]: 0,
615
- [`${homeTeamKey}_6_0`]: 0,
616
- [`${homeTeamKey}_6_1`]: 0,
617
- [`${homeTeamKey}_6_2`]: 0,
618
- [`${homeTeamKey}_6_3`]: 0,
619
- [`${homeTeamKey}_6_4`]: 0,
620
- [`${homeTeamKey}_6_5`]: 0,
621
- [`${homeTeamKey}_7_0`]: 0,
622
- [`${homeTeamKey}_7_1`]: 0,
623
- [`${homeTeamKey}_7_2`]: 0,
624
- [`${homeTeamKey}_7_3`]: 0,
625
- [`${homeTeamKey}_7_4`]: 0,
626
- [`${homeTeamKey}_7_5`]: 0,
627
- [`${homeTeamKey}_7_6`]: 0,
628
- [`${homeTeamKey}_8_0`]: 0,
629
- [`${homeTeamKey}_8_1`]: 0,
630
- [`${homeTeamKey}_8_2`]: 0,
631
- [`${homeTeamKey}_8_3`]: 0,
632
- [`${homeTeamKey}_8_4`]: 0,
633
- [`${homeTeamKey}_8_5`]: 0,
634
- [`${homeTeamKey}_8_6`]: 0,
635
- [`${homeTeamKey}_8_7`]: 0,
636
- [`${homeTeamKey}_9_0`]: 0,
637
- [`${homeTeamKey}_9_1`]: 0,
638
- [`${homeTeamKey}_9_2`]: 0,
639
- [`${homeTeamKey}_9_3`]: 0,
640
- [`${homeTeamKey}_9_4`]: 0,
641
- [`${homeTeamKey}_9_5`]: 0,
642
- [`${homeTeamKey}_9_6`]: 0,
643
- [`${homeTeamKey}_9_7`]: 0,
644
- [`${homeTeamKey}_9_8`]: 0,
645
- [`${homeTeamKey}_10_0`]: 0,
646
- [`${homeTeamKey}_10_1`]: 0,
647
- [`${homeTeamKey}_10_2`]: 0,
648
- [`${homeTeamKey}_10_3`]: 0,
649
- [`${homeTeamKey}_10_4`]: 0,
650
- [`${homeTeamKey}_10_5`]: 0,
651
- [`${homeTeamKey}_10_6`]: 0,
652
- [`${homeTeamKey}_10_7`]: 0,
653
- [`${homeTeamKey}_10_8`]: 0,
654
- [`${homeTeamKey}_10_9`]: 0,
655
- [`${homeTeamKey}_11_0`]: 0,
656
- [`${homeTeamKey}_11_1`]: 0,
657
- [`${homeTeamKey}_11_2`]: 0,
658
- [`${homeTeamKey}_11_3`]: 0,
659
- [`${homeTeamKey}_11_4`]: 0,
660
- [`${homeTeamKey}_11_5`]: 0,
661
- [`${homeTeamKey}_11_6`]: 0,
662
- [`${homeTeamKey}_11_7`]: 0,
663
- [`${homeTeamKey}_11_8`]: 0,
664
- [`${homeTeamKey}_11_9`]: 0,
665
- [`${homeTeamKey}_11_10`]: 0,
666
- [`${homeTeamKey}_12_0`]: 0,
667
- [`${homeTeamKey}_12_1`]: 0,
668
- [`${homeTeamKey}_12_2`]: 0,
669
- [`${homeTeamKey}_12_3`]: 0,
670
- [`${homeTeamKey}_12_4`]: 0,
671
- [`${homeTeamKey}_12_5`]: 0,
672
- [`${homeTeamKey}_12_6`]: 0,
673
- [`${homeTeamKey}_12_7`]: 0,
674
- [`${homeTeamKey}_12_8`]: 0,
675
- [`${homeTeamKey}_12_9`]: 0,
676
- [`${homeTeamKey}_12_10`]: 0,
677
- [`${homeTeamKey}_12_11`]: 0,
678
- [`${homeTeamKey}_13_0`]: 0,
679
- [`${homeTeamKey}_13_1`]: 0,
680
- [`${homeTeamKey}_13_2`]: 0,
681
- [`${homeTeamKey}_13_3`]: 0,
682
- [`${homeTeamKey}_13_4`]: 0,
683
- [`${homeTeamKey}_13_5`]: 0,
684
- [`${homeTeamKey}_13_6`]: 0,
685
- [`${homeTeamKey}_13_7`]: 0,
686
- [`${homeTeamKey}_13_8`]: 0,
687
- [`${homeTeamKey}_13_9`]: 0,
688
- [`${homeTeamKey}_13_10`]: 0,
689
- [`${homeTeamKey}_13_11`]: 0,
690
- [`${homeTeamKey}_13_12`]: 0,
691
- [`${homeTeamKey}_14_0`]: 0,
692
- [`${homeTeamKey}_14_1`]: 0,
693
- [`${homeTeamKey}_14_2`]: 0,
694
- [`${homeTeamKey}_14_3`]: 0,
695
- [`${homeTeamKey}_14_4`]: 0,
696
- [`${homeTeamKey}_14_5`]: 0,
697
- [`${homeTeamKey}_14_6`]: 0,
698
- [`${homeTeamKey}_14_7`]: 0,
699
- [`${homeTeamKey}_14_8`]: 0,
700
- [`${homeTeamKey}_14_9`]: 0,
701
- [`${homeTeamKey}_14_10`]: 0,
702
- [`${homeTeamKey}_14_11`]: 0,
703
- [`${homeTeamKey}_14_12`]: 0,
704
- [`${homeTeamKey}_14_13`]: 0,
705
- [`${awayTeamKey}_5_0`]: 0,
706
- [`${awayTeamKey}_5_1`]: 0,
707
- [`${awayTeamKey}_5_2`]: 0,
708
- [`${awayTeamKey}_5_3`]: 0,
709
- [`${awayTeamKey}_5_4`]: 0,
710
- [`${awayTeamKey}_6_0`]: 0,
711
- [`${awayTeamKey}_6_1`]: 0,
712
- [`${awayTeamKey}_6_2`]: 0,
713
- [`${awayTeamKey}_6_3`]: 0,
714
- [`${awayTeamKey}_6_4`]: 0,
715
- [`${awayTeamKey}_6_5`]: 0,
716
- [`${awayTeamKey}_7_0`]: 0,
717
- [`${awayTeamKey}_7_1`]: 0,
718
- [`${awayTeamKey}_7_2`]: 0,
719
- [`${awayTeamKey}_7_3`]: 0,
720
- [`${awayTeamKey}_7_4`]: 0,
721
- [`${awayTeamKey}_7_5`]: 0,
722
- [`${awayTeamKey}_7_6`]: 0,
723
- [`${awayTeamKey}_8_0`]: 0,
724
- [`${awayTeamKey}_8_1`]: 0,
725
- [`${awayTeamKey}_8_2`]: 0,
726
- [`${awayTeamKey}_8_3`]: 0,
727
- [`${awayTeamKey}_8_4`]: 0,
728
- [`${awayTeamKey}_8_5`]: 0,
729
- [`${awayTeamKey}_8_6`]: 0,
730
- [`${awayTeamKey}_8_7`]: 0,
731
- [`${awayTeamKey}_9_0`]: 0,
732
- [`${awayTeamKey}_9_1`]: 0,
733
- [`${awayTeamKey}_9_2`]: 0,
734
- [`${awayTeamKey}_9_3`]: 0,
735
- [`${awayTeamKey}_9_4`]: 0,
736
- [`${awayTeamKey}_9_5`]: 0,
737
- [`${awayTeamKey}_9_6`]: 0,
738
- [`${awayTeamKey}_9_7`]: 0,
739
- [`${awayTeamKey}_9_8`]: 0,
740
- [`${awayTeamKey}_10_0`]: 0,
741
- [`${awayTeamKey}_10_1`]: 0,
742
- [`${awayTeamKey}_10_2`]: 0,
743
- [`${awayTeamKey}_10_3`]: 0,
744
- [`${awayTeamKey}_10_4`]: 0,
745
- [`${awayTeamKey}_10_5`]: 0,
746
- [`${awayTeamKey}_10_6`]: 0,
747
- [`${awayTeamKey}_10_7`]: 0,
748
- [`${awayTeamKey}_10_8`]: 0,
749
- [`${awayTeamKey}_10_9`]: 0,
750
- [`${awayTeamKey}_11_0`]: 0,
751
- [`${awayTeamKey}_11_1`]: 0,
752
- [`${awayTeamKey}_11_2`]: 0,
753
- [`${awayTeamKey}_11_3`]: 0,
754
- [`${awayTeamKey}_11_4`]: 0,
755
- [`${awayTeamKey}_11_5`]: 0,
756
- [`${awayTeamKey}_11_6`]: 0,
757
- [`${awayTeamKey}_11_7`]: 0,
758
- [`${awayTeamKey}_11_8`]: 0,
759
- [`${awayTeamKey}_11_9`]: 0,
760
- [`${awayTeamKey}_11_10`]: 0,
761
- [`${awayTeamKey}_12_0`]: 0,
762
- [`${awayTeamKey}_12_1`]: 0,
763
- [`${awayTeamKey}_12_2`]: 0,
764
- [`${awayTeamKey}_12_3`]: 0,
765
- [`${awayTeamKey}_12_4`]: 0,
766
- [`${awayTeamKey}_12_5`]: 0,
767
- [`${awayTeamKey}_12_6`]: 0,
768
- [`${awayTeamKey}_12_7`]: 0,
769
- [`${awayTeamKey}_12_8`]: 0,
770
- [`${awayTeamKey}_12_9`]: 0,
771
- [`${awayTeamKey}_12_10`]: 0,
772
- [`${awayTeamKey}_12_11`]: 0,
773
- [`${awayTeamKey}_13_0`]: 0,
774
- [`${awayTeamKey}_13_1`]: 0,
775
- [`${awayTeamKey}_13_2`]: 0,
776
- [`${awayTeamKey}_13_3`]: 0,
777
- [`${awayTeamKey}_13_4`]: 0,
778
- [`${awayTeamKey}_13_5`]: 0,
779
- [`${awayTeamKey}_13_6`]: 0,
780
- [`${awayTeamKey}_13_7`]: 0,
781
- [`${awayTeamKey}_13_8`]: 0,
782
- [`${awayTeamKey}_13_9`]: 0,
783
- [`${awayTeamKey}_13_10`]: 0,
784
- [`${awayTeamKey}_13_11`]: 0,
785
- [`${awayTeamKey}_13_12`]: 0,
786
- [`${awayTeamKey}_14_0`]: 0,
787
- [`${awayTeamKey}_14_1`]: 0,
788
- [`${awayTeamKey}_14_2`]: 0,
789
- [`${awayTeamKey}_14_3`]: 0,
790
- [`${awayTeamKey}_14_4`]: 0,
791
- [`${awayTeamKey}_14_5`]: 0,
792
- [`${awayTeamKey}_14_6`]: 0,
793
- [`${awayTeamKey}_14_7`]: 0,
794
- [`${awayTeamKey}_14_8`]: 0,
795
- [`${awayTeamKey}_14_9`]: 0,
796
- [`${awayTeamKey}_14_10`]: 0,
797
- [`${awayTeamKey}_14_11`]: 0,
798
- [`${awayTeamKey}_14_12`]: 0,
799
- [`${awayTeamKey}_14_13`]: 0,
800
- other: 0,
801
- };
802
-
803
- // Populate the oddsMap with the odds from this typeId's odds
804
- odds.forEach((odd: any) => {
805
- const normalizedSelection = `${odd.selection.toLowerCase().replace(/\s+/g, '_')}_${normalizeScoreLine(
806
- odd.selectionLine
807
- )}`;
808
- if (oddsMap.hasOwnProperty(normalizedSelection)) {
809
- oddsMap[normalizedSelection] = odd.price;
810
- }
811
- });
812
-
813
- const allOddsAreZero = Object.values(oddsMap).every((odd) => odd === ZERO);
814
-
815
- if (!allOddsAreZero) {
816
- const positionNames = Object.keys(oddsMap);
817
-
818
- // Create a market object for this typeId
819
- const marketObject = {
820
- homeTeam: commonData.homeTeam,
821
- awayTeam: commonData.awayTeam,
822
- line: 0,
823
- positionNames: positionNames,
824
- odds: Object.values(oddsMap),
825
- type: odds?.[0]?.type ? odds[0].type : 'Correct Score',
826
- typeId: Number(typeId),
827
- sportId: odds?.[0]?.sportId ? odds[0].sportId : undefined,
828
- };
829
- marketObjects.push(marketObject);
830
- }
831
- });
832
-
833
- return marketObjects;
834
- };
835
-
836
- /**
837
- * Groups double chance odds by their lines and formats the result.
838
- *
839
- * @param {Array} oddsArray - The input array of odds objects.
840
- * @param {Object} commonData - The common data object containing homeTeam and awayTeam information.
841
- * @returns {Array} The grouped and formatted correct score odds.
842
- */
843
- export const groupAndFormatDoubleChanceOdds = (oddsArray: any[], commonData: HomeAwayTeams) => {
844
- let probability1X = 0;
845
- let probability12 = 0;
846
- let probabilityX2 = 0;
847
-
848
- let sportId;
849
-
850
- oddsArray.forEach((odd) => {
851
- if (odd.sportId) {
852
- sportId = odd.sportId;
853
- }
854
- if (odd.selection.includes(commonData.homeTeam)) {
855
- if (odd.selection.includes(commonData.awayTeam)) {
856
- probability12 = odd.price;
857
- } else {
858
- probability1X = odd.price;
859
- }
860
- } else if (odd.selection.includes(commonData.awayTeam)) {
861
- probabilityX2 = odd.price;
862
- }
863
- });
864
-
865
- if ([probability1X, probability12, probabilityX2].every((odd) => odd === ZERO)) {
866
- return [];
867
- }
868
- // Create the market object
869
- const marketObject = {
870
- homeTeam: commonData.homeTeam,
871
- awayTeam: commonData.awayTeam,
872
- line: 0,
873
- odds: [probability1X, probability12, probabilityX2],
874
- type: 'Double Chance',
875
- typeId: 10003,
876
- sportId: sportId,
877
- };
878
- return [marketObject];
879
- };
880
-
881
- // used for home/away markets
882
- export const adjustSpreadOnChildOdds = (
883
- iterableGroupedOdds: any[],
884
- spreadDataForSport: any,
885
- defaultSpreadForLiveMarkets: any
886
- ) => {
887
- const result: any[] = [];
888
- iterableGroupedOdds.forEach((data) => {
889
- const hasDrawOdds = data.odds.length === 3;
890
- const homeTeamOdds = convertOddsToImpl(data.odds[0]) || ZERO;
891
- const awayTeamOdds = convertOddsToImpl(data.odds[1]) || ZERO;
892
- const drawOdds = convertOddsToImpl(data.odds[2]) || ZERO;
893
- const odds = hasDrawOdds ? [homeTeamOdds, awayTeamOdds, drawOdds] : [homeTeamOdds, awayTeamOdds];
894
-
895
- const isZeroOddsChild = homeTeamOdds === ZERO || awayTeamOdds === ZERO || (hasDrawOdds && drawOdds === ZERO);
896
- if (!isZeroOddsChild) {
897
- const spreadData = getSpreadData(
898
- spreadDataForSport,
899
- data.sportId,
900
- data.typeId,
901
- defaultSpreadForLiveMarkets
902
- );
903
-
904
- let adjustedOdds;
905
- if (spreadData !== null) {
906
- adjustedOdds = adjustSpreadOnOdds(odds, spreadData.minSpread, spreadData.targetSpread);
907
- } else {
908
- adjustedOdds = adjustSpreadOnOdds(odds, defaultSpreadForLiveMarkets, 0);
909
- }
910
-
911
- result.push({
912
- ...data,
913
- odds: adjustedOdds,
914
- });
915
- }
916
- });
917
- return result;
918
- };
1
+ import * as oddslib from 'oddslib';
2
+ import { MarketType, MarketTypeMap } from 'overtime-utils';
3
+ import { DRAW, MIN_ODDS_FOR_DIFF_CHECKING, MONEYLINE_TYPE_ID, ZERO } from '../constants/common';
4
+ import { LAST_POLLED_TOO_OLD, NO_MARKETS_FOR_LEAGUE_ID } from '../constants/errors';
5
+ import { MoneylineTypes } from '../enums/sports';
6
+ import { HomeAwayTeams, Odds, OddsObject } from '../types/odds';
7
+ import { ChildMarket, LastPolledArray, LeagueConfigInfo } from '../types/sports';
8
+ import {
9
+ checkOddsFromBookmakers,
10
+ checkOddsFromBookmakersForChildMarkets,
11
+ getPrimaryAndSecondaryBookmakerForTypeId,
12
+ isLastPolledForBookmakersValid,
13
+ } from './bookmakers';
14
+ import { getLeagueInfo } from './sports';
15
+ import { adjustSpreadOnOdds, getSpreadData } from './spread';
16
+
17
+ /**
18
+ * Converts a given odds value from one format to another.
19
+ * Specifically, it converts from 'moneyline' to 'impliedProbability', handling special cases.
20
+ *
21
+ * @param {Number} odds - The odds value to convert.
22
+ * @returns {Number} The converted odds value.
23
+ */
24
+ export const convertOddsToImpl = (odds: number): number => {
25
+ return odds === ZERO ? 0 : getOddsFromTo('decimal', 'impliedProbability', odds);
26
+ };
27
+
28
+ /**
29
+ * Converts odds from one format to another.
30
+ * @param {String} from - The original odds format.
31
+ * @param {String} to - The target odds format.
32
+ * @param {Number} input - The odds value.
33
+ * @returns {Number} The converted odds.
34
+ */
35
+ export const getOddsFromTo = (from: string, to: string, input: number): number => {
36
+ try {
37
+ return oddslib.from(from, input).to(to);
38
+ } catch (error) {
39
+ return 0;
40
+ }
41
+ };
42
+
43
+ /**
44
+ * Filters the odds array to find entries matching the specified market name and bookmaker.
45
+ *
46
+ * @param {Array} oddsArray - The array of odds objects.
47
+ * @param {string} marketName - The market name to filter by.
48
+ * @param {Array} liveOddsProviders - Odds providers for live odds
49
+ * @param {Object} commonData - The common data object.
50
+ * @param {boolean} isTwoPositionalSport - Indicates if the sport is a two positional sport.,
51
+ * @returns {Map} The filtered map for odds per provider.
52
+ */
53
+ export const filterOddsByMarketNameTeamNameBookmaker = (
54
+ oddsArray: Odds,
55
+ marketName: MoneylineTypes,
56
+ liveOddsProviders: any[],
57
+ commonData: HomeAwayTeams,
58
+ isTwoPositionalSport: boolean
59
+ ) => {
60
+ const linesMap = new Map<any, any>();
61
+ liveOddsProviders.forEach((oddsProvider) => {
62
+ let homeOdds = 0;
63
+ const homeTeamOddsObject = oddsArray.filter((odd) => {
64
+ return (
65
+ odd &&
66
+ odd.marketName.toLowerCase() === marketName.toLowerCase() &&
67
+ odd.sportsBookName.toLowerCase() == oddsProvider.toLowerCase() &&
68
+ odd.selection.toLowerCase() === commonData.homeTeam.toLowerCase()
69
+ );
70
+ });
71
+ if (homeTeamOddsObject.length !== 0) {
72
+ homeOdds = homeTeamOddsObject[0].price;
73
+ }
74
+
75
+ let awayOdds = 0;
76
+ const awayTeamOddsObject = oddsArray.filter(
77
+ (odd) =>
78
+ odd &&
79
+ odd.marketName.toLowerCase() === marketName.toLowerCase() &&
80
+ odd.sportsBookName.toLowerCase() == oddsProvider.toLowerCase() &&
81
+ odd.selection.toLowerCase() === commonData.awayTeam.toLowerCase()
82
+ );
83
+
84
+ if (awayTeamOddsObject.length !== 0) {
85
+ awayOdds = awayTeamOddsObject[0].price;
86
+ }
87
+
88
+ let drawOdds = 0;
89
+ if (!isTwoPositionalSport) {
90
+ const drawOddsObject = oddsArray.filter(
91
+ (odd) =>
92
+ odd &&
93
+ odd.marketName.toLowerCase() === marketName.toLowerCase() &&
94
+ odd.sportsBookName.toLowerCase() == oddsProvider.toLowerCase() &&
95
+ odd.selection.toLowerCase() === DRAW.toLowerCase()
96
+ );
97
+
98
+ if (drawOddsObject.length !== 0) {
99
+ drawOdds = drawOddsObject[0].price;
100
+ }
101
+ }
102
+
103
+ linesMap.set(oddsProvider.toLowerCase(), {
104
+ homeOdds: homeOdds,
105
+ awayOdds: awayOdds,
106
+ drawOdds: drawOdds,
107
+ });
108
+ });
109
+ return linesMap;
110
+ };
111
+
112
+ /**
113
+ * Retrieves the parent odds for the given event.
114
+ *
115
+ * @param {boolean} isTwoPositionalSport - Indicates if the sport is a two positional sport.
116
+ * @param {Array} sportSpreadData - Spread data specific to the sport.
117
+ * @param {Array} liveOddsProviders - Odds providers for live odds
118
+ * @param {Object} oddsApiObject - Odds data from the API.
119
+ * @param {String} sportId - Sport ID API.
120
+ * @param {Number} defaultSpreadForLiveMarkets - Default spread for live markets,
121
+ * @param {Number} maxPercentageDiffBetwenOdds - Maximum allowed percentage difference between same position odds from different providers
122
+ * @returns {Array} The parent odds for the event [homeOdds, awayOdds, drawOdds].
123
+ */
124
+ export const getParentOdds = (
125
+ isTwoPositionalSport: boolean,
126
+ sportSpreadData: any[],
127
+ liveOddsProviders: any[],
128
+ oddsApiObject: OddsObject,
129
+ sportId: string,
130
+ defaultSpreadForLiveMarkets: number,
131
+ maxPercentageDiffBetwenOdds: number,
132
+ leagueInfo: LeagueConfigInfo[],
133
+ lastPolledMap: LastPolledArray,
134
+ MAX_ALLOWED_PROVIDER_DATA_STALE_DELAY: number
135
+ ) => {
136
+ const commonData = { homeTeam: oddsApiObject.homeTeam, awayTeam: oddsApiObject.awayTeam };
137
+
138
+ const { primaryBookmaker, secondaryBookmaker } = getPrimaryAndSecondaryBookmakerForTypeId(
139
+ liveOddsProviders,
140
+ leagueInfo,
141
+ 0 // typeId for moneyline
142
+ );
143
+
144
+ const isValidLastPolled = isLastPolledForBookmakersValid(
145
+ lastPolledMap,
146
+ MAX_ALLOWED_PROVIDER_DATA_STALE_DELAY,
147
+ primaryBookmaker,
148
+ secondaryBookmaker
149
+ );
150
+
151
+ if (!isValidLastPolled) {
152
+ return {
153
+ odds: isTwoPositionalSport ? [0, 0] : [0, 0, 0],
154
+ errorMessage: LAST_POLLED_TOO_OLD,
155
+ };
156
+ }
157
+
158
+ // EXTRACTING ODDS FROM THE RESPONSE PER MARKET NAME AND BOOKMAKER
159
+ const moneylineOddsMap = filterOddsByMarketNameTeamNameBookmaker(
160
+ oddsApiObject.odds,
161
+ MoneylineTypes.MONEYLINE,
162
+ liveOddsProviders,
163
+ commonData,
164
+ isTwoPositionalSport
165
+ );
166
+
167
+ // CHECKING AND COMPARING ODDS FOR THE GIVEN BOOKMAKERS
168
+ const oddsObject = checkOddsFromBookmakers(
169
+ moneylineOddsMap,
170
+ liveOddsProviders,
171
+ isTwoPositionalSport,
172
+ maxPercentageDiffBetwenOdds,
173
+ MIN_ODDS_FOR_DIFF_CHECKING
174
+ );
175
+
176
+ if (oddsObject.errorMessage) {
177
+ return {
178
+ odds: isTwoPositionalSport ? [0, 0] : [0, 0, 0],
179
+ errorMessage: oddsObject.errorMessage,
180
+ };
181
+ }
182
+ const primaryBookmakerOdds = isTwoPositionalSport
183
+ ? [oddsObject.homeOdds, oddsObject.awayOdds]
184
+ : [oddsObject.homeOdds, oddsObject.awayOdds, oddsObject.drawOdds];
185
+
186
+ let parentOdds = primaryBookmakerOdds.map((odd) => convertOddsToImpl(odd));
187
+ const spreadData = getSpreadData(sportSpreadData, sportId, MONEYLINE_TYPE_ID, defaultSpreadForLiveMarkets);
188
+
189
+ if (spreadData !== null) {
190
+ parentOdds = adjustSpreadOnOdds(parentOdds, spreadData.minSpread, spreadData.targetSpread);
191
+ } else {
192
+ // Use min spread by sport if available, otherwise use default min spread
193
+ parentOdds = adjustSpreadOnOdds(parentOdds, defaultSpreadForLiveMarkets, 0);
194
+ }
195
+ return { odds: parentOdds };
196
+ };
197
+
198
+ /**
199
+ * Creates child markets based on the given parameters.
200
+ *
201
+ * @param {Object} leagueId - leagueId AKA sportId
202
+ * @param {Array} spreadDataForSport - Spread data for sport.
203
+ * @param {Object} apiResponseWithOdds - API response from the provider
204
+ * @param {Array} liveOddsProviders - Odds providers for live odds
205
+ * @param {Number} defaultSpreadForLiveMarkets - Default spread for live markets
206
+ * @param {Boolean} leagueMap - League Map info
207
+ * @returns {Array} The child markets.
208
+ */
209
+ export const createChildMarkets: (
210
+ apiResponseWithOdds: OddsObject,
211
+ spreadDataForSport: any,
212
+ leagueId: number,
213
+ liveOddsProviders: any,
214
+ defaultSpreadForLiveMarkets: any,
215
+ leagueMap: any,
216
+ lastPolledMap: LastPolledArray,
217
+ MAX_ALLOWED_PROVIDER_DATA_STALE_DELAY: number
218
+ ) => ChildMarket[] = (
219
+ apiResponseWithOdds,
220
+ spreadDataForSport,
221
+ leagueId,
222
+ liveOddsProviders,
223
+ defaultSpreadForLiveMarkets,
224
+ leagueMap,
225
+ lastPolledMap,
226
+ MAX_ALLOWED_PROVIDER_DATA_STALE_DELAY
227
+ ) => {
228
+ const [spreadOdds, totalOdds, moneylineOdds, correctScoreOdds, doubleChanceOdds, ggOdds, childMarkets]: any[] = [
229
+ [],
230
+ [],
231
+ [],
232
+ [],
233
+ [],
234
+ [],
235
+ [],
236
+ ];
237
+ const leagueInfo = getLeagueInfo(leagueId, leagueMap);
238
+ const commonData = {
239
+ homeTeam: apiResponseWithOdds.homeTeam,
240
+ awayTeam: apiResponseWithOdds.awayTeam,
241
+ };
242
+
243
+ if (leagueInfo.length > 0) {
244
+ const allChildOdds = filterOddsByMarketNameBookmaker(apiResponseWithOdds.odds, leagueInfo);
245
+ const checkedChildOdds = checkOddsFromBookmakersForChildMarkets(
246
+ allChildOdds,
247
+ leagueInfo,
248
+ liveOddsProviders,
249
+ lastPolledMap,
250
+ MAX_ALLOWED_PROVIDER_DATA_STALE_DELAY
251
+ );
252
+ checkedChildOdds.forEach((odd) => {
253
+ if (odd.type === 'Total') {
254
+ if (Math.abs(Number(odd.points) % 1) === 0.5) totalOdds.push(odd);
255
+ } else if (odd.type === 'Spread') {
256
+ if (Math.abs(Number(odd.points) % 1) === 0.5) spreadOdds.push(odd);
257
+ } else if (odd.type === 'Moneyline') {
258
+ moneylineOdds.push(odd);
259
+ } else if (odd.type === 'Correct Score') {
260
+ correctScoreOdds.push(odd);
261
+ } else if (odd.type === 'Double Chance') {
262
+ doubleChanceOdds.push(odd);
263
+ } else if (odd.type === 'Both Teams To Score') {
264
+ ggOdds.push(odd);
265
+ }
266
+ });
267
+
268
+ const homeAwayFormattedOdds = [
269
+ ...groupAndFormatSpreadOdds(spreadOdds, commonData),
270
+ ...groupAndFormatTotalOdds(totalOdds, commonData),
271
+ ...groupAndFormatMoneylineOdds(moneylineOdds, commonData),
272
+ ...groupAndFormatGGOdds(ggOdds),
273
+ ...groupAndFormatDoubleChanceOdds(doubleChanceOdds, commonData),
274
+ ];
275
+ const otherFormattedOdds = [...groupAndFormatCorrectScoreOdds(correctScoreOdds, commonData)];
276
+
277
+ const homeAwayOddsWithSpreadAdjusted = adjustSpreadOnChildOdds(
278
+ homeAwayFormattedOdds,
279
+ spreadDataForSport,
280
+ defaultSpreadForLiveMarkets
281
+ );
282
+
283
+ homeAwayOddsWithSpreadAdjusted.forEach((data) => {
284
+ const childMarket = {
285
+ leagueId: Number(data.sportId),
286
+ typeId: Number(data.typeId),
287
+ type: MarketTypeMap[data.typeId as MarketType]?.key || '',
288
+ line: Number(data.line || 0),
289
+ odds: data.odds,
290
+ };
291
+ const leagueInfoByTypeId = leagueInfo.find((league) => Number(league.typeId) === Number(data.typeId));
292
+ const minOdds = leagueInfoByTypeId?.minOdds;
293
+ const maxOdds = leagueInfoByTypeId?.maxOdds;
294
+ if (
295
+ !(
296
+ minOdds &&
297
+ maxOdds &&
298
+ (data.odds[0] >= minOdds ||
299
+ data.odds[0] <= maxOdds ||
300
+ data.odds[1] >= minOdds ||
301
+ data.odds[1] <= maxOdds)
302
+ )
303
+ ) {
304
+ childMarkets.push(childMarket);
305
+ }
306
+ });
307
+
308
+ otherFormattedOdds.forEach((data) => {
309
+ const leagueInfoByTypeId = leagueInfo.find((league) => Number(league.typeId) === Number(data.typeId));
310
+ const minOdds = leagueInfoByTypeId?.minOdds;
311
+ const maxOdds = leagueInfoByTypeId?.maxOdds;
312
+
313
+ const childMarket = {
314
+ leagueId: Number(data.sportId),
315
+ typeId: Number(data.typeId),
316
+ type: MarketTypeMap[data.typeId as MarketType]?.key || '',
317
+ line: Number(data.line || 0),
318
+ odds: data.odds.map((odd: any) => {
319
+ const impliedOdds = convertOddsToImpl(odd) || ZERO;
320
+ return !minOdds || !maxOdds || impliedOdds >= minOdds || impliedOdds <= maxOdds ? 0 : impliedOdds;
321
+ }),
322
+ positionNames: data.positionNames,
323
+ };
324
+ childMarkets.push(childMarket);
325
+ });
326
+ } else {
327
+ console.warn(`${NO_MARKETS_FOR_LEAGUE_ID}: ${Number(leagueId)}`);
328
+ }
329
+
330
+ return childMarkets;
331
+ };
332
+
333
+ /**
334
+ * Filters the odds array to find entries matching the specified market name.
335
+ *
336
+ * @param {Array} oddsArray - The array of odds objects.
337
+ * @param {string} leagueInfos - The market names to filter by.
338
+ * @param {string} oddsProvider - The main odds provider to filter by.
339
+ * @returns {Array} The filtered odds array.
340
+ */
341
+ export const filterOddsByMarketNameBookmaker = (oddsArray: Odds, leagueInfos: LeagueConfigInfo[]): any => {
342
+ const allChildMarketsTypes = leagueInfos
343
+ .filter(
344
+ (leagueInfo) =>
345
+ leagueInfo.marketName.toLowerCase() !== MoneylineTypes.MONEYLINE.toLowerCase() &&
346
+ leagueInfo.enabled === 'true'
347
+ )
348
+ .map((leagueInfo) => leagueInfo.marketName.toLowerCase());
349
+ return oddsArray.reduce((acc: any, odd: any) => {
350
+ if (allChildMarketsTypes.includes(odd.marketName.toLowerCase())) {
351
+ const { points, marketName, selection, selectionLine, sportsBookName } = odd;
352
+ const key = `${sportsBookName.toLowerCase()}_${marketName.toLowerCase()}_${points}_${selection}_${selectionLine}`;
353
+ acc[key] = {
354
+ ...odd,
355
+ ...leagueInfos.find(
356
+ (leagueInfo) => leagueInfo.marketName.toLowerCase() === odd.marketName.toLowerCase()
357
+ ), // using .find() for team totals means that we will always assign 10017 as typeID at this point
358
+ };
359
+ }
360
+
361
+ return acc;
362
+ }, {}) as any;
363
+ };
364
+
365
+ /**
366
+ * Groups spread odds by their lines and formats the result.
367
+ *
368
+ * @param {Array} oddsArray - The input array of odds objects.
369
+ * @param {Object} commonData - The common data object containing homeTeam information.
370
+ * @returns {Array} The grouped and formatted spread odds.
371
+ */
372
+ export const groupAndFormatSpreadOdds = (oddsArray: any[], commonData: HomeAwayTeams) => {
373
+ // Group odds by their selection points and selection
374
+ const groupedOdds = oddsArray.reduce((acc: any, odd: any) => {
375
+ const { points, marketName, price, selection, typeId, sportId, type } = odd;
376
+ const isHomeTeam = selection === commonData.homeTeam;
377
+
378
+ const key = `${marketName}_${isHomeTeam ? points : -points}`;
379
+
380
+ if (!acc[key]) {
381
+ acc[key] = { home: null, away: null, typeId: null, sportId: null };
382
+ }
383
+
384
+ if (isHomeTeam) {
385
+ acc[key].home = price;
386
+ } else {
387
+ acc[key].away = price;
388
+ }
389
+
390
+ acc[key].typeId = typeId;
391
+ acc[key].type = type;
392
+ acc[key].sportId = sportId;
393
+
394
+ return acc;
395
+ }, {}) as any;
396
+ // Format the grouped odds into the desired output
397
+ const formattedOdds = Object.entries(groupedOdds as any).reduce((acc: any, [key, value]) => {
398
+ const [_marketName, lineFloat] = key.split('_');
399
+ const line = parseFloat(lineFloat);
400
+ if ((value as any).home !== null && (value as any).away !== null) {
401
+ acc.push({
402
+ line: line as any,
403
+ odds: [(value as any).home, (value as any).away],
404
+ typeId: (value as any).typeId,
405
+ sportId: (value as any).sportId,
406
+ type: (value as any).type,
407
+ });
408
+ }
409
+ return acc;
410
+ }, []);
411
+
412
+ return formattedOdds;
413
+ };
414
+
415
+ /**
416
+ * Groups odds by selection and points over/under.
417
+ *
418
+ * @param {Array} oddsArray - The array of odds objects.
419
+ * @returns {Object} The grouped odds.
420
+ */
421
+ export const groupAndFormatTotalOdds = (oddsArray: any[], commonData: HomeAwayTeams) => {
422
+ // Group odds by their selection points and selection
423
+ const groupedOdds = oddsArray.reduce((acc, odd) => {
424
+ if (odd) {
425
+ const key = `${odd.marketName}_${odd.selection}_${odd.points}`;
426
+ if (!acc[key]) {
427
+ acc[key] = { over: null, under: null };
428
+ }
429
+ if (odd.selectionLine === 'over') {
430
+ acc[key].over = odd.price;
431
+ } else if (odd.selectionLine === 'under') {
432
+ acc[key].under = odd.price;
433
+ }
434
+
435
+ acc[key].typeId = odd.typeId;
436
+ acc[key].type = odd.type;
437
+ acc[key].sportId = odd.sportId;
438
+ }
439
+
440
+ return acc;
441
+ }, {});
442
+
443
+ // Format the grouped odds into the desired output
444
+ const formattedOdds = Object.entries(groupedOdds as any).reduce((acc: any, [key, value]) => {
445
+ const [_marketName, selection, selectionLine] = key.split('_');
446
+ const line = parseFloat(selectionLine);
447
+
448
+ // if we have away team in total odds we know the market is team total and we need to increase typeId by one.
449
+ // if this is false typeId is already mapped correctly
450
+ const isAwayTeam = selection === commonData.awayTeam;
451
+ if ((value as any).over !== null && (value as any).under !== null) {
452
+ acc.push({
453
+ line: line as any,
454
+ odds: [(value as any).over, (value as any).under],
455
+ typeId: !isAwayTeam ? (value as any).typeId : Number((value as any).typeId) + 1,
456
+ sportId: (value as any).sportId,
457
+ type: (value as any).type,
458
+ });
459
+ }
460
+ return acc;
461
+ }, []);
462
+
463
+ return formattedOdds;
464
+ };
465
+
466
+ /**
467
+ * Groups moneyline odds by their lines and formats the result.
468
+ *
469
+ * @param {Array} oddsArray - The input array of odds objects.
470
+ * @param {Object} commonData - The common data object containing homeTeam information.
471
+ * @returns {Array} The grouped and formatted moneyline odds.
472
+ */
473
+ export const groupAndFormatMoneylineOdds = (oddsArray: any[], commonData: HomeAwayTeams) => {
474
+ // Group odds by their selection points and selection
475
+ const groupedOdds = oddsArray.reduce((acc: any, odd: any) => {
476
+ const { price, selection, typeId, sportId, type } = odd;
477
+ const key = typeId;
478
+
479
+ if (!acc[key]) {
480
+ acc[key] = { home: null, away: null, draw: null, typeId: null, sportId: null };
481
+ }
482
+
483
+ if (selection.toLowerCase() === commonData.homeTeam.toLowerCase()) acc[key].home = price;
484
+ else if (selection.toLowerCase() === commonData.awayTeam.toLowerCase()) acc[key].away = price;
485
+ else if (selection.toLowerCase() === DRAW.toLowerCase()) acc[key].draw = price;
486
+
487
+ acc[key].typeId = typeId;
488
+ acc[key].type = type;
489
+ acc[key].sportId = sportId;
490
+
491
+ return acc;
492
+ }, {}) as any;
493
+
494
+ // Format the grouped odds into the desired output
495
+ const formattedOdds = Object.entries(groupedOdds as any).reduce((acc: any, [_key, value]) => {
496
+ if ((value as any).home !== null && (value as any).away !== null) {
497
+ acc.push({
498
+ odds: (value as any).draw
499
+ ? [(value as any).home, (value as any).away, (value as any).draw]
500
+ : [(value as any).home, (value as any).away],
501
+ typeId: (value as any).typeId,
502
+ sportId: (value as any).sportId,
503
+ type: (value as any).type,
504
+ });
505
+ }
506
+ return acc;
507
+ }, []);
508
+
509
+ return formattedOdds;
510
+ };
511
+
512
+ /**w
513
+ * Groups GG (Both Teams to Score) odds by their lines and formats the result.
514
+ *
515
+ * @param {Array} oddsArray - The input array of odds objects.
516
+ * @returns {Array} The grouped and formatted moneyline odds.
517
+ */
518
+ export const groupAndFormatGGOdds = (oddsArray: any[]) => {
519
+ const groupedOdds = oddsArray.reduce((acc: any, odd: any) => {
520
+ const { price, selection, typeId, sportId, type } = odd;
521
+ const key = typeId;
522
+
523
+ if (!acc[key]) {
524
+ acc[key] = { home: null, away: null, draw: null, typeId: null, sportId: null };
525
+ }
526
+
527
+ if (selection.toLowerCase() === 'yes') acc[key].home = price;
528
+ else if (selection.toLowerCase() === 'no') acc[key].away = price;
529
+
530
+ acc[key].typeId = typeId;
531
+ acc[key].type = type;
532
+ acc[key].sportId = sportId;
533
+
534
+ return acc;
535
+ }, {}) as any;
536
+
537
+ // Format the grouped odds into the desired output
538
+ const formattedOdds = Object.entries(groupedOdds as any).reduce((acc: any, [_key, value]) => {
539
+ if ((value as any).home !== null && (value as any).away !== null) {
540
+ acc.push({
541
+ odds: (value as any).draw
542
+ ? [(value as any).home, (value as any).away, (value as any).draw]
543
+ : [(value as any).home, (value as any).away],
544
+ typeId: (value as any).typeId,
545
+ sportId: (value as any).sportId,
546
+ type: (value as any).type,
547
+ });
548
+ }
549
+ return acc;
550
+ }, []);
551
+
552
+ return formattedOdds;
553
+ };
554
+
555
+ /**
556
+ * Normalize a score line like "A:B" to a map-friendly key "A_B" with
557
+ * numeric normalization (removing any leading zeros).
558
+ *
559
+ * Behavior:
560
+ * - Accepts optional whitespace around the colon (e.g., "03 : 10").
561
+ * - Parses both sides as numbers to strip leading zeros:
562
+ * "13:09" "13_9", "13:00" → "13_0", "03:10" → "3_10".
563
+ * - If the input does NOT match "<digits> : <digits>", falls back to a simple
564
+ * string replace of ":" with "_" without numeric parsing.
565
+ *
566
+ * @param {string|number} line
567
+ * A score string (e.g., "13:09") or any value coercible to string.
568
+ *
569
+ * @returns {string}
570
+ * The normalized key (e.g., "13_9"). If not a digit:digit pattern, returns
571
+ * the string with ":" replaced by "_".
572
+ */
573
+ function normalizeScoreLine(line: any) {
574
+ const m = String(line).match(/(\d+)\s*:\s*(\d+)/);
575
+ return m ? `${Number(m[1])}_${Number(m[2])}` : String(line).replace(':', '_');
576
+ }
577
+
578
+ /**
579
+ * Groups correct score odds by their lines and formats the result.
580
+ *
581
+ * @param {Array} oddsArray - The input array of odds objects.
582
+ * @param {Object} commonData - The common data object containing homeTeam and awayTeam information.
583
+ * @returns {Array} The grouped and formatted correct score odds.
584
+ */
585
+ export const groupAndFormatCorrectScoreOdds = (oddsArray: any[], commonData: HomeAwayTeams): any[] => {
586
+ const homeTeamKey = commonData.homeTeam.toLowerCase().replace(/\s+/g, '_');
587
+ const awayTeamKey = commonData.awayTeam.toLowerCase().replace(/\s+/g, '_');
588
+
589
+ // Group odds by typeId first
590
+ const oddsByTypeId = oddsArray.reduce((acc: any, odd: any) => {
591
+ const typeId = odd.typeId || 10100;
592
+ if (!acc[typeId]) {
593
+ acc[typeId] = [];
594
+ }
595
+ acc[typeId].push(odd);
596
+ return acc;
597
+ }, {});
598
+
599
+ // Create market objects for each unique typeId
600
+ const marketObjects: any[] = [];
601
+
602
+ Object.entries(oddsByTypeId).forEach(([typeId, odds]: [string, any]) => {
603
+ const oddsMap = {
604
+ draw_0_0: 0,
605
+ draw_1_1: 0,
606
+ draw_2_2: 0,
607
+ draw_3_3: 0,
608
+ draw_4_4: 0,
609
+ [`${homeTeamKey}_1_0`]: 0,
610
+ [`${homeTeamKey}_2_0`]: 0,
611
+ [`${homeTeamKey}_2_1`]: 0,
612
+ [`${homeTeamKey}_3_0`]: 0,
613
+ [`${homeTeamKey}_3_1`]: 0,
614
+ [`${homeTeamKey}_3_2`]: 0,
615
+ [`${homeTeamKey}_4_0`]: 0,
616
+ [`${homeTeamKey}_4_1`]: 0,
617
+ [`${homeTeamKey}_4_2`]: 0,
618
+ [`${homeTeamKey}_4_3`]: 0,
619
+ [`${awayTeamKey}_1_0`]: 0,
620
+ [`${awayTeamKey}_2_0`]: 0,
621
+ [`${awayTeamKey}_2_1`]: 0,
622
+ [`${awayTeamKey}_3_0`]: 0,
623
+ [`${awayTeamKey}_3_1`]: 0,
624
+ [`${awayTeamKey}_3_2`]: 0,
625
+ [`${awayTeamKey}_4_0`]: 0,
626
+ [`${awayTeamKey}_4_1`]: 0,
627
+ [`${awayTeamKey}_4_2`]: 0,
628
+ [`${awayTeamKey}_4_3`]: 0,
629
+ draw_5_5: 0,
630
+ draw_6_6: 0,
631
+ draw_7_7: 0,
632
+ draw_8_8: 0,
633
+ draw_9_9: 0,
634
+ draw_10_10: 0,
635
+ draw_11_11: 0,
636
+ draw_12_12: 0,
637
+ draw_13_13: 0,
638
+ draw_14_14: 0,
639
+ [`${homeTeamKey}_5_0`]: 0,
640
+ [`${homeTeamKey}_5_1`]: 0,
641
+ [`${homeTeamKey}_5_2`]: 0,
642
+ [`${homeTeamKey}_5_3`]: 0,
643
+ [`${homeTeamKey}_5_4`]: 0,
644
+ [`${homeTeamKey}_6_0`]: 0,
645
+ [`${homeTeamKey}_6_1`]: 0,
646
+ [`${homeTeamKey}_6_2`]: 0,
647
+ [`${homeTeamKey}_6_3`]: 0,
648
+ [`${homeTeamKey}_6_4`]: 0,
649
+ [`${homeTeamKey}_6_5`]: 0,
650
+ [`${homeTeamKey}_7_0`]: 0,
651
+ [`${homeTeamKey}_7_1`]: 0,
652
+ [`${homeTeamKey}_7_2`]: 0,
653
+ [`${homeTeamKey}_7_3`]: 0,
654
+ [`${homeTeamKey}_7_4`]: 0,
655
+ [`${homeTeamKey}_7_5`]: 0,
656
+ [`${homeTeamKey}_7_6`]: 0,
657
+ [`${homeTeamKey}_8_0`]: 0,
658
+ [`${homeTeamKey}_8_1`]: 0,
659
+ [`${homeTeamKey}_8_2`]: 0,
660
+ [`${homeTeamKey}_8_3`]: 0,
661
+ [`${homeTeamKey}_8_4`]: 0,
662
+ [`${homeTeamKey}_8_5`]: 0,
663
+ [`${homeTeamKey}_8_6`]: 0,
664
+ [`${homeTeamKey}_8_7`]: 0,
665
+ [`${homeTeamKey}_9_0`]: 0,
666
+ [`${homeTeamKey}_9_1`]: 0,
667
+ [`${homeTeamKey}_9_2`]: 0,
668
+ [`${homeTeamKey}_9_3`]: 0,
669
+ [`${homeTeamKey}_9_4`]: 0,
670
+ [`${homeTeamKey}_9_5`]: 0,
671
+ [`${homeTeamKey}_9_6`]: 0,
672
+ [`${homeTeamKey}_9_7`]: 0,
673
+ [`${homeTeamKey}_9_8`]: 0,
674
+ [`${homeTeamKey}_10_0`]: 0,
675
+ [`${homeTeamKey}_10_1`]: 0,
676
+ [`${homeTeamKey}_10_2`]: 0,
677
+ [`${homeTeamKey}_10_3`]: 0,
678
+ [`${homeTeamKey}_10_4`]: 0,
679
+ [`${homeTeamKey}_10_5`]: 0,
680
+ [`${homeTeamKey}_10_6`]: 0,
681
+ [`${homeTeamKey}_10_7`]: 0,
682
+ [`${homeTeamKey}_10_8`]: 0,
683
+ [`${homeTeamKey}_10_9`]: 0,
684
+ [`${homeTeamKey}_11_0`]: 0,
685
+ [`${homeTeamKey}_11_1`]: 0,
686
+ [`${homeTeamKey}_11_2`]: 0,
687
+ [`${homeTeamKey}_11_3`]: 0,
688
+ [`${homeTeamKey}_11_4`]: 0,
689
+ [`${homeTeamKey}_11_5`]: 0,
690
+ [`${homeTeamKey}_11_6`]: 0,
691
+ [`${homeTeamKey}_11_7`]: 0,
692
+ [`${homeTeamKey}_11_8`]: 0,
693
+ [`${homeTeamKey}_11_9`]: 0,
694
+ [`${homeTeamKey}_11_10`]: 0,
695
+ [`${homeTeamKey}_12_0`]: 0,
696
+ [`${homeTeamKey}_12_1`]: 0,
697
+ [`${homeTeamKey}_12_2`]: 0,
698
+ [`${homeTeamKey}_12_3`]: 0,
699
+ [`${homeTeamKey}_12_4`]: 0,
700
+ [`${homeTeamKey}_12_5`]: 0,
701
+ [`${homeTeamKey}_12_6`]: 0,
702
+ [`${homeTeamKey}_12_7`]: 0,
703
+ [`${homeTeamKey}_12_8`]: 0,
704
+ [`${homeTeamKey}_12_9`]: 0,
705
+ [`${homeTeamKey}_12_10`]: 0,
706
+ [`${homeTeamKey}_12_11`]: 0,
707
+ [`${homeTeamKey}_13_0`]: 0,
708
+ [`${homeTeamKey}_13_1`]: 0,
709
+ [`${homeTeamKey}_13_2`]: 0,
710
+ [`${homeTeamKey}_13_3`]: 0,
711
+ [`${homeTeamKey}_13_4`]: 0,
712
+ [`${homeTeamKey}_13_5`]: 0,
713
+ [`${homeTeamKey}_13_6`]: 0,
714
+ [`${homeTeamKey}_13_7`]: 0,
715
+ [`${homeTeamKey}_13_8`]: 0,
716
+ [`${homeTeamKey}_13_9`]: 0,
717
+ [`${homeTeamKey}_13_10`]: 0,
718
+ [`${homeTeamKey}_13_11`]: 0,
719
+ [`${homeTeamKey}_13_12`]: 0,
720
+ [`${homeTeamKey}_14_0`]: 0,
721
+ [`${homeTeamKey}_14_1`]: 0,
722
+ [`${homeTeamKey}_14_2`]: 0,
723
+ [`${homeTeamKey}_14_3`]: 0,
724
+ [`${homeTeamKey}_14_4`]: 0,
725
+ [`${homeTeamKey}_14_5`]: 0,
726
+ [`${homeTeamKey}_14_6`]: 0,
727
+ [`${homeTeamKey}_14_7`]: 0,
728
+ [`${homeTeamKey}_14_8`]: 0,
729
+ [`${homeTeamKey}_14_9`]: 0,
730
+ [`${homeTeamKey}_14_10`]: 0,
731
+ [`${homeTeamKey}_14_11`]: 0,
732
+ [`${homeTeamKey}_14_12`]: 0,
733
+ [`${homeTeamKey}_14_13`]: 0,
734
+ [`${awayTeamKey}_5_0`]: 0,
735
+ [`${awayTeamKey}_5_1`]: 0,
736
+ [`${awayTeamKey}_5_2`]: 0,
737
+ [`${awayTeamKey}_5_3`]: 0,
738
+ [`${awayTeamKey}_5_4`]: 0,
739
+ [`${awayTeamKey}_6_0`]: 0,
740
+ [`${awayTeamKey}_6_1`]: 0,
741
+ [`${awayTeamKey}_6_2`]: 0,
742
+ [`${awayTeamKey}_6_3`]: 0,
743
+ [`${awayTeamKey}_6_4`]: 0,
744
+ [`${awayTeamKey}_6_5`]: 0,
745
+ [`${awayTeamKey}_7_0`]: 0,
746
+ [`${awayTeamKey}_7_1`]: 0,
747
+ [`${awayTeamKey}_7_2`]: 0,
748
+ [`${awayTeamKey}_7_3`]: 0,
749
+ [`${awayTeamKey}_7_4`]: 0,
750
+ [`${awayTeamKey}_7_5`]: 0,
751
+ [`${awayTeamKey}_7_6`]: 0,
752
+ [`${awayTeamKey}_8_0`]: 0,
753
+ [`${awayTeamKey}_8_1`]: 0,
754
+ [`${awayTeamKey}_8_2`]: 0,
755
+ [`${awayTeamKey}_8_3`]: 0,
756
+ [`${awayTeamKey}_8_4`]: 0,
757
+ [`${awayTeamKey}_8_5`]: 0,
758
+ [`${awayTeamKey}_8_6`]: 0,
759
+ [`${awayTeamKey}_8_7`]: 0,
760
+ [`${awayTeamKey}_9_0`]: 0,
761
+ [`${awayTeamKey}_9_1`]: 0,
762
+ [`${awayTeamKey}_9_2`]: 0,
763
+ [`${awayTeamKey}_9_3`]: 0,
764
+ [`${awayTeamKey}_9_4`]: 0,
765
+ [`${awayTeamKey}_9_5`]: 0,
766
+ [`${awayTeamKey}_9_6`]: 0,
767
+ [`${awayTeamKey}_9_7`]: 0,
768
+ [`${awayTeamKey}_9_8`]: 0,
769
+ [`${awayTeamKey}_10_0`]: 0,
770
+ [`${awayTeamKey}_10_1`]: 0,
771
+ [`${awayTeamKey}_10_2`]: 0,
772
+ [`${awayTeamKey}_10_3`]: 0,
773
+ [`${awayTeamKey}_10_4`]: 0,
774
+ [`${awayTeamKey}_10_5`]: 0,
775
+ [`${awayTeamKey}_10_6`]: 0,
776
+ [`${awayTeamKey}_10_7`]: 0,
777
+ [`${awayTeamKey}_10_8`]: 0,
778
+ [`${awayTeamKey}_10_9`]: 0,
779
+ [`${awayTeamKey}_11_0`]: 0,
780
+ [`${awayTeamKey}_11_1`]: 0,
781
+ [`${awayTeamKey}_11_2`]: 0,
782
+ [`${awayTeamKey}_11_3`]: 0,
783
+ [`${awayTeamKey}_11_4`]: 0,
784
+ [`${awayTeamKey}_11_5`]: 0,
785
+ [`${awayTeamKey}_11_6`]: 0,
786
+ [`${awayTeamKey}_11_7`]: 0,
787
+ [`${awayTeamKey}_11_8`]: 0,
788
+ [`${awayTeamKey}_11_9`]: 0,
789
+ [`${awayTeamKey}_11_10`]: 0,
790
+ [`${awayTeamKey}_12_0`]: 0,
791
+ [`${awayTeamKey}_12_1`]: 0,
792
+ [`${awayTeamKey}_12_2`]: 0,
793
+ [`${awayTeamKey}_12_3`]: 0,
794
+ [`${awayTeamKey}_12_4`]: 0,
795
+ [`${awayTeamKey}_12_5`]: 0,
796
+ [`${awayTeamKey}_12_6`]: 0,
797
+ [`${awayTeamKey}_12_7`]: 0,
798
+ [`${awayTeamKey}_12_8`]: 0,
799
+ [`${awayTeamKey}_12_9`]: 0,
800
+ [`${awayTeamKey}_12_10`]: 0,
801
+ [`${awayTeamKey}_12_11`]: 0,
802
+ [`${awayTeamKey}_13_0`]: 0,
803
+ [`${awayTeamKey}_13_1`]: 0,
804
+ [`${awayTeamKey}_13_2`]: 0,
805
+ [`${awayTeamKey}_13_3`]: 0,
806
+ [`${awayTeamKey}_13_4`]: 0,
807
+ [`${awayTeamKey}_13_5`]: 0,
808
+ [`${awayTeamKey}_13_6`]: 0,
809
+ [`${awayTeamKey}_13_7`]: 0,
810
+ [`${awayTeamKey}_13_8`]: 0,
811
+ [`${awayTeamKey}_13_9`]: 0,
812
+ [`${awayTeamKey}_13_10`]: 0,
813
+ [`${awayTeamKey}_13_11`]: 0,
814
+ [`${awayTeamKey}_13_12`]: 0,
815
+ [`${awayTeamKey}_14_0`]: 0,
816
+ [`${awayTeamKey}_14_1`]: 0,
817
+ [`${awayTeamKey}_14_2`]: 0,
818
+ [`${awayTeamKey}_14_3`]: 0,
819
+ [`${awayTeamKey}_14_4`]: 0,
820
+ [`${awayTeamKey}_14_5`]: 0,
821
+ [`${awayTeamKey}_14_6`]: 0,
822
+ [`${awayTeamKey}_14_7`]: 0,
823
+ [`${awayTeamKey}_14_8`]: 0,
824
+ [`${awayTeamKey}_14_9`]: 0,
825
+ [`${awayTeamKey}_14_10`]: 0,
826
+ [`${awayTeamKey}_14_11`]: 0,
827
+ [`${awayTeamKey}_14_12`]: 0,
828
+ [`${awayTeamKey}_14_13`]: 0,
829
+ other: 0,
830
+ };
831
+
832
+ // Populate the oddsMap with the odds from this typeId's odds
833
+ odds.forEach((odd: any) => {
834
+ const normalizedSelection = `${odd.selection.toLowerCase().replace(/\s+/g, '_')}_${normalizeScoreLine(
835
+ odd.selectionLine
836
+ )}`;
837
+ if (oddsMap.hasOwnProperty(normalizedSelection)) {
838
+ oddsMap[normalizedSelection] = odd.price;
839
+ }
840
+ });
841
+
842
+ const allOddsAreZero = Object.values(oddsMap).every((odd) => odd === ZERO);
843
+
844
+ if (!allOddsAreZero) {
845
+ const positionNames = Object.keys(oddsMap);
846
+
847
+ // Create a market object for this typeId
848
+ const marketObject = {
849
+ homeTeam: commonData.homeTeam,
850
+ awayTeam: commonData.awayTeam,
851
+ line: 0,
852
+ positionNames: positionNames,
853
+ odds: Object.values(oddsMap),
854
+ type: odds?.[0]?.type ? odds[0].type : 'Correct Score',
855
+ typeId: Number(typeId),
856
+ sportId: odds?.[0]?.sportId ? odds[0].sportId : undefined,
857
+ };
858
+ marketObjects.push(marketObject);
859
+ }
860
+ });
861
+
862
+ return marketObjects;
863
+ };
864
+
865
+ /**
866
+ * Groups double chance odds by their lines and formats the result.
867
+ *
868
+ * @param {Array} oddsArray - The input array of odds objects.
869
+ * @param {Object} commonData - The common data object containing homeTeam and awayTeam information.
870
+ * @returns {Array} The grouped and formatted correct score odds.
871
+ */
872
+ export const groupAndFormatDoubleChanceOdds = (oddsArray: any[], commonData: HomeAwayTeams) => {
873
+ let probability1X = 0;
874
+ let probability12 = 0;
875
+ let probabilityX2 = 0;
876
+
877
+ let sportId;
878
+
879
+ oddsArray.forEach((odd) => {
880
+ if (odd.sportId) {
881
+ sportId = odd.sportId;
882
+ }
883
+ if (odd.selection.includes(commonData.homeTeam)) {
884
+ if (odd.selection.includes(commonData.awayTeam)) {
885
+ probability12 = odd.price;
886
+ } else {
887
+ probability1X = odd.price;
888
+ }
889
+ } else if (odd.selection.includes(commonData.awayTeam)) {
890
+ probabilityX2 = odd.price;
891
+ }
892
+ });
893
+
894
+ if ([probability1X, probability12, probabilityX2].every((odd) => odd === ZERO)) {
895
+ return [];
896
+ }
897
+ // Create the market object
898
+ const marketObject = {
899
+ homeTeam: commonData.homeTeam,
900
+ awayTeam: commonData.awayTeam,
901
+ line: 0,
902
+ odds: [probability1X, probability12, probabilityX2],
903
+ type: 'Double Chance',
904
+ typeId: 10003,
905
+ sportId: sportId,
906
+ };
907
+ return [marketObject];
908
+ };
909
+
910
+ // used for home/away markets
911
+ export const adjustSpreadOnChildOdds = (
912
+ iterableGroupedOdds: any[],
913
+ spreadDataForSport: any,
914
+ defaultSpreadForLiveMarkets: any
915
+ ) => {
916
+ const result: any[] = [];
917
+ iterableGroupedOdds.forEach((data) => {
918
+ const hasDrawOdds = data.odds.length === 3;
919
+ const homeTeamOdds = convertOddsToImpl(data.odds[0]) || ZERO;
920
+ const awayTeamOdds = convertOddsToImpl(data.odds[1]) || ZERO;
921
+ const drawOdds = convertOddsToImpl(data.odds[2]) || ZERO;
922
+ const odds = hasDrawOdds ? [homeTeamOdds, awayTeamOdds, drawOdds] : [homeTeamOdds, awayTeamOdds];
923
+
924
+ const isZeroOddsChild = homeTeamOdds === ZERO || awayTeamOdds === ZERO || (hasDrawOdds && drawOdds === ZERO);
925
+ if (!isZeroOddsChild) {
926
+ const spreadData = getSpreadData(
927
+ spreadDataForSport,
928
+ data.sportId,
929
+ data.typeId,
930
+ defaultSpreadForLiveMarkets
931
+ );
932
+
933
+ let adjustedOdds;
934
+ if (spreadData !== null) {
935
+ adjustedOdds = adjustSpreadOnOdds(odds, spreadData.minSpread, spreadData.targetSpread);
936
+ } else {
937
+ adjustedOdds = adjustSpreadOnOdds(odds, defaultSpreadForLiveMarkets, 0);
938
+ }
939
+
940
+ result.push({
941
+ ...data,
942
+ odds: adjustedOdds,
943
+ });
944
+ }
945
+ });
946
+ return result;
947
+ };