import Store from "../state/store.js"; const getGameData = (game, hasOdds=false) => { if (!game) { return null; } const { id, leagueId, leagueName, teamHomeName, teamAwayName, timestamp, odds, evtime } = game; const gameData = { id, leagueId, leagueName, teamHomeName, teamAwayName, timestamp }; if (hasOdds) { gameData.odds = odds; gameData.evtime = evtime; } return gameData; } export const getGamesRelationsMap = (hasOdds=false) => { const gamesRelations = Store.get('gamesRelations') ?? {}; const pmOdds = Store.get('polymarket', 'odds')?.games ?? []; const pmEvtime = Store.get('polymarket', 'odds')?.timestamp ?? 0; const pcOdds = Store.get('pinnacle', 'odds')?.games ?? []; const hgOdds = Store.get('huanguan', 'odds')?.games ?? []; const obOdds = Store.get('obsports', 'odds')?.games ?? []; const pmOddsMap = new Map(pmOdds.map(item => [item.id, item])); const pcOddsMap = new Map(pcOdds.map(item => [item.id, item])); const hgOddsMap = new Map(hgOdds.map(item => [item.id, item])); const obOddsMap = new Map(obOdds.map(item => [item.id, item])); const relationsMap = {}; Object.entries(gamesRelations).forEach(([id, relation]) => { const { platforms } = relation; const { polymarket, pinnacle, huanguan, obsports } = platforms; const { id: pmId = 0 } = polymarket; const { id: pcId = 0 } = pinnacle; const { id: hgId = 0 } = huanguan; const { id: obId = 0 } = obsports; const pmGame = getGameData(pmOddsMap.get(pmId), hasOdds) ?? {}; const pcGame = getGameData(pcOddsMap.get(pcId), hasOdds) ?? {}; const hgGame = getGameData(hgOddsMap.get(hgId), hasOdds) ?? {}; const obGame = getGameData(obOddsMap.get(obId), hasOdds) ?? {}; relationsMap[id] = { ...relation, platforms: { polymarket: { ...polymarket, ...pmGame, evtime: pmEvtime }, pinnacle: { ...pinnacle, ...pcGame }, huanguan: { ...huanguan, ...hgGame }, obsports: { ...obsports, ...obGame }, }}; }); return relationsMap; } export const getSolutionsWithRelations = async (solutionsList, maxLength=0) => { const gamesRelations = getGamesRelationsMap(true); const selectedRelations = {}; solutionsList.forEach(solution => { const rid = solution.rid; if (!gamesRelations[rid]) { return; } if (!selectedRelations[rid]) { selectedRelations[rid] = { ...gamesRelations[rid] }; } if (!selectedRelations[rid]['solutions']) { selectedRelations[rid]['solutions'] = []; } if (maxLength > 0 && selectedRelations[rid]['solutions'].length >= maxLength) { return; } selectedRelations[rid]['solutions'].push(solution); }); const relationsList = Object.values(selectedRelations).sort((a, b) => { return b.solutions[0].sol.win_profit_rate - a.solutions[0].sol.win_profit_rate; }); return Promise.resolve(relationsList); }