eventsMatch.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. const Logs = require('../libs/logs');
  2. const eventsCombination = require('./trangleCalc');
  3. const Request = {
  4. callbacks: {},
  5. count: 0,
  6. }
  7. // const WIN_STEP = 15;
  8. // const SOL_FREEZ_TIME = 1000 * 30;
  9. const GLOBAL_DATA = {
  10. relationLength: 0,
  11. };
  12. const getDataFromParent = (type, callback) => {
  13. const id = ++Request.count;
  14. Request.callbacks[id] = callback;
  15. process.send({ method: 'get', id, type });
  16. }
  17. const postDataToParent = (type, data) => {
  18. process.send({ method: 'post', type, data });
  19. }
  20. process.on('message', (message) => {
  21. const { callbacks } = Request;
  22. const { method, id, type, data } = message;
  23. if (method == 'get' && id) {
  24. let responseData = null;
  25. // if (data == 'getSolution') {
  26. // responseData = Solution.get();
  27. // }
  28. process.send({ type: 'response', id, data: responseData });
  29. }
  30. else if (type == 'response' && id && callbacks[id]) {
  31. callbacks[id](data);
  32. delete callbacks[id];
  33. }
  34. });
  35. /**
  36. * 精确浮点数字
  37. * @param {number} number
  38. * @param {number} x
  39. * @returns {number}
  40. */
  41. const fixFloat = (number, x=2) => {
  42. return parseFloat(number.toFixed(x));
  43. }
  44. const getGamesRelation = () => {
  45. return new Promise(resolve => {
  46. getDataFromParent('getGamesRelation', (relations) => {
  47. resolve(relations);
  48. });
  49. });
  50. }
  51. const getSolutionHistory = () => {
  52. return new Promise(resolve => {
  53. getDataFromParent('getSolutionHistory', (solutions) => {
  54. resolve(solutions);
  55. });
  56. });
  57. }
  58. const setSolution = (solutions) => {
  59. postDataToParent('setSolution', solutions);
  60. }
  61. const extractOdds = ({ evtime, events, sptime, special }) => {
  62. const expireTime = Date.now() - 15000;
  63. let odds = {};
  64. if (evtime > expireTime) {
  65. odds = { ...odds, ...events };
  66. }
  67. if (sptime > expireTime) {
  68. odds = { ...odds, ...special };
  69. }
  70. return odds;
  71. }
  72. // const getOptimalOdds = (oddsMap) => {
  73. // const oddsInfo = {};
  74. // Object.keys(oddsMap).forEach(platform => {
  75. // const odds = oddsMap[platform];
  76. // Object.keys(odds).forEach(ior => {
  77. // const oddsValue = odds[ior];
  78. // if (!oddsInfo[ior] || oddsInfo[ior]?.v < oddsValue) {
  79. // oddsInfo[ior] = {
  80. // p: platform,
  81. // v: oddsValue
  82. // }
  83. // }
  84. // });
  85. // });
  86. // return oddsInfo;
  87. // }
  88. const eventMatch = () => {
  89. getGamesRelation()
  90. .then(relations => {
  91. const nowTime = Date.now();
  92. relations = relations.filter(relaiton => {
  93. const expire = Object.values(relaiton.rel).find(event => event.timestamp <= nowTime);
  94. if (expire) {
  95. return false;
  96. }
  97. return true;
  98. });
  99. const relationLength = relations.length;
  100. if (!relationLength) {
  101. if (GLOBAL_DATA.relationLength) {
  102. GLOBAL_DATA.relationLength = 0;
  103. Logs.out('relation list is empty');
  104. }
  105. return [];
  106. }
  107. GLOBAL_DATA.relationLength = relationLength;
  108. const passableEvents = relations.map(({ id, rel }) => {
  109. const eventsMap = {};
  110. const oddsMap = {};
  111. Object.keys(rel).forEach(platform => {
  112. const { leagueName, teamHomeName, teamAwayName, timestamp, evtime, events, sptime, special } = rel[platform];
  113. if (!events && !special) {
  114. return;
  115. }
  116. if (!eventsMap.info) {
  117. eventsMap.info = { leagueName, teamHomeName, teamAwayName, id, timestamp };
  118. }
  119. const odds = extractOdds({ evtime, events, sptime, special });
  120. Object.keys(odds).forEach(ior => {
  121. if (!oddsMap[ior]) {
  122. oddsMap[ior] = {};
  123. }
  124. oddsMap[ior][platform] = odds[ior];
  125. });
  126. });
  127. eventsMap.odds = oddsMap;
  128. return eventsMap;
  129. });
  130. const solutions = eventsCombination(passableEvents);
  131. postDataToParent('setSolutions', solutions);
  132. })
  133. .finally(() => {
  134. setTimeout(() => {
  135. eventMatch();
  136. }, 2000);
  137. });
  138. };
  139. eventMatch();