Fix the 'watching channel reselect after cleanup' logic to be less binding in terms of game priorities

This commit is contained in:
DevilXD committed 2022-04-06 22:23:14 +02:00
1 parent 8d2fde9447
commit dbb27e186d
1 file changed
+38 -33
+38 -33
View File
@@ -330,13 +330,14 @@ class Twitch:
]) ])
# relink watching channel after cleanup, # relink watching channel after cleanup,
# or stop watching it if it no longer qualifies # or stop watching it if it no longer qualifies
# NOTE: this replaces 'self.watching_channel's internal value with the new object
watching_channel = self.watching_channel.get_with_default(None) watching_channel = self.watching_channel.get_with_default(None)
if watching_channel is not None: if watching_channel is not None:
new_watching = channels.get(watching_channel.id) new_watching = channels.get(watching_channel.id)
if new_watching is not None and self.can_watch(new_watching): if new_watching is not None and self.can_watch(new_watching):
self.watch(new_watching) self.watch(new_watching)
else: else:
# we're removing a channel we're watching # we've removed a channel we were watching
self.stop_watching() self.stop_watching()
# pre-display the active drop with a substracted minute # pre-display the active drop with a substracted minute
for channel in channels.values(): for channel in channels.values():
@@ -349,25 +350,18 @@ class Twitch:
elif self._state is State.CHANNEL_SWITCH: elif self._state is State.CHANNEL_SWITCH:
# Change into the selected channel, stay in the watching channel, # Change into the selected channel, stay in the watching channel,
# or select a new channel that meets the required conditions # or select a new channel that meets the required conditions
priority_channels: list[Channel] = []
selected_channel = self.gui.channels.get_selection()
if selected_channel is not None:
self.gui.channels.clear_selection()
priority_channels.append(selected_channel)
watching_channel = self.watching_channel.get_with_default(None)
if watching_channel is not None:
priority_channels.append(watching_channel)
# If there's no selected channel, change into a channel we can watch
new_watching = None new_watching = None
for channel in priority_channels: selected_channel = self.gui.channels.get_selection()
if self.can_watch(channel): if selected_channel is not None and self.can_watch(selected_channel):
new_watching = channel # selected channel is checked first, and set as long as we can watch it
break new_watching = selected_channel
if new_watching is None: else:
# other channels additionally need to have a good reason
# for a switch (including the watching one)
# NOTE: we need to sort the channels every time because one channel # NOTE: we need to sort the channels every time because one channel
# can end up streaming any game, since channels aren't game-tied # can end up streaming any game - channels aren't game-tied
for channel in sorted(channels.values(), key=self._game_key): for channel in sorted(channels.values(), key=self._game_key):
if self.can_watch(channel): if self.can_watch(channel) and self.should_switch(channel):
new_watching = channel new_watching = channel
break break
if new_watching is not None: if new_watching is not None:
@@ -461,6 +455,9 @@ class Twitch:
await asyncio.sleep(60) await asyncio.sleep(60)
def can_watch(self, channel: Channel) -> bool: def can_watch(self, channel: Channel) -> bool:
"""
Determines if the given channel qualifies as a watching candidate.
"""
if not self.games: if not self.games:
return False return False
return ( return (
@@ -472,6 +469,28 @@ class Twitch:
and any(campaign.can_earn(channel) for campaign in self.inventory) and any(campaign.can_earn(channel) for campaign in self.inventory)
) )
def should_switch(self, channel: Channel) -> bool:
"""
Determines if the given channel qualifies as a switch candidate.
"""
watching_channel = self.watching_channel.get_with_default(None)
channel_order = self._game_key(channel)
if watching_channel is not None:
watching_order = self._game_key(watching_channel)
else:
# stub it with some high value, it doesn't really matter
# since 'is None' check returns earlier anyway
watching_order = 1
return (
watching_channel is None # there's no current watching channel
# or this channel's game is higher order than the watching one's
# NOTE: order is tied to the priority list position, so lower == higher
or channel_order < watching_order
or channel_order == watching_order # or the order is the same
# and this channel has priority over the watching channel
and channel.priority > watching_channel.priority
)
def watch(self, channel: Channel): def watch(self, channel: Channel):
self.gui.channels.set_watching(channel) self.gui.channels.set_watching(channel)
self.watching_channel.set(channel) self.watching_channel.set(channel)
@@ -516,23 +535,9 @@ class Twitch:
Called by a Channel when it goes online (after pending). Called by a Channel when it goes online (after pending).
""" """
logger.debug(f"{channel.name} goes ONLINE") logger.debug(f"{channel.name} goes ONLINE")
channel_order = self._game_key(channel)
watching_channel = self.watching_channel.get_with_default(None)
if watching_channel is not None:
watching_order = self._game_key(watching_channel)
else:
watching_order = 1
if ( if (
( self.can_watch(channel) # we can watch the channel that just got ONLINE
self._state is State.IDLE # we're currently idle and self.should_switch(channel) # and we should!
or watching_channel is None # or aren't watching anything
# or this channel is higher order than the watching one
# NOTE: order is tied to the list position, so lower == higher
or channel_order < watching_order
or channel_order == watching_order # or the order is the same
and channel.priority # and this channel has priority
and not watching_channel.priority # and we're watching a non-priority channel
) and self.can_watch(channel)
): ):
self.gui.print(f"{channel.name} goes ONLINE, switching...") self.gui.print(f"{channel.name} goes ONLINE, switching...")
self.watch(channel) self.watch(channel)