Move ACL check into drop's can_earn

This commit is contained in:
DevilXD
2022-01-23 14:01:35 +01:00
parent 1e552919ac
commit d51c3f53bd
2 changed files with 14 additions and 22 deletions

View File

@@ -12,11 +12,6 @@ if TYPE_CHECKING:
from twitch import Twitch
class Benefit:
def __init__(self) -> None:
pass
class BaseDrop:
def __init__(self, campaign: DropsCampaign, data: JsonType, claimed_benefits: Set[str]):
self._twitch: Twitch = campaign._twitch
@@ -41,7 +36,7 @@ class BaseDrop:
def __repr__(self) -> str:
if self.is_claimed:
additional = ", claimed=True"
elif self.can_earn:
elif self.can_earn():
additional = ", can_earn=True"
else:
additional = ''
@@ -52,14 +47,16 @@ class BaseDrop:
campaign = self.campaign
return all(campaign.timed_drops[pid].is_claimed for pid in self._precondition_drops)
@property
def can_earn(self) -> bool:
def can_earn(self, channel: Optional[Channel] = None) -> bool:
allowed_channels = self.campaign.allowed_channels
return (
self.preconditions # preconditions are met
and not self.is_claimed # drop isn't already claimed
and self.campaign.active # campaign is active
# it's within the active timeframe
# drop is within the active timeframe
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
and (channel is None or not allowed_channels or channel in allowed_channels)
)
@property
@@ -125,7 +122,7 @@ class TimedDrop(BaseDrop):
def __repr__(self) -> str:
if self.is_claimed:
additional = ", claimed=True"
elif self.can_earn:
elif self.can_earn():
additional = ", can_earn=True"
else:
additional = ''

View File

@@ -186,7 +186,7 @@ class Twitch:
if drop.can_claim:
await drop.claim()
# add game only for active campaigns
if active and not add_game and drop.can_earn:
if active and not add_game and drop.can_earn():
add_game = True
if add_game:
games.append(game)
@@ -353,7 +353,7 @@ class Twitch:
if drop is None:
use_active = True
logger.error(f"Missing drop: {drop_id}")
elif not drop.can_earn:
elif not drop.can_earn(channel):
use_active = True
else:
drop.update_minutes(drop_data["currentMinutesWatched"])
@@ -386,9 +386,8 @@ class Twitch:
and channel.game == self.game # it's a game we've selected
# we can progress any campaign for the selected game
and any(
drop.can_earn
drop.can_earn(channel)
for campaign in self.inventory[self.game]
if not campaign.allowed_channels or channel in campaign.allowed_channels
for drop in campaign.drops
)
)
@@ -495,11 +494,12 @@ class Twitch:
await asyncio.sleep(2)
return
assert msg_type == "drop-progress"
watching_channel = self.watching_channel.get_with_default(None)
if self._drop_update is None:
# we aren't actually waiting for a progress update right now, so we can just
# ignore the event this time
return
elif drop is not None and drop.can_earn:
elif drop is not None and drop.can_earn(watching_channel):
drop.update_minutes(message["data"]["current_progress_min"])
drop.display()
# Let the watch loop know we've handled it here
@@ -768,7 +768,7 @@ class Twitch:
# add campaigns that remained, that can be earned but are not in-progress yet
for campaign_id in available_campaigns:
campaign = await self.fetch_campaign(campaign_id, claimed_benefits)
if any(drop.can_earn for drop in campaign.drops):
if any(drop.can_earn() for drop in campaign.drops):
campaigns.append(campaign)
campaigns.sort(key=lambda c: c.ends_at)
self.inventory.clear()
@@ -805,12 +805,7 @@ class Twitch:
for campaign in self.inventory[self.game]
if campaign.active
for drop in campaign.drops
if drop.can_earn and (
not campaign.allowed_channels # campaign has no ACL
or watching_channel is None # we aren't watching anything yet
# we are watching a channel that applies to this campaign
or watching_channel in campaign.allowed_channels
)
if drop.can_earn(watching_channel)
),
key=lambda d: d.remaining_minutes,
)