Go back to refreshing inventory every hour;

implement inventory lock
This commit is contained in:
DevilXD committed 2022-01-31 15:09:14 +01:00
1 parent 4b97b26630
commit 6839247322
1 file changed
+6 -7
+6 -7
View File
@@ -55,6 +55,7 @@ class Twitch:
self._state_change = asyncio.Event() self._state_change = asyncio.Event()
self.game: Optional[Game] = None self.game: Optional[Game] = None
self.inventory: Dict[Game, List[DropsCampaign]] = {} self.inventory: Dict[Game, List[DropsCampaign]] = {}
self._inv_lock = asyncio.Lock()
# GUI # GUI
self.gui = GUIManager(self) self.gui = GUIManager(self)
# Cookies, session and auth # Cookies, session and auth
@@ -388,8 +389,8 @@ class Twitch:
# ensure every 30 minutes that we don't have unclaimed points bonus # ensure every 30 minutes that we don't have unclaimed points bonus
await channel.claim_bonus() await channel.claim_bonus()
if i % 60 == 0: if i % 60 == 0:
# cleanup channels every hour # refresh inventory and cleanup channels every hour
self.change_state(State.CHANNELS_CLEANUP) self.change_state(State.INVENTORY_FETCH)
i = (i + 1) % 3600 i = (i + 1) % 3600
await self._watch_sleep(last_watch + interval - time()) await self._watch_sleep(last_watch + interval - time())
@@ -483,6 +484,7 @@ class Twitch:
if msg_type not in ("drop-progress", "drop-claim"): if msg_type not in ("drop-progress", "drop-claim"):
return return
drop_id: str = message["data"]["drop_id"] drop_id: str = message["data"]["drop_id"]
async with self._inv_lock:
drop: Optional[TimedDrop] = self.get_drop(drop_id) drop: Optional[TimedDrop] = self.get_drop(drop_id)
if msg_type == "drop-claim": if msg_type == "drop-claim":
if drop is None: if drop is None:
@@ -511,13 +513,9 @@ class Twitch:
context = await self.gql_request(GQL_OPERATIONS["CurrentDrop"]) context = await self.gql_request(GQL_OPERATIONS["CurrentDrop"])
drop_data: JsonType = context["data"]["currentUser"]["dropCurrentSession"] drop_data: JsonType = context["data"]["currentUser"]["dropCurrentSession"]
if drop_data["dropID"] != drop.id: if drop_data["dropID"] != drop.id:
# When two drops are ready to be claimed, we receive an update only for one.
# This means we have to refresh the games list and claim any additional drops
# after each earned drop, claimed or not.
break break
await asyncio.sleep(2) await asyncio.sleep(2)
# This also restarts the watch loop as needed here. self.restart_watching()
self.change_state(State.GAMES_UPDATE)
return return
assert msg_type == "drop-progress" assert msg_type == "drop-progress"
watching_channel = self.watching_channel.get_with_default(None) watching_channel = self.watching_channel.get_with_default(None)
@@ -771,6 +769,7 @@ class Twitch:
return DropsCampaign(self, response["data"]["user"]["dropCampaign"], claimed_benefits) return DropsCampaign(self, response["data"]["user"]["dropCampaign"], claimed_benefits)
async def fetch_inventory(self) -> None: async def fetch_inventory(self) -> None:
async with self._inv_lock:
# fetch all available campaign IDs, that are currently ACTIVE and account is connected # fetch all available campaign IDs, that are currently ACTIVE and account is connected
response = await self.gql_request(GQL_OPERATIONS["Campaigns"]) response = await self.gql_request(GQL_OPERATIONS["Campaigns"])
data = response["data"]["currentUser"]["dropCampaigns"] or [] data = response["data"]["currentUser"]["dropCampaigns"] or []