Explorar el Código

got finishing a playlist to work, and removed a bad log/output line

tarfeef101 hace 4 años
padre
commit
419f3c0212
Se han modificado 3 ficheros con 107 adiciones y 30 borrados
  1. 11 25
      src/Queue.js
  2. 90 0
      src/Queue.js.bad
  3. 6 5
      src/viki.js

+ 11 - 25
src/Queue.js

@@ -4,25 +4,18 @@ class Queue
   constructor()
   {
     this.arr = [];
-    this.pos = 0;
   }
-  
+
   // uses the array x to fill the queue
   makeFilled(x)
   {
     this.arr = x;
-    this.offset = 0;
   }
 
   // Returns true if the queue is empty, and false otherwise.
   isEmpty()
   {
-    return (this.arr.length == 0);
-  }
-  
-  getPos()
-  {
-    return this.pos;
+    return (this.arr.length === 0);
   }
 
   // Enqueues x in the queue (to the end)
@@ -30,13 +23,13 @@ class Queue
   {
     this.arr.push(x);
   }
-  
+
   // enqueues x to position i
   insert(x, i)
   {
     this.arr.splice(i, 0, x);
   }
-  
+
   // removes item at index i
   remove(i)
   {
@@ -47,20 +40,14 @@ class Queue
   dequeue()
   {
     // if the queue is empty, throw
-    if (this.arr.length == 0)
+    if (this.arr.length === 0)
     {
       throw "Queue empty!";
     }
-    
-    // if the queue is at the end already, return undefined
-    if (this.pos >= this.arr.length)
-    {
-      return undefined;
-    }
 
     // store the item at the front of the queue
-    var item = this.arr[this.pos];
-    ++this.pos;
+    const item = this.arr[0];
+    this.remove(0);
 
     // return the dequeued item
     return item;
@@ -70,20 +57,19 @@ class Queue
   // queue is empty then undefined is returned.
   peek()
   {
-    return (this.arr.length > 0 ? this.arr[this.pos] : undefined);
+    return (this.arr.length > 0 ? this.arr[0] : undefined);
   }
-  
+
   // returns an array of all items in the queue (again, without dequeuing) from the current pos.
   read()
   {
-    return this.arr.slice(this.pos);
+    return this.arr;
   }
-  
+
   // Deletes all the data, resets to as on construction
   reset()
   {
     this.arr = [];
-    this.pos = 0;
   }
 }
 

+ 90 - 0
src/Queue.js.bad

@@ -0,0 +1,90 @@
+class Queue
+{
+  // initializes the queue
+  constructor()
+  {
+    this.arr = [];
+    this.pos = 0;
+  }
+  
+  // uses the array x to fill the queue
+  makeFilled(x)
+  {
+    this.arr = x;
+    this.pos = 0;
+  }
+
+  // Returns true if the queue is empty, and false otherwise.
+  isEmpty()
+  {
+    return (this.arr.length == 0 || this.pos == (this.arr.length - 1));
+  }
+  
+  getPos()
+  {
+    return this.pos;
+  }
+
+  // Enqueues x in the queue (to the end)
+  enqueue(x)
+  {
+    this.arr.push(x);
+  }
+  
+  // enqueues x to position i
+  insert(x, i)
+  {
+    this.arr.splice(i, 0, x);
+  }
+  
+  // removes item at index i
+  remove(i)
+  {
+    this.arr.splice(i, 1);
+  }
+
+  // Dequeues an item and returns it. If the queue is empty, throws an error
+  dequeue()
+  {
+    // if the queue is empty, throw
+    if (this.arr.length == 0)
+    {
+      throw "Queue empty!";
+    }
+    
+    // if the queue is at the end already, return undefined
+    if (this.pos >= this.arr.length)
+    {
+      return undefined;
+    }
+
+    // store the item at the front of the queue
+    var item = this.arr[this.pos];
+    ++this.pos;
+
+    // return the dequeued item
+    return item;
+  }
+
+  // Returns the item at the front of the queue (without dequeuing it). If the
+  // queue is empty then undefined is returned.
+  peek()
+  {
+    return (this.arr.length > 0 ? this.arr[this.pos] : undefined);
+  }
+  
+  // returns an array of all items in the queue (again, without dequeuing) from the current pos.
+  read()
+  {
+    return this.arr.slice(this.pos);
+  }
+  
+  // Deletes all the data, resets to as on construction
+  reset()
+  {
+    this.arr = [];
+    this.pos = 0;
+  }
+}
+
+module.exports = QueueButBad;

+ 6 - 5
src/viki.js

@@ -44,7 +44,7 @@ function play()
   var nextSong = cursong;
   
   // if we aren't repeating cursong, dequeue
-  if (!repeatone)
+  if (!repeatone && cursong)
   {
     played.push(cursong);
     nextSong = playlist.dequeue();
@@ -68,7 +68,6 @@ function play()
     if (!(playlist.isEmpty()))
     {
       play();
-      console.log(reason);
     }
     else
     {
@@ -88,7 +87,7 @@ function play()
 
   // what to do if it ends
   dispatcher.on("finish", endHandler);
-  dispatcher.on("close", endHandler);
+  //dispatcher.on("close", endHandler);
 }
 
 // riven stuff
@@ -322,7 +321,7 @@ client.on('message', async msg => {
                   playlist.enqueue([filepathRaw, msg, stdouts]);
                   
                   // check if music is playing, if not start it
-                  if ((dispatcher === undefined || dispatcher.ended == true) && !(playlist.isEmpty()))
+                  if ((!dispatcher || dispatcher.ended || dispatcher.destroyed || dispatcher.writableFinished || dispatcher.writableEnded) && !(playlist.isEmpty()))
                   {
                     play();
                   }
@@ -384,11 +383,13 @@ client.on('message', async msg => {
     else
     {
       const list = playlist.read();
+      var retstr = ""
       
       for (var i = 0; i < list.length; i++)
       {
-        msg.channel.send(`Song #${i + 1} is: ${list[i][2]}.`);
+        retstr += `Song #${i + 1} is: ${list[i][2]}.\n`;
       }
+      msg.channel.send(retstr);
     }
   }