GamesPs.js 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359
  1. const axios = require('axios');
  2. const Logs = require('../libs/logs');
  3. const Cache = require('../libs/cache');
  4. const Setting = require('./Setting');
  5. const { eventSolutions } = require('../triangle/eventSolutions');
  6. const { calcTotalProfit, calcTotalProfitWithFixedFirst } = require('../triangle/totalProfitCalc');
  7. const { getSetting, updateSetting } = require('../triangle/settings');
  8. const fs = require('fs');
  9. const path = require('path');
  10. const GamesCacheFile = path.join(__dirname, '../data/games.cache');
  11. const DevGameTastFile = path.join(__dirname, '../data/gameTast.json');
  12. const childOptions = process.env.NODE_ENV == 'development' ? {
  13. execArgv: ['--inspect=9230'],
  14. stdio: ['pipe', 'pipe', 'pipe', 'ipc']
  15. } : {};
  16. const { fork } = require('child_process');
  17. const events_child = fork('./triangle/eventsMatch.js', [], childOptions);
  18. const PS_IOR_KEYS = [
  19. ['0', 'ior_mh', 'ior_mn', 'ior_mc'],
  20. ['-1', 'ior_rh_15', 'ior_wmh_1', 'ior_rac_05'],
  21. ['-2', 'ior_rh_25', 'ior_wmh_2', 'ior_rac_15'],
  22. ['-3', 'ior_rh_35', 'ior_wmh_3', 'ior_rac_25'],
  23. ['-4', 'ior_rh_45', 'ior_wmh_4', 'ior_rac_35'],
  24. ['-5', 'ior_rh_55', 'ior_wmh_5', 'ior_rac_45'],
  25. ['+1', 'ior_rah_05', 'ior_wmc_1', 'ior_rc_15'],
  26. ['+2', 'ior_rah_15', 'ior_wmc_2', 'ior_rc_25'],
  27. ['+3', 'ior_rah_25', 'ior_wmc_3', 'ior_rc_35'],
  28. ['+4', 'ior_rah_35', 'ior_wmc_4', 'ior_rc_45'],
  29. ['+5', 'ior_rah_45', 'ior_wmc_5', 'ior_rc_55'],
  30. ['jqs', 'ior_ot_1', 'ior_ot_2', 'ior_ot_3', 'ior_ot_4', 'ior_ot_5', 'ior_ot_6', 'ior_ot_7'],
  31. ];
  32. const IOR_KEYS_TYPE = {
  33. A: 1, L: 1,
  34. D: 2, K: 2,
  35. }
  36. // 测试环境
  37. // const BASE_API_URL = 'https://dev.api.czxd8.com/api/p';
  38. const IS_DEV = process.env.NODE_ENV == 'development';
  39. const BASE_API_URL = IS_DEV ? 'https://api.qboss.vip/api' : 'http://172.17.222.37/api';
  40. const GAMES = {
  41. Leagues: {},
  42. Baselist: {},
  43. Relations: {},
  44. Solutions: {},
  45. ObOriginalData: {},
  46. UpdateTimestamp: {},
  47. };
  48. const Request = {
  49. callbacks: {},
  50. count: 0,
  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 getMarketType = (mk) => {
  65. if (mk == 0) {
  66. return 'early';
  67. }
  68. else if (mk == 2) {
  69. return 'rollball';
  70. }
  71. return 'today';
  72. }
  73. /**
  74. * 获取策略类型
  75. */
  76. const getRuleType = (rule) => {
  77. const rulePrefix = rule.split(':')[0];
  78. return IOR_KEYS_TYPE[rulePrefix];
  79. }
  80. /**
  81. * 关键词匹配比赛
  82. */
  83. const matchGame = (relation, sk) => {
  84. const keys = [];
  85. Object.keys(relation).forEach(platform => {
  86. const { leagueName, teamHomeName, teamAwayName } = relation[platform];
  87. if (platform == 'ps') {
  88. keys.push(leagueName);
  89. }
  90. keys.push(teamHomeName, teamAwayName);
  91. });
  92. return keys.some(key => key.includes(sk));
  93. }
  94. /**
  95. * 同步联赛列表
  96. */
  97. const syncLeaguesList = ({ mk, leagues }) => {
  98. if (IS_DEV) {
  99. return Logs.out('syncLeaguesList', { mk, leagues });
  100. }
  101. axios.post(`${BASE_API_URL}/p/syncLeague`, { mk, leagues }, { proxy: false })
  102. .then(res => {
  103. // Logs.out('syncLeaguesList', res.data);
  104. })
  105. .catch(err => {
  106. Logs.out('syncLeaguesList', err.message);
  107. });
  108. }
  109. /**
  110. * 更新联赛列表
  111. */
  112. const updateLeaguesList = ({ mk, leagues, platform='ps' }) => {
  113. const { Leagues } = GAMES;
  114. if (!Leagues[platform]) {
  115. Leagues[platform] = {};
  116. }
  117. const leaguesMap = Leagues[platform];
  118. const nowTime = Date.now();
  119. const expireTime = nowTime - 1000 * 60 * 5;
  120. if (!leaguesMap[mk]) {
  121. leaguesMap[mk] = {
  122. timestamp: 0,
  123. leagues: [],
  124. };
  125. }
  126. if (leaguesMap[mk].timestamp < expireTime ||
  127. JSON.stringify(leaguesMap[mk].leagues) != JSON.stringify(leagues)) {
  128. leaguesMap[mk].leagues = leagues;
  129. leaguesMap[mk].timestamp = nowTime;
  130. if (platform == 'ps') {
  131. syncLeaguesList({ mk, leagues });
  132. }
  133. return leagues.length;
  134. }
  135. return 0;
  136. }
  137. /**
  138. * 获取筛选过的联赛
  139. */
  140. const getFilteredLeagues = async (mk) => {
  141. return axios.get(`${BASE_API_URL}/p/getLeagueTast?mk=${mk ?? ''}`, { proxy: false })
  142. .then(res => {
  143. if (res.data.code == 0) {
  144. return res.data.data;
  145. }
  146. return Promise.reject(new Error(res.data.message));
  147. });
  148. }
  149. /**
  150. * 更新OB原始数据
  151. */
  152. const updateOriginalData = ({ leagues, matches }) => {
  153. const { ObOriginalData } = GAMES;
  154. ObOriginalData.leagues = leagues;
  155. ObOriginalData.matches = matches;
  156. ObOriginalData.timestamp = Date.now();
  157. }
  158. /**
  159. * 获取OB原始数据
  160. */
  161. const getOriginalData = () => {
  162. const { ObOriginalData } = GAMES;
  163. return ObOriginalData;
  164. }
  165. /**
  166. * 同步比赛列表到服务器
  167. */
  168. const syncGamesList = ({ platform, mk, games }) => {
  169. if (IS_DEV) {
  170. return Logs.out('syncGamesList', { platform, mk, games });
  171. }
  172. axios.post(`${BASE_API_URL}/p/syncGames`, { platform, mk, games })
  173. .then(res => {
  174. // Logs.out('syncGamesList', { platform, mk, count: games.length }, res.data);
  175. })
  176. .catch(err => {
  177. Logs.out('syncGamesList', { platform, mk }, err.message);
  178. });
  179. }
  180. /**
  181. * 同步基准比赛列表
  182. */
  183. const syncBaseList = ({ marketType, games }) => {
  184. const baseList = GAMES.Baselist;
  185. // 直接创建新列表
  186. if (!baseList[marketType]) {
  187. baseList[marketType] = games;
  188. return;
  189. }
  190. const newMap = new Map(games.map(item => [item.eventId, item]));
  191. // 删除不存在的项
  192. for (let i = baseList[marketType].length - 1; i >= 0; i--) {
  193. if (!newMap.has(baseList[marketType][i].eventId)) {
  194. baseList[marketType].splice(i, 1);
  195. }
  196. }
  197. // 添加或更新
  198. const oldIds = new Set(baseList[marketType].map(item => item.eventId));
  199. games.forEach(game => {
  200. if (!oldIds.has(game.eventId)) {
  201. // 添加新项
  202. baseList[marketType].push(game);
  203. }
  204. });
  205. }
  206. /**
  207. * 清理基准比赛列表
  208. */
  209. // const cleanupBaseList = () => {
  210. // const baseList = GAMES.Baselist;
  211. // const nowTime = Date.now();
  212. // const expireTime = nowTime - 1000*60*60*3;
  213. // Object.keys(baseList).forEach(marketType => {
  214. // baseList[marketType] = baseList[marketType].filter(item => item.timestamp < expireTime);
  215. // });
  216. // }
  217. /**
  218. * 更新比赛列表
  219. */
  220. const updateGamesList = (({ platform, mk, games } = {}) => {
  221. return new Promise((resolve, reject) => {
  222. if (!platform || !games) {
  223. return reject(new Error('PLATFORM_GAMES_INVALID'));
  224. }
  225. syncGamesList({ platform, mk, games });
  226. resolve();
  227. });
  228. });
  229. /**
  230. * 提交盘口数据
  231. */
  232. // const submitOdds = ({ platform, mk, games }) => {
  233. // if (IS_DEV) {
  234. // return Logs.out('syncOdds', { platform, mk, games });
  235. // }
  236. // axios.post(`${BASE_API_URL}/p/syncOdds`, { platform, mk, games}, { proxy: false })
  237. // .then(res => {
  238. // // Logs.out('syncOdds', { platform, mk, count: games.length }, res.data);
  239. // })
  240. // .catch(err => {
  241. // Logs.out('syncOdds', { platform, mk }, err.message);
  242. // });
  243. // }
  244. /**
  245. * 同步基准盘口
  246. */
  247. const syncBaseEvents = ({ mk, games, outrights }) => {
  248. Logs.out('syncBaseEvents', { mk, games });
  249. const {
  250. expireTimeEvents, expireTimeSpecial,
  251. subsidyTime, subsidyAmount,
  252. subsidyRbWmAmount, subsidyRbOtAmount
  253. } = getSetting();
  254. const nowTime = Date.now();
  255. const marketType = getMarketType(mk);
  256. const baseList = GAMES.Baselist;
  257. if (!baseList[marketType]) {
  258. return 0;
  259. }
  260. const baseMap = new Map(baseList[marketType].map(item => [item.eventId, item]));
  261. games?.forEach(game => {
  262. const { eventId, originId, stage, retime, score, wm, evtime, events } = game;
  263. const baseGame = baseMap.get(eventId);
  264. if (baseGame) {
  265. const { mk, timestamp } = baseGame;
  266. const isRb = (mk == 2);
  267. const isSubsidy = timestamp > nowTime && timestamp < nowTime + 1000*60*60*subsidyTime;
  268. Object.keys(events).forEach(ior => {
  269. if (isRb && (ior.startsWith('ior_r') || ior.startsWith('ior_m') || ior.startsWith('ior_wm')) && subsidyRbWmAmount) {
  270. // 滚球胜平负
  271. const sourceOdds = events[ior].v;
  272. events[ior].v = fixFloat(sourceOdds * (1 + subsidyRbWmAmount), 3);
  273. events[ior].s = sourceOdds
  274. }
  275. else if (isRb && ior.startsWith('ior_ot') && subsidyRbOtAmount) {
  276. // 滚球进球数
  277. const sourceOdds = events[ior].v;
  278. events[ior].v = fixFloat(sourceOdds * (1 + subsidyRbOtAmount), 3);
  279. events[ior].s = sourceOdds
  280. }
  281. else if (!isRb && isSubsidy && ior.startsWith('ior_ot') && subsidyAmount) {
  282. // 赛前进球数
  283. const sourceOdds = events[ior].v;
  284. events[ior].v = fixFloat(sourceOdds * (1 + subsidyAmount), 3);
  285. events[ior].s = sourceOdds
  286. }
  287. });
  288. baseGame.originId = originId;
  289. baseGame.stage = stage;
  290. baseGame.retime = retime;
  291. baseGame.score = score;
  292. baseGame.wm = wm;
  293. baseGame.evtime = evtime;
  294. baseGame.events = events;
  295. }
  296. });
  297. outrights?.forEach(outright => {
  298. const { parentId, sptime, special } = outright;
  299. const baseGame = baseMap.get(parentId);
  300. if (baseGame) { // 赛前特殊盘口
  301. const { timestamp } = baseGame;
  302. const isSubsidy = timestamp > nowTime && timestamp < nowTime + 1000*60*60*subsidyTime;
  303. if (isSubsidy) {
  304. Object.keys(special).forEach(ior => {
  305. if (ior.startsWith('ior_ot') && subsidyAmount) {
  306. const sourceOdds = special[ior].v;
  307. special[ior].v = fixFloat(sourceOdds * (1 + subsidyAmount), 3);
  308. special[ior].s = sourceOdds
  309. }
  310. });
  311. }
  312. baseGame.sptime = sptime;
  313. baseGame.special = special;
  314. }
  315. else {
  316. const originBaseMap = new Map(baseList[marketType].map(item => [item.originId, item]));
  317. const originBaseGame = originBaseMap.get(parentId);
  318. if (originBaseGame) { // 滚球特殊盘口
  319. Object.keys(special).forEach(ior => {
  320. if (ior.startsWith('ior_wm') && subsidyRbWmAmount) {
  321. const sourceOdds = special[ior].v;
  322. special[ior].v = fixFloat(sourceOdds * (1 + subsidyRbWmAmount), 3);
  323. special[ior].s = sourceOdds
  324. }
  325. else if (ior.startsWith('ior_ot') && subsidyRbOtAmount) {
  326. const sourceOdds = special[ior].v;
  327. special[ior].v = fixFloat(sourceOdds * (1 + subsidyRbOtAmount), 3);
  328. special[ior].s = sourceOdds
  329. }
  330. });
  331. originBaseGame.sptime = sptime;
  332. originBaseGame.special = special;
  333. }
  334. }
  335. });
  336. if (games?.length) {
  337. const gamesList = baseList[marketType]?.map(game => {
  338. const { evtime, events, sptime, special, ...gameInfo } = game;
  339. const expireTimeEv = nowTime - expireTimeEvents;
  340. const expireTimeSP = nowTime - expireTimeSpecial;
  341. let odds = {};
  342. if (evtime > expireTimeEv) {
  343. odds = { ...odds, ...events };
  344. }
  345. if (sptime > expireTimeSP) {
  346. odds = { ...odds, ...special };
  347. }
  348. const matches = PS_IOR_KEYS.map(([label, ...keys]) => {
  349. let match = keys.map(key => {
  350. return {
  351. key,
  352. value: odds[key]?.v ?? 0,
  353. origin: odds[key]?.r,
  354. source: odds[key]?.s,
  355. }
  356. });
  357. if (label == 'jqs') {
  358. match = match.filter(item => item.value !== 0);
  359. }
  360. return {
  361. label,
  362. match
  363. };
  364. }).filter(item => {
  365. if (item.label == 'jqs') {
  366. return item.match.length;
  367. }
  368. else {
  369. return item.match.every(entry => entry.value !== 0);
  370. }
  371. });
  372. game.matches = matches; // matches 也记录下来
  373. let uptime = evtime ?? 0;
  374. return { ...gameInfo, matches, uptime };
  375. });
  376. // if (gamesList.filter(item => item.uptime > 0).length) {
  377. // // if (mk == 2) {
  378. // // Logs.out('syncBaseEvents', JSON.stringify({ mk, gamesList }, null, 2));
  379. // // }
  380. // submitOdds({ platform: 'ps', mk, games: gamesList });
  381. // }
  382. const relatedGames = Object.values(GAMES.Relations).map(item => item.rel?.['ps'] ?? {});
  383. if (!relatedGames.length) {
  384. return 0;
  385. }
  386. let update = 0;
  387. const relatedMap = new Map(relatedGames.map(item => [item.eventId, item]));
  388. gamesList?.forEach(game => {
  389. const { eventId, matches, uptime, stage, retime, score, wm } = game;
  390. const relatedGame = relatedMap.get(eventId);
  391. if (relatedGame) {
  392. const events = {};
  393. matches.forEach(({ label, match }) => {
  394. match.forEach(({ key, value, origin, source }) => {
  395. events[key] = {
  396. v: value,
  397. r: origin,
  398. s: source
  399. };
  400. });
  401. });
  402. relatedGame.evtime = uptime;
  403. relatedGame.events = events;
  404. relatedGame.stage = stage;
  405. relatedGame.retime = retime;
  406. relatedGame.score = score;
  407. relatedGame.wm = wm;
  408. update ++;
  409. }
  410. });
  411. return update;
  412. }
  413. }
  414. const updateBaseEvents = ({ games, timestamp }) => {
  415. return new Promise((resolve, reject) => {
  416. if (!games) {
  417. return reject(new Error('GAMES_INVALID'));
  418. }
  419. if (!timestamp) {
  420. return reject(new Error('TIMESTAMP_INVALID'));
  421. }
  422. let update = 0;
  423. const { UpdateTimestamp } = GAMES;
  424. const tp = 'ps_9_9';
  425. if (!UpdateTimestamp[tp] || UpdateTimestamp[tp] < timestamp) {
  426. UpdateTimestamp[tp] = timestamp;
  427. }
  428. else {
  429. return resolve({ update });
  430. }
  431. Object.keys(games).forEach(mk => {
  432. update += syncBaseEvents({ mk, games: games[mk] ?? [] });
  433. });
  434. resolve({ update });
  435. });
  436. }
  437. const updateGamesEvents = ({ platform, mk, games, outrights, timestamp, tp }) => {
  438. return new Promise((resolve, reject) => {
  439. if (!platform || (!games && !outrights)) {
  440. return reject(new Error('PLATFORM_GAMES_INVALID'));
  441. }
  442. const { UpdateTimestamp } = GAMES;
  443. if (!UpdateTimestamp[tp] || UpdateTimestamp[tp] < timestamp) {
  444. // Logs.out('updateGamesEvents', { tp, timestamp });
  445. UpdateTimestamp[tp] = timestamp;
  446. }
  447. else {
  448. return resolve({ update: 0 });
  449. }
  450. if (platform == 'ps') {
  451. const update = syncBaseEvents({ mk, games, outrights });
  452. return resolve({ update });
  453. }
  454. const relatedGames = Object.values(GAMES.Relations).map(item => item.rel?.[platform] ?? {});
  455. if (!relatedGames.length) {
  456. return resolve({ update: 0 });
  457. }
  458. const updateCount = {
  459. update: 0
  460. };
  461. const relatedMap = new Map(relatedGames.map(item => [item.eventId, item]));
  462. games?.forEach(game => {
  463. const { eventId, evtime, events, stage, retime, score, wm } = game;
  464. const relatedGame = relatedMap.get(eventId);
  465. if (relatedGame) {
  466. relatedGame.evtime = evtime;
  467. relatedGame.events = events;
  468. relatedGame.stage = stage;
  469. relatedGame.retime = retime;
  470. relatedGame.score = score;
  471. relatedGame.wm = wm;
  472. updateCount.update ++;
  473. }
  474. });
  475. outrights?.forEach(outright => {
  476. const { parentId, sptime, special } = outright;
  477. const relatedGame = relatedMap.get(parentId);
  478. if (relatedGame) {
  479. relatedGame.sptime = sptime;
  480. relatedGame.special = special;
  481. updateCount.update ++;
  482. }
  483. });
  484. resolve(updateCount);
  485. });
  486. }
  487. /**
  488. * 获取比赛盘口
  489. */
  490. const getGamesEvents = ({ platform, relIds = [] } = {}) => {
  491. if (!relIds.length) {
  492. return null;
  493. }
  494. const idSet = new Set(relIds);
  495. const relations = { ...GAMES.Relations };
  496. Object.keys(relations).forEach(id => {
  497. if (idSet.size && !idSet.has(+id)) {
  498. delete relations[id];
  499. }
  500. });
  501. if (platform) {
  502. return Object.values(relations).map(rel => rel[platform] ?? {});
  503. }
  504. const gamesEvents = {};
  505. Object.values(relations).forEach(({ rel }) => {
  506. Object.keys(rel).forEach(platform => {
  507. const game = rel[platform] ?? {};
  508. const { eventId, events, special } = game;
  509. if (!gamesEvents[platform]) {
  510. gamesEvents[platform] = {};
  511. }
  512. gamesEvents[platform][eventId] = { ...events, ...special };
  513. });
  514. });
  515. return gamesEvents;
  516. }
  517. /**
  518. * 获取关联比赛
  519. */
  520. const getDevGameTast = () => {
  521. return new Promise((resolve) => {
  522. const data = Cache.getData(DevGameTastFile, true);
  523. resolve({data});
  524. });
  525. }
  526. const fetchGamesRelation = async (mk='') => {
  527. const getGameTast = Promise.all([
  528. getDevGameTast(),
  529. axios.get(`${BASE_API_URL}/p/getGameTast?mk=${mk}`, { proxy: false })
  530. ]);
  531. return getGameTast.then(([res1, res2]) => {
  532. const resData = res1.data ?? res2.data;
  533. if (resData.code == 0) {
  534. const nowTime = Date.now();
  535. const gamesRelation = resData.data?.filter(item => {
  536. const timestamp = new Date(item.timestamp).getTime();
  537. if (nowTime > timestamp) {
  538. item.mk = 2;
  539. }
  540. item.timestamp = timestamp;
  541. const expireTime = timestamp + 1000*60*60*3;
  542. return expireTime > nowTime;
  543. }).map(item => {
  544. const {
  545. id, mk, league_name,
  546. event_id: ps_event_id,
  547. league_id: ps_league_id,
  548. team_home_name: ps_team_home_name,
  549. team_away_name: ps_team_away_name,
  550. ob_event_id, ob_league_id,
  551. ob_team_home_name,
  552. ob_team_away_name,
  553. hg_event_id, hg_league_id,
  554. hg_team_home_name,
  555. hg_team_away_name,
  556. timestamp,
  557. } = item;
  558. const rel = {
  559. ps: {
  560. eventId: +ps_event_id,
  561. leagueId: +ps_league_id,
  562. leagueName: league_name,
  563. teamHomeName: ps_team_home_name,
  564. teamAwayName: ps_team_away_name,
  565. timestamp
  566. },
  567. ob: ob_event_id ? {
  568. eventId: +ob_event_id,
  569. leagueId: +ob_league_id,
  570. leagueName: league_name,
  571. teamHomeName: ob_team_home_name,
  572. teamAwayName: ob_team_away_name,
  573. timestamp
  574. } : null,
  575. hg: hg_event_id ? {
  576. eventId: +hg_event_id,
  577. leagueId: +hg_league_id,
  578. leagueName: league_name,
  579. teamHomeName: hg_team_home_name,
  580. teamAwayName: hg_team_away_name,
  581. timestamp
  582. } : null
  583. };
  584. return { id: ps_event_id, mk, rel, timestamp };
  585. }) ?? [];
  586. return gamesRelation;
  587. }
  588. return Promise.reject(new Error(resData.message));
  589. });
  590. }
  591. const getGamesRelation = ({ mk=-1, ids, listEvents } = {}) => {
  592. let idsSet = null;
  593. if (ids?.length) {
  594. idsSet = new Set(ids);
  595. }
  596. const relations = Object.values(GAMES.Relations).filter(item => {
  597. if (idsSet && !idsSet.has(item.id)) {
  598. return false;
  599. }
  600. return mk == -1 || item.mk == mk;
  601. }).sort((a, b) => a.timestamp - b.timestamp);
  602. if (listEvents) {
  603. return relations;
  604. }
  605. const gamesRelation = relations.map(item => {
  606. const { rel, ...relationInfo } = item;
  607. const tempRel = { ...rel };
  608. Object.keys(tempRel).forEach(platform => {
  609. const { events, evtime, sptime, special, ...gameInfo } = tempRel[platform];
  610. tempRel[platform] = gameInfo;
  611. });
  612. return { ...relationInfo, rel: tempRel };
  613. });
  614. return gamesRelation;
  615. }
  616. /**
  617. * 定时更新关联比赛列表
  618. */
  619. const updateGamesRelation = () => {
  620. fetchGamesRelation()
  621. .then(gamesRelation => {
  622. const baseList = {};
  623. gamesRelation.map(item => {
  624. const baseGame = item.rel?.['ps'] ?? {};
  625. return { ...baseGame, mk: item.mk };
  626. }).forEach(item => {
  627. const marketType = getMarketType(item.mk);
  628. if (!baseList[marketType]) {
  629. baseList[marketType] = [];
  630. }
  631. baseList[marketType].push(item);
  632. });
  633. Object.keys(baseList).forEach(marketType => {
  634. syncBaseList({ marketType, games: baseList[marketType] });
  635. });
  636. const updateCount = {
  637. add: 0,
  638. update: 0,
  639. delete: 0
  640. };
  641. gamesRelation.forEach(item => {
  642. const { id, mk } = item;
  643. const oldItem = GAMES.Relations[id];
  644. if (!oldItem) {
  645. GAMES.Relations[id] = item;
  646. updateCount.add ++;
  647. }
  648. else if (oldItem.mk != mk) {
  649. GAMES.Relations[id] = item;
  650. updateCount.update ++;
  651. }
  652. });
  653. const relations = new Set(gamesRelation.map(item => +item.id));
  654. Object.keys(GAMES.Relations).forEach(id => {
  655. if (!relations.has(+id)) {
  656. delete GAMES.Relations[id];
  657. updateCount.delete ++;
  658. }
  659. });
  660. if (updateCount.add || updateCount.update || updateCount.delete) {
  661. Logs.out('updateGamesRelation', updateCount);
  662. }
  663. else {
  664. Logs.outDev('updateGamesRelation', updateCount);
  665. }
  666. })
  667. .catch(err => {
  668. Logs.out('updateGamesRelation', err.message);
  669. })
  670. .finally(() => {
  671. setTimeout(updateGamesRelation, 60000);
  672. });
  673. }
  674. updateGamesRelation();
  675. const gamesRelationCleanup = () => {
  676. const relations = Object.values(GAMES.Relations);
  677. const expireTime = Date.now() - 1000*60;
  678. relations.forEach(item => {
  679. const { rel } = item;
  680. Object.keys(rel).forEach(platform => {
  681. const { evtime, sptime } = rel[platform];
  682. if (evtime && evtime < expireTime) {
  683. delete rel[platform].events;
  684. delete rel[platform].evtime;
  685. }
  686. if (sptime && sptime < expireTime) {
  687. delete rel[platform].special;
  688. delete rel[platform].sptime;
  689. }
  690. });
  691. });
  692. }
  693. /**
  694. * 同步比赛结果
  695. */
  696. const syncGamesResult = async (result) => {
  697. if (IS_DEV) {
  698. return Logs.out('updateGamesResult', result);
  699. }
  700. // axios.post(`${BASE_API_URL}/p/syncMatchResult`, result, { proxy: false })
  701. // .then(res => {
  702. // // Logs.out('syncMatchResult', res.data);
  703. // })
  704. // .catch(err => {
  705. // Logs.out('syncMatchResult', err.message);
  706. // });
  707. }
  708. /**
  709. * 更新比赛结果
  710. */
  711. const updateGamesResult = (result) => {
  712. syncGamesResult(result);
  713. return Promise.resolve();
  714. }
  715. /**
  716. * 同步中单方案
  717. */
  718. // const syncSolutions = (solutions) => {
  719. // if (IS_DEV) {
  720. // return Logs.out('syncSolutions', solutions);
  721. // }
  722. // axios.post(`${BASE_API_URL}/p/syncDsOpportunity`, solutions, { proxy: false })
  723. // .then(res => {
  724. // // Logs.out('syncSolutions', res.data);
  725. // })
  726. // .catch(err => {
  727. // Logs.out('syncSolutions', err.message);
  728. // });
  729. // }
  730. /**
  731. * 更新中单方案
  732. */
  733. const getCprKey = (cpr) => {
  734. const { k, p, v } = cpr;
  735. return `${k}_${p}_${v}`;
  736. }
  737. const compareCpr = (cpr1, cpr2) => {
  738. const key1 = getCprKey(cpr1);
  739. const key2 = getCprKey(cpr2);
  740. return key1 === key2;
  741. }
  742. const updateSolutions = (solutions, eventsLogsMap) => {
  743. if (solutions?.length) {
  744. const solutionsHistory = GAMES.Solutions;
  745. const updateIds = { add: [], update: [], retain: [], remove: [] }
  746. solutions.forEach(item => {
  747. const { sid, cpr, sol: { win_average, win_profit_rate } } = item;
  748. if (!solutionsHistory[sid]) {
  749. solutionsHistory[sid] = item;
  750. updateIds.add.push(sid);
  751. return;
  752. }
  753. const historySolution = solutionsHistory[sid];
  754. if (historySolution.sol.win_average !== win_average ||
  755. historySolution.sol.win_profit_rate !== win_profit_rate ||
  756. !compareCpr(historySolution.cpr, cpr)) {
  757. solutionsHistory[sid] = item;
  758. updateIds.update.push(sid);
  759. return;
  760. }
  761. const { timestamp } = item;
  762. historySolution.timestamp = timestamp;
  763. updateIds.retain.push(sid);
  764. });
  765. const solutionsMap = new Map(solutions.map(item => [item.sid, item]));
  766. Object.keys(solutionsHistory).forEach(sid => {
  767. if (!solutionsMap.has(sid)) {
  768. delete solutionsHistory[sid];
  769. updateIds.remove.push(sid);
  770. }
  771. });
  772. const solutionUpdate = {};
  773. Object.keys(updateIds).forEach(key => {
  774. if (key == 'retain' || key == 'remove') {
  775. solutionUpdate[key] = updateIds[key];
  776. }
  777. else {
  778. solutionUpdate[key] = updateIds[key].map(sid => solutionsHistory[sid]);
  779. }
  780. });
  781. // syncSolutions(solutionUpdate);
  782. if (updateIds.add.length / solutions.length > 0.25 ||
  783. updateIds.remove.length / solutions.length > 0.25
  784. ) {
  785. const { expireEvents, removeEvents } = eventsLogsMap;
  786. const expireEvemtsMap = {};
  787. expireEvents.forEach(item => {
  788. const { mk, platform, info, evExpire, spExpire, evtime, sptime } = item;
  789. if (!expireEvemtsMap[mk]) {
  790. expireEvemtsMap[mk] = {};
  791. }
  792. if (!expireEvemtsMap[mk][platform]) {
  793. expireEvemtsMap[mk][platform] = {};
  794. }
  795. if (!expireEvemtsMap[mk][platform].list) {
  796. expireEvemtsMap[mk][platform].list = [];
  797. }
  798. if (!expireEvemtsMap[mk][platform].evtime) {
  799. expireEvemtsMap[mk][platform].evtime = evtime;
  800. }
  801. if (!expireEvemtsMap[mk][platform].sptime) {
  802. expireEvemtsMap[mk][platform].sptime = sptime;
  803. }
  804. expireEvemtsMap[mk][platform].list.push({ info, evExpire, spExpire, evtime, sptime });
  805. });
  806. Object.keys(expireEvemtsMap).forEach(mk => {
  807. Object.keys(expireEvemtsMap[mk]).forEach(platform => {
  808. Logs.out('invalid events, mk %d, platform %s, expire %d, evtime %d, sptime %d',
  809. mk, platform,
  810. expireEvemtsMap[mk][platform].list.length,
  811. expireEvemtsMap[mk][platform].evtime,
  812. expireEvemtsMap[mk][platform].sptime,
  813. )
  814. });
  815. });
  816. Logs.out('solutions add %d, update %d, retain %d, remove %d',
  817. updateIds.add.length, updateIds.update.length, updateIds.retain.length, updateIds.remove.length);
  818. }
  819. else {
  820. Logs.outDev('solutions update complete', updateIds);
  821. }
  822. }
  823. }
  824. /**
  825. * 获取中单方案
  826. */
  827. const getSolutions = async ({ win_min, with_events, mk=-1 }) => {
  828. // Logs.out('getSolutions', win_min);
  829. const filterMarketType = +mk;
  830. const { minShowAmount } = getSetting();
  831. const solutionsList = Object.values(GAMES.Solutions);
  832. const gamesRelation = getGamesRelation();
  833. const relationsMap = new Map(gamesRelation.map(item => [item.id, item]));
  834. const mkCount = {
  835. all: 0,
  836. rollball: 0,
  837. today: 0,
  838. early: 0,
  839. }
  840. let solutions = solutionsList.filter(item => {
  841. const { sol: { win_average } } = item;
  842. return win_average >= (win_min ?? minShowAmount);
  843. })
  844. .map(item => {
  845. const { info: { id } } = item;
  846. const { mk, rel } = relationsMap.get(id);
  847. const marketType = getMarketType(mk);
  848. mkCount.all ++;
  849. mkCount[marketType] ++;
  850. return {
  851. ...item,
  852. info: { id, mk, ...rel }
  853. }
  854. });
  855. if (mk >= 0) {
  856. solutions = solutions.filter(item => {
  857. const { info: { mk } } = item;
  858. return mk == filterMarketType;
  859. });
  860. }
  861. solutions = solutions.sort((a, b) => b.sol.win_average - a.sol.win_average);
  862. const relIds = solutions.map(item => item.info.id);
  863. let gamesEvents;
  864. if (with_events) {
  865. gamesEvents = getGamesEvents({ relIds });
  866. }
  867. return { solutions, gamesEvents, mkCount };
  868. }
  869. /**
  870. * 获取中单方案并按照比赛分组
  871. */
  872. const getGamesSolutions = async ({ win_min, with_events, mk=-1, tp=0, sk }) => {
  873. const filterMarketType = +mk;
  874. const filterDataType = +tp;
  875. const { minShowAmount } = getSetting();
  876. const solutionsList = Object.values(GAMES.Solutions);
  877. const gamesRelation = getGamesRelation({ listEvents: with_events });
  878. const relationsMap = new Map(gamesRelation.map(item => [item.id, item]));
  879. const mkCount = {
  880. all: 0,
  881. rollball: 0,
  882. today: 0,
  883. early: 0,
  884. }
  885. const solutionsMap = {};
  886. solutionsList.forEach(item => {
  887. const { info: { id }, ...solution } = item;
  888. const { rule, sol: { win_average } } = solution;
  889. const ruleType = getRuleType(rule);
  890. if ((filterDataType == 0 || filterDataType == ruleType) && (!!sk || win_average >= (win_min ?? minShowAmount))) {
  891. const gameRelation = relationsMap.get(id);
  892. if (!solutionsMap[id]) {
  893. solutionsMap[id] = { ...gameRelation, solutions: [] };
  894. }
  895. solutionsMap[id].solutions.push(solution);
  896. }
  897. })
  898. const gamesSolutions = Object.values(solutionsMap)
  899. .filter(item => {
  900. const { mk, rel, solutions } = item;
  901. const marketType = getMarketType(mk);
  902. if (!sk || matchGame(rel, sk)) {
  903. mkCount.all ++;
  904. mkCount[marketType] ++;
  905. solutions.sort((a, b) => b.sol.win_average - a.sol.win_average);
  906. return filterMarketType == -1 || filterMarketType == mk;
  907. }
  908. return false;
  909. })
  910. .sort((a, b) => b.solutions[0].sol.win_average - a.solutions[0].sol.win_average);
  911. return { gamesSolutions, mkCount };
  912. }
  913. /**
  914. * 获取单个中单方案
  915. */
  916. const getSolution = async (sid) => {
  917. if (!sid) {
  918. return Promise.reject(new Error('sid is required'));
  919. }
  920. const solution = GAMES.Solutions[sid];
  921. if (!solution) {
  922. return Promise.reject(new Error('solution not found'));
  923. }
  924. return solution;
  925. }
  926. /**
  927. * 通过比赛 ID 获取中单方案
  928. */
  929. const getSolutionsByIds = async (ids) => {
  930. const baseList = Object.values(GAMES.Baselist).flat();
  931. const baseMap = new Map(baseList.map(item => [item.eventId, item]));
  932. const result = {};
  933. ids.forEach(id => {
  934. const baseGame = baseMap.get(id);
  935. result[id] = {};
  936. result[id].matches = baseGame?.matches ?? [];
  937. result[id].sols = [];
  938. });
  939. Object.values(GAMES.Solutions).forEach(item => {
  940. const { info: { id } } = item;
  941. if (result[id]) {
  942. result[id].sols.push(item);
  943. }
  944. });
  945. return result;
  946. }
  947. /**
  948. * 清理中单方案
  949. */
  950. const solutionsCleanup = () => {
  951. const solutionsHistory = GAMES.Solutions;
  952. const updateIds = { remove: [] }
  953. Object.keys(solutionsHistory).forEach(sid => {
  954. const { timestamp } = solutionsHistory[sid];
  955. const nowTime = Date.now();
  956. if (nowTime - timestamp > 1000*60) {
  957. delete solutionsHistory[sid];
  958. updateIds.remove.push(sid);
  959. return;
  960. }
  961. const solution = solutionsHistory[sid];
  962. const eventTime = solution.info.timestamp;
  963. if (nowTime > eventTime) {
  964. delete solutionsHistory[sid];
  965. updateIds.remove.push(sid);
  966. }
  967. });
  968. // if (updateIds.remove.length) {
  969. // syncSolutions(updateIds);
  970. // }
  971. }
  972. /**
  973. * 清理更新时间戳
  974. */
  975. const cleanupUpdateTimestamp = () => {
  976. const updateTimestamp = GAMES.UpdateTimestamp;
  977. const nowTime = Date.now();
  978. const expireTime = nowTime - 1000*60;
  979. Object.keys(updateTimestamp).forEach(key => {
  980. if (updateTimestamp[key] < expireTime) {
  981. delete updateTimestamp[key];
  982. }
  983. });
  984. }
  985. /**
  986. * 定时清理中单方案
  987. * 定时清理盘口信息
  988. */
  989. setInterval(() => {
  990. // cleanupBaseList();
  991. solutionsCleanup();
  992. gamesRelationCleanup();
  993. cleanupUpdateTimestamp();
  994. }, 1000*30);
  995. /**
  996. * 获取综合利润
  997. */
  998. const getTotalProfit = async (sol1, sol2, inner_base, inner_rebate) => {
  999. const { innerDefaultAmount, innerRebateRatio } = getSetting();
  1000. inner_base = inner_base ? +inner_base : innerDefaultAmount;
  1001. inner_rebate = inner_rebate ? +inner_rebate : fixFloat(innerRebateRatio / 100, 3);
  1002. const profit = calcTotalProfit(sol1, sol2, inner_base, inner_rebate);
  1003. return profit;
  1004. }
  1005. /**
  1006. * 通过 sid 获取综合利润
  1007. */
  1008. const getTotalProfitWithSid = async (sid1, sid2, inner_base, inner_rebate) => {
  1009. const preSolution = GAMES.Solutions[sid1];
  1010. const subSolution = GAMES.Solutions[sid2];
  1011. const sol1 = preSolution?.sol;
  1012. const sol2 = subSolution?.sol;
  1013. if (!sol1) {
  1014. return Promise.reject(new Error('sid1 已失效'));
  1015. }
  1016. if (!sol2) {
  1017. return Promise.reject(new Error('sid2 已失效'));
  1018. }
  1019. const profit = await getTotalProfit(sol1, sol2, inner_base, inner_rebate);
  1020. return { profit, solutions: [preSolution, subSolution] };
  1021. }
  1022. /**
  1023. * 通过盘口信息获取综合利润
  1024. */
  1025. const getTotalProfitWithBetInfo = async (betInfo1, betInfo2, fixed=false, inner_base, inner_rebate) => {
  1026. if (!betInfo1?.cross_type) {
  1027. return Promise.reject(new Error('第一个下注信息无效'));
  1028. }
  1029. if (!betInfo2?.cross_type) {
  1030. return Promise.reject(new Error('第二个下注信息无效'));
  1031. }
  1032. const { innerDefaultAmount, innerRebateRatio } = getSetting();
  1033. inner_base = inner_base ? +inner_base : innerDefaultAmount;
  1034. inner_rebate = inner_rebate ? +inner_rebate : fixFloat(innerRebateRatio / 100, 3);
  1035. if (fixed) {
  1036. return calcTotalProfitWithFixedFirst(betInfo1, betInfo2, inner_base, inner_rebate);
  1037. }
  1038. const [sol1, sol2] = [betInfo1, betInfo2].map(betinfo => eventSolutions({...betinfo, inner_base, inner_rebate }));
  1039. return getTotalProfit(sol1, sol2, inner_base, inner_rebate);
  1040. }
  1041. /**
  1042. * 同步Qboss平台配置
  1043. */
  1044. const syncQbossConfig = () => {
  1045. const setting = getSetting();
  1046. if (!setting.syncSettingEnabled) {
  1047. // Logs.outDev('syncQbossConfig disabled');
  1048. return setTimeout(syncQbossConfig, 1000*5);
  1049. }
  1050. axios.get(`${BASE_API_URL}/p/QbossSystemConfig`, { proxy: false })
  1051. .then(res => {
  1052. const { data } = res;
  1053. if (!data?.data) {
  1054. throw new Error('syncQbossConfig data is empty');
  1055. }
  1056. const {
  1057. qboss_return_ratio,
  1058. ob_return_ratio, ob_return_type,
  1059. hg_return_ratio, hg_return_type,
  1060. qboss_jq_add_odds, qboss_jq_add_hours,
  1061. qboss_gq_add_dy_odds, qboss_gq_add_jq_odds,
  1062. } = data.data;
  1063. const qbossSetting = {
  1064. innerRebateRatio: +qboss_return_ratio,
  1065. obRebateRatio: +ob_return_ratio,
  1066. obRebateType: +ob_return_type,
  1067. hgRebateRatio: +hg_return_ratio,
  1068. hgRebateType: +hg_return_type,
  1069. subsidyTime: +qboss_jq_add_hours,
  1070. subsidyAmount: +qboss_jq_add_odds,
  1071. subsidyRbWmAmount: +qboss_gq_add_dy_odds,
  1072. subsidyRbOtAmount: +qboss_gq_add_jq_odds,
  1073. };
  1074. const settingFields = {};
  1075. let needUpdate = false;
  1076. Object.keys(qbossSetting).forEach(key => {
  1077. if (qbossSetting[key] !== setting[key]) {
  1078. settingFields[key] = qbossSetting[key];
  1079. needUpdate = true;
  1080. }
  1081. });
  1082. if (needUpdate) {
  1083. Setting.update(settingFields);
  1084. // Logs.outDev('syncQbossConfig', settingFields);
  1085. }
  1086. // else {
  1087. // Logs.outDev('syncQbossConfig no change');
  1088. // }
  1089. })
  1090. .catch(err => {
  1091. Logs.out('syncQbossConfig error', err.message);
  1092. })
  1093. .finally(() => {
  1094. setTimeout(syncQbossConfig, 1000*15);
  1095. });
  1096. }
  1097. syncQbossConfig();
  1098. /**
  1099. * 异常通知
  1100. */
  1101. const notifyException = (message) => {
  1102. if (IS_DEV) {
  1103. return Logs.out('notifyException', { message });
  1104. }
  1105. const chat_id = -1003032820471;
  1106. axios.get(`${BASE_API_URL}/telegram/jump`, { params: { chat_id, message } }, { proxy: false })
  1107. .then(() => {
  1108. Logs.out('notifyException', '通知成功');
  1109. })
  1110. .catch(err => {
  1111. Logs.out('notifyException', err.message);
  1112. });
  1113. }
  1114. /**
  1115. * 从子进程获取数据
  1116. */
  1117. const getDataFromChild = (type, callback) => {
  1118. const id = ++Request.count;
  1119. Request.callbacks[id] = callback;
  1120. events_child.send({ method: 'get', id, type });
  1121. }
  1122. /**
  1123. * 向子进程发送数据
  1124. */
  1125. const postDataToChild = (type, data) => {
  1126. events_child.send({ method: 'post', type, data });
  1127. }
  1128. /**
  1129. * 处理子进程消息
  1130. */
  1131. events_child.on('message', async (message) => {
  1132. const { callbacks } = Request;
  1133. const { method, id, type, data } = message;
  1134. if (method == 'get' && id) {
  1135. let responseData = null;
  1136. if (type == 'getGamesRelation') {
  1137. responseData = getGamesRelation({ listEvents: true });
  1138. }
  1139. else if (type == 'getSetting') {
  1140. responseData = getSetting();
  1141. }
  1142. // else if (type == 'getSolutionHistory') {
  1143. // responseData = getSolutionHistory();
  1144. // }
  1145. events_child.send({ type: 'response', id, data: responseData });
  1146. }
  1147. else if (method == 'post') {
  1148. if (type == 'updateSolutions') {
  1149. updateSolutions(data.solutions, data.eventsLogsMap);
  1150. }
  1151. }
  1152. else if (method == 'response' && id && callbacks[id]) {
  1153. callbacks[id](data);
  1154. delete callbacks[id];
  1155. }
  1156. });
  1157. events_child.stderr?.on('data', data => {
  1158. Logs.out('events_child stderr', data.toString());
  1159. });
  1160. const settingUpdate = (fields) => {
  1161. updateSetting(fields);
  1162. postDataToChild('updateSetting', fields);
  1163. }
  1164. const syncSetting = async () => {
  1165. const setting = await Setting.get();
  1166. settingUpdate(setting.toObject());
  1167. }
  1168. syncSetting();
  1169. Setting.onUpdate(settingUpdate);
  1170. /**
  1171. * 保存GAMES数据到缓存文件
  1172. */
  1173. const saveGamesToCache = () => {
  1174. Cache.setData(GamesCacheFile, GAMES, err => {
  1175. if (err) {
  1176. Logs.out('Failed to save games cache:', err.message);
  1177. }
  1178. else {
  1179. Logs.out('Games cache saved successfully');
  1180. }
  1181. });
  1182. }
  1183. /**
  1184. * 从缓存文件加载GAMES数据
  1185. */
  1186. const loadGamesFromCache = () => {
  1187. const gamesCacheData = Cache.getData(GamesCacheFile, true);
  1188. Object.keys(GAMES).forEach(key => {
  1189. if (gamesCacheData[key]) {
  1190. GAMES[key] = gamesCacheData[key];
  1191. }
  1192. });
  1193. Logs.out('Games cache loaded successfully');
  1194. }
  1195. // 在模块加载时尝试从缓存恢复数据
  1196. loadGamesFromCache();
  1197. // 监听进程退出事件,保存GAMES数据
  1198. process.on('exit', saveGamesToCache);
  1199. process.on('SIGINT', () => {
  1200. process.exit(0);
  1201. });
  1202. process.on('SIGTERM', () => {
  1203. process.exit(0);
  1204. });
  1205. process.on('SIGUSR2', () => {
  1206. process.exit(0);
  1207. });
  1208. module.exports = {
  1209. updateLeaguesList, getFilteredLeagues,
  1210. updateGamesList, updateGamesEvents, updateBaseEvents,
  1211. getGamesRelation,
  1212. updateGamesResult,
  1213. updateOriginalData, getOriginalData,
  1214. getSolutions, getGamesSolutions, getSolution, getSolutionsByIds,
  1215. getTotalProfitWithSid,
  1216. getTotalProfitWithBetInfo,
  1217. notifyException,
  1218. }