Games.js 10 KB

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