| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453 |
- const axios = require('axios');
- const Logs = require('../libs/logs');
- const Setting = require('./Setting');
- const childOptions = process.env.NODE_ENV == 'development' ? {
- execArgv: ['--inspect=9228']
- } : {};
- const { fork } = require('child_process');
- const events_child = fork('./triangle/eventsMatch.js', [], childOptions);
- const PS_IOR_KEYS = [
- ['0', 'ior_mh', 'ior_mn', 'ior_mc'],
- // ['0', 'ior_rh_05', 'ior_mn', 'ior_rc_05'],
- ['-1', 'ior_rh_15', 'ior_wmh_1', 'ior_rac_05'],
- ['-2', 'ior_rh_25', 'ior_wmh_2', 'ior_rac_15'],
- ['+1', 'ior_rah_05', 'ior_wmc_1', 'ior_rc_15'],
- ['+2', 'ior_rah_15', 'ior_wmc_2', 'ior_rc_25'],
- ];
- const BASE_URL = 'https://api.isthe.me/api/p';
- const IS_DEV = process.env.NODE_ENV == 'development';
- const GAMES = {
- Leagues: {},
- List: {},
- Baselist: {},
- Relations: {},
- };
- const Request = {
- callbacks: {},
- count: 0,
- }
- /**
- * 精确浮点数字
- * @param {number} number
- * @param {number} x
- * @returns {number}
- */
- const fixFloat = (number, x=2) => {
- return parseFloat(number.toFixed(x));
- }
- /**
- * 获取市场类型
- */
- const getMarketType = (mk) => {
- return mk == 0 ? 'early' : 'today';
- }
- /**
- * 同步联赛列表
- */
- const syncLeaguesList = ({ mk, leagues }) => {
- if (IS_DEV) {
- return Logs.out('syncLeaguesList', { mk, leagues });
- }
- axios.post(`${BASE_URL}/syncLeague`, { mk, leagues })
- .then(res => {
- Logs.out('syncLeaguesList', res.data);
- })
- .catch(err => {
- Logs.out('syncLeaguesList', err.message);
- });
- }
- /**
- * 更新联赛列表
- */
- const updateLeaguesList = ({ mk, leagues }) => {
- const leaguesList = GAMES.Leagues;
- if (JSON.stringify(leaguesList[mk]) != JSON.stringify(leagues)) {
- leaguesList[mk] = leagues;
- syncLeaguesList({ mk, leagues });
- return leagues.length;
- }
- return 0;
- }
- /**
- * 获取筛选过的联赛
- */
- const getFilteredLeagues = async (mk) => {
- return axios.get(`${BASE_URL}/getLeagueTast?mk=${mk}`)
- .then(res => {
- if (res.data.code == 0) {
- return res.data.data;
- }
- return Promise.reject(new Error(res.data.message));
- });
- }
- /**
- * 同步比赛列表到服务器
- */
- const syncGamesList = ({ platform, mk, games }) => {
- if (IS_DEV) {
- return Logs.out('syncGamesList', { platform, mk, games });
- }
- axios.post(`${BASE_URL}/syncGames`, { platform, mk, games })
- .then(res => {
- Logs.out('syncGamesList', { platform, mk, count: games.length }, res.data);
- })
- .catch(err => {
- Logs.out('syncGamesList', { platform, mk }, err.message);
- });
- }
- /**
- * 同步基准比赛列表
- */
- const syncBaseList = ({ marketType, games }) => {
- const baseList = GAMES.Baselist;
- if (!baseList[marketType]) {
- baseList[marketType] = games;
- }
- const newMap = new Map(games.map(item => [item.eventId, item]));
- // 删除不存在的项
- for (let i = baseList[marketType].length - 1; i >= 0; i--) {
- if (!newMap.has(baseList[marketType][i].eventId)) {
- baseList[marketType].splice(i, 1);
- }
- }
- // 添加或更新
- const oldIds = new Set(baseList[marketType].map(item => item.eventId));
- games.forEach(game => {
- if (!oldIds.has(game.eventId)) {
- // 添加新项
- baseList[marketType].push(game);
- }
- });
- }
- /**
- * 更新比赛列表
- */
- const updateGamesList = (({ platform, mk, games } = {}) => {
- return new Promise((resolve, reject) => {
- if (!platform || !games) {
- return reject(new Error('PLATFORM_GAMES_INVALID'));
- }
- const marketType = getMarketType(mk);
- syncGamesList({ platform, mk, games });
- if (platform == 'ps') {
- syncBaseList({ marketType, games });
- }
- resolve();
- });
- });
- /**
- * 提交盘口数据
- */
- const submitOdds = ({ platform, mk, games }) => {
- if (IS_DEV) {
- return Logs.out('syncOdds', { platform, mk, games });
- }
- axios.post(`${BASE_URL}/syncOdds`, { platform, mk, games})
- .then(res => {
- Logs.out('syncOdds', { platform, mk, count: games.length }, res.data);
- })
- .catch(err => {
- Logs.out('syncOdds', { platform, mk }, err.message);
- });
- }
- /**
- * 同步基准盘口
- */
- const syncBaseEvents = ({ mk, games, outrights }) => {
- const marketType = getMarketType(mk);
- const baseList = GAMES.Baselist;
- if (!baseList[marketType]) {
- return;
- }
- const baseMap = new Map(baseList[marketType].map(item => [item.eventId, item]));
- games?.forEach(game => {
- const { eventId, evtime, events } = game;
- const baseGame = baseMap.get(eventId);
- if (baseGame) {
- baseGame.evtime = evtime;
- baseGame.events = events;
- }
- });
- outrights?.forEach(outright => {
- const { parentId, sptime, special } = outright;
- const baseGame = baseMap.get(parentId);
- if (baseGame) {
- baseGame.sptime = sptime;
- baseGame.special = special;
- }
- });
- if (games?.length) {
- const gamesList = baseList[marketType]?.map(game => {
- const { evtime, events, sptime, special, ...gameInfo } = game;
- const expireTime = Date.now() - 15000;
- let odds = {};
- if (evtime > expireTime) {
- odds = { ...odds, ...events };
- }
- if (sptime > expireTime) {
- odds = { ...odds, ...special };
- }
- const matches = PS_IOR_KEYS.map(([label, ...keys]) => {
- const match = keys.map(key => ({
- key,
- value: odds[key] ?? 0
- }));
- return {
- label,
- match
- };
- }).filter(item => item.match.every(entry => entry.value !== 0));
- return { ...gameInfo, matches };
- });
- // Logs.out('baseList', baseList[marketType]);
- submitOdds({ platform: 'ps', mk, games: gamesList });
- }
- }
- /**
- * 更新比赛盘口
- */
- const updateGamesEvents = ({ platform, mk, games, outrights }) => {
- return new Promise((resolve, reject) => {
- if (!platform || (!games && !outrights)) {
- return reject(new Error('PLATFORM_GAMES_INVALID'));
- }
- if (platform == 'ps') {
- syncBaseEvents({ mk, games, outrights });
- }
- const relatedGames = Object.values(GAMES.Relations).map(item => item.rel?.[platform] ?? {});
- if (!relatedGames.length) {
- return resolve({ update: 0 });
- }
- const updateCount = {
- update: 0
- };
- const relatedMap = new Map(relatedGames.map(item => [item.eventId, item]));
- games?.forEach(game => {
- const { eventId, evtime, events } = game;
- const relatedGame = relatedMap.get(eventId);
- if (relatedGame) {
- relatedGame.evtime = evtime;
- relatedGame.events = events;
- updateCount.update ++;
- }
- });
- outrights?.forEach(outright => {
- const { parentId, sptime, special } = outright;
- const relatedGame = relatedMap.get(parentId);
- if (relatedGame) {
- relatedGame.sptime = sptime;
- relatedGame.special = special;
- updateCount.update ++;
- }
- });
- resolve(updateCount);
- });
- }
- /**
- * 获取关联比赛
- */
- const fetchGamesRelation = async (mk='') => {
- return axios.get(`${BASE_URL}/getGameTast?mk=${mk}`)
- .then(res => {
- if (res.data.code == 0) {
- const now = Date.now();
- const gamesRelation = res.data.data?.filter(item => {
- const timestamp = new Date(item.timestamp).getTime();
- return timestamp > now;
- }).map(item => {
- const {
- id, mk,
- event_id: ps_event_id,
- league_id: ps_league_id,
- ob_event_id, ob_league_id,
- hg_event_id, hg_league_id,
- } = item;
- const rel = {
- ps: {
- eventId: +ps_event_id,
- leagueId: +ps_league_id
- },
- ob: ob_event_id ? {
- eventId: +ob_event_id,
- leagueId: +ob_league_id
- } : null,
- hg: hg_event_id ? {
- eventId: +hg_event_id,
- leagueId: +hg_league_id
- } : null
- };
- return { id, mk, rel };
- }) ?? [];
- return gamesRelation;
- }
- return Promise.reject(new Error(res.data.message));
- });
- }
- const getGamesRelation = ({ mk, listEvents }) => {
- const relations = Object.values(GAMES.Relations).filter(item => {
- if (!mk) {
- return true;
- }
- return item.mk == mk;
- });
- if (listEvents) {
- return relations;
- }
- return relations.map(item => {
- const { rel, ...relationInfo } = item;
- Object.keys(rel).forEach(platform => {
- const { events, evtime, sptime, special, ...gameInfo } = rel[platform];
- rel[platform] = gameInfo;
- });
- return { ...relationInfo, rel };
- });
- }
- /**
- * 定时更新关联比赛列表
- */
- const updateGamesRelation = () => {
- fetchGamesRelation()
- .then(res => {
- const gamesRelation = res.flat();
- gamesRelation.forEach(item => {
- const { id } = item;
- if (!GAMES.Relations[id]) {
- GAMES.Relations[id] = item;
- }
- });
- const relations = new Set(gamesRelation.map(item => +item.id));
- Object.keys(GAMES.Relations).forEach(id => {
- if (!relations.has(+id)) {
- delete GAMES.Relations[id];
- }
- });
- })
- .catch(err => {
- Logs.out('updateGamesRelation', err.message);
- })
- .finally(() => {
- setTimeout(updateGamesRelation, 60000);
- });
- }
- updateGamesRelation();
- /**
- * 同步比赛结果
- */
- const syncGamesResult = async (result) => {
- if (IS_DEV) {
- return Logs.out('updateGamesResult', result);
- }
- axios.post(`${BASE_URL}/syncMatchResult`, result)
- .then(res => {
- Logs.out('syncMatchResult', res.data);
- })
- .catch(err => {
- Logs.out('syncMatchResult', err.message);
- });
- }
- /**
- * 更新比赛结果
- */
- const updateGamesResult = (result) => {
- syncGamesResult(result);
- return Promise.resolve();
- }
- /**
- * 获取后台设置
- */
- const getSetting = async () => {
- return Setting.get();
- }
- /**
- * 从子进程获取数据
- */
- const getDataFromChild = (type, callback) => {
- const id = ++Request.count;
- Request.callbacks[id] = callback;
- events_child.send({ method: 'get', id, type });
- }
- /**
- * 处理子进程消息
- */
- events_child.on('message', async (message) => {
- const { callbacks } = Request;
- const { method, id, type, data } = message;
- if (method == 'get' && id) {
- let responseData = null;
- if (type == 'getGamesRelation') {
- responseData = getGamesRelation({ listEvents: true });
- Logs.out('getGamesRelation', responseData);
- }
- else if (type == 'getSetting') {
- responseData = await getSetting();
- }
- // else if (type == 'getSolutionHistory') {
- // responseData = getSolutionHistory();
- // }
- events_child.send({ type: 'response', id, data: responseData });
- }
- else if (method == 'post') {
- if (type == 'setSolutions') {
- setSolutions(data);
- }
- }
- else if (method == 'response' && id && callbacks[id]) {
- callbacks[id](data);
- delete callbacks[id];
- }
- });
- module.exports = {
- updateLeaguesList, getFilteredLeagues,
- updateGamesList, updateGamesEvents,
- getGamesRelation,
- updateGamesResult,
- }
|