Games.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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 getGamesEvents = (platform) => {
  149. if (platform) {
  150. return Object.values(GAMES.Relations).map(rel => rel[platform] ?? {});
  151. }
  152. const gamesEvents = {};
  153. Object.values(GAMES.Relations).forEach(rel => {
  154. Object.keys(rel).forEach(platform => {
  155. const game = rel[platform];
  156. const { eventId, events, special } = game;
  157. if (!gamesEvents[platform]) {
  158. gamesEvents[platform] = {};
  159. }
  160. gamesEvents[platform][eventId] = { ...events, ...special };
  161. });
  162. });
  163. return gamesEvents;
  164. }
  165. /**
  166. * 更新关联比赛
  167. */
  168. const updateGamesRelation = async (relation) => {
  169. const { id, rel } = relation;
  170. return Relation.findOne({ id })
  171. .then(result => {
  172. if (!result) {
  173. const gameRelation = new Relation(relation);
  174. return gameRelation.save();
  175. }
  176. return Relation.updateOne({ id }, { $set: { rel } });
  177. })
  178. .then(result => {
  179. GAMES.Relations[id] = rel;
  180. return result;
  181. });
  182. }
  183. /**
  184. * 删除关联比赛
  185. */
  186. const removeGamesRelation = async (id) => {
  187. if (!id) {
  188. return Promise.reject(new Error('ID_INVALID'));
  189. }
  190. return Relation.deleteOne({ id })
  191. .then(result => {
  192. delete GAMES.Relations[id];
  193. return result;
  194. });
  195. }
  196. /**
  197. * 获取关联比赛
  198. */
  199. const getGamesRelation = async (listEvents) => {
  200. const relationIds = Object.keys(GAMES.Relations);
  201. if (listEvents) {
  202. return relationIds.map(id => {
  203. const rel = GAMES.Relations[id];
  204. return { id, rel };
  205. });
  206. }
  207. return relationIds.map(id => {
  208. const rel = { ...GAMES.Relations[id] };
  209. Object.keys(rel).forEach(platform => {
  210. const game = { ...rel[platform] };
  211. delete game.events;
  212. delete game.evtime;
  213. delete game.special;
  214. delete game.sptime;
  215. rel[platform] = game;
  216. });
  217. return { id, rel };
  218. });
  219. }
  220. /**
  221. * 清理关联比赛
  222. */
  223. const relationsCleanup = () => {
  224. const expireTime = Date.now() - 1000*60*5;
  225. getGamesRelation()
  226. .then(gamesRelation => {
  227. gamesRelation.forEach(item => {
  228. const { id, rel } = item;
  229. const expire = Object.values(rel).find(event => {
  230. return event.timestamp <= expireTime;
  231. });
  232. if (expire) {
  233. Logs.out('relation cleanup', id);
  234. removeGamesRelation(id);
  235. }
  236. return true;
  237. });
  238. });
  239. }
  240. /**
  241. * 从数据库中同步关联比赛
  242. */
  243. const relationSync = () => {
  244. Relation.find().then(relation => relation.forEach(item => {
  245. const { id, rel } = item.toObject();
  246. GAMES.Relations[id] = rel;
  247. }));
  248. }
  249. /**
  250. * 更新中单方案
  251. */
  252. const setSolutions = (solutions) => {
  253. if (solutions?.length) {
  254. const solutionsHistory = GAMES.Solutions;
  255. const updateIds = { add: [], update: [] }
  256. solutions.forEach(item => {
  257. const { sid, sol: { win_average } } = item;
  258. if (!solutionsHistory[sid]) {
  259. solutionsHistory[sid] = item;
  260. updateIds.add.push({ sid, win_average });
  261. return;
  262. }
  263. const historyWinAverage = solutionsHistory[sid].sol.win_average;
  264. if (win_average != historyWinAverage) {
  265. solutionsHistory[sid] = item;
  266. updateIds.update.push({ sid, win_average, his_average: historyWinAverage, diff: fixFloat(win_average - historyWinAverage) });
  267. return;
  268. }
  269. const { timestamp } = item;
  270. solutionsHistory[sid].timestamp = timestamp;
  271. });
  272. // if (updateIds.add.length || updateIds.update.length) {
  273. // const solutionsList = Object.values(solutionsHistory).sort((a, b) => b.sol.win_average - a.sol.win_average);
  274. // Logs.outDev('solutions history update', JSON.stringify(solutionsList, null, 2), JSON.stringify(updateIds, null, 2));
  275. // }
  276. }
  277. }
  278. /**
  279. * 获取中单方案
  280. */
  281. const getSolutions = async () => {
  282. const gamesRelations = await getGamesRelation();
  283. const relationsMap = new Map(gamesRelations.map(item => [item.id, item.rel]));
  284. const solutions = Object.values(GAMES.Solutions).sort((a, b) => b.sol.win_average - a.sol.win_average);
  285. return solutions.map(item => {
  286. const { info: { id } } = item;
  287. const relation = relationsMap.get(id);
  288. return {
  289. ...item,
  290. info: { id, ...relation }
  291. }
  292. });
  293. }
  294. /**
  295. * 清理中单方案
  296. */
  297. const solutionsCleanup = () => {
  298. const solutionsHistory = GAMES.Solutions;
  299. Object.keys(solutionsHistory).forEach(sid => {
  300. const { timestamp } = solutionsHistory[sid];
  301. const nowTime = Date.now();
  302. if (nowTime - timestamp > 1000*60) {
  303. delete solutionsHistory[sid];
  304. Logs.out('solution history timeout', sid);
  305. return;
  306. }
  307. const solution = solutionsHistory[sid];
  308. const eventTime = solution.info.timestamp;
  309. if (nowTime > eventTime) {
  310. delete solutionsHistory[sid];
  311. Logs.out('solution history expired', sid);
  312. }
  313. });
  314. }
  315. const getDataFromChild = (type, callback) => {
  316. const id = ++Request.count;
  317. Request.callbacks[id] = callback;
  318. events_child.send({ method: 'get', id, type });
  319. }
  320. events_child.on('message', async (message) => {
  321. const { callbacks } = Request;
  322. const { method, id, type, data } = message;
  323. if (method == 'get' && id) {
  324. let responseData = null;
  325. if (type == 'getGamesRelation') {
  326. responseData = await getGamesRelation(true);
  327. }
  328. else if (type == 'getSolutionHistory') {
  329. responseData = getSolutionHistory();
  330. }
  331. events_child.send({ type: 'response', id, data: responseData });
  332. }
  333. else if (method == 'post') {
  334. if (type == 'setSolutions') {
  335. setSolutions(data);
  336. }
  337. }
  338. else if (method == 'response' && id && callbacks[id]) {
  339. callbacks[id](data);
  340. delete callbacks[id];
  341. }
  342. });
  343. relationSync();
  344. setInterval(() => {
  345. relationsCleanup();
  346. }, 5000);
  347. setInterval(() => {
  348. solutionsCleanup();
  349. }, 1000*30);
  350. module.exports = {
  351. updateGamesList, updateGamesEvents, getGamesList,
  352. updateGamesRelation, getGamesRelation, removeGamesRelation,
  353. getGamesEvents, getSolutions,
  354. }