eventsMatch.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. const Logs = require('../libs/logs');
  2. const { getPassableEvents, 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, eventsLogsMap) => {
  66. postDataToParent('updateSolutions', { solutions, eventsLogsMap });
  67. }
  68. const eventMatch = () => {
  69. getGamesRelation()
  70. .then(relations => {
  71. const relationLength = relations?.length;
  72. if (!relationLength) {
  73. if (GLOBAL_DATA.relationLength) {
  74. GLOBAL_DATA.relationLength = 0;
  75. Logs.out('relation list is empty');
  76. }
  77. return [];
  78. }
  79. GLOBAL_DATA.relationLength = relationLength;
  80. /** 日志 盘口信息 */
  81. const eventsLogsMap = {
  82. expireEvents: [],
  83. removeEvents: []
  84. };
  85. /** 日志 End */
  86. const passableEvents = getPassableEvents(relations, eventsLogsMap);
  87. const solutions = eventsCombination(passableEvents);
  88. // Logs.out('eventMatch solutions', solutions);
  89. if (solutions?.length) {
  90. updateSolutions(solutions, eventsLogsMap);
  91. }
  92. })
  93. .finally(() => {
  94. setTimeout(() => {
  95. eventMatch();
  96. }, 2000);
  97. });
  98. };
  99. const syncSetting = () => {
  100. getDataFromParent('getSetting', (setting) => {
  101. if (setting) {
  102. updateSetting(setting);
  103. }
  104. else {
  105. Logs.out('syncSetting setting is empty');
  106. }
  107. });
  108. }
  109. syncSetting();
  110. eventMatch();