浏览代码

Merge branch 'master' of https://github.com/AkiraLaine/music-app

unknown 9 年之前
父节点
当前提交
9466d4b46e
共有 2 个文件被更改,包括 136 次插入32 次删除
  1. 132 30
      app/app.js
  2. 4 2
      app/templates/admin.html

+ 132 - 30
app/app.js

@@ -111,7 +111,11 @@ if (Meteor.isClient) {
         },
         "click #croom_create": function() {
             Meteor.call("createRoom", $("#croom").val(), function (err, res) {
-                console.log(err, res);
+                if (err) {
+                    alert("Error " + err.error + ": " + err.reason);
+                } else {
+                    window.location = "/" + $("#croom").val();
+                }
             });
         }
     });
@@ -189,6 +193,12 @@ if (Meteor.isClient) {
         }
     });
 
+    Template.admin.helpers({
+        rooms: function() {
+            return Rooms.find({});
+        }
+    });
+
     Template.playlist.helpers({
         playlist_songs: function() {
             var data = Playlists.find({type: type}).fetch();
@@ -200,6 +210,8 @@ if (Meteor.isClient) {
         }
     });
 
+    Meteor.subscribe("rooms");
+
     Template.room.onCreated(function () {
         var tag = document.createElement("script");
         tag.src = "https://www.youtube.com/iframe_api";
@@ -334,6 +346,7 @@ if (Meteor.isClient) {
 
         Meteor.subscribe("history");
         Meteor.subscribe("playlists");
+        Session.set("loaded", true);
         Meteor.subscribe("rooms", function() {
             Session.set("loaded", false);
             console.log(Rooms.find({type: type}).fetch().length);
@@ -427,49 +440,63 @@ if (Meteor.isServer) {
     //var room_types = ["edm", "nightcore"];
     var songsArr = [];
 
+    function getSongsByType(type) {
+        if (type === "edm") {
+            return [
+                {id: "aE2GCa-_nyU", title: "Radioactive - Lindsey Stirling and Pentatonix", duration: getSongDuration("Radioactive - Lindsey Stirling and Pentatonix"), albumart: getSongAlbumArt("Radioactive - Lindsey Stirling and Pentatonix"), type: "youtube"},
+                {id: "aHjpOzsQ9YI", title: "Crystallize", artist: "Linsdey Stirling", duration: getSongDuration("Crystallize"), albumart: getSongAlbumArt("Crystallize"), type: "youtube"}
+            ];
+        } else if (type === "nightcore") {
+            return [{id: "f7RKOP87tt4", title: "Monster (DotEXE Remix)", duration: getSongDuration("Monster (DotEXE Remix)"), albumart: getSongAlbumArt("Monster (DotEXE Remix)"), type: "youtube"}];
+        } else {
+            return [{id: "dQw4w9WgXcQ", title: "Never Gonna Give You Up", duration: getSongDuration("Never Gonna Give You Up"), albumart: getSongAlbumArt("Never Gonna Give You Up"), type: "youtube"}];
+        }
+    }
 
     Rooms.find({}).fetch().forEach(function(room) {
         var type = room.type;
-        if (Playlists.find({type: type}).fetch().length === 0) {
+        if (Playlists.find({type: type}).count() === 0) {
             if (type === "edm") {
-                Playlists.insert({type: type, songs: [
-                  {id: "aE2GCa-_nyU", title: "Radioactive - Lindsey Stirling and Pentatonix", duration: getSongDuration("Radioactive - Lindsey Stirling and Pentatonix"), albumart: getSongAlbumArt("Radioactive - Lindsey Stirling and Pentatonix"), type: "youtube"},
-                  {id: "aHjpOzsQ9YI", title: "Crystallize", artist: "Linsdey Stirling", duration: getSongDuration("Crystallize"), albumart: getSongAlbumArt("Crystallize"), type: "youtube"}
-                ]});
+                Playlists.insert({type: type, songs: getSongsByType(type)});
             } else if (type === "nightcore") {
-                Playlists.insert({type: type, songs: [{id: "f7RKOP87tt4", title: "Monster (DotEXE Remix)", duration: getSongDuration("Monster (DotEXE Remix)"), albumart: getSongAlbumArt("Monster (DotEXE Remix)"), type: "youtube"}]});
+                Playlists.insert({type: type, songs: getSongsByType(type)});
+            } else {
+                Playlists.insert({type: type, songs: getSongsByType(type)});
             }
         }
-        if (History.find({type: type}).fetch().length === 0) {
+        if (History.find({type: type}).count() === 0) {
             History.insert({type: type, history: []});
         }
+        if (Playlists.find({type: type}).fetch()[0].songs.length === 0) {
+            // Add a global video to Playlist so it can proceed
+        } else {
+            var startedAt = Date.now();
+            var songs = Playlists.find({type: type}).fetch()[0].songs;
+            var currentSong = 0;
+            addToHistory(songs[currentSong], startedAt);
 
-        var startedAt = Date.now();
-        var songs = Playlists.find({type: type}).fetch()[0].songs;
-        var currentSong = 0;
-        addToHistory(songs[currentSong], startedAt);
+            function addToHistory(song, startedAt) {
+                History.update({type: type}, {$push: {history: {song: song, started: startedAt}}});
+            }
 
-        function addToHistory(song, startedAt) {
-            History.update({type: type}, {$push: {history: {song: song, started: startedAt}}});
-        }
+            function skipSong() {
+                songs = Playlists.find({type: type}).fetch()[0].songs;
+                if (currentSong < (songs.length - 1)) {
+                    currentSong++;
+                } else currentSong = 0;
+                songTimer();
+                addToHistory(songs[currentSong], startedAt);
+            }
 
-        function skipSong() {
-            songs = Playlists.find({type: type}).fetch()[0].songs;
-            if (currentSong < (songs.length - 1)) {
-                currentSong++;
-            } else currentSong = 0;
-            songTimer();
-            addToHistory(songs[currentSong], startedAt);
-        }
+            function songTimer() {
+                startedAt = Date.now();
+                Meteor.setTimeout(function() {
+                    skipSong();
+                }, songs[currentSong].duration * 1000);
+            }
 
-        function songTimer() {
-            startedAt = Date.now();
-            Meteor.setTimeout(function() {
-                skipSong();
-            }, songs[currentSong].duration * 1000);
+            songTimer();
         }
-
-        songTimer();
     });
 
 
@@ -535,6 +562,81 @@ if (Meteor.isServer) {
             } else {
                 throw new Meteor.error(403, "Invalid genre.");
             }
+        },
+        createRoom: function(type) {
+            if (Rooms.find({type: type}).count() === 0) {
+                Rooms.insert({type: type}, function(err) {
+                    if (err) {
+                        throw err;
+                    } else {
+                        if (Playlists.find({type: type}).count() === 1) {
+                            if (History.find({type: type}).count() === 0) {
+                                History.insert({type: type, history: []}, function(err3) {
+                                    if (err3) {
+                                        throw err3;
+                                    } else {
+                                        startStation();
+                                        return true;
+                                    }
+                                });
+                            } else {
+                                startStation();
+                                return true;
+                            }
+                        } else {
+                            Playlists.insert({type: type, songs: getSongsByType(type)}, function (err2) {
+                                if (err2) {
+                                    throw err2;
+                                } else {
+                                    if (History.find({type: type}).count() === 0) {
+                                        History.insert({type: type, history: []}, function(err3) {
+                                            if (err3) {
+                                                throw err3;
+                                            } else {
+                                                startStation();
+                                                return true;
+                                            }
+                                        });
+                                    } else {
+                                        startStation();
+                                        return true;
+                                    }
+                                }
+                            });
+                        }
+                    }
+                });
+            } else {
+                throw "Room already exists";
+            }
+            function startStation() {
+                var startedAt = Date.now();
+                var songs = Playlists.find({type: type}).fetch()[0].songs;
+                var currentSong = 0;
+                addToHistory(songs[currentSong], startedAt);
+
+                function addToHistory(song, startedAt) {
+                    History.update({type: type}, {$push: {history: {song: song, started: startedAt}}});
+                }
+
+                function skipSong() {
+                    songs = Playlists.find({type: type}).fetch()[0].songs;
+                    if (currentSong < (songs.length - 1)) {
+                        currentSong++;
+                    } else currentSong = 0;
+                    songTimer();
+                    addToHistory(songs[currentSong], startedAt);
+                }
+
+                function songTimer() {
+                    startedAt = Date.now();
+                    Meteor.setTimeout(function() {
+                        skipSong();
+                    }, songs[currentSong].duration * 1000);
+                }
+
+                songTimer();
+            }
         }
     });
 }

+ 4 - 2
app/templates/admin.html

@@ -4,8 +4,10 @@
         <form>
             <label for="genre">Song Genre</label>
             <select name="genre" id="genre">
-                <option name="edm" id="edm">EDM</option>
-                <option name="nightcore" id="nightcore">Nightcore</option>
+                {{#each rooms}}
+                    <option name="{{type}}" id="{{type}}">{{type}}</option>
+                {{/each}}
+                <!--option name="nightcore" id="nightcore">Nightcore</option-->
             </select>
             <label for="type">Song Type</label>
             <select name="type" id="type">