Skip to content

Commit

Permalink
Round robin patch (#2396)
Browse files Browse the repository at this point in the history
* Fix index errors in reorder_for_round_robin function.

* Update comments
  • Loading branch information
itsTheFae committed Apr 5, 2024
1 parent 4c287a4 commit 2b2b88a
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions musicbot/playlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,24 +330,37 @@ def get_next_song_from_author(

def reorder_for_round_robin(self) -> None:
"""
Reorders the queue for round-robin
Reorders the current queue for round-robin, one song per author.
Entries added by the auto playlist will be removed.
"""
new_queue: Deque[EntryTypes] = deque()
all_authors: List["discord.abc.User"] = []

# Make a list of unique authors from the current queue.
for entry in self.entries:
author = entry.author
if author and author not in all_authors:
all_authors.append(author)
if entry.author and entry.author not in all_authors:
all_authors.append(entry.author)

# If all queue entries have no author, do nothing.
if len(all_authors) == 0:
return

# Loop over the queue and organize it by requesting author.
# This will remove entries which are added by the auto-playlist.
request_counter = 0
song: Optional[EntryTypes] = None
while self.entries:
if request_counter == len(all_authors):
# Do not continue if we have no more authors.
if len(all_authors) == 0:
break

# Reset the requesting author if needed.
if request_counter >= len(all_authors):
request_counter = 0

song = self.get_next_song_from_author(all_authors[request_counter])

# Remove the authors with no further songs.
if song is None:
all_authors.pop(request_counter)
continue
Expand Down

0 comments on commit 2b2b88a

Please sign in to comment.