Games.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. Solutions: {},
  16. };
  17. /**
  18. * 精确浮点数字
  19. * @param {number} number
  20. * @param {number} x
  21. * @returns {number}
  22. */
  23. const fixFloat = (number, x=2) => {
  24. return parseFloat(number.toFixed(x));
  25. }
  26. /**
  27. * 更新比赛列表
  28. */
  29. const updateGamesList = (({ platform, mk, games } = {}) => {
  30. return new Promise((resolve, reject) => {
  31. if (!platform || !games) {
  32. return reject(new Error('PLATFORM_GAMES_INVALID'));
  33. }
  34. const marketType = mk == 0 ? 'early' : 'today';
  35. let gamesList = games;
  36. if (platform == 'jc') {
  37. gamesList = [];
  38. const gamesEvents = [];
  39. const gamesOutrights = [];
  40. games.forEach(game => {
  41. const { eventId, events, evtime, special, sptime, ...gameInfo } = game;
  42. gamesList.push({ eventId, ...gameInfo });
  43. gamesEvents.push({ eventId, events, evtime });
  44. gamesOutrights.push({ parentId: eventId, special, sptime });
  45. });
  46. updateGamesEvents({ platform, games: gamesEvents, outrights: gamesOutrights });
  47. }
  48. const timestamp = Date.now();
  49. const GAMES_LIST = GAMES.List;
  50. if (!GAMES_LIST[platform]) {
  51. GAMES_LIST[platform] = {};
  52. }
  53. if (!GAMES_LIST[platform][marketType]) {
  54. GAMES_LIST[platform][marketType] = { games: gamesList, timestamp };
  55. return resolve({ add: gamesList.length, del: 0 });
  56. }
  57. const oldGames = GAMES_LIST[platform][marketType].games;
  58. const newGames = gamesList;
  59. const updateCount = {
  60. add: 0,
  61. del: 0,
  62. };
  63. const newMap = new Map(newGames.map(item => [item.eventId, item]));
  64. for (let i = oldGames.length - 1; i >= 0; i--) {
  65. if (!newMap.has(oldGames[i].eventId)) {
  66. oldGames.splice(i, 1);
  67. updateCount.del += 1;
  68. }
  69. }
  70. const oldIds = new Set(oldGames.map(item => item.eventId));
  71. const relatedGames = Object.values(GAMES.Relations).map(rel => rel[platform] ?? {});
  72. const relatedMap = new Map(relatedGames.map(item => [item.eventId, item]));
  73. newGames.forEach(item => {
  74. if (!oldIds.has(item.eventId)) {
  75. oldGames.push(item);
  76. updateCount.add += 1;
  77. }
  78. if (relatedMap.has(item.eventId)) {
  79. const relatedGame = relatedMap.get(item.eventId);
  80. relatedGame.mk = mk;
  81. }
  82. });
  83. GAMES_LIST[platform][marketType].timestamp = timestamp;
  84. resolve(updateCount);
  85. });
  86. });
  87. /**
  88. * 更新比赛盘口
  89. */
  90. const updateGamesEvents = ({ platform, mk, games, outrights }) => {
  91. return new Promise((resolve, reject) => {
  92. if (!platform || (!games && !outrights)) {
  93. return reject(new Error('PLATFORM_GAMES_INVALID'));
  94. }
  95. const relatedGames = Object.values(GAMES.Relations).map(rel => rel[platform] ?? {});
  96. if (!relatedGames.length) {
  97. return resolve({ update: 0 });
  98. }
  99. const updateCount = {
  100. update: 0
  101. };
  102. const relatedMap = new Map(relatedGames.map(item => [item.eventId, item]));
  103. games?.forEach(game => {
  104. const { eventId, evtime, events } = game;
  105. const relatedGame = relatedMap.get(eventId);
  106. if (relatedGame) {
  107. relatedGame.evtime = evtime;
  108. relatedGame.events = events;
  109. updateCount.update ++;
  110. }
  111. });
  112. outrights?.forEach(outright => {
  113. const { parentId, sptime, special } = outright;
  114. const relatedGame = relatedMap.get(parentId);
  115. if (relatedGame) {
  116. relatedGame.sptime = sptime;
  117. relatedGame.special = special;
  118. updateCount.update ++;
  119. }
  120. });
  121. resolve(updateCount);
  122. });
  123. }
  124. /**
  125. * 获取比赛列表
  126. */
  127. const getGamesList = () => {
  128. const gamesListMap = {};
  129. Object.keys(GAMES.List).forEach(platform => {
  130. const { today, early } = GAMES.List[platform];
  131. const todayList = today?.games ?? [];
  132. const earlyList = early?.games ?? [];
  133. const timestamp_today = today?.timestamp ?? 0;
  134. const timestamp_early = early?.timestamp ?? 0;
  135. const timestamp = Math.max(timestamp_today, timestamp_early);
  136. gamesListMap[platform] = {
  137. games: [...todayList, ...earlyList],
  138. timestamp,
  139. timestamp_today,
  140. timestamp_early,
  141. }
  142. });
  143. return gamesListMap;
  144. }
  145. /**
  146. * 更新关联比赛
  147. */
  148. const updateGamesRelation = async (relation) => {
  149. const { id, rel } = relation;
  150. return Relation.findOne({ id })
  151. .then(result => {
  152. if (!result) {
  153. const gameRelation = new Relation(relation);
  154. return gameRelation.save();
  155. }
  156. return Relation.updateOne({ id }, { $set: { rel } });
  157. })
  158. .then(result => {
  159. GAMES.Relations[id] = rel;
  160. return result;
  161. });
  162. }
  163. /**
  164. * 删除关联比赛
  165. */
  166. const removeGamesRelation = async (id) => {
  167. if (!id) {
  168. return Promise.reject(new Error('ID_INVALID'));
  169. }
  170. return Relation.deleteOne({ id })
  171. .then(result => {
  172. delete GAMES.Relations[id];
  173. return result;
  174. });
  175. }
  176. /**
  177. * 获取关联比赛
  178. */
  179. const getGamesRelation = async (listEvents) => {
  180. const relationIds = Object.keys(GAMES.Relations);
  181. if (listEvents) {
  182. return relationIds.map(id => {
  183. const rel = GAMES.Relations[id];
  184. return { id, rel };
  185. });
  186. }
  187. return relationIds.map(id => {
  188. const rel = { ...GAMES.Relations[id] };
  189. Object.keys(rel).forEach(platform => {
  190. const game = { ...rel[platform] };
  191. delete game.events;
  192. delete game.evtime;
  193. delete game.special;
  194. delete game.sptime;
  195. rel[platform] = game;
  196. });
  197. return { id, rel };
  198. });
  199. }
  200. /**
  201. * 清理关联比赛
  202. */
  203. const relationsCleanup = () => {
  204. const expireTime = Date.now() - 1000*60*5;
  205. getGamesRelation()
  206. .then(gamesRelation => {
  207. gamesRelation.forEach(item => {
  208. const { id, rel } = item;
  209. const expire = Object.values(rel).find(event => {
  210. return event.timestamp <= expireTime;
  211. });
  212. if (expire) {
  213. Logs.out('relation cleanup', id);
  214. removeGamesRelation(id);
  215. }
  216. return true;
  217. });
  218. });
  219. }
  220. /**
  221. * 从数据库中同步关联比赛
  222. */
  223. const relationSync = () => {
  224. Relation.find().then(relation => relation.forEach(item => {
  225. const { id, rel } = item.toObject();
  226. GAMES.Relations[id] = rel;
  227. }));
  228. }
  229. /**
  230. * 更新中单方案
  231. */
  232. const setSolutions = (solutions) => {
  233. if (solutions?.length) {
  234. const solutionsHistory = GAMES.Solutions;
  235. const updateIds = { add: [], update: [] }
  236. solutions.forEach(item => {
  237. const { sid, sol: { win_average } } = item;
  238. if (!solutionsHistory[sid]) {
  239. solutionsHistory[sid] = item;
  240. updateIds.add.push({ sid, win_average });
  241. return;
  242. }
  243. const historyWinAverage = solutionsHistory[sid].sol.win_average;
  244. if (win_average != historyWinAverage) {
  245. solutionsHistory[sid] = item;
  246. updateIds.update.push({ sid, win_average, his_average: historyWinAverage, diff: fixFloat(win_average - historyWinAverage) });
  247. return;
  248. }
  249. const { timestamp } = item;
  250. solutionsHistory[sid].timestamp = timestamp;
  251. });
  252. if (updateIds.add.length || updateIds.update.length) {
  253. const solutionsList = Object.values(solutionsHistory).sort((a, b) => b.sol.win_average - a.sol.win_average);
  254. Logs.outDev('solutions history update', JSON.stringify(solutionsList, null, 2), JSON.stringify(updateIds, null, 2));
  255. }
  256. }
  257. }
  258. /**
  259. * 获取中单方案
  260. */
  261. const getSolutions = () => {
  262. return Object.values(GAMES.Solutions).sort((a, b) => b.sol.win_average - a.sol.win_average);
  263. }
  264. /**
  265. * 清理中单方案
  266. */
  267. const solutionsCleanup = () => {
  268. const solutionsHistory = GAMES.Solutions;
  269. Object.keys(solutionsHistory).forEach(sid => {
  270. const { timestamp } = solutionsHistory[sid];
  271. const nowTime = Date.now();
  272. if (nowTime - timestamp > 1000*60) {
  273. delete solutionsHistory[sid];
  274. Logs.out('solution history timeout', sid);
  275. return;
  276. }
  277. const solution = solutionsHistory[sid];
  278. const eventTime = solution.info.timestamp;
  279. if (nowTime > eventTime) {
  280. delete solutionsHistory[sid];
  281. Logs.out('solution history expired', sid);
  282. }
  283. });
  284. }
  285. const getDataFromChild = (type, callback) => {
  286. const id = ++Request.count;
  287. Request.callbacks[id] = callback;
  288. events_child.send({ method: 'get', id, type });
  289. }
  290. events_child.on('message', async (message) => {
  291. const { callbacks } = Request;
  292. const { method, id, type, data } = message;
  293. if (method == 'get' && id) {
  294. let responseData = null;
  295. if (type == 'getGamesRelation') {
  296. responseData = await getGamesRelation(true);
  297. }
  298. else if (type == 'getSolutionHistory') {
  299. responseData = getSolutionHistory();
  300. }
  301. events_child.send({ type: 'response', id, data: responseData });
  302. }
  303. else if (method == 'post') {
  304. if (type == 'setSolutions') {
  305. setSolutions(data);
  306. }
  307. }
  308. else if (method == 'response' && id && callbacks[id]) {
  309. callbacks[id](data);
  310. delete callbacks[id];
  311. }
  312. });
  313. relationSync();
  314. setInterval(() => {
  315. relationsCleanup();
  316. }, 5000);
  317. setInterval(() => {
  318. solutionsCleanup();
  319. }, 1000*30);
  320. module.exports = {
  321. updateGamesList, updateGamesEvents, getGamesList,
  322. updateGamesRelation, getGamesRelation, removeGamesRelation,
  323. getSolutions,
  324. }