eventsMatch.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. console.log(JSON.stringify(relations, null, 2));
  101. const relationLength = relations.length;
  102. if (!relationLength) {
  103. if (GLOBAL_DATA.relationLength) {
  104. GLOBAL_DATA.relationLength = 0;
  105. Logs.out('relation list is empty');
  106. }
  107. return [];
  108. }
  109. GLOBAL_DATA.relationLength = relationLength;
  110. const passableEvents = relations.map(({ id, rel }) => {
  111. const eventsMap = {};
  112. const oddsMap = {};
  113. Object.keys(rel).forEach(platform => {
  114. const { leagueName, teamHomeName, teamAwayName, timestamp, evtime, events, sptime, special } = rel[platform];
  115. if (!events && !special) {
  116. return;
  117. }
  118. if (!eventsMap.info) {
  119. eventsMap.info = { leagueName, teamHomeName, teamAwayName, id, timestamp };
  120. }
  121. const odds = extractOdds({ evtime, events, sptime, special });
  122. Object.keys(odds).forEach(ior => {
  123. if (!oddsMap[ior]) {
  124. oddsMap[ior] = {};
  125. }
  126. oddsMap[ior][platform] = odds[ior];
  127. });
  128. });
  129. eventsMap.odds = oddsMap;
  130. return eventsMap;
  131. });
  132. const solutions = eventsCombination(passableEvents);
  133. if (solutions?.length) {
  134. const solutionsHistory = GLOBAL_DATA.solutions;
  135. const updateIds = { add: [], update: [] }
  136. solutions.forEach(item => {
  137. const { sid, sol: { win_average } } = item;
  138. if (!solutionsHistory[sid]) {
  139. solutionsHistory[sid] = item;
  140. updateIds.add.push({ sid, win_average });
  141. return;
  142. }
  143. const historyWinAverage = solutionsHistory[sid].sol.win_average;
  144. if (win_average != historyWinAverage) {
  145. solutionsHistory[sid] = item;
  146. updateIds.update.push({ sid, win_average, his_average: historyWinAverage, diff: fixFloat(win_average - historyWinAverage) });
  147. return;
  148. }
  149. const { timestamp } = item;
  150. solutionsHistory[sid].timestamp = timestamp;
  151. });
  152. if (updateIds.add.length || updateIds.update.length) {
  153. const solutionsList = Object.values(solutionsHistory).sort((a, b) => b.sol.win_average - a.sol.win_average);
  154. Logs.out('solutions history update', solutionsList, updateIds);
  155. }
  156. }
  157. })
  158. .finally(() => {
  159. setTimeout(() => {
  160. eventMatch();
  161. }, 2000);
  162. });
  163. };
  164. const solutionsCleanup = () => {
  165. const solutionsHistory = GLOBAL_DATA.solutions;
  166. Object.keys(solutionsHistory).forEach(sid => {
  167. const { timestamp } = solutionsHistory[sid];
  168. const nowTime = Date.now();
  169. if (nowTime - timestamp > 1000*60) {
  170. delete solutionsHistory[sid];
  171. Logs.out('solution history timeout', sid);
  172. return;
  173. }
  174. const solution = solutionsHistory[sid];
  175. const eventTime = solution.info.timestamp;
  176. if (nowTime > eventTime) {
  177. delete solutionsHistory[sid];
  178. Logs.out('solution history expired', sid);
  179. }
  180. });
  181. }
  182. setInterval(() => {
  183. solutionsCleanup();
  184. }, 1000*30);
  185. eventMatch();