aboutsummaryrefslogtreecommitdiff
path: root/src/services/queue.ts
diff options
context:
space:
mode:
authorMax Isom <[email protected]>2020-03-15 14:36:59 -0500
committerMax Isom <[email protected]>2020-03-15 14:36:59 -0500
commit3408c7a0c2ee35fc9a47c37b94b9ed789314f4cc (patch)
tree2b35d0b2ab90d014ab07f4d2b62e5c225fac07a1 /src/services/queue.ts
parent0cebca7917e66166a3b5d62127555e5ca45e3154 (diff)
downloadmuse-3408c7a0c2ee35fc9a47c37b94b9ed789314f4cc.tar.xz
muse-3408c7a0c2ee35fc9a47c37b94b9ed789314f4cc.zip
Use manager instances for guild services
Diffstat (limited to 'src/services/queue.ts')
-rw-r--r--src/services/queue.ts84
1 files changed, 23 insertions, 61 deletions
diff --git a/src/services/queue.ts b/src/services/queue.ts
index 0b98b8e..f439a5c 100644
--- a/src/services/queue.ts
+++ b/src/services/queue.ts
@@ -1,4 +1,3 @@
-import {injectable} from 'inversify';
import shuffle from 'array-shuffle';
export interface QueuedPlaylist {
@@ -14,62 +13,40 @@ export interface QueuedSong {
playlist: QueuedPlaylist | null;
}
-@injectable()
export default class {
- private readonly guildQueues = new Map<string, QueuedSong[]>();
- private readonly queuePositions = new Map<string, number>();
+ private queue: QueuedSong[] = [];
+ private position = 0;
- forward(guildId: string): void {
- const currentPosition = this.queuePositions.get(guildId);
-
- if (currentPosition && currentPosition + 1 <= this.size(guildId)) {
- this.queuePositions.set(guildId, currentPosition + 1);
+ forward(): void {
+ if (this.position + 1 <= this.size()) {
+ this.position++;
} else {
throw new Error('No songs in queue to forward to.');
}
}
- back(guildId: string): void {
- const currentPosition = this.queuePositions.get(guildId);
-
- if (currentPosition && currentPosition - 1 >= 0) {
- this.queuePositions.set(guildId, currentPosition - 1);
+ back(): void {
+ if (this.position - 1 >= 0) {
+ this.position--;
} else {
throw new Error('No songs in queue to go back to.');
}
}
- get(guildId: string): QueuedSong[] {
- const currentPosition = this.queuePositions.get(guildId);
-
- if (currentPosition === undefined) {
- return [];
- }
-
- const guildQueue = this.guildQueues.get(guildId);
-
- if (!guildQueue) {
- throw new Error('Bad state. Queue for guild exists but position does not.');
- }
-
- return guildQueue.slice(currentPosition);
+ get(): QueuedSong[] {
+ return this.queue.slice(this.position);
}
- add(guildId: string, song: QueuedSong): void {
- this.initQueue(guildId);
-
+ add(song: QueuedSong): void {
if (song.playlist) {
// Add to end of queue
- this.guildQueues.set(guildId, [...this.guildQueues.get(guildId)!, song]);
- } else if (this.guildQueues.get(guildId)!.length === 0) {
- // Queue is currently empty
- this.guildQueues.set(guildId, [song]);
+ this.queue.push(song);
} else {
// Not from playlist, add immediately
let insertAt = 0;
// Loop until playlist song
- this.guildQueues.get(guildId)!.some(song => {
+ this.queue.some(song => {
if (song.playlist) {
return true;
}
@@ -78,41 +55,26 @@ export default class {
return false;
});
- this.guildQueues.set(guildId, [...this.guildQueues.get(guildId)!.slice(0, insertAt), song, ...this.guildQueues.get(guildId)!.slice(insertAt)]);
+ this.queue = [...this.queue.slice(0, insertAt), song, ...this.queue.slice(insertAt)];
}
}
- shuffle(guildId: string): void {
- const queue = this.guildQueues.get(guildId);
-
- if (!queue) {
- throw new Error('Queue doesn\'t exist yet.');
- }
-
- this.guildQueues.set(guildId, [queue[0], ...shuffle(queue.slice(1))]);
+ shuffle(): void {
+ this.queue = [this.queue[0], ...shuffle(this.queue.slice(1))];
}
- clear(guildId: string): void {
- this.initQueue(guildId);
- const queue = this.guildQueues.get(guildId);
-
+ clear(): void {
const newQueue = [];
- if (queue!.length > 0) {
- newQueue.push(queue![0]);
+ // Don't clear curently playing song
+ if (this.queue.length > 0) {
+ newQueue.push(this.queue[0]);
}
- this.guildQueues.set(guildId, newQueue);
- }
-
- size(guildId: string): number {
- return this.get(guildId).length;
+ this.queue = newQueue;
}
- private initQueue(guildId: string): void {
- if (!this.guildQueues.get(guildId)) {
- this.guildQueues.set(guildId, []);
- this.queuePositions.set(guildId, 0);
- }
+ size(): number {
+ return this.queue.length;
}
}