Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: add option to prioritize drops ending soonest #433

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -1456,6 +1456,7 @@ class _SettingsVars(TypedDict):
proxy: StringVar
autostart: IntVar
priority_only: IntVar
priority_by_time: IntVar
tray_notifications: IntVar


Expand All @@ -1471,6 +1472,7 @@ def __init__(self, manager: GUIManager, master: ttk.Widget):
"tray": IntVar(master, self._settings.autostart_tray),
"autostart": IntVar(master, self._settings.autostart),
"priority_only": IntVar(master, self._settings.priority_only),
"priority_by_time": IntVar(master, self._settings.priority_by_time),
"tray_notifications": IntVar(master, self._settings.tray_notifications),
}
master.rowconfigure(0, weight=1)
Expand Down Expand Up @@ -1528,6 +1530,12 @@ def __init__(self, manager: GUIManager, master: ttk.Widget):
ttk.Checkbutton(
checkboxes_frame, variable=self._vars["priority_only"], command=self.priority_only
).grid(column=1, row=irow, sticky="w")
ttk.Label(
checkboxes_frame, text=_("gui", "settings", "general", "priority_by_time")
).grid(column=0, row=(irow := irow + 1), sticky="e")
ttk.Checkbutton(
checkboxes_frame, variable=self._vars["priority_by_time"], command=self.priority_by_time
).grid(column=1, row=irow, sticky="w")
# proxy frame
proxy_frame = ttk.Frame(center_frame2)
proxy_frame.grid(column=0, row=2)
Expand Down Expand Up @@ -1740,6 +1748,9 @@ def priority_delete(self) -> None:
def priority_only(self) -> None:
self._settings.priority_only = bool(self._vars["priority_only"].get())

def priority_by_time(self) -> None:
self._settings.priority_by_time = bool(self._vars["priority_by_time"].get())

def exclude_add(self) -> None:
game_name: str = self._exclude_entry.get()
if not game_name:
Expand Down Expand Up @@ -2257,6 +2268,7 @@ async def main(exit_event: asyncio.Event):
autostart=False,
language="English",
priority_only=False,
priority_by_time=False,
autostart_tray=False,
exclude={"Lit Game"},
)
Expand Down
3 changes: 3 additions & 0 deletions settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class SettingsFile(TypedDict):
exclude: set[str]
priority: list[str]
priority_only: bool
priority_by_time: bool
autostart_tray: bool
connection_quality: int
tray_notifications: bool
Expand All @@ -29,6 +30,7 @@ class SettingsFile(TypedDict):
"exclude": set(),
"autostart": False,
"priority_only": True,
"priority_by_time": False,
"autostart_tray": False,
"connection_quality": 1,
"language": DEFAULT_LANG,
Expand All @@ -52,6 +54,7 @@ class Settings:
exclude: set[str]
priority: list[str]
priority_only: bool
priority_by_time: bool
autostart_tray: bool
connection_quality: int
tray_notifications: bool
Expand Down
2 changes: 2 additions & 0 deletions translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ class GUISettingsGeneral(TypedDict):
tray: str
tray_notifications: str
priority_only: str
priority_by_time: str
proxy: str


Expand Down Expand Up @@ -363,6 +364,7 @@ class Translation(TypedDict):
"tray": "Autostart into tray: ",
"tray_notifications": "Tray notifications: ",
"priority_only": "Priority Only: ",
"priority_by_time": "Prioritize campaigns by end date: ",
"proxy": "Proxy (requires restart):",
},
"game_name": "Game name",
Expand Down
31 changes: 26 additions & 5 deletions twitch.py
Original file line number Diff line number Diff line change
Expand Up @@ -828,19 +828,40 @@ async def _run(self):
exclude = self.settings.exclude
priority = self.settings.priority
priority_only = self.settings.priority_only
priority_by_time = self.settings.priority_by_time
next_hour = datetime.now(timezone.utc) + timedelta(hours=1)
for campaign in self.inventory:
campaigns = self.inventory
campaigns.sort(key=lambda c: c.ends_at, reverse=False) # lets us give priority to campaigns ending soonest later on
max_priority = len(campaigns)
max_wanted_priority = max_priority + len(priorities)
jaredcat marked this conversation as resolved.
Show resolved Hide resolved
for campaign in campaigns:
game = campaign.game
if (
game not in self.wanted_games # isn't already there
and game.name not in exclude # and isn't excluded
game not in self.wanted_games # isn't already there
and game.name not in exclude # and isn't excluded
# and isn't excluded by priority_only
and (not priority_only or game.name in priority)
# and can be progressed within the next hour
and campaign.can_earn_within(next_hour)
):
# non-excluded games with no priority are placed last, below priority ones
self.wanted_games[game] = priorities.get(game.name, 0)
# get users priority preference
game_priority = priorities.get(game.name, 0)

if (game_priority):
if (not priority_by_time):
# keep games ordered by priority list
# max_wanted_priority does not change in this case, consider it "0"
self.wanted_games[game] = game_priority + max_wanted_priority
jaredcat marked this conversation as resolved.
Show resolved Hide resolved
else:
# sort priority games by time ending soonest
# games are already sorted by ends_at, max_wanted_priority only goes down for each added to the wanted_games list
self.wanted_games[game] = max_wanted_priority
max_wanted_priority -= 1
jaredcat marked this conversation as resolved.
Show resolved Hide resolved
else:
# non-excluded games with no priority are placed last, below priority ones
# but sorted by end time to maximize amount of potential drops
self.wanted_games[game] = max_priority
max_priority -= 1
jaredcat marked this conversation as resolved.
Show resolved Hide resolved
full_cleanup = True
self.restart_watching()
self.change_state(State.CHANNELS_CLEANUP)
Expand Down