Explorar o código

优化特殊盘口更新规则

flyzto hai 1 mes
pai
achega
7a2c7cd214
Modificáronse 1 ficheiros con 63 adicións e 15 borrados
  1. 63 15
      pinnacle/main.js

+ 63 - 15
pinnacle/main.js

@@ -15,10 +15,12 @@ const GLOBAL_DATA = {
   straightFixturesVersion: 0,
   straightFixturesCount: 0,
   specialFixturesVersion: 0,
+  specialFixturesCount: 0,
   straightOddsVersion: 0,
   specialsOddsVersion: 0,
   requestErrorCount: 0,
   loopActive: false,
+  loopResultTime: Date.now(),
 };
 
 
@@ -86,7 +88,7 @@ const updateFiltedGames = async () => {
     if (res.statusCode !== 200) {
       throw new Error(`Failed to update filted leagues: ${res.message}`);
     }
-    Logs.outDev('res', res);
+    Logs.outDev('update filted games', res.data);
     const games = res.data.map(game => {
       const { eventId, leagueId } = game?.rel?.ps ?? {};
       return {
@@ -114,7 +116,6 @@ const getStraightFixtures = async () => {
     since = 0;
     GLOBAL_DATA.straightFixturesCount = 0;
   }
-  GLOBAL_DATA.straightFixturesCount++;
   return pinnacleGet('/v3/fixtures', { sportId: 29, leagueIds, since })
   .then(data => {
     const { league, last } = data;
@@ -154,6 +155,7 @@ const updateStraightFixtures = async () => {
       });
     }
     if (update && update == 'full') {
+      Logs.outDev('full update straight fixtures');
       const gamesSet = new Set(games.map(game => game.id));
       Object.keys(gamesMap).forEach(key => {
         if (!gamesSet.has(+key)) {
@@ -204,7 +206,11 @@ const updateStraightOdds = async () => {
 
 const getSpecialFixtures = async () => {
   const leagueIds = GLOBAL_DATA.filtedLeagues.join(',');
-  const since = GLOBAL_DATA.specialFixturesVersion;
+  let since = GLOBAL_DATA.specialFixturesVersion;
+  if (GLOBAL_DATA.specialFixturesCount > 18) {
+    since = 0;
+    GLOBAL_DATA.specialFixturesCount = 6;
+  }
   return pinnacleGet('/v2/fixtures/special', { sportId: 29, leagueIds, since })
   .then(data => {
     const { leagues, last } = data;
@@ -226,16 +232,38 @@ const getSpecialFixtures = async () => {
         return false;
       }
       return true;
-    });
-    return specials ?? [];
+    }) ?? [];
+    const update = since == 0 ? 'full' : 'increment';
+    return { specials, update };
   });
 }
 
 
+const mergeContestants = (localContestants=[], remoteContestants=[]) => {
+  const localContestantsMap = new Map(localContestants.map(contestant => [contestant.id, contestant]));
+  remoteContestants.forEach(contestant => {
+    const localContestant = localContestantsMap.get(contestant.id);
+    if (localContestant) {
+      Object.assign(localContestant, contestant);
+    }
+    else {
+      localContestants.push(contestant);
+    }
+  });
+  const remoteContestantsMap = new Map(remoteContestants.map(contestant => [contestant.id, contestant]));
+  for (let i = localContestants.length - 1; i >= 0; i--) {
+    if (!remoteContestantsMap.has(localContestants[i].id)) {
+      localContestants.splice(i, 1);
+    }
+  }
+  return localContestants;
+}
+
 const updateSpecialFixtures = async () => {
   return getSpecialFixtures()
-  .then(specials => {
-    if (specials.length) {
+  .then(data => {
+    const { specials, update } = data;
+    if (specials?.length) {
       const { gamesMap } = GLOBAL_DATA;
       const gamesSpecialsMap = {};
       specials.forEach(special => {
@@ -245,7 +273,8 @@ const updateSpecialFixtures = async () => {
         }
         if (special.name == 'Winning Margin') {
           gamesSpecialsMap[eventId].winningMargin = special;
-        } else if (special.name == 'Exact Total Goals') {
+        }
+        else if (special.name == 'Exact Total Goals') {
           gamesSpecialsMap[eventId].exactTotalGoals = special;
         }
       });
@@ -262,26 +291,36 @@ const updateSpecialFixtures = async () => {
         const localSpecials = gamesMap[eventId].specials;
         const remoteSpecials = gamesSpecialsMap[eventId];
 
-        if (localSpecials.winningMargin && !remoteSpecials.winningMargin) {
+        if (localSpecials.winningMargin && remoteSpecials.winningMargin) {
+          const { contestants: winningMarginContestants, ...winningMarginRest } = remoteSpecials.winningMargin;
+          Object.assign(localSpecials.winningMargin, winningMarginRest);
+          mergeContestants(localSpecials.winningMargin.contestants, winningMarginContestants);
+        }
+        else if (localSpecials.winningMargin && !remoteSpecials.winningMargin) {
           Logs.outDev('delete winningMargin', localSpecials.winningMargin);
           delete localSpecials.winningMargin;
         }
-        else if (!localSpecials.winningMargin && remoteSpecials.winningMargin) {
-          // Logs.outDev('add winningMargin', remoteSpecials.winningMargin);
+        else if (remoteSpecials.winningMargin) {
           localSpecials.winningMargin = remoteSpecials.winningMargin;
         }
 
-        if (localSpecials.exactTotalGoals && !remoteSpecials.exactTotalGoals) {
+        if (localSpecials.exactTotalGoals && remoteSpecials.exactTotalGoals) {
+          const { contestants: exactTotalGoalsContestants, ...exactTotalGoalsRest } = remoteSpecials.exactTotalGoals;
+          Object.assign(localSpecials.exactTotalGoals, exactTotalGoalsRest);
+          mergeContestants(localSpecials.exactTotalGoals.contestants, exactTotalGoalsContestants);
+        }
+        else if (localSpecials.exactTotalGoals && !remoteSpecials.exactTotalGoals) {
           Logs.outDev('delete exactTotalGoals', localSpecials.exactTotalGoals);
           delete localSpecials.exactTotalGoals;
         }
-        else if (!localSpecials.exactTotalGoals && remoteSpecials.exactTotalGoals) {
-          // Logs.outDev('add exactTotalGoals', remoteSpecials.exactTotalGoals);
+        else if (remoteSpecials.exactTotalGoals) {
           localSpecials.exactTotalGoals = remoteSpecials.exactTotalGoals;
         }
-
       });
     }
+    if (update && update == 'full') {
+      Logs.outDev('full update special fixtures');
+    }
   });
 }
 
@@ -585,13 +624,22 @@ const pinnacleDataLoop = () => {
     return updateSpecialsOdds();
   })
   .then(() => {
+    const nowTime = Date.now();
+    const loopDuration = nowTime - GLOBAL_DATA.loopResultTime;
+    Logs.outDev('loop duration', loopDuration);
+
+    GLOBAL_DATA.loopResultTime = nowTime;
+    GLOBAL_DATA.straightFixturesCount++;
+    GLOBAL_DATA.specialFixturesCount++;
     GLOBAL_DATA.requestErrorCount = 0;
+
     const { straightFixturesVersion: sfv, specialFixturesVersion: pfv, straightOddsVersion: sov, specialsOddsVersion: pov } = GLOBAL_DATA;
     const timestamp = Math.max(sfv, pfv, sov, pov);
     const games = getGames();
     const data = { games, timestamp };
     updateBaseEvents(data);
     Logs.outDev('games data', data);
+
     writeFileSync(cacheFilePath, JSON.stringify(GLOBAL_DATA.gamesMap, null, 2));
   })
   .catch(err => {