Platforms.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. import { fork } from "child_process";
  2. import Store from "../state/store.js";
  3. import ProcessData from "../libs/processData.js";
  4. import Logs from "../libs/logs.js";
  5. import { getSolutionsWithRelations } from "../libs/getSolutions.js";
  6. import { updateSolutions } from "./Partner.js";
  7. // import { getPlatformIorInfo } from "./Markets.js";
  8. const getChildOptions = (inspect=9230) => {
  9. return process.env.NODE_ENV == 'development' ? {
  10. execArgv: [`--inspect=${inspect}`],
  11. stdio: ['pipe', 'pipe', 'pipe', 'ipc']
  12. } : {
  13. stdio: ['pipe', 'pipe', 'pipe', 'ipc']
  14. };
  15. }
  16. const triangleProcess = fork("triangle/main.js", [], getChildOptions(9229));
  17. const triangleData = new ProcessData(triangleProcess, 'triangle');
  18. triangleData.registerResponse('gamesRelations', async () => {
  19. const gamesRelations = Object.values(Store.get('gamesRelations') ?? {});
  20. const polymarketOdds = Store.get('polymarket', 'odds') ?? {};
  21. const pinnacleOdds = Store.get('pinnacle', 'odds') ?? {};
  22. const expireTime = Date.now() - 1000 * 15;
  23. const { games: polymarketGames = [], timestamp: polymarketTimestamp = 0 } = polymarketOdds;
  24. const { games: pinnacleGames = [], timestamp: pinnacleTimestamp = 0 } = pinnacleOdds;
  25. const polymarketOddsMap = polymarketTimestamp > expireTime ? new Map(polymarketGames.map(item => [item.id, item])) : new Map();
  26. const pinnacleOddsMap = pinnacleTimestamp > expireTime ? new Map(pinnacleGames.map(item => [item.id, item])) : new Map();
  27. const newRelations = gamesRelations.map(relation => {
  28. const { platforms: { polymarket, pinnacle }, ...rest } = relation;
  29. const polymarketId = polymarket.id;
  30. const pinnacleId = pinnacle.id;
  31. const polymarketOdds = polymarketOddsMap.get(polymarketId)?.odds;
  32. const pinnacleOdds = pinnacleOddsMap.get(pinnacleId)?.odds;
  33. return { ...rest, platforms: {
  34. polymarket: { ...polymarket, odds: polymarketOdds, evtime: polymarketTimestamp },
  35. pinnacle: { ...pinnacle, odds: pinnacleOdds, evtime: pinnacleTimestamp },
  36. }};
  37. });
  38. return Promise.resolve(newRelations);
  39. });
  40. triangleData.registerRequest('solutions', solutions => {
  41. const oldSolutions = new Map((Store.get('solutions') ?? []).map(item => [item.sid, item]));
  42. const newSolutions = new Map(solutions.map(item => [item.sid, item]));
  43. const changed = {
  44. add: [],
  45. update: [],
  46. remove: [],
  47. }
  48. oldSolutions.forEach((item, sid) => {
  49. if (!newSolutions.has(sid)) {
  50. changed.remove.push(sid);
  51. }
  52. else if (newSolutions.get(sid).sol.win_profit_rate != item.sol.win_profit_rate || JSON.stringify(newSolutions.get(sid).cpr) != JSON.stringify(item.cpr)) {
  53. changed.update.push(sid);
  54. }
  55. });
  56. newSolutions.forEach((item, sid) => {
  57. if (!oldSolutions.has(sid)) {
  58. changed.add.push(sid);
  59. }
  60. });
  61. if (changed.update.length || changed.add.length || changed.remove.length) {
  62. Store.set('solutions', solutions);
  63. const gamesRelations = Store.get('gamesRelations') ?? {};
  64. getSolutionsWithRelations(solutions, gamesRelations, 5)
  65. .then(solutionsList => {
  66. Logs.outDev('get solutions with relations', solutionsList);
  67. return updateSolutions(solutionsList)
  68. })
  69. .then(res => {
  70. Logs.outDev('update solutions res', res);
  71. })
  72. .catch(error => {
  73. Logs.err('get and update solutions error', error);
  74. });
  75. }
  76. });
  77. /**
  78. * 通用的平台数据更新函数
  79. * @param {string} platform - 平台名称
  80. * @param {Array} newItems - 新的数据项数组
  81. * @param {string} storeKey - Store 中的键名
  82. * @returns {Promise}
  83. */
  84. const updatePlatformData = async ({ platform, newItems, storeKey }) => {
  85. if (!platform || !newItems?.length) {
  86. return Promise.reject(new Error('invalid request', { cause: 400 }));
  87. }
  88. let changed = false;
  89. const storeData = Store.get(storeKey) ?? {};
  90. const { [platform]: storePlatformItems = [] } = storeData;
  91. const storePlatformItemsMap = new Map(storePlatformItems.map(item => [item.id, item]));
  92. const newPlatformItemsMap = new Map(newItems.map(item => [item.id, item]));
  93. // 删除不存在的项
  94. storePlatformItemsMap.forEach(item => {
  95. if (!newPlatformItemsMap.has(item.id)) {
  96. storePlatformItemsMap.delete(item.id);
  97. changed = true;
  98. }
  99. });
  100. // 添加新的项
  101. newPlatformItemsMap.forEach(item => {
  102. if (!storePlatformItemsMap.has(item.id)) {
  103. storePlatformItemsMap.set(item.id, item);
  104. changed = true;
  105. }
  106. });
  107. // 更新 Store 中的数据
  108. if (changed) {
  109. const updatedPlatformItems = Array.from(storePlatformItemsMap.values());
  110. storeData[platform] = updatedPlatformItems;
  111. Store.set(storeKey, storeData);
  112. }
  113. return Promise.resolve();
  114. };
  115. /**
  116. * 更新联赛数据
  117. * @param {string} platform - 平台名称
  118. * @param {Array} leagues - 联赛数据
  119. * @returns
  120. */
  121. export const updateLeagues = async ({ platform, leagues }) => {
  122. return updatePlatformData({ platform, newItems: leagues, storeKey: 'leagues' });
  123. };
  124. /**
  125. * 获取过滤后的联赛数据
  126. * @param {string} platform - 平台名称
  127. * @returns
  128. */
  129. export const getRelatedLeagues = async (platform) => {
  130. const polymarketLeagues = Store.get('polymarket', 'leagues') ?? [];
  131. const polymarketLeaguesSet = new Set(polymarketLeagues.map(item => item.id));
  132. const leaguesRelations = Store.get('leaguesRelations') ?? {};
  133. const filteredLeagues = Object.values(leaguesRelations).filter(relation => {
  134. return polymarketLeaguesSet.has(relation.platforms.polymarket.id);
  135. }).map(relation => relation.platforms[platform]);
  136. return filteredLeagues;
  137. }
  138. /**
  139. * 更新比赛数据
  140. * @param {string} platform - 平台名称
  141. * @param {Array} games - 比赛数据
  142. * @returns
  143. */
  144. export const updateGames = async ({ platform, games }) => {
  145. return updatePlatformData({ platform, newItems: games, storeKey: 'games' });
  146. };
  147. /**
  148. * 获取过滤后的比赛数据
  149. * @param {string} platform - 平台名称
  150. * @returns
  151. */
  152. export const getRelatedGames = async (platform) => {
  153. const gamesRelations = Store.get('gamesRelations') ?? {};
  154. const filteredGames = Object.values(gamesRelations).map(relation => relation.platforms[platform]);
  155. return filteredGames;
  156. }
  157. /**
  158. * 更新赔率数据
  159. * @param {string} platform - 平台名称
  160. * @param {Array} games - 赔率数据
  161. * @param {number} timestamp - 时间戳
  162. * @returns
  163. */
  164. export const updateOdds = async ({ platform, games, timestamp }) => {
  165. Store.set(platform, { games, timestamp }, 'odds');
  166. return Promise.resolve();
  167. };
  168. export default {
  169. updateLeagues, getRelatedLeagues,
  170. updateGames, getRelatedGames,
  171. updateOdds,
  172. };