eventsMatch.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 SETTING = {
  10. innerDefaultAmount: 10000,
  11. minProfitAmount: 0,
  12. innerRebateRatio: 0,
  13. obRebateRatio: 0,
  14. hgRebateRatio: 0,
  15. runWorkerEnabled: false
  16. }
  17. const GLOBAL_DATA = {
  18. relationLength: 0,
  19. loopStatus: -1,
  20. };
  21. const getDataFromParent = (type, callback) => {
  22. const id = ++Request.count;
  23. Request.callbacks[id] = callback;
  24. process.send({ method: 'get', id, type });
  25. }
  26. const postDataToParent = (type, data) => {
  27. process.send({ method: 'post', type, data });
  28. }
  29. process.on('message', (message) => {
  30. const { callbacks } = Request;
  31. const { method, id, type, data } = message;
  32. if (method == 'get' && id) {
  33. let responseData = null;
  34. // if (data == 'getSolution') {
  35. // responseData = Solution.get();
  36. // }
  37. process.send({ type: 'response', id, data: responseData });
  38. }
  39. else if (type == 'response' && id && callbacks[id]) {
  40. callbacks[id](data);
  41. delete callbacks[id];
  42. }
  43. });
  44. /**
  45. * 精确浮点数字
  46. * @param {number} number
  47. * @param {number} x
  48. * @returns {number}
  49. */
  50. const fixFloat = (number, x=2) => {
  51. return parseFloat(number.toFixed(x));
  52. }
  53. const getSetting = () => {
  54. return new Promise(resolve => {
  55. getDataFromParent('getSetting', (setting) => {
  56. resolve(setting);
  57. });
  58. });
  59. }
  60. const getGamesRelation = () => {
  61. const status = +SETTING.runWorkerEnabled;
  62. if (GLOBAL_DATA.loopStatus !== status) {
  63. GLOBAL_DATA.loopStatus = status;
  64. Logs.out('loop status changed to', status);
  65. }
  66. if (!SETTING.runWorkerEnabled) {
  67. return Promise.resolve([]);
  68. }
  69. return new Promise(resolve => {
  70. getDataFromParent('getGamesRelation', (relations) => {
  71. resolve(relations);
  72. });
  73. });
  74. }
  75. // const getSolutionHistory = () => {
  76. // return new Promise(resolve => {
  77. // getDataFromParent('getSolutionHistory', (solutions) => {
  78. // resolve(solutions);
  79. // });
  80. // });
  81. // }
  82. const updateSolutions = (solutions) => {
  83. postDataToParent('updateSolutions', solutions);
  84. }
  85. const extractOdds = ({ evtime, events, sptime, special }) => {
  86. const expireTime = Date.now() - 15000;
  87. let odds = {};
  88. if (evtime > expireTime) {
  89. odds = { ...odds, ...events };
  90. }
  91. if (sptime > expireTime) {
  92. odds = { ...odds, ...special };
  93. }
  94. return odds;
  95. }
  96. // const getOptimalOdds = (oddsMap) => {
  97. // const oddsInfo = {};
  98. // Object.keys(oddsMap).forEach(platform => {
  99. // const odds = oddsMap[platform];
  100. // Object.keys(odds).forEach(ior => {
  101. // const oddsValue = odds[ior];
  102. // if (!oddsInfo[ior] || oddsInfo[ior]?.v < oddsValue) {
  103. // oddsInfo[ior] = {
  104. // p: platform,
  105. // v: oddsValue
  106. // }
  107. // }
  108. // });
  109. // });
  110. // return oddsInfo;
  111. // }
  112. const eventMatch = () => {
  113. getGamesRelation()
  114. .then(relations => {
  115. // Logs.out('eventMatch', relations);
  116. // if (!relations?.length) {
  117. // return;
  118. // }
  119. // const nowTime = Date.now();
  120. // relations = relations.filter(relaiton => {
  121. // const expire = Object.values(relaiton.rel).find(event => event.timestamp <= nowTime);
  122. // if (expire) {
  123. // return false;
  124. // }
  125. // return true;
  126. // });
  127. // Logs.out('eventMatch relations', relations);
  128. const relationLength = relations?.length;
  129. if (!relationLength) {
  130. if (GLOBAL_DATA.relationLength) {
  131. GLOBAL_DATA.relationLength = 0;
  132. Logs.out('relation list is empty');
  133. }
  134. return [];
  135. }
  136. GLOBAL_DATA.relationLength = relationLength;
  137. const passableEvents = relations.map(({ id, rel }) => {
  138. const eventsMap = {};
  139. const oddsMap = {};
  140. Object.keys(rel).forEach(platform => {
  141. const { leagueName, teamHomeName, teamAwayName, timestamp, evtime, events, sptime, special } = rel[platform];
  142. if (!events && !special) {
  143. return;
  144. }
  145. if (platform == 'ps') {
  146. eventsMap.info = { leagueName, teamHomeName, teamAwayName, id, timestamp };
  147. }
  148. const odds = extractOdds({ evtime, events, sptime, special });
  149. Object.keys(odds).forEach(ior => {
  150. if (!oddsMap[ior]) {
  151. oddsMap[ior] = {};
  152. }
  153. oddsMap[ior][platform] = odds[ior];
  154. });
  155. });
  156. eventsMap.odds = oddsMap;
  157. return eventsMap;
  158. })
  159. .filter(item => item.info);
  160. // Logs.out('eventMatch passableEvents', passableEvents);
  161. const solutions = eventsCombination(passableEvents, SETTING);
  162. // Logs.out('eventMatch solutions', solutions);
  163. if (solutions?.length) {
  164. updateSolutions(solutions);
  165. }
  166. })
  167. .finally(() => {
  168. setTimeout(() => {
  169. eventMatch();
  170. }, 2000);
  171. });
  172. };
  173. const syncSetting = () => {
  174. getSetting()
  175. .then(setting => {
  176. if (setting) {
  177. Object.keys(setting).forEach(key => {
  178. SETTING[key] = setting[key];
  179. });
  180. }
  181. })
  182. .finally(() => {
  183. setTimeout(() => {
  184. syncSetting();
  185. }, 10000);
  186. });
  187. }
  188. syncSetting();
  189. eventMatch();