getGamesRelations.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import Store from "../state/store.js";
  2. const getGameData = (game, hasOdds=false) => {
  3. if (!game) {
  4. return null;
  5. }
  6. const { id, leagueId, leagueName, teamHomeName, teamAwayName, timestamp, odds, evtime } = game;
  7. const gameData = { id, leagueId, leagueName, teamHomeName, teamAwayName, timestamp };
  8. if (hasOdds) {
  9. gameData.odds = odds;
  10. gameData.evtime = evtime;
  11. }
  12. return gameData;
  13. }
  14. export const getGamesRelationsMap = (hasOdds=false) => {
  15. const gamesRelations = Store.get('gamesRelations') ?? {};
  16. const pmOdds = Store.get('polymarket', 'odds')?.games ?? [];
  17. const pmEvtime = Store.get('polymarket', 'odds')?.timestamp ?? 0;
  18. const pcOdds = Store.get('pinnacle', 'odds')?.games ?? [];
  19. const hgOdds = Store.get('huangguan', 'odds')?.games ?? [];
  20. const obOdds = Store.get('obsports', 'odds')?.games ?? [];
  21. const pmOddsMap = new Map(pmOdds.map(item => [item.id, item]));
  22. const pcOddsMap = new Map(pcOdds.map(item => [item.id, item]));
  23. const hgOddsMap = new Map(hgOdds.map(item => [item.id, item]));
  24. const obOddsMap = new Map(obOdds.map(item => [item.id, item]));
  25. const relationsMap = {};
  26. Object.entries(gamesRelations).forEach(([id, relation]) => {
  27. const { platforms } = relation;
  28. const { polymarket, pinnacle, huangguan, obsports } = platforms;
  29. const { id: pmId = 0 } = polymarket ?? {};
  30. const { id: pcId = 0 } = pinnacle ?? {};
  31. const { id: hgId = 0 } = huangguan ?? {};
  32. const { id: obId = 0 } = obsports ?? {};
  33. const pmGame = getGameData(pmOddsMap.get(pmId), hasOdds) ?? {};
  34. const pcGame = getGameData(pcOddsMap.get(pcId), hasOdds) ?? {};
  35. const hgGame = getGameData(hgOddsMap.get(hgId), hasOdds) ?? {};
  36. const obGame = getGameData(obOddsMap.get(obId), hasOdds) ?? {};
  37. relationsMap[id] = { ...relation, platforms: {
  38. polymarket: { ...polymarket, ...pmGame, evtime: pmEvtime },
  39. pinnacle: { ...pinnacle, ...pcGame },
  40. huangguan: { ...huangguan, ...hgGame },
  41. obsports: { ...obsports, ...obGame },
  42. }};
  43. });
  44. return relationsMap;
  45. }
  46. export const getSolutionsWithRelations = async (solutionsList, maxLength=0) => {
  47. const gamesRelations = getGamesRelationsMap(true);
  48. const selectedRelations = {};
  49. solutionsList.forEach(solution => {
  50. const rid = solution.rid;
  51. if (!gamesRelations[rid]) {
  52. return;
  53. }
  54. if (!selectedRelations[rid]) {
  55. selectedRelations[rid] = { ...gamesRelations[rid] };
  56. }
  57. if (!selectedRelations[rid]['solutions']) {
  58. selectedRelations[rid]['solutions'] = [];
  59. }
  60. if (maxLength > 0 && selectedRelations[rid]['solutions'].length >= maxLength) {
  61. return;
  62. }
  63. selectedRelations[rid]['solutions'].push(solution);
  64. });
  65. const relationsList = Object.values(selectedRelations).sort((a, b) => {
  66. return b.solutions[0].sol.win_profit_rate - a.solutions[0].sol.win_profit_rate;
  67. });
  68. return Promise.resolve(relationsList);
  69. }