eventsMatch.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. if (solutions?.length) {
  133. const solutionsHistory = GLOBAL_DATA.solutions;
  134. const updateIds = { add: [], update: [] }
  135. solutions.forEach(item => {
  136. const { sid, sol: { win_average } } = item;
  137. if (!solutionsHistory[sid]) {
  138. solutionsHistory[sid] = item;
  139. updateIds.add.push({ sid, win_average });
  140. return;
  141. }
  142. const historyWinAverage = solutionsHistory[sid].sol.win_average;
  143. if (win_average != historyWinAverage) {
  144. solutionsHistory[sid] = item;
  145. updateIds.update.push({ sid, win_average, his_average: historyWinAverage, diff: fixFloat(win_average - historyWinAverage) });
  146. return;
  147. }
  148. const { timestamp } = item;
  149. solutionsHistory[sid].timestamp = timestamp;
  150. });
  151. if (updateIds.add.length || updateIds.update.length) {
  152. const solutionsList = Object.values(solutionsHistory).sort((a, b) => b.sol.win_average - a.sol.win_average);
  153. Logs.out('solutions history update', JSON.stringify(solutionsList, null, 2), JSON.stringify(updateIds, null, 2));
  154. }
  155. }
  156. })
  157. .finally(() => {
  158. setTimeout(() => {
  159. eventMatch();
  160. }, 2000);
  161. });
  162. };
  163. const solutionsCleanup = () => {
  164. const solutionsHistory = GLOBAL_DATA.solutions;
  165. Object.keys(solutionsHistory).forEach(sid => {
  166. const { timestamp } = solutionsHistory[sid];
  167. const nowTime = Date.now();
  168. if (nowTime - timestamp > 1000*60) {
  169. delete solutionsHistory[sid];
  170. Logs.out('solution history timeout', sid);
  171. return;
  172. }
  173. const solution = solutionsHistory[sid];
  174. const eventTime = solution.info.timestamp;
  175. if (nowTime > eventTime) {
  176. delete solutionsHistory[sid];
  177. Logs.out('solution history expired', sid);
  178. }
  179. });
  180. }
  181. setInterval(() => {
  182. solutionsCleanup();
  183. }, 1000*30);
  184. eventMatch();