app.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. History = new Mongo.Collection("history");
  2. if (Meteor.isClient) {
  3. var hpSound = undefined;
  4. Template.register.events({
  5. "submit form": function(e){
  6. e.preventDefault();
  7. var username = e.target.registerUsername.value;
  8. var email = e.target.registerEmail.value;
  9. var password = e.target.registerPassword.value;
  10. Accounts.createUser({
  11. username: username,
  12. email: email,
  13. password: password
  14. });
  15. },
  16. "click #facebook-login": function(){
  17. Meteor.loginWithFacebook()
  18. },
  19. "click #github-login": function(){
  20. Meteor.loginWithGithub()
  21. },
  22. "click #login": function(){
  23. $("#register-view").hide();
  24. $("#login-view").show();
  25. }
  26. });
  27. Template.login.events({
  28. "submit form": function(e){
  29. e.preventDefault();
  30. var username = e.target.loginUsername.value;
  31. var password = e.target.loginPassword.value;
  32. Meteor.loginWithPassword(username, password);
  33. Accounts.onLoginFailure(function(){
  34. $("input").css("background-color","indianred").addClass("animated shake");
  35. $("input").on("click",function(){
  36. $("input").css({
  37. "background-color": "transparent",
  38. "width": "250px"
  39. });
  40. })
  41. });
  42. },
  43. "click #facebook-login": function(){
  44. Meteor.loginWithFacebook()
  45. },
  46. "click #github-login": function(){
  47. Meteor.loginWithGithub()
  48. },
  49. "click #register": function(){
  50. $("#login-view").hide();
  51. $("#register-view").show();
  52. }
  53. });
  54. Template.dashboard.events({
  55. "click .logout": function(e){
  56. e.preventDefault();
  57. Meteor.logout();
  58. if (hpSound !== undefined) {
  59. hpSound.stop();
  60. }
  61. },
  62. "click .button-tunein": function(){
  63. SC.stream("/tracks/172055891/", function(sound){
  64. sound._player._volume = 0.3;
  65. sound.play();
  66. });
  67. },
  68. "click #play": function(){
  69. $("#play").hide();
  70. SC.stream("/tracks/172055891/", function(sound){
  71. hpSound = sound;
  72. sound._player._volume = 0.3;
  73. sound.play();
  74. $("#stop").on("click", function(){
  75. $("#stop").hide();
  76. $("#play").show();
  77. sound._player.pause();
  78. })
  79. });
  80. $("#stop").show();
  81. }
  82. });
  83. Template.room.helpers({
  84. type: function() {
  85. var parts = location.href.split('/');
  86. var id = parts.pop();
  87. return id.toUpperCase();
  88. },
  89. title: function(){
  90. return Session.get("title");
  91. },
  92. artist: function(){
  93. return Session.get("artist");
  94. },
  95. duration: function(){
  96. }
  97. });
  98. Template.room.onCreated(function () {
  99. var currentSong = undefined;
  100. var _sound = undefined;
  101. var size = 0;
  102. function getTimeElapsed() {
  103. if (currentSong !== undefined) {
  104. return Date.now() - currentSong.started;
  105. }
  106. return 0;
  107. }
  108. function startSong() {
  109. if (currentSong !== undefined) {
  110. if (_sound !== undefined)_sound.stop();
  111. SC.stream("/tracks/" + currentSong.song.id + "/", function(sound){
  112. _sound = sound;
  113. sound._player._volume = 0.3;
  114. sound.play();
  115. Session.set("title", currentSong.song.title || "Title");
  116. Session.set("artist", currentSong.song.artist || "Artist");
  117. Session.set("albumArt", currentSong.song.albumArt);
  118. Session.set("duration", currentSong.song.duration);
  119. Session.set("increment", 1400 / currentSong.song.duration);
  120. setTimeout(function() { // HACK, otherwise seek doesn't work.
  121. sound._player.seek(getTimeElapsed());
  122. }, 500);
  123. });
  124. }
  125. }
  126. Meteor.subscribe("history");
  127. Meteor.setInterval(function() {
  128. var data = undefined;
  129. var dataCursor = History.find({type: "edm"});
  130. dataCursor.map(function(doc) {
  131. if (data === undefined) {
  132. data = doc;
  133. }
  134. });
  135. if (data.history.length > size) {
  136. currentSong = data.history[data.history.length-1];
  137. size = data.history.length;
  138. startSong();
  139. }
  140. $("#seeker-bar").width($("#seeker-bar").width() + Session.get("increment"));
  141. console.log($("#seeker-bar").width());
  142. }, 1000);
  143. });
  144. }
  145. if (Meteor.isServer) {
  146. var startedAt = Date.now();
  147. var songs = [{id: 172055891, title: "Immortals", artist: "Fall Out Boy", duration: 90}];
  148. var currentSong = 0;
  149. addToHistory(songs[currentSong], startedAt);
  150. function addToHistory(song, startedAt) {
  151. History.update({type: "edm"}, {$push: {history: {song: song, started: startedAt}}});
  152. }
  153. function skipSong() {
  154. if (currentSong < (songs.length - 1)) {
  155. currentSong++;
  156. } else currentSong = 0;
  157. songTimer();
  158. addToHistory(songs[currentSong], startedAt);
  159. }
  160. function songTimer() {
  161. startedAt = Date.now();
  162. Meteor.setTimeout(function() {
  163. skipSong();
  164. }, songs[currentSong].duration * 1000);
  165. }
  166. ServiceConfiguration.configurations.remove({
  167. service: "facebook"
  168. });
  169. ServiceConfiguration.configurations.insert({
  170. service: "facebook",
  171. appId: "1496014310695890",
  172. secret: "9a039f254a08a1488c08bb0737dbd2a6"
  173. });
  174. ServiceConfiguration.configurations.remove({
  175. service: "github"
  176. });
  177. ServiceConfiguration.configurations.insert({
  178. service: "github",
  179. clientId: "dcecd720f47c0e4001f7",
  180. secret: "375939d001ef1a0ca67c11dbf8fb9aeaa551e01b"
  181. });
  182. songTimer();
  183. Meteor.publish("history", function() {
  184. return History.find({type: "edm"})
  185. });
  186. }
  187. Router.route("/", {
  188. template: "home"
  189. });
  190. Router.route("/:type", {
  191. template: "room"
  192. });