eventsMatch.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. const Logs = require('../libs/logs');
  2. const { eventsCombination } = require('./trangleCalc');
  3. const { getSetting, updateSetting } = require('./settings');
  4. const Request = {
  5. callbacks: {},
  6. count: 0,
  7. }
  8. const GLOBAL_DATA = {
  9. relationLength: 0,
  10. loopStatus: -1,
  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 (method == 'post') {
  31. if (type == 'updateSetting') {
  32. updateSetting(data);
  33. }
  34. }
  35. else if (type == 'response' && id && callbacks[id]) {
  36. callbacks[id](data);
  37. delete callbacks[id];
  38. }
  39. });
  40. /**
  41. * 精确浮点数字
  42. * @param {number} number
  43. * @param {number} x
  44. * @returns {number}
  45. */
  46. const fixFloat = (number, x=2) => {
  47. return parseFloat(number.toFixed(x));
  48. }
  49. const getGamesRelation = () => {
  50. const setting = getSetting();
  51. const status = +setting.runWorkerEnabled;
  52. if (GLOBAL_DATA.loopStatus !== status) {
  53. GLOBAL_DATA.loopStatus = status;
  54. Logs.out('loop status changed to', status);
  55. }
  56. if (!setting.runWorkerEnabled) {
  57. return Promise.resolve([]);
  58. }
  59. return new Promise(resolve => {
  60. getDataFromParent('getGamesRelation', (relations) => {
  61. resolve(relations);
  62. });
  63. });
  64. }
  65. const updateSolutions = (solutions) => {
  66. postDataToParent('updateSolutions', solutions);
  67. }
  68. const extractOdds = ({ evtime, events, sptime, special }) => {
  69. const expireTime = Date.now() - 15000;
  70. let odds = {};
  71. if (evtime > expireTime) {
  72. odds = { ...odds, ...events };
  73. }
  74. if (sptime > expireTime) {
  75. odds = { ...odds, ...special };
  76. }
  77. Object.keys(odds).forEach(ior => {
  78. if (odds[ior] <= 0) {
  79. delete odds[ior];
  80. }
  81. });
  82. return odds;
  83. }
  84. const eventMatch = () => {
  85. getGamesRelation()
  86. .then(relations => {
  87. const relationLength = relations?.length;
  88. if (!relationLength) {
  89. if (GLOBAL_DATA.relationLength) {
  90. GLOBAL_DATA.relationLength = 0;
  91. Logs.out('relation list is empty');
  92. }
  93. return [];
  94. }
  95. GLOBAL_DATA.relationLength = relationLength;
  96. const passableEvents = relations.map(({ id, rel }) => {
  97. const eventsMap = {};
  98. const oddsMap = {};
  99. Object.keys(rel).forEach(platform => {
  100. const { leagueName, teamHomeName, teamAwayName, timestamp, evtime, events, sptime, special } = rel[platform];
  101. if (!events && !special) {
  102. return;
  103. }
  104. if (platform == 'ps') {
  105. eventsMap.info = { leagueName, teamHomeName, teamAwayName, id, timestamp };
  106. }
  107. const odds = extractOdds({ evtime, events, sptime, special });
  108. Object.keys(odds).forEach(ior => {
  109. if (!oddsMap[ior]) {
  110. oddsMap[ior] = {};
  111. }
  112. oddsMap[ior][platform] = odds[ior];
  113. });
  114. });
  115. eventsMap.odds = oddsMap;
  116. return eventsMap;
  117. })
  118. .filter(item => item.info);
  119. const solutions = eventsCombination(passableEvents);
  120. // Logs.out('eventMatch solutions', solutions);
  121. if (solutions?.length) {
  122. updateSolutions(solutions);
  123. }
  124. })
  125. .finally(() => {
  126. setTimeout(() => {
  127. eventMatch();
  128. }, 2000);
  129. });
  130. };
  131. const syncSetting = () => {
  132. getDataFromParent('getSetting', (setting) => {
  133. if (setting) {
  134. updateSetting(setting);
  135. }
  136. else {
  137. Logs.out('syncSetting setting is empty');
  138. }
  139. });
  140. }
  141. syncSetting();
  142. eventMatch();