overtime-live-trading-utils 2.1.23 → 2.1.25

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/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 +7 -7
  9. package/src/constants/errors.ts +6 -6
  10. package/src/constants/sports.ts +78 -78
  11. package/src/enums/sports.ts +109 -109
  12. package/src/tests/mock/MockLeagueMap.ts +170 -170
  13. package/src/tests/mock/MockOpticOddsEvents.ts +518 -518
  14. package/src/tests/mock/MockOpticSoccer.ts +9378 -9378
  15. package/src/tests/mock/MockSoccerRedis.ts +2308 -2308
  16. package/src/tests/unit/bookmakers.test.ts +79 -79
  17. package/src/tests/unit/markets.test.ts +156 -156
  18. package/src/tests/unit/odds.test.ts +92 -92
  19. package/src/tests/unit/resolution.test.ts +935 -935
  20. package/src/tests/unit/sports.test.ts +58 -58
  21. package/src/tests/unit/spread.test.ts +131 -131
  22. package/src/types/missing-types.d.ts +2 -2
  23. package/src/types/odds.ts +61 -61
  24. package/src/types/resolution.ts +96 -96
  25. package/src/types/sports.ts +19 -19
  26. package/src/utils/bookmakers.ts +159 -159
  27. package/src/utils/constraints.ts +210 -210
  28. package/src/utils/gameMatching.ts +81 -81
  29. package/src/utils/markets.ts +119 -119
  30. package/src/utils/odds.ts +874 -674
  31. package/src/utils/opticOdds.ts +71 -71
  32. package/src/utils/resolution.ts +291 -275
  33. package/src/utils/sports.ts +51 -51
  34. package/src/utils/spread.ts +97 -97
  35. package/tsconfig.json +16 -16
  36. package/webpack.config.js +24 -24
  37. package/CLAUDE.md +0 -77
  38. package/resolution_live_markets.md +0 -351
