eventsMatch.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 expireTimeEv = Date.now() - 30000;
  70. const expireTimeSP = Date.now() - 45000;
  71. let odds = {};
  72. if (evtime > expireTimeEv) {
  73. odds = { ...odds, ...events };
  74. }
  75. if (sptime > expireTimeSP) {
  76. odds = { ...odds, ...special };
  77. }
  78. Object.keys(odds).forEach(ior => {
  79. if (odds[ior] <= 0) {
  80. delete odds[ior];
  81. }
  82. });
  83. return odds;
  84. }
  85. const eventMatch = () => {
  86. getGamesRelation()
  87. .then(relations => {
  88. const relationLength = relations?.length;
  89. if (!relationLength) {
  90. if (GLOBAL_DATA.relationLength) {
  91. GLOBAL_DATA.relationLength = 0;
  92. Logs.out('relation list is empty');
  93. }
  94. return [];
  95. }
  96. GLOBAL_DATA.relationLength = relationLength;
  97. const passableEvents = relations.map(({ id, rel }) => {
  98. const eventsMap = {};
  99. const oddsMap = {};
  100. Object.keys(rel).forEach(platform => {
  101. const { leagueName, teamHomeName, teamAwayName, timestamp, evtime, events, sptime, special } = rel[platform];
  102. if (!events && !special) {
  103. return;
  104. }
  105. if (platform == 'ps') {
  106. eventsMap.info = { leagueName, teamHomeName, teamAwayName, id, timestamp };
  107. }
  108. const odds = extractOdds({ evtime, events, sptime, special });
  109. Object.keys(odds).forEach(ior => {
  110. if (!oddsMap[ior]) {
  111. oddsMap[ior] = {};
  112. }
  113. oddsMap[ior][platform] = odds[ior];
  114. });
  115. });
  116. eventsMap.odds = oddsMap;
  117. return eventsMap;
  118. })
  119. .filter(item => item.info);
  120. const solutions = eventsCombination(passableEvents);
  121. // Logs.out('eventMatch solutions', solutions);
  122. if (solutions?.length) {
  123. updateSolutions(solutions);
  124. }
  125. })
  126. .finally(() => {
  127. setTimeout(() => {
  128. eventMatch();
  129. }, 2000);
  130. });
  131. };
  132. const syncSetting = () => {
  133. getDataFromParent('getSetting', (setting) => {
  134. if (setting) {
  135. updateSetting(setting);
  136. }
  137. else {
  138. Logs.out('syncSetting setting is empty');
  139. }
  140. });
  141. }
  142. syncSetting();
  143. eventMatch();