Games.js 5.7 KB

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