package/src/utils/odds.ts CHANGED
@@ -1,674 +1,874 @@
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
- * Groups correct score odds by their lines and formats the result.
528
- *
529
- * @param {Array} oddsArray - The input array of odds objects.
530
- * @param {Object} commonData - The common data object containing homeTeam and awayTeam information.
531
- * @returns {Array} The grouped and formatted correct score odds.
532
- */
533
- export const groupAndFormatCorrectScoreOdds = (oddsArray: any[], commonData: HomeAwayTeams): any[] => {
534
- const homeTeamKey = commonData.homeTeam.toLowerCase().replace(/\s+/g, '_');
535
- const awayTeamKey = commonData.awayTeam.toLowerCase().replace(/\s+/g, '_');
536
-
537
- const oddsMap = {
538
- draw_0_0: 0,
539
- draw_1_1: 0,
540
- draw_2_2: 0,
541
- draw_3_3: 0,
542
- draw_4_4: 0,
543
- [`${homeTeamKey}_1_0`]: 0,
544
- [`${homeTeamKey}_2_0`]: 0,
545
- [`${homeTeamKey}_2_1`]: 0,
546
- [`${homeTeamKey}_3_0`]: 0,
547
- [`${homeTeamKey}_3_1`]: 0,
548
- [`${homeTeamKey}_3_2`]: 0,
549
- [`${homeTeamKey}_4_0`]: 0,
550
- [`${homeTeamKey}_4_1`]: 0,
551
- [`${homeTeamKey}_4_2`]: 0,
552
- [`${homeTeamKey}_4_3`]: 0,
553
- [`${awayTeamKey}_1_0`]: 0,
554
- [`${awayTeamKey}_2_0`]: 0,
555
- [`${awayTeamKey}_2_1`]: 0,
556
- [`${awayTeamKey}_3_0`]: 0,
557
- [`${awayTeamKey}_3_1`]: 0,
558
- [`${awayTeamKey}_3_2`]: 0,
559
- [`${awayTeamKey}_4_0`]: 0,
560
- [`${awayTeamKey}_4_1`]: 0,
561
- [`${awayTeamKey}_4_2`]: 0,
562
- [`${awayTeamKey}_4_3`]: 0,
563
- other: 0,
564
- };
565
-
566
- // Populate the oddsMap with the odds from the provided array
567
- oddsArray.forEach((odd) => {
568
- const normalizedSelection = `${odd.selection.toLowerCase().replace(/\s+/g, '_')}_${odd.selectionLine.replace(
569
- ':',
570
- '_'
571
- )}`;
572
- if (oddsMap.hasOwnProperty(normalizedSelection)) {
573
- oddsMap[normalizedSelection] = odd.price;
574
- }
575
- });
576
-
577
- const allOddsAreZero = Object.values(oddsMap).every((odd) => odd === ZERO);
578
-
579
- if (allOddsAreZero) {
580
- return [];
581
- }
582
-
583
- const positionNames = Object.keys(oddsMap);
584
-
585
- // Create the market object
586
- const marketObject = {
587
- homeTeam: commonData.homeTeam,
588
- awayTeam: commonData.awayTeam,
589
- line: 0,
590
- positionNames: positionNames,
591
- odds: Object.values(oddsMap),
592
- type: 'Correct Score',
593
- typeId: 10100,
594
- };
595
- return [marketObject];
596
- };
597
-
598
- /**
599
- * Groups double chance odds by their lines and formats the result.
600
- *
601
- * @param {Array} oddsArray - The input array of odds objects.
602
- * @param {Object} commonData - The common data object containing homeTeam and awayTeam information.
603
- * @returns {Array} The grouped and formatted correct score odds.
604
- */
605
- export const groupAndFormatDoubleChanceOdds = (oddsArray: any[], commonData: HomeAwayTeams) => {
606
- let probability1X = 0;
607
- let probability12 = 0;
608
- let probabilityX2 = 0;
609
-
610
- oddsArray.forEach((odd) => {
611
- if (odd.selection.includes(commonData.homeTeam)) {
612
- if (odd.selection.includes(commonData.awayTeam)) {
613
- probability12 = odd.price;
614
- } else {
615
- probability1X = odd.price;
616
- }
617
- } else if (odd.selection.includes(commonData.awayTeam)) {
618
- probabilityX2 = odd.price;
619
- }
620
- });
621
-
622
- if ([probability1X, probability12, probabilityX2].every((odd) => odd === ZERO)) {
623
- return [];
624
- }
625
- // Create the market object
626
- const marketObject = {
627
- homeTeam: commonData.homeTeam,
628
- awayTeam: commonData.awayTeam,
629
- line: 0,
630
- odds: [probability1X, probability12, probabilityX2],
631
- type: 'Double Chance',
632
- typeId: 10003,
633
- };
634
- return [marketObject];
635
- };
636
-
637
- // used for home/away markets
638
- export const adjustSpreadOnChildOdds = (
639
- iterableGroupedOdds: any[],
640
- spreadDataForSport: any,
641
- defaultSpreadForLiveMarkets: any
642
- ) => {
643
- const result: any[] = [];
644
- iterableGroupedOdds.forEach((data) => {
645
- const hasDrawOdds = data.odds.length === 3;
646
- const homeTeamOdds = convertOddsToImpl(data.odds[0]) || ZERO;
647
- const awayTeamOdds = convertOddsToImpl(data.odds[1]) || ZERO;
648
- const drawOdds = convertOddsToImpl(data.odds[2]) || ZERO;
649
- const odds = hasDrawOdds ? [homeTeamOdds, awayTeamOdds, drawOdds] : [homeTeamOdds, awayTeamOdds];
650
-
651
- const isZeroOddsChild = homeTeamOdds === ZERO || awayTeamOdds === ZERO || (hasDrawOdds && drawOdds === ZERO);
652
- if (!isZeroOddsChild) {
653
- const spreadData = getSpreadData(
654
- spreadDataForSport,
655
- data.sportId,
656
- data.typeId,
657
- defaultSpreadForLiveMarkets
658
- );
659
-
660
- let adjustedOdds;
661
- if (spreadData !== null) {
662
- adjustedOdds = adjustSpreadOnOdds(odds, spreadData.minSpread, spreadData.targetSpread);
663
- } else {
664
- adjustedOdds = adjustSpreadOnOdds(odds, defaultSpreadForLiveMarkets, 0);
665
- }
666
-
667
- result.push({
668
- ...data,
669
- odds: adjustedOdds,
670
- });
671
- }
672
- });
673
- return result;
674
- };
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
+ * Groups correct score odds by their lines and formats the result.
528
+ *
529
+ * @param {Array} oddsArray - The input array of odds objects.
530
+ * @param {Object} commonData - The common data object containing homeTeam and awayTeam information.
531
+ * @returns {Array} The grouped and formatted correct score odds.
532
+ */
533
+ export const groupAndFormatCorrectScoreOdds = (oddsArray: any[], commonData: HomeAwayTeams): any[] => {
534
+ const homeTeamKey = commonData.homeTeam.toLowerCase().replace(/\s+/g, '_');
535
+ const awayTeamKey = commonData.awayTeam.toLowerCase().replace(/\s+/g, '_');
536
+
537
+ const oddsMap = {
538
+ draw_0_0: 0,
539
+ draw_1_1: 0,
540
+ draw_2_2: 0,
541
+ draw_3_3: 0,
542
+ draw_4_4: 0,
543
+ [`${homeTeamKey}_1_0`]: 0,
544
+ [`${homeTeamKey}_2_0`]: 0,
545
+ [`${homeTeamKey}_2_1`]: 0,
546
+ [`${homeTeamKey}_3_0`]: 0,
547
+ [`${homeTeamKey}_3_1`]: 0,
548
+ [`${homeTeamKey}_3_2`]: 0,
549
+ [`${homeTeamKey}_4_0`]: 0,
550
+ [`${homeTeamKey}_4_1`]: 0,
551
+ [`${homeTeamKey}_4_2`]: 0,
552
+ [`${homeTeamKey}_4_3`]: 0,
553
+ [`${awayTeamKey}_1_0`]: 0,
554
+ [`${awayTeamKey}_2_0`]: 0,
555
+ [`${awayTeamKey}_2_1`]: 0,
556
+ [`${awayTeamKey}_3_0`]: 0,
557
+ [`${awayTeamKey}_3_1`]: 0,
558
+ [`${awayTeamKey}_3_2`]: 0,
559
+ [`${awayTeamKey}_4_0`]: 0,
560
+ [`${awayTeamKey}_4_1`]: 0,
561
+ [`${awayTeamKey}_4_2`]: 0,
562
+ [`${awayTeamKey}_4_3`]: 0,
563
+ draw_5_5: 0,
564
+ draw_6_6: 0,
565
+ draw_7_7: 0,
566
+ draw_8_8: 0,
567
+ draw_9_9: 0,
568
+ draw_10_10: 0,
569
+ draw_11_11: 0,
570
+ draw_12_12: 0,
571
+ draw_13_13: 0,
572
+ draw_14_14: 0,
573
+ [`${homeTeamKey}_5_0`]: 0,
574
+ [`${homeTeamKey}_5_1`]: 0,
575
+ [`${homeTeamKey}_5_2`]: 0,
576
+ [`${homeTeamKey}_5_3`]: 0,
577
+ [`${homeTeamKey}_5_4`]: 0,
578
+ [`${homeTeamKey}_6_0`]: 0,
579
+ [`${homeTeamKey}_6_1`]: 0,
580
+ [`${homeTeamKey}_6_2`]: 0,
581
+ [`${homeTeamKey}_6_3`]: 0,
582
+ [`${homeTeamKey}_6_4`]: 0,
583
+ [`${homeTeamKey}_6_5`]: 0,
584
+ [`${homeTeamKey}_7_0`]: 0,
585
+ [`${homeTeamKey}_7_1`]: 0,
586
+ [`${homeTeamKey}_7_2`]: 0,
587
+ [`${homeTeamKey}_7_3`]: 0,
588
+ [`${homeTeamKey}_7_4`]: 0,
589
+ [`${homeTeamKey}_7_5`]: 0,
590
+ [`${homeTeamKey}_7_6`]: 0,
591
+ [`${homeTeamKey}_8_0`]: 0,
592
+ [`${homeTeamKey}_8_1`]: 0,
593
+ [`${homeTeamKey}_8_2`]: 0,
594
+ [`${homeTeamKey}_8_3`]: 0,
595
+ [`${homeTeamKey}_8_4`]: 0,
596
+ [`${homeTeamKey}_8_5`]: 0,
597
+ [`${homeTeamKey}_8_6`]: 0,
598
+ [`${homeTeamKey}_8_7`]: 0,
599
+ [`${homeTeamKey}_9_0`]: 0,
600
+ [`${homeTeamKey}_9_1`]: 0,
601
+ [`${homeTeamKey}_9_2`]: 0,
602
+ [`${homeTeamKey}_9_3`]: 0,
603
+ [`${homeTeamKey}_9_4`]: 0,
604
+ [`${homeTeamKey}_9_5`]: 0,
605
+ [`${homeTeamKey}_9_6`]: 0,
606
+ [`${homeTeamKey}_9_7`]: 0,
607
+ [`${homeTeamKey}_9_8`]: 0,
608
+ [`${homeTeamKey}_10_0`]: 0,
609
+ [`${homeTeamKey}_10_1`]: 0,
610
+ [`${homeTeamKey}_10_2`]: 0,
611
+ [`${homeTeamKey}_10_3`]: 0,
612
+ [`${homeTeamKey}_10_4`]: 0,
613
+ [`${homeTeamKey}_10_5`]: 0,
614
+ [`${homeTeamKey}_10_6`]: 0,
615
+ [`${homeTeamKey}_10_7`]: 0,
616
+ [`${homeTeamKey}_10_8`]: 0,
617
+ [`${homeTeamKey}_10_9`]: 0,
618
+ [`${homeTeamKey}_11_0`]: 0,
619
+ [`${homeTeamKey}_11_1`]: 0,
620
+ [`${homeTeamKey}_11_2`]: 0,
621
+ [`${homeTeamKey}_11_3`]: 0,
622
+ [`${homeTeamKey}_11_4`]: 0,
623
+ [`${homeTeamKey}_11_5`]: 0,
624
+ [`${homeTeamKey}_11_6`]: 0,
625
+ [`${homeTeamKey}_11_7`]: 0,
626
+ [`${homeTeamKey}_11_8`]: 0,
627
+ [`${homeTeamKey}_11_9`]: 0,
628
+ [`${homeTeamKey}_11_10`]: 0,
629
+ [`${homeTeamKey}_12_0`]: 0,
630
+ [`${homeTeamKey}_12_1`]: 0,
631
+ [`${homeTeamKey}_12_2`]: 0,
632
+ [`${homeTeamKey}_12_3`]: 0,
633
+ [`${homeTeamKey}_12_4`]: 0,
634
+ [`${homeTeamKey}_12_5`]: 0,
635
+ [`${homeTeamKey}_12_6`]: 0,
636
+ [`${homeTeamKey}_12_7`]: 0,
637
+ [`${homeTeamKey}_12_8`]: 0,
638
+ [`${homeTeamKey}_12_9`]: 0,
639
+ [`${homeTeamKey}_12_10`]: 0,
640
+ [`${homeTeamKey}_12_11`]: 0,
641
+ [`${homeTeamKey}_13_0`]: 0,
642
+ [`${homeTeamKey}_13_1`]: 0,
643
+ [`${homeTeamKey}_13_2`]: 0,
644
+ [`${homeTeamKey}_13_3`]: 0,
645
+ [`${homeTeamKey}_13_4`]: 0,
646
+ [`${homeTeamKey}_13_5`]: 0,
647
+ [`${homeTeamKey}_13_6`]: 0,
648
+ [`${homeTeamKey}_13_7`]: 0,
649
+ [`${homeTeamKey}_13_8`]: 0,
650
+ [`${homeTeamKey}_13_9`]: 0,
651
+ [`${homeTeamKey}_13_10`]: 0,
652
+ [`${homeTeamKey}_13_11`]: 0,
653
+ [`${homeTeamKey}_13_12`]: 0,
654
+ [`${homeTeamKey}_14_0`]: 0,
655
+ [`${homeTeamKey}_14_1`]: 0,
656
+ [`${homeTeamKey}_14_2`]: 0,
657
+ [`${homeTeamKey}_14_3`]: 0,
658
+ [`${homeTeamKey}_14_4`]: 0,
659
+ [`${homeTeamKey}_14_5`]: 0,
660
+ [`${homeTeamKey}_14_6`]: 0,
661
+ [`${homeTeamKey}_14_7`]: 0,
662
+ [`${homeTeamKey}_14_8`]: 0,
663
+ [`${homeTeamKey}_14_9`]: 0,
664
+ [`${homeTeamKey}_14_10`]: 0,
665
+ [`${homeTeamKey}_14_11`]: 0,
666
+ [`${homeTeamKey}_14_12`]: 0,
667
+ [`${homeTeamKey}_14_13`]: 0,
668
+ [`${awayTeamKey}_5_0`]: 0,
669
+ [`${awayTeamKey}_5_1`]: 0,
670
+ [`${awayTeamKey}_5_2`]: 0,
671
+ [`${awayTeamKey}_5_3`]: 0,
672
+ [`${awayTeamKey}_5_4`]: 0,
673
+ [`${awayTeamKey}_6_0`]: 0,
674
+ [`${awayTeamKey}_6_1`]: 0,
675
+ [`${awayTeamKey}_6_2`]: 0,
676
+ [`${awayTeamKey}_6_3`]: 0,
677
+ [`${awayTeamKey}_6_4`]: 0,
678
+ [`${awayTeamKey}_6_5`]: 0,
679
+ [`${awayTeamKey}_7_0`]: 0,
680
+ [`${awayTeamKey}_7_1`]: 0,
681
+ [`${awayTeamKey}_7_2`]: 0,
682
+ [`${awayTeamKey}_7_3`]: 0,
683
+ [`${awayTeamKey}_7_4`]: 0,
684
+ [`${awayTeamKey}_7_5`]: 0,
685
+ [`${awayTeamKey}_7_6`]: 0,
686
+ [`${awayTeamKey}_8_0`]: 0,
687
+ [`${awayTeamKey}_8_1`]: 0,
688
+ [`${awayTeamKey}_8_2`]: 0,
689
+ [`${awayTeamKey}_8_3`]: 0,
690
+ [`${awayTeamKey}_8_4`]: 0,
691
+ [`${awayTeamKey}_8_5`]: 0,
692
+ [`${awayTeamKey}_8_6`]: 0,
693
+ [`${awayTeamKey}_8_7`]: 0,
694
+ [`${awayTeamKey}_9_0`]: 0,
695
+ [`${awayTeamKey}_9_1`]: 0,
696
+ [`${awayTeamKey}_9_2`]: 0,
697
+ [`${awayTeamKey}_9_3`]: 0,
698
+ [`${awayTeamKey}_9_4`]: 0,
699
+ [`${awayTeamKey}_9_5`]: 0,
700
+ [`${awayTeamKey}_9_6`]: 0,
701
+ [`${awayTeamKey}_9_7`]: 0,
702
+ [`${awayTeamKey}_9_8`]: 0,
703
+ [`${awayTeamKey}_10_0`]: 0,
704
+ [`${awayTeamKey}_10_1`]: 0,
705
+ [`${awayTeamKey}_10_2`]: 0,
706
+ [`${awayTeamKey}_10_3`]: 0,
707
+ [`${awayTeamKey}_10_4`]: 0,
708
+ [`${awayTeamKey}_10_5`]: 0,
709
+ [`${awayTeamKey}_10_6`]: 0,
710
+ [`${awayTeamKey}_10_7`]: 0,
711
+ [`${awayTeamKey}_10_8`]: 0,
712
+ [`${awayTeamKey}_10_9`]: 0,
713
+ [`${awayTeamKey}_11_0`]: 0,
714
+ [`${awayTeamKey}_11_1`]: 0,
715
+ [`${awayTeamKey}_11_2`]: 0,
716
+ [`${awayTeamKey}_11_3`]: 0,
717
+ [`${awayTeamKey}_11_4`]: 0,
718
+ [`${awayTeamKey}_11_5`]: 0,
719
+ [`${awayTeamKey}_11_6`]: 0,
720
+ [`${awayTeamKey}_11_7`]: 0,
721
+ [`${awayTeamKey}_11_8`]: 0,
722
+ [`${awayTeamKey}_11_9`]: 0,
723
+ [`${awayTeamKey}_11_10`]: 0,
724
+ [`${awayTeamKey}_12_0`]: 0,
725
+ [`${awayTeamKey}_12_1`]: 0,
726
+ [`${awayTeamKey}_12_2`]: 0,
727
+ [`${awayTeamKey}_12_3`]: 0,
728
+ [`${awayTeamKey}_12_4`]: 0,
729
+ [`${awayTeamKey}_12_5`]: 0,
730
+ [`${awayTeamKey}_12_6`]: 0,
731
+ [`${awayTeamKey}_12_7`]: 0,
732
+ [`${awayTeamKey}_12_8`]: 0,
733
+ [`${awayTeamKey}_12_9`]: 0,
734
+ [`${awayTeamKey}_12_10`]: 0,
735
+ [`${awayTeamKey}_12_11`]: 0,
736
+ [`${awayTeamKey}_13_0`]: 0,
737
+ [`${awayTeamKey}_13_1`]: 0,
738
+ [`${awayTeamKey}_13_2`]: 0,
739
+ [`${awayTeamKey}_13_3`]: 0,
740
+ [`${awayTeamKey}_13_4`]: 0,
741
+ [`${awayTeamKey}_13_5`]: 0,
742
+ [`${awayTeamKey}_13_6`]: 0,
743
+ [`${awayTeamKey}_13_7`]: 0,
744
+ [`${awayTeamKey}_13_8`]: 0,
745
+ [`${awayTeamKey}_13_9`]: 0,
746
+ [`${awayTeamKey}_13_10`]: 0,
747
+ [`${awayTeamKey}_13_11`]: 0,
748
+ [`${awayTeamKey}_13_12`]: 0,
749
+ [`${awayTeamKey}_14_0`]: 0,
750
+ [`${awayTeamKey}_14_1`]: 0,
751
+ [`${awayTeamKey}_14_2`]: 0,
752
+ [`${awayTeamKey}_14_3`]: 0,
753
+ [`${awayTeamKey}_14_4`]: 0,
754
+ [`${awayTeamKey}_14_5`]: 0,
755
+ [`${awayTeamKey}_14_6`]: 0,
756
+ [`${awayTeamKey}_14_7`]: 0,
757
+ [`${awayTeamKey}_14_8`]: 0,
758
+ [`${awayTeamKey}_14_9`]: 0,
759
+ [`${awayTeamKey}_14_10`]: 0,
760
+ [`${awayTeamKey}_14_11`]: 0,
761
+ [`${awayTeamKey}_14_12`]: 0,
762
+ [`${awayTeamKey}_14_13`]: 0,
763
+ other: 0,
764
+ };
765
+
766
+ // Populate the oddsMap with the odds from the provided array
767
+ oddsArray.forEach((odd) => {
768
+ const normalizedSelection = `${odd.selection.toLowerCase().replace(/\s+/g, '_')}_${odd.selectionLine.replace(
769
+ ':',
770
+ '_'
771
+ )}`;
772
+ if (oddsMap.hasOwnProperty(normalizedSelection)) {
773
+ oddsMap[normalizedSelection] = odd.price;
774
+ }
775
+ });
776
+
777
+ const allOddsAreZero = Object.values(oddsMap).every((odd) => odd === ZERO);
778
+
779
+ if (allOddsAreZero) {
780
+ return [];
781
+ }
782
+
783
+ const positionNames = Object.keys(oddsMap);
784
+
785
+ // Create the market object
786
+ const marketObject = {
787
+ homeTeam: commonData.homeTeam,
788
+ awayTeam: commonData.awayTeam,
789
+ line: 0,
790
+ positionNames: positionNames,
791
+ odds: Object.values(oddsMap),
792
+ type: oddsArray.length > 0 && oddsArray[0].type ? oddsArray[0].type : 'Correct Score',
793
+ typeId: oddsArray.length > 0 && oddsArray[0].typeId ? oddsArray[0].typeId : 10100,
794
+ };
795
+ return [marketObject];
796
+ };
797
+
798
+ /**
799
+ * Groups double chance odds by their lines and formats the result.
800
+ *
801
+ * @param {Array} oddsArray - The input array of odds objects.
802
+ * @param {Object} commonData - The common data object containing homeTeam and awayTeam information.
803
+ * @returns {Array} The grouped and formatted correct score odds.
804
+ */
805
+ export const groupAndFormatDoubleChanceOdds = (oddsArray: any[], commonData: HomeAwayTeams) => {
806
+ let probability1X = 0;
807
+ let probability12 = 0;
808
+ let probabilityX2 = 0;
809
+
810
+ oddsArray.forEach((odd) => {
811
+ if (odd.selection.includes(commonData.homeTeam)) {
812
+ if (odd.selection.includes(commonData.awayTeam)) {
813
+ probability12 = odd.price;
814
+ } else {
815
+ probability1X = odd.price;
816
+ }
817
+ } else if (odd.selection.includes(commonData.awayTeam)) {
818
+ probabilityX2 = odd.price;
819
+ }
820
+ });
821
+
822
+ if ([probability1X, probability12, probabilityX2].every((odd) => odd === ZERO)) {
823
+ return [];
824
+ }
825
+ // Create the market object
826
+ const marketObject = {
827
+ homeTeam: commonData.homeTeam,
828
+ awayTeam: commonData.awayTeam,
829
+ line: 0,
830
+ odds: [probability1X, probability12, probabilityX2],
831
+ type: 'Double Chance',
832
+ typeId: 10003,
833
+ };
834
+ return [marketObject];
835
+ };
836
+
837
+ // used for home/away markets
838
+ export const adjustSpreadOnChildOdds = (
839
+ iterableGroupedOdds: any[],
840
+ spreadDataForSport: any,
841
+ defaultSpreadForLiveMarkets: any
842
+ ) => {
843
+ const result: any[] = [];
844
+ iterableGroupedOdds.forEach((data) => {
845
+ const hasDrawOdds = data.odds.length === 3;
846
+ const homeTeamOdds = convertOddsToImpl(data.odds[0]) || ZERO;
847
+ const awayTeamOdds = convertOddsToImpl(data.odds[1]) || ZERO;
848
+ const drawOdds = convertOddsToImpl(data.odds[2]) || ZERO;
849
+ const odds = hasDrawOdds ? [homeTeamOdds, awayTeamOdds, drawOdds] : [homeTeamOdds, awayTeamOdds];
850
+
851
+ const isZeroOddsChild = homeTeamOdds === ZERO || awayTeamOdds === ZERO || (hasDrawOdds && drawOdds === ZERO);
852
+ if (!isZeroOddsChild) {
853
+ const spreadData = getSpreadData(
854
+ spreadDataForSport,
855
+ data.sportId,
856
+ data.typeId,
857
+ defaultSpreadForLiveMarkets
858
+ );
859
+
860
+ let adjustedOdds;
861
+ if (spreadData !== null) {
862
+ adjustedOdds = adjustSpreadOnOdds(odds, spreadData.minSpread, spreadData.targetSpread);
863
+ } else {
864
+ adjustedOdds = adjustSpreadOnOdds(odds, defaultSpreadForLiveMarkets, 0);
865
+ }
866
+
867
+ result.push({
868
+ ...data,
869
+ odds: adjustedOdds,
870
+ });
871
+ }
872
+ });
873
+ return result;
874
+ };