Games.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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, mk, games } = {}) => {
  17. return new Promise((resolve, reject) => {
  18. if (!platform || !games) {
  19. return reject(new Error('PLATFORM_GAMES_INVALID'));
  20. }
  21. const marketType = mk == 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. const relatedGames = Object.values(GAMES.Relations).map(rel => rel[platform] ?? {});
  59. const relatedMap = new Map(relatedGames.map(item => [item.eventId, item]));
  60. newGames.forEach(item => {
  61. if (!oldIds.has(item.eventId)) {
  62. oldGames.push(item);
  63. updateCount.add += 1;
  64. }
  65. if (relatedMap.has(item.eventId)) {
  66. const relatedGame = relatedMap.get(item.eventId);
  67. relatedGame.mk = mk;
  68. }
  69. });
  70. GAMES_LIST[platform][marketType].timestamp = timestamp;
  71. resolve(updateCount);
  72. });
  73. });
  74. const updateGamesEvents = ({ platform, mk, games, outrights }) => {
  75. return new Promise((resolve, reject) => {
  76. if (!platform || (!games && !outrights)) {
  77. return reject(new Error('PLATFORM_GAMES_INVALID'));
  78. }
  79. const relatedGames = Object.values(GAMES.Relations).map(rel => rel[platform] ?? {});
  80. if (!relatedGames.length) {
  81. return resolve({ update: 0 });
  82. }
  83. const updateCount = {
  84. update: 0
  85. };
  86. const relatedMap = new Map(relatedGames.map(item => [item.eventId, item]));
  87. games?.forEach(game => {
  88. const { eventId, evtime, events } = game;
  89. const relatedGame = relatedMap.get(eventId);
  90. if (relatedGame) {
  91. relatedGame.evtime = evtime;
  92. relatedGame.events = events;
  93. updateCount.update ++;
  94. }
  95. });
  96. outrights?.forEach(outright => {
  97. const { parentId, sptime, special } = outright;
  98. const relatedGame = relatedMap.get(parentId);
  99. if (relatedGame) {
  100. relatedGame.sptime = sptime;
  101. relatedGame.special = special;
  102. updateCount.update ++;
  103. }
  104. });
  105. resolve(updateCount);
  106. });
  107. }
  108. const getGamesList = () => {
  109. const gamesListMap = {};
  110. Object.keys(GAMES.List).forEach(platform => {
  111. const { today, early } = GAMES.List[platform];
  112. const todayList = today?.games ?? [];
  113. const earlyList = early?.games ?? [];
  114. const timestamp_today = today?.timestamp ?? 0;
  115. const timestamp_early = early?.timestamp ?? 0;
  116. const timestamp = Math.max(timestamp_today, timestamp_early);
  117. gamesListMap[platform] = {
  118. games: [...todayList, ...earlyList],
  119. timestamp,
  120. timestamp_today,
  121. timestamp_early,
  122. }
  123. });
  124. return gamesListMap;
  125. }
  126. const updateGamesRelation = async (relation) => {
  127. const { id, rel } = relation;
  128. return Relation.findOne({ id })
  129. .then(result => {
  130. if (!result) {
  131. const gameRelation = new Relation(relation);
  132. return gameRelation.save();
  133. }
  134. return Relation.updateOne({ id }, { $set: { rel } });
  135. })
  136. .then(result => {
  137. GAMES.Relations[id] = rel;
  138. return result;
  139. });
  140. }
  141. const removeGamesRelation = async (id) => {
  142. if (!id) {
  143. return Promise.reject(new Error('ID_INVALID'));
  144. }
  145. return Relation.deleteOne({ id })
  146. .then(result => {
  147. delete GAMES.Relations[id];
  148. return result;
  149. });
  150. }
  151. const getGamesRelation = async (listEvents) => {
  152. const relationIds = Object.keys(GAMES.Relations);
  153. if (listEvents) {
  154. return relationIds.map(id => {
  155. const rel = GAMES.Relations[id];
  156. return { id, rel };
  157. });
  158. }
  159. return relationIds.map(id => {
  160. const rel = { ...GAMES.Relations[id] };
  161. Object.keys(rel).forEach(platform => {
  162. const game = { ...rel[platform] };
  163. delete game.events;
  164. delete game.evtime;
  165. delete game.special;
  166. delete game.sptime;
  167. rel[platform] = game;
  168. });
  169. return { id, rel };
  170. });
  171. }
  172. const relationsCleanup = () => {
  173. const expireTime = Date.now() - 1000*60*5;
  174. getGamesRelation()
  175. .then(gamesRelation => {
  176. gamesRelation.forEach(item => {
  177. const { id, rel } = item;
  178. const expire = Object.values(rel).find(event => {
  179. return event.timestamp <= expireTime;
  180. });
  181. if (expire) {
  182. Logs.out('relation cleanup', id);
  183. removeGamesRelation(id);
  184. }
  185. return true;
  186. });
  187. });
  188. }
  189. const relationSync = () => {
  190. Relation.find().then(relation => relation.forEach(item => {
  191. const { id, rel } = item.toObject();
  192. GAMES.Relations[id] = rel;
  193. }));
  194. }
  195. const getDataFromChild = (type, callback) => {
  196. const id = ++Request.count;
  197. Request.callbacks[id] = callback;
  198. events_child.send({ method: 'get', id, type });
  199. }
  200. events_child.on('message', async (message) => {
  201. const { callbacks } = Request;
  202. const { method, id, type, data } = message;
  203. if (method == 'get' && id) {
  204. let responseData = null;
  205. if (type == 'getGamesRelation') {
  206. responseData = await getGamesRelation(true);
  207. }
  208. else if (type == 'getSolutionHistory') {
  209. responseData = getSolutionHistory();
  210. }
  211. events_child.send({ type: 'response', id, data: responseData });
  212. }
  213. else if (method == 'post') {
  214. if (type == 'setSolution') {
  215. setSolution(data);
  216. }
  217. }
  218. else if (method == 'response' && id && callbacks[id]) {
  219. callbacks[id](data);
  220. delete callbacks[id];
  221. }
  222. });
  223. relationSync();
  224. setInterval(() => {
  225. relationsCleanup();
  226. }, 5000);
  227. module.exports = {
  228. updateGamesList, updateGamesEvents, getGamesList,
  229. updateGamesRelation, getGamesRelation, removeGamesRelation,
  230. }