Games.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. import GetTranslation from "../libs/getTranslation.js";
  2. import { getSolutionsWithRelations, getGamesRelationsMap } from "../libs/getGamesRelations.js";
  3. import Store from "../state/store.js";
  4. import { getPlatformIorsDetailInfo, getSolutionByLatestIors, getSoulutionBetResult } from "./Markets.js";
  5. export const getLeagues = async () => {
  6. const { polymarket, pinnacle } = Store.get('leagues') ?? { polymarket: [], pinnacle: [] };
  7. const polymarketNames = polymarket.map(item => item.name);
  8. const translatedNames = await GetTranslation(polymarketNames);
  9. const newPolymarket = polymarket.map(item => {
  10. const { name } = item;
  11. const localesName = translatedNames[name] ?? name;
  12. return { ...item, localesName };
  13. });
  14. return { polymarket: newPolymarket, pinnacle };
  15. }
  16. export const setLeaguesRelation = async (relation) => {
  17. const { id, platforms } = relation;
  18. if (!id || !platforms) {
  19. return Promise.reject(new Error('invalid request', { cause: 400 }));
  20. }
  21. const storeRelations = Store.get('leaguesRelations') ?? {};
  22. if (storeRelations[id]) {
  23. return Promise.reject(new Error('relation already exists', { cause: 400 }));
  24. }
  25. storeRelations[id] = relation;
  26. Store.set('leaguesRelations', storeRelations);
  27. return Promise.resolve();
  28. };
  29. export const removeLeaguesRelation = async (id) => {
  30. if (!id) {
  31. return Promise.reject(new Error('invalid request', { cause: 400 }));
  32. }
  33. const storeRelations = Store.get('leaguesRelations') ?? {};
  34. if (!storeRelations[id]) {
  35. return Promise.reject(new Error('relation not found', { cause: 400 }));
  36. }
  37. delete storeRelations[id];
  38. Store.set('leaguesRelations', storeRelations);
  39. return Promise.resolve();
  40. };
  41. export const getLeaguesRelations = async () => {
  42. const storeRelations = Object.values(Store.get('leaguesRelations') ?? {});
  43. const polymarketNames = storeRelations.map(item => item.platforms.polymarket.name);
  44. const translatedNames = await GetTranslation(polymarketNames);
  45. const newRelations = storeRelations.map(item => {
  46. const { platforms: { polymarket, pinnacle } } = item;
  47. const { name } = polymarket;
  48. const localesName = translatedNames[name] ?? name;
  49. return { ...item, platforms: { polymarket: { ...polymarket, localesName }, pinnacle } };
  50. });
  51. return Promise.resolve(newRelations);
  52. };
  53. export const getGames = async () => {
  54. const { polymarket, pinnacle } = Store.get('games') ?? { polymarket: [], pinnacle: [] };
  55. const polymarketNames = [ ...new Set(polymarket.map(item => [item.teamHomeName, item.teamAwayName, item.leagueName]).flat()) ];
  56. const translatedNames = await GetTranslation(polymarketNames);
  57. const newPolymarket = polymarket.map(item => {
  58. const { leagueName, teamHomeName, teamAwayName } = item;
  59. const localesTeamHomeName = translatedNames[teamHomeName] ?? teamHomeName;
  60. const localesTeamAwayName = translatedNames[teamAwayName] ?? teamAwayName;
  61. const localesLeagueName = translatedNames[leagueName] ?? leagueName;
  62. return { ...item, localesTeamHomeName, localesTeamAwayName, localesLeagueName };
  63. }).filter(item => {
  64. const { timestamp } = item;
  65. const now = Date.now();
  66. return (timestamp + 1000 * 60 * 60 * 2) > now;
  67. }).sort((a, b) => a.timestamp - b.timestamp);
  68. return { polymarket: newPolymarket, pinnacle };
  69. }
  70. export const setGamesRelation = async (relation) => {
  71. const { id, platforms, timestamp } = relation;
  72. if (!id || !platforms || !timestamp) {
  73. return Promise.reject(new Error('invalid request', { cause: 400 }));
  74. }
  75. const storeRelations = Store.get('gamesRelations') ?? {};
  76. if (storeRelations[id]) {
  77. return Promise.reject(new Error('relation already exists', { cause: 400 }));
  78. }
  79. storeRelations[id] = relation;
  80. Store.set('gamesRelations', storeRelations);
  81. return Promise.resolve();
  82. }
  83. export const removeGamesRelation = async (id) => {
  84. if (!id) {
  85. return Promise.reject(new Error('invalid request', { cause: 400 }));
  86. }
  87. const storeRelations = Store.get('gamesRelations') ?? {};
  88. if (!storeRelations[id]) {
  89. return Promise.reject(new Error('relation not found', { cause: 400 }));
  90. }
  91. delete storeRelations[id];
  92. Store.set('gamesRelations', storeRelations);
  93. return Promise.resolve();
  94. }
  95. export const getGamesRelations = async () => {
  96. // const storeData = Store.get('gamesRelations') ?? {};
  97. // console.log('get games relations', storeData);
  98. // const storeRelations = Object.values(storeData);
  99. // console.log('store relations', storeRelations);
  100. // const polymarketNames = [ ...new Set(storeRelations.map(item => {
  101. // const { teamHomeName, teamAwayName, leagueName } = item.platforms.polymarket;
  102. // return [teamHomeName, teamAwayName, leagueName];
  103. // }).flat()) ];
  104. // const translatedNames = await GetTranslation(polymarketNames);
  105. // const newRelations = storeRelations.map(item => {
  106. // const { platforms: { polymarket, pinnacle } } = item;
  107. // const { teamHomeName, teamAwayName, leagueName } = polymarket;
  108. // const localesTeamHomeName = translatedNames[teamHomeName] ?? teamHomeName;
  109. // const localesTeamAwayName = translatedNames[teamAwayName] ?? teamAwayName;
  110. // const localesLeagueName = translatedNames[leagueName] ?? leagueName;
  111. // return { ...item, platforms: { polymarket: { ...polymarket, localesTeamHomeName, localesTeamAwayName, localesLeagueName }, pinnacle } };
  112. // }).sort((a, b) => a.timestamp - b.timestamp);
  113. // const newRelations = storeRelations.map(item => {
  114. // return { ...item };
  115. // }).sort((a, b) => a.timestamp - b.timestamp);
  116. const gamesRelations = Object.values(getGamesRelationsMap()).sort((a, b) => a.timestamp - b.timestamp);
  117. return Promise.resolve(gamesRelations);
  118. }
  119. /**
  120. * 获取解决方案
  121. * @param {*} param0
  122. * @returns
  123. */
  124. export const getSolutions = async ({ min_profit_rate = 0 } = {}) => {
  125. const solutions = Store.get('solutions') ?? [];
  126. const solutionsList = solutions.filter(solution => {
  127. const { sol: { win_profit_rate } } = solution;
  128. return win_profit_rate >= min_profit_rate;
  129. }).sort((a, b) => {
  130. return b.sol.win_profit_rate - a.sol.win_profit_rate;
  131. });
  132. return getSolutionsWithRelations(solutionsList, 5);
  133. }
  134. /**
  135. * 获取策略对应的盘口信息
  136. */
  137. export const getSolutionIorsInfo = async (sid) => {
  138. const solution = Store.get('solutions')?.find(item => item.sid == sid);
  139. if (!solution) {
  140. return Promise.reject(new Error('solution not found', { cause: 400 }));
  141. }
  142. const { info: { id }, cpr, sol: { cross_type } } = solution;
  143. const gamesRelations = Store.get('gamesRelations') ?? {};
  144. const gameRelation = gamesRelations[id];
  145. if (!gameRelation) {
  146. return Promise.reject(new Error('game relation not found', { cause: 400 }));
  147. }
  148. const { platforms: { polymarket, pinnacle } } = gameRelation;
  149. const idMap = { polymarket: polymarket.id, pinnacle: pinnacle.id };
  150. const iorsInfo = await Promise.all(cpr.map(item => {
  151. const { k, p } = item;
  152. return getPlatformIorsDetailInfo(k, p, idMap[p]);
  153. }));
  154. return { cpr, iorsInfo, cross_type };
  155. // const accountBalance = await getAccountBalance();
  156. // const solutionInfo = getSolutionByLatestIors(iorsInfo, cross_type);
  157. // return { cpr, iorsInfo, ...solutionInfo };
  158. }
  159. /**
  160. * 根据策略下注
  161. */
  162. export const betSolution = async (sid, stake=0) => {
  163. const solutionIorsInfo = await getSolutionIorsInfo(sid);
  164. const { iorsInfo, cross_type } = solutionIorsInfo;
  165. const solutionInfo = getSolutionByLatestIors(iorsInfo, cross_type);
  166. return solutionInfo;
  167. if (solutionInfo?.error) {
  168. const error = new Error(solutionInfo.error, { cause: 400 });
  169. error.data = solutionInfo.data;
  170. return Promise.reject(error);
  171. }
  172. const betResult = await getSoulutionBetResult({ ...solutionIorsInfo, ...solutionInfo, stake });
  173. return { betResult, ...solutionIorsInfo, ...solutionInfo };
  174. }
  175. /**
  176. * 清理过期关系
  177. */
  178. const cleanGamesRelations = () => {
  179. const now = Date.now();
  180. const storeRelations = Store.get('gamesRelations') ?? [];
  181. Object.keys(storeRelations).forEach(key => {
  182. const relation = storeRelations[key];
  183. const { timestamp } = relation;
  184. if ((timestamp + 1000 * 60 * 60 * 2) < now) {
  185. delete storeRelations[key];
  186. }
  187. });
  188. Store.set('gamesRelations', storeRelations);
  189. }
  190. setInterval(cleanGamesRelations, 1000 * 60);
  191. export default {
  192. getLeagues, setLeaguesRelation, removeLeaguesRelation, getLeaguesRelations,
  193. getGames, setGamesRelation, removeGamesRelation, getGamesRelations,
  194. getSolutions, getSolutionIorsInfo, betSolution,
  195. };