Games.js 12 KB

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