app.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. });
  96. Template.room.onCreated(function () {
  97. var currentSong = undefined;
  98. var _sound = undefined;
  99. var size = 0;
  100. function getTimeElapsed() {
  101. if (currentSong !== undefined) {
  102. return Date.now() - currentSong.started;
  103. }
  104. return 0;
  105. }
  106. function startSong() {
  107. if (currentSong !== undefined) {
  108. if (_sound !== undefined)_sound.stop();
  109. SC.stream("/tracks/" + currentSong.song.id + "/", function(sound){
  110. _sound = sound;
  111. sound._player._volume = 0.3;
  112. //sound.play();
  113. Session.set("title", currentSong.song.title || "Title");
  114. Session.set("artist", currentSong.song.artist || "Artist");
  115. Session.set("albumArt", currentSong.song.albumArt);
  116. Session.set("duration", currentSong.song.duration);
  117. setTimeout(function() { // HACK, otherwise seek doesn't work.
  118. sound._player.seek(getTimeElapsed());
  119. console.log(sound._player.seek(getTimeElapsed()));
  120. }, 500);
  121. });
  122. }
  123. }
  124. Meteor.subscribe("history");
  125. Meteor.setInterval(function() {
  126. var data = undefined;
  127. var dataCursor = History.find({type: "edm"});
  128. dataCursor.map(function(doc) {
  129. if (data === undefined) {
  130. data = doc;
  131. }
  132. });
  133. if (data.history.length > size) {
  134. currentSong = data.history[data.history.length-1];
  135. size = data.history.length;
  136. startSong();
  137. }
  138. }, 1000);
  139. });
  140. }
  141. if (Meteor.isServer) {
  142. var startedAt = Date.now();
  143. var songs = [{id: 172055891, title: "Immortals", artist: "Fall Out Boy", duration: 90}];
  144. var currentSong = 0;
  145. addToHistory(songs[currentSong], startedAt);
  146. function addToHistory(song, startedAt) {
  147. History.update({type: "edm"}, {$push: {history: {song: song, started: startedAt}}});
  148. }
  149. function skipSong() {
  150. if (currentSong < (songs.length - 1)) {
  151. currentSong++;
  152. } else currentSong = 0;
  153. songTimer();
  154. addToHistory(songs[currentSong], startedAt);
  155. }
  156. function songTimer() {
  157. startedAt = Date.now();
  158. Meteor.setTimeout(function() {
  159. skipSong();
  160. }, songs[currentSong].duration * 1000);
  161. }
  162. ServiceConfiguration.configurations.remove({
  163. service: "facebook"
  164. });
  165. ServiceConfiguration.configurations.insert({
  166. service: "facebook",
  167. appId: "1496014310695890",
  168. secret: "9a039f254a08a1488c08bb0737dbd2a6"
  169. });
  170. ServiceConfiguration.configurations.remove({
  171. service: "github"
  172. });
  173. ServiceConfiguration.configurations.insert({
  174. service: "github",
  175. clientId: "dcecd720f47c0e4001f7",
  176. secret: "375939d001ef1a0ca67c11dbf8fb9aeaa551e01b"
  177. });
  178. songTimer();
  179. Meteor.publish("history", function() {
  180. return History.find({type: "edm"})
  181. });
  182. }
  183. Router.route("/", {
  184. template: "home"
  185. });
  186. Router.route("/:type", {
  187. template: "room"
  188. });