Games.js 6.6 KB

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