eventsMatch.js 3.8 KB

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