Skip to content

Commit d0ff2d5

Browse files
committed
Fix queue system
1 parent 80c2564 commit d0ff2d5

File tree

3 files changed

+97
-22
lines changed

3 files changed

+97
-22
lines changed

bot/src/main/java/me/duncte123/skybot/audio/AudioLoader.java

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.slf4j.Logger;
3636
import org.slf4j.LoggerFactory;
3737

38+
import java.util.ArrayList;
3839
import java.util.List;
3940
import java.util.Objects;
4041
import java.util.Optional;
@@ -140,19 +141,17 @@ public void onPlaylistLoaded(@NotNull PlaylistLoaded playlistLoaded) {
140141
tracksRaw = tracksRaw.subList(selectedTrackIndex, tracksRaw.size());
141142
}
142143

143-
final List<Track> limitedTracks = tracksRaw.stream().peek((track) -> {
144+
final List<Track> userTracks = tracksRaw.stream().peek((track) -> {
144145
track.setUserData(new UUIDUserData());
145146

146147
mng.getScheduler().storeUserData(track, new TrackUserData(this.requester));
147148
}).toList();
149+
final var tracksCopy = new ArrayList<>(userTracks);
148150

149-
for (final Track track : limitedTracks) {
150-
trackScheduler.addToQueue(track, this.isPatron);
151-
}
151+
final int totalTracksQueued = trackScheduler.queuePlaylistTracks(tracksCopy, this.isPatron);
152152

153153
if (this.announce) {
154-
// TODO: find a way to fix up BigChungusPlaylist
155-
final String msg = getPlaylistMsg(tracks, playlistInfo);
154+
final String msg = getPlaylistMsg(tracks, totalTracksQueued, playlistInfo);
156155

157156
sendMsg(
158157
new MessageConfig.Builder()
@@ -240,22 +239,6 @@ public void loadFailed(@NotNull LoadFailed loadFailed) {
240239
);
241240
}
242241

243-
private String getPlaylistMsg(List<Track> tracks, PlaylistInfo playlistInfo) {
244-
final String sizeMsg = String.valueOf(tracks.size());
245-
246-
/*if (playlist instanceof BigChungusPlaylist bigBoi && bigBoi.isBig()) {
247-
sizeMsg = tracks.size() + "/" + bigBoi.getOriginalSize();
248-
} else {
249-
sizeMsg = String.valueOf(tracks.size());
250-
}*/
251-
252-
return String.format(
253-
"Adding **%s** tracks to the queue from **%s**",
254-
sizeMsg,
255-
playlistInfo.getName()
256-
);
257-
}
258-
259242
// private void searchLoaded(LoadResult.SearchResult searchResult) {
260243
// System.out.println("WARNING A SEARCH RESULT WAS TRIGGERED " + searchResult);
261244
// }
@@ -273,6 +256,22 @@ public void noMatches() {
273256
}
274257
}
275258

259+
private String getPlaylistMsg(List<Track> tracks, int totalTracksAdded, PlaylistInfo playlistInfo) {
260+
final String sizeMsg;
261+
262+
if (tracks.size() < totalTracksAdded) {
263+
sizeMsg = totalTracksAdded + "/" + tracks.size();
264+
} else {
265+
sizeMsg = String.valueOf(tracks.size());
266+
}
267+
268+
return String.format(
269+
"Adding **%s** tracks to the queue from **%s**",
270+
sizeMsg,
271+
playlistInfo.getName()
272+
);
273+
}
274+
276275
private static String getSteamTitle(Track track, String rawTitle, CommandManager commandManager) {
277276
String title = rawTitle;
278277

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Skybot, a multipurpose discord bot
3+
* Copyright (C) 2017 Duncan "duncte123" Sterken & Ramid "ramidzkh" Khan & Maurice R S "Sanduhr32"
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU Affero General Public License as published
7+
* by the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU Affero General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Affero General Public License
16+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
*/
18+
19+
package me.duncte123.skybot.audio;
20+
21+
import dev.arbjerg.lavalink.client.protocol.Track;
22+
23+
import java.util.List;
24+
25+
public class BigChungusPlaylist {
26+
private final int originalSize;
27+
private final String name;
28+
private final List<Track> tracks;
29+
30+
public BigChungusPlaylist(String name, List<Track> tracks, int originalSize) {
31+
this.name = name;
32+
this.tracks = tracks;
33+
this.originalSize = originalSize;
34+
}
35+
36+
public String getName() {
37+
return name;
38+
}
39+
40+
public List<Track> getTracks() {
41+
return tracks;
42+
}
43+
44+
public int getOriginalSize() {
45+
return originalSize;
46+
}
47+
48+
public boolean isBig() {
49+
return originalSize > tracks.size();
50+
}
51+
}

bot/src/main/java/me/duncte123/skybot/audio/TrackScheduler.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,31 @@ public void addToQueue(Track track, boolean isPatron) throws LimitReachedExcepti
9090
}
9191
}
9292

93+
/**
94+
*
95+
* @param tracks
96+
* @param isPatron
97+
* @return The total amount of tracks that were added to the queue.
98+
* @throws LimitReachedException
99+
*/
100+
public int queuePlaylistTracks(List<Track> tracks, boolean isPatron) throws LimitReachedException {
101+
final List<Track> tmpQueue = new ArrayList<>();
102+
103+
while (!tracks.isEmpty() && (tmpQueue.size() + queue.size() + 1 < MAX_QUEUE_SIZE || isPatron)) {
104+
tmpQueue.add(tracks.removeFirst());
105+
}
106+
107+
final int tracksAdded = tmpQueue.size();
108+
109+
if (this.guildMusicManager.getPlayer().getCurrentTrack() == null) {
110+
this.play(tmpQueue.removeFirst());
111+
}
112+
113+
queue.addAll(tmpQueue);
114+
115+
return tracksAdded;
116+
}
117+
93118
/**
94119
* This is a special case for the skip command where it has to announce the next track
95120
* due to it being a user interaction

0 commit comments

Comments
 (0)