Ver Fonte

fix 盘口丢失问题

flyzto há 1 mês atrás
pai
commit
fce361f15e
1 ficheiros alterados com 83 adições e 39 exclusões
  1. 83 39
      pinnacle/main.js

+ 83 - 39
pinnacle/main.js

@@ -17,7 +17,9 @@ const GLOBAL_DATA = {
   specialFixturesVersion: 0,
   specialFixturesCount: 0,
   straightOddsVersion: 0,
+  // straightOddsCount: 0,
   specialsOddsVersion: 0,
+  // specialsOddsCount: 0,
   requestErrorCount: 0,
   loopActive: false,
   loopResultTime: Date.now(),
@@ -82,40 +84,46 @@ const pinnacleGet = async (endpoint, params) => {
 // };
 
 
-const updateFiltedGames = async () => {
-  return getPsteryRelations()
-  .then(res => {
-    if (res.statusCode !== 200) {
-      throw new Error(`Failed to update filted leagues: ${res.message}`);
-    }
-    // Logs.outDev('update filted games', res.data);
-    const games = res.data.map(game => {
-      const { eventId, leagueId } = game?.rel?.ps ?? {};
-      return {
-        eventId,
-        leagueId,
-      };
-    });
+const getFiltedGames = () => {
+  return new Promise(resolve => {
+    const updateFiltedGames = () => {
+      getPsteryRelations()
+      .then(res => {
+        if (res.statusCode !== 200) {
+          throw new Error(res.message);
+        }
+        // Logs.outDev('update filted games', res.data);
+        const games = res.data.map(game => {
+          const { eventId, leagueId } = game?.rel?.ps ?? {};
+          return {
+            eventId,
+            leagueId,
+          };
+        });
 
-    GLOBAL_DATA.filtedLeagues = [...new Set(games.map(game => game.leagueId).filter(leagueId => leagueId))];
-    GLOBAL_DATA.filtedGames = games.map(game => game.eventId).filter(eventId => eventId);
-  })
-  .catch(err => {
-    return Promise.reject(err);
-  })
-  .finally(() => {
-    setTimeout(updateFiltedGames, 1000 * 30);
+        GLOBAL_DATA.filtedLeagues = [...new Set(games.map(game => game.leagueId).filter(leagueId => leagueId))];
+        GLOBAL_DATA.filtedGames = games.map(game => game.eventId).filter(eventId => eventId);
+
+        resolve();
+        setTimeout(updateFiltedGames, 1000 * 30);
+      })
+      .catch(err => {
+        Logs.err('failed to update filted games:', err.message);
+        setTimeout(updateFiltedGames, 1000 * 5);
+      });
+    }
+    updateFiltedGames();
   });
 }
 
 
 const getStraightFixtures = async () => {
   if (!GLOBAL_DATA.filtedLeagues.length) {
-    return Promise.reject(new Error('No filted leagues'));
+    return Promise.reject(new Error('no filted leagues'));
   }
   const leagueIds = GLOBAL_DATA.filtedLeagues.join(',');
   let since = GLOBAL_DATA.straightFixturesVersion;
-  if (GLOBAL_DATA.straightFixturesCount > 12) {
+  if (GLOBAL_DATA.straightFixturesCount >= 12) {
     since = 0;
     GLOBAL_DATA.straightFixturesCount = 0;
   }
@@ -174,10 +182,17 @@ const updateStraightFixtures = async () => {
 
 const getStraightOdds = async () => {
   if (!GLOBAL_DATA.filtedLeagues.length) {
-    return Promise.reject(new Error('No filted leagues'));
+    return Promise.reject(new Error('no filted leagues'));
   }
   const leagueIds = GLOBAL_DATA.filtedLeagues.join(',');
-  const since = GLOBAL_DATA.straightOddsVersion;
+  let since = GLOBAL_DATA.straightOddsVersion;
+  if (GLOBAL_DATA.straightOddsCount >= 27) {
+    since = 0;
+    GLOBAL_DATA.straightOddsCount = 3;
+  }
+  if (since == 0) {
+    Logs.outDev('full update straight odds');
+  }
   return pinnacleGet('/v3/odds', { sportId: 29, oddsFormat: 'Decimal', leagueIds, since })
   .then(data => {
     const { leagues, last } = data;
@@ -188,7 +203,10 @@ const getStraightOdds = async () => {
     const games = leagues?.flatMap(league => league.events);
     return games?.map(item => {
       const { periods, ...rest } = item;
-      const period = periods?.find(period => period.number == 0) ?? {};
+      const period = periods?.find(period => period.number == 0);
+      if (!period) {
+        return rest;
+      }
       return { ...rest, period };
     }) ?? [];
   });
@@ -214,14 +232,14 @@ const updateStraightOdds = async () => {
 
 const getSpecialFixtures = async () => {
   if (!GLOBAL_DATA.filtedLeagues.length) {
-    return Promise.reject(new Error('No filted leagues'));
+    return Promise.reject(new Error('no filted leagues'));
   }
   const leagueIds = GLOBAL_DATA.filtedLeagues.join(',');
-  let since = GLOBAL_DATA.specialFixturesVersion;
-  if (GLOBAL_DATA.specialFixturesCount > 18) {
-    since = 0;
-    GLOBAL_DATA.specialFixturesCount = 6;
-  }
+  const since = GLOBAL_DATA.specialFixturesVersion;
+  // if (GLOBAL_DATA.specialFixturesCount >= 18) {
+  //   since = 0;
+  //   GLOBAL_DATA.specialFixturesCount = 6;
+  // }
   if (since == 0) {
     Logs.outDev('full update special fixtures');
   }
@@ -338,10 +356,17 @@ const updateSpecialFixtures = async () => {
 
 const getSpecialsOdds = async () => {
   if (!GLOBAL_DATA.filtedLeagues.length) {
-    return Promise.reject(new Error('No filted leagues'));
+    return Promise.reject(new Error('no filted leagues'));
   }
   const leagueIds = GLOBAL_DATA.filtedLeagues.join(',');
   const since = GLOBAL_DATA.specialsOddsVersion;
+  // if (GLOBAL_DATA.specialsOddsCount >= 33) {
+  //   since = 0;
+  //   GLOBAL_DATA.specialsOddsCount = 9;
+  // }
+  if (since == 0) {
+    Logs.outDev('full update specials odds');
+  }
   return pinnacleGet('/v2/odds/special', { sportId: 29, oddsFormat: 'Decimal', leagueIds, since })
   .then(data => {
     const { leagues, last } = data;
@@ -657,8 +682,14 @@ const pinnacleDataLoop = () => {
     const timestamp = Math.max(sfv, pfv, sov, pov);
     const games = getGames();
     const data = { games, timestamp };
-    updateBaseEvents(data);
-    Logs.outDev('games data', data);
+
+    updateBaseEvents(data)
+    .then(() => {
+      Logs.outDev('games data', data);
+    })
+    .catch(err => {
+      Logs.err('failed to update base events:', err.message);
+    });
 
     writeFileSync(cacheFilePath, JSON.stringify(GLOBAL_DATA.gamesMap, null, 2));
   })
@@ -666,10 +697,19 @@ const pinnacleDataLoop = () => {
     Logs.err(err.message, err.source);
     GLOBAL_DATA.requestErrorCount++;
     if (GLOBAL_DATA.loopActive && GLOBAL_DATA.requestErrorCount > 5) {
-      Logs.out('request error count reached the limit');
+      const exceptionMessage = 'request errors have reached the limit';
+      Logs.out(exceptionMessage);
       GLOBAL_DATA.loopActive = false;
+
       Logs.out('loop inactive');
-      notifyException('Pinnacle API request errors have reached the limit.');
+
+      notifyException(`Pinnacle API ${exceptionMessage}, ${err.message}`)
+      .then(() => {
+        Logs.out('notify exception success');
+      })
+      .catch(err => {
+        Logs.err('failed to notify exception:', err.message);
+      });
     }
   })
   .finally(() => {
@@ -684,10 +724,14 @@ const pinnacleDataLoop = () => {
 
       GLOBAL_DATA.straightFixturesCount = 0;
       GLOBAL_DATA.specialFixturesCount = 0;
+      // GLOBAL_DATA.straightOddsCount = 0;
+      // GLOBAL_DATA.specialsOddsCount = 0;
     }
     else {
       GLOBAL_DATA.straightFixturesCount++;
       GLOBAL_DATA.specialFixturesCount++;
+      // GLOBAL_DATA.straightOddsCount++;
+      // GLOBAL_DATA.specialsOddsCount++;
     }
     setTimeout(pinnacleDataLoop, loopDelay);
   });
@@ -700,7 +744,7 @@ const pinnacleDataLoop = () => {
     return;
   }
   GLOBAL_DATA.loopActive = true;
-  updateFiltedGames().then(pinnacleDataLoop);
+  getFiltedGames().then(pinnacleDataLoop);
 })();