Games.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. const { fork } = require('child_process');
  2. const events_child = fork('./triangle/eventsMatch.js', [], {
  3. execArgv: ['--inspect=9228']
  4. });
  5. const Logs = require('../libs/logs');
  6. const Relation = require('./Relation');
  7. const Request = {
  8. callbacks: {},
  9. count: 0,
  10. }
  11. const GAMES = {
  12. List: {},
  13. Relations: {}
  14. };
  15. const updateGamesList = (({ platform, type, games } = {}) => {
  16. return new Promise((resolve, reject) => {
  17. if (!platform || !games) {
  18. return reject(new Error('PLATFORM_GAMES_INVALID'));
  19. }
  20. const marketType = type == 0 ? 'early' : 'today';
  21. let gamesList = games;
  22. if (platform == 'jc') {
  23. gamesList = [];
  24. const gamesEvents = [];
  25. const gamesOutrights = [];
  26. games.forEach(game => {
  27. const { eventId, events, evtime, special, sptime, ...gameInfo } = game;
  28. gamesList.push({ eventId, ...gameInfo });
  29. gamesEvents.push({ eventId, events, evtime });
  30. gamesOutrights.push({ parentId: eventId, special, sptime });
  31. });
  32. updateGamesEvents({ platform, games: gamesEvents, outrights: gamesOutrights });
  33. }
  34. const timestamp = Date.now();
  35. const GAMES_LIST = GAMES.List;
  36. if (!GAMES_LIST[platform]) {
  37. GAMES_LIST[platform] = {};
  38. }
  39. if (!GAMES_LIST[platform][marketType]) {
  40. GAMES_LIST[platform][marketType] = { games: gamesList, timestamp };
  41. return resolve({ add: gamesList.length, del: 0 });
  42. }
  43. const oldGames = GAMES_LIST[platform][marketType].games;
  44. const newGames = gamesList;
  45. const updateCount = {
  46. add: 0,
  47. del: 0,
  48. };
  49. const newMap = new Map(newGames.map(item => [item.eventId, item]));
  50. for (let i = oldGames.length - 1; i >= 0; i--) {
  51. if (!newMap.has(oldGames[i].eventId)) {
  52. oldGames.splice(i, 1);
  53. updateCount.del += 1;
  54. }
  55. }
  56. const oldIds = new Set(oldGames.map(item => item.eventId));
  57. newGames.forEach(item => {
  58. if (!oldIds.has(item.eventId)) {
  59. oldGames.push(item);
  60. updateCount.add += 1;
  61. }
  62. });
  63. GAMES_LIST[platform][marketType].timestamp = timestamp;
  64. resolve(updateCount);
  65. });
  66. });
  67. const updateGamesEvents = ({ platform, type, games, outrights }) => {
  68. return new Promise((resolve, reject) => {
  69. if (!platform || (!games && !outrights)) {
  70. return reject(new Error('PLATFORM_GAMES_INVALID'));
  71. }
  72. const oldGames = Object.values(GAMES.Relations).map(rel => rel[platform] ?? {}).flat();
  73. if (!oldGames.length) {
  74. return resolve({ update: 0 });
  75. }
  76. const updateCount = {
  77. update: 0
  78. };
  79. const oldMap = new Map(oldGames.map(item => [item.eventId, item]));
  80. games?.forEach(game => {
  81. const { eventId, evtime, events } = game;
  82. const oldGame = oldMap.get(eventId);
  83. if (oldGame) {
  84. oldGame.evtime = evtime;
  85. oldGame.events = events;
  86. updateCount.update ++;
  87. }
  88. });
  89. outrights?.forEach(outright => {
  90. const { parentId, sptime, special } = outright;
  91. const oldGame = oldMap.get(parentId);
  92. if (oldGame) {
  93. oldGame.sptime = sptime;
  94. oldGame.special = special;
  95. updateCount.update ++;
  96. }
  97. });
  98. resolve(updateCount);
  99. });
  100. }
  101. const getGamesList = () => {
  102. const gamesListMap = {};
  103. Object.keys(GAMES.List).forEach(platform => {
  104. const { today, early } = GAMES.List[platform];
  105. const todayList = today?.games ?? [];
  106. const earlyList = early?.games ?? [];
  107. const timestamp_today = today?.timestamp ?? 0;
  108. const timestamp_early = early?.timestamp ?? 0;
  109. const timestamp = Math.max(timestamp_today, timestamp_early);
  110. gamesListMap[platform] = {
  111. games: [...todayList, ...earlyList],
  112. timestamp,
  113. timestamp_today,
  114. timestamp_early,
  115. }
  116. });
  117. return gamesListMap;
  118. }
  119. const updateGamesRelation = async (relation) => {
  120. const { id, rel } = relation;
  121. return Relation.findOne({ id })
  122. .then(result => {
  123. if (!result) {
  124. const gameRelation = new Relation(relation);
  125. return gameRelation.save();
  126. }
  127. return Relation.updateOne({ id }, { $set: { rel } });
  128. })
  129. .then(result => {
  130. GAMES.Relations[id] = rel;
  131. return result;
  132. });
  133. }
  134. const removeGamesRelation = async (id) => {
  135. if (!id) {
  136. return Promise.reject(new Error('ID_INVALID'));
  137. }
  138. return Relation.deleteOne({ id })
  139. .then(result => {
  140. delete GAMES.Relations[id];
  141. return result;
  142. });
  143. }
  144. const getGamesRelation = async (listEvents) => {
  145. const relationIds = Object.keys(GAMES.Relations);
  146. if (listEvents) {
  147. return relationIds.map(id => {
  148. const rel = GAMES.Relations[id];
  149. return { id, rel };
  150. });
  151. }
  152. return relationIds.map(id => {
  153. const rel = { ...GAMES.Relations[id] };
  154. Object.keys(rel).forEach(platform => {
  155. const game = { ...rel[platform] };
  156. delete game.events;
  157. delete game.evtime;
  158. delete game.special;
  159. delete game.sptime;
  160. rel[platform] = game;
  161. });
  162. return { id, rel };
  163. });
  164. }
  165. const relationsCleanup = () => {
  166. const expireTime = Date.now() - 1000*60*5;
  167. getGamesRelation()
  168. .then(gamesRelation => {
  169. gamesRelation.forEach(item => {
  170. const { id, rel } = item;
  171. const expire = Object.values(rel).find(event => {
  172. return event.timestamp <= expireTime;
  173. });
  174. if (expire) {
  175. Logs.out('relation cleanup', id);
  176. removeGamesRelation(id);
  177. }
  178. return true;
  179. });
  180. });
  181. }
  182. const relationSync = () => {
  183. Relation.find().then(relation => relation.forEach(item => {
  184. const { id, rel } = item.toObject();
  185. GAMES.Relations[id] = rel;
  186. }));
  187. }
  188. const getDataFromChild = (type, callback) => {
  189. const id = ++Request.count;
  190. Request.callbacks[id] = callback;
  191. events_child.send({ method: 'get', id, type });
  192. }
  193. events_child.on('message', async (message) => {
  194. const { callbacks } = Request;
  195. const { method, id, type, data } = message;
  196. if (method == 'get' && id) {
  197. let responseData = null;
  198. if (type == 'getGamesRelation') {
  199. responseData = await getGamesRelation(true);
  200. }
  201. else if (type == 'getSolutionHistory') {
  202. responseData = getSolutionHistory();
  203. }
  204. events_child.send({ type: 'response', id, data: responseData });
  205. }
  206. else if (method == 'post') {
  207. if (type == 'setSolution') {
  208. setSolution(data);
  209. }
  210. }
  211. else if (method == 'response' && id && callbacks[id]) {
  212. callbacks[id](data);
  213. delete callbacks[id];
  214. }
  215. });
  216. relationSync();
  217. setInterval(() => {
  218. relationsCleanup();
  219. }, 5000);
  220. module.exports = {
  221. updateGamesList, updateGamesEvents, getGamesList,
  222. updateGamesRelation, getGamesRelation, removeGamesRelation,
  223. }