|
|
@@ -37,6 +37,7 @@ const relationSchema = new Schema({
|
|
|
|
|
|
const Relation = mongoose.model('Relation', relationSchema);
|
|
|
|
|
|
+const axios = require('axios');
|
|
|
const { fork } = require('child_process');
|
|
|
const calcTotalProfit = require('../triangle/totalProfitCalc');
|
|
|
|
|
|
@@ -55,6 +56,7 @@ const Request = {
|
|
|
}
|
|
|
|
|
|
const GAMES = {
|
|
|
+ Leagues: {},
|
|
|
List: {},
|
|
|
Relations: {},
|
|
|
Solutions: {},
|
|
|
@@ -70,10 +72,55 @@ const fixFloat = (number, x=2) => {
|
|
|
return parseFloat(number.toFixed(x));
|
|
|
}
|
|
|
|
|
|
+/**
|
|
|
+ * 更新联赛列表
|
|
|
+ */
|
|
|
+const syncLeaguesList = ({ mk, leagues }) => {
|
|
|
+ axios.post('https://api.isthe.me/api/p/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(`https://api.isthe.me/api/p/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 }) => {
|
|
|
+ axios.post('https://api.isthe.me/api/p/syncGames', { platform, mk, games })
|
|
|
+ .then(res => {
|
|
|
+ Logs.out('syncGamesList', res.data);
|
|
|
+ })
|
|
|
+ .catch(err => {
|
|
|
+ Logs.out('syncGamesList', err.message);
|
|
|
+ });
|
|
|
+}
|
|
|
const updateGamesList = (({ platform, mk, games } = {}) => {
|
|
|
+ syncGamesList({ platform, mk, games });
|
|
|
return new Promise((resolve, reject) => {
|
|
|
if (!platform || !games) {
|
|
|
return reject(new Error('PLATFORM_GAMES_INVALID'));
|
|
|
@@ -218,14 +265,21 @@ const getGamesList = () => {
|
|
|
/**
|
|
|
* 获取比赛盘口
|
|
|
*/
|
|
|
-const getGamesEvents = (platform) => {
|
|
|
+const getGamesEvents = ({ platform, relIds = [] } = {}) => {
|
|
|
+ const idSet = new Set(relIds);
|
|
|
+ const relations = { ...GAMES.Relations };
|
|
|
+ Object.keys(relations).forEach(id => {
|
|
|
+ if (idSet.size && !idSet.has(id)) {
|
|
|
+ delete relations[id];
|
|
|
+ }
|
|
|
+ });
|
|
|
if (platform) {
|
|
|
- return Object.values(GAMES.Relations).map(rel => rel[platform] ?? {});
|
|
|
+ return Object.values(relations).map(rel => rel[platform] ?? {});
|
|
|
}
|
|
|
const gamesEvents = {};
|
|
|
- Object.values(GAMES.Relations).forEach(rel => {
|
|
|
+ Object.values(relations).forEach(rel => {
|
|
|
Object.keys(rel).forEach(platform => {
|
|
|
- const game = rel[platform];
|
|
|
+ const game = rel[platform] ?? {};
|
|
|
const { eventId, events, special } = game;
|
|
|
if (!gamesEvents[platform]) {
|
|
|
gamesEvents[platform] = {};
|
|
|
@@ -272,7 +326,7 @@ const removeGamesRelation = async (id) => {
|
|
|
/**
|
|
|
* 获取关联比赛
|
|
|
*/
|
|
|
-const getGamesRelation = async (listEvents) => {
|
|
|
+const getGamesRelation = (listEvents) => {
|
|
|
const relationIds = Object.keys(GAMES.Relations);
|
|
|
if (listEvents) {
|
|
|
return relationIds.map(id => {
|
|
|
@@ -299,19 +353,17 @@ const getGamesRelation = async (listEvents) => {
|
|
|
*/
|
|
|
const relationsCleanup = () => {
|
|
|
const expireTime = Date.now() - 1000*60*5;
|
|
|
- getGamesRelation()
|
|
|
- .then(gamesRelation => {
|
|
|
- gamesRelation.forEach(item => {
|
|
|
- const { id, rel } = item;
|
|
|
- const expire = Object.values(rel).find(event => {
|
|
|
- return event.timestamp <= expireTime;
|
|
|
- });
|
|
|
- if (expire) {
|
|
|
- Logs.out('relation cleanup', id);
|
|
|
- removeGamesRelation(id);
|
|
|
- }
|
|
|
- return true;
|
|
|
+ const gamesRelation = getGamesRelation();
|
|
|
+ gamesRelation.forEach(item => {
|
|
|
+ const { id, rel } = item;
|
|
|
+ const expire = Object.values(rel).find(event => {
|
|
|
+ return event.timestamp <= expireTime;
|
|
|
});
|
|
|
+ if (expire) {
|
|
|
+ Logs.out('relation cleanup', id);
|
|
|
+ removeGamesRelation(id);
|
|
|
+ }
|
|
|
+ return true;
|
|
|
});
|
|
|
}
|
|
|
|
|
|
@@ -363,10 +415,12 @@ const setSolutions = (solutions) => {
|
|
|
* 获取中单方案
|
|
|
*/
|
|
|
const getSolutions = async () => {
|
|
|
- const gamesEvents = getGamesEvents();
|
|
|
- const gamesRelations = await getGamesRelation();
|
|
|
+ const solutionsList = Object.values(GAMES.Solutions);
|
|
|
+ const relIds = solutionsList.map(item => item.info.id);
|
|
|
+ const gamesEvents = getGamesEvents({ relIds });
|
|
|
+ const gamesRelations = getGamesRelation();
|
|
|
const relationsMap = new Map(gamesRelations.map(item => [item.id, item.rel]));
|
|
|
- const solutions = Object.values(GAMES.Solutions).sort((a, b) => b.sol.win_average - a.sol.win_average).map(item => {
|
|
|
+ const solutions = solutionsList.sort((a, b) => b.sol.win_average - a.sol.win_average).map(item => {
|
|
|
const { info: { id } } = item;
|
|
|
const relation = relationsMap.get(id);
|
|
|
return {
|
|
|
@@ -402,13 +456,25 @@ const solutionsCleanup = () => {
|
|
|
const getTotalProfit = (sid1, sid2, gold_side_jc) => {
|
|
|
const preSolution = GAMES.Solutions[sid1];
|
|
|
const subSolution = GAMES.Solutions[sid2];
|
|
|
+ const relId1 = preSolution?.info?.id;
|
|
|
+ const relId2 = subSolution?.info?.id;
|
|
|
+ const relIds = [relId1, relId2];
|
|
|
+ const gamesEvents = getGamesEvents({ relIds });
|
|
|
+
|
|
|
+ const gamesRelations = getGamesRelation();
|
|
|
+ const relationsMap = new Map(gamesRelations.map(item => [item.id, item.rel]));
|
|
|
+ const preRelation = relationsMap.get(relId1);
|
|
|
+ const subRelation = relationsMap.get(relId2);
|
|
|
+ preSolution.info = { id: relId1, ...preRelation };
|
|
|
+ subSolution.info = { id: relId2, ...subRelation };
|
|
|
+
|
|
|
const sol1 = preSolution?.sol;
|
|
|
const sol2 = subSolution?.sol;
|
|
|
if (!sol1 || !sol2 || !gold_side_jc) {
|
|
|
return {};
|
|
|
}
|
|
|
const profit = calcTotalProfit(sol1, sol2, gold_side_jc);
|
|
|
- return { profit, preSolution, subSolution };
|
|
|
+ return { profit, preSolution, subSolution, gamesEvents };
|
|
|
}
|
|
|
|
|
|
const getSetting = async () => {
|
|
|
@@ -427,7 +493,7 @@ events_child.on('message', async (message) => {
|
|
|
if (method == 'get' && id) {
|
|
|
let responseData = null;
|
|
|
if (type == 'getGamesRelation') {
|
|
|
- responseData = await getGamesRelation(true);
|
|
|
+ responseData = getGamesRelation(true);
|
|
|
}
|
|
|
else if (type == 'getSetting') {
|
|
|
responseData = await getSetting();
|
|
|
@@ -458,6 +524,7 @@ setInterval(() => {
|
|
|
}, 1000*30);
|
|
|
|
|
|
module.exports = {
|
|
|
+ updateLeaguesList, getFilteredLeagues,
|
|
|
updateGamesList, updateGamesEvents, getGamesList,
|
|
|
updateGamesRelation, getGamesRelation, removeGamesRelation,
|
|
|
getGamesEvents, getSolutions, getTotalProfit,
|