overtime-live-trading-utils 2.1.32 → 2.1.33

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 (38) hide show
  1. package/.circleci/config.yml +32 -32
  2. package/.prettierrc +9 -9
  3. package/CLAUDE.md +77 -0
  4. package/codecov.yml +20 -20
  5. package/index.ts +26 -26
  6. package/jest.config.ts +16 -16
  7. package/main.js +1 -1
  8. package/package.json +30 -30
  9. package/resolution_live_markets.md +351 -0
  10. package/src/constants/common.ts +7 -7
  11. package/src/constants/errors.ts +6 -6
  12. package/src/constants/sports.ts +78 -78
  13. package/src/enums/sports.ts +109 -109
  14. package/src/tests/mock/MockLeagueMap.ts +170 -170
  15. package/src/tests/mock/MockOpticOddsEvents.ts +518 -518
  16. package/src/tests/mock/MockOpticSoccer.ts +9378 -9378
  17. package/src/tests/mock/MockSoccerRedis.ts +2308 -2308
  18. package/src/tests/unit/bookmakers.test.ts +79 -79
  19. package/src/tests/unit/markets.test.ts +156 -156
  20. package/src/tests/unit/odds.test.ts +92 -92
  21. package/src/tests/unit/resolution.test.ts +993 -935
  22. package/src/tests/unit/sports.test.ts +58 -58
  23. package/src/tests/unit/spread.test.ts +131 -131
  24. package/src/types/missing-types.d.ts +2 -2
  25. package/src/types/odds.ts +61 -61
  26. package/src/types/resolution.ts +96 -96
  27. package/src/types/sports.ts +19 -19
  28. package/src/utils/bookmakers.ts +159 -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 +918 -918
  33. package/src/utils/opticOdds.ts +71 -71
  34. package/src/utils/resolution.ts +298 -291
  35. package/src/utils/sports.ts +51 -51
  36. package/src/utils/spread.ts +97 -97
  37. package/tsconfig.json +16 -16
  38. package/webpack.config.js +24 -24
package/src/utils/odds.ts CHANGED
@@ -1,918 +1,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 { 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 { 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
+ };