Attribute lookup performance improvements

This commit is contained in:
DevilXD committed 2022-02-06 21:23:17 +01:00
1 parent 32f475d7ed
commit 5e129d41d8
3 files changed
+13 -6

No files matched your search

+4 -3
View File
@@ -17,6 +17,7 @@ from constants import JsonType, BASE_URL, GQL_OPERATIONS, ONLINE_DELAY, DROPS_EN
if TYPE_CHECKING: if TYPE_CHECKING:
from twitch import Twitch from twitch import Twitch
from gui import ChannelList
logger = logging.getLogger("TwitchDrops") logger = logging.getLogger("TwitchDrops")
@@ -52,6 +53,7 @@ class Stream:
class Channel: class Channel:
def __init__(self, twitch: Twitch, data: JsonType, *, priority: bool = False): def __init__(self, twitch: Twitch, data: JsonType, *, priority: bool = False):
self._twitch: Twitch = twitch self._twitch: Twitch = twitch
self._gui_channels: ChannelList = twitch.gui.channels
self.id: int = int(data["id"]) self.id: int = int(data["id"])
self._login: str = data["name"] self._login: str = data["name"]
self._display_name: Optional[str] = data.get("displayName") self._display_name: Optional[str] = data.get("displayName")
@@ -169,7 +171,6 @@ class Channel:
def viewers(self, value: int): def viewers(self, value: int):
if self._stream is not None: if self._stream is not None:
self._stream.viewers = value self._stream.viewers = value
self.display()
@property @property
def drops_enabled(self) -> bool: def drops_enabled(self) -> bool:
@@ -178,13 +179,13 @@ class Channel:
return False return False
def display(self): def display(self):
self._twitch.gui.channels.display(self) self._gui_channels.display(self)
def remove(self): def remove(self):
if self._pending_stream_up is not None: if self._pending_stream_up is not None:
self._pending_stream_up.cancel() self._pending_stream_up.cancel()
self._pending_stream_up = None self._pending_stream_up = None
self._twitch.gui.channels.remove(self) self._gui_channels.remove(self)
async def get_spade_url(self) -> str: async def get_spade_url(self) -> str:
""" """
+8 -3
View File
@@ -10,6 +10,7 @@ from utils import timestamp, invalidate_cache, Game
if TYPE_CHECKING: if TYPE_CHECKING:
from twitch import Twitch from twitch import Twitch
from gui import CampaignProgress
class BaseDrop: class BaseDrop:
@@ -62,7 +63,6 @@ class BaseDrop:
return all(campaign.timed_drops[pid].is_claimed for pid in self._precondition_drops) return all(campaign.timed_drops[pid].is_claimed for pid in self._precondition_drops)
def can_earn(self, channel: Optional[Channel] = None) -> bool: def can_earn(self, channel: Optional[Channel] = None) -> bool:
allowed_channels = self.campaign.allowed_channels
return ( return (
self.preconditions # preconditions are met self.preconditions # preconditions are met
and not self.is_claimed # drop isn't already claimed and not self.is_claimed # drop isn't already claimed
@@ -70,7 +70,11 @@ class BaseDrop:
# drop is within the active timeframe # drop is within the active timeframe
and self.starts_at <= datetime.now(timezone.utc) < self.ends_at and self.starts_at <= datetime.now(timezone.utc) < self.ends_at
# channel isn't specified, or there's no ACL, or the channel is in the ACL # channel isn't specified, or there's no ACL, or the channel is in the ACL
and (channel is None or not allowed_channels or channel in allowed_channels) and (
channel is None
or not (allowed_channels := self.campaign.allowed_channels)
or channel in allowed_channels
)
) )
@property @property
@@ -127,6 +131,7 @@ class TimedDrop(BaseDrop):
self, campaign: DropsCampaign, data: JsonType, claimed_benefits: Dict[str, datetime] self, campaign: DropsCampaign, data: JsonType, claimed_benefits: Dict[str, datetime]
): ):
super().__init__(campaign, data, claimed_benefits) super().__init__(campaign, data, claimed_benefits)
self._gui_progress: CampaignProgress = self._twitch.gui.progress
self.current_minutes: int = 0 self.current_minutes: int = 0
if "self" in data: if "self" in data:
self.current_minutes = data["self"]["currentMinutesWatched"] self.current_minutes = data["self"]["currentMinutesWatched"]
@@ -171,7 +176,7 @@ class TimedDrop(BaseDrop):
self._on_minutes_changed() self._on_minutes_changed()
def display(self, *, countdown: bool = True, subone: bool = False): def display(self, *, countdown: bool = True, subone: bool = False):
self.campaign._twitch.gui.progress.display(self, countdown=countdown, subone=subone) self._gui_progress.display(self, countdown=countdown, subone=subone)
def bump_minutes(self): def bump_minutes(self):
if self.current_minutes < self.required_minutes: if self.current_minutes < self.required_minutes:
+1
View File
@@ -450,6 +450,7 @@ class Twitch:
else: else:
viewers = message["viewers"] viewers = message["viewers"]
channel.viewers = viewers channel.viewers = viewers
channel.display()
# logger.debug(f"{channel.name} viewers: {viewers}") # logger.debug(f"{channel.name} viewers: {viewers}")
def on_online(self, channel: Channel): def on_online(self, channel: Channel):