app.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. console.log(sound);
  65. sound._player._volume = 0.3;
  66. sound.play();
  67. });
  68. },
  69. "click #play": function(){
  70. $("#play").hide();
  71. SC.stream("/tracks/172055891/", function(sound){
  72. hpSound = sound;
  73. sound._player._volume = 0.3;
  74. sound.play();
  75. $("#stop").on("click", function(){
  76. $("#stop").hide();
  77. $("#play").show();
  78. sound._player.pause();
  79. })
  80. });
  81. $("#stop").show();
  82. }
  83. });
  84. Template.Room.helpers({
  85. type: function() {
  86. var parts = location.href.split('/');
  87. var id = parts.pop();
  88. return id;
  89. },
  90. duration: function() {
  91. return Session.get("duration");
  92. }
  93. });
  94. Template.Room.onCreated(function () {
  95. var currentSong = undefined;
  96. var _sound = undefined;
  97. var size = 0;
  98. function getTimeElapsed() {
  99. if (currentSong !== undefined) {
  100. return Date.now() - currentSong.started;
  101. }
  102. return 0;
  103. }
  104. function startSong() {
  105. if (currentSong !== undefined) {
  106. if (_sound !== undefined)_sound.stop();
  107. SC.stream("/tracks/" + currentSong.song.id + "/", function(sound){
  108. _sound = sound;
  109. sound._player._volume = 0.3;
  110. sound.play();
  111. setTimeout(function() { // HACK, otherwise seek doesn't work.
  112. sound._player.seek(getTimeElapsed());
  113. }, 500);
  114. });
  115. }
  116. }
  117. Meteor.subscribe("history");
  118. Meteor.setInterval(function() {
  119. var data = undefined;
  120. var dataCursor = History.find({type: "edm"});
  121. dataCursor.map(function(doc) {
  122. if (data === undefined) {
  123. data = doc;
  124. }
  125. });
  126. console.log(data);
  127. if (data.history.length > size) {
  128. currentSong = data.history[data.history.length-1];
  129. size = data.history.length;
  130. startSong();
  131. }
  132. }, 1000);
  133. });
  134. }
  135. if (Meteor.isServer) {
  136. var startedAt = Date.now();
  137. var songs = [{id: 172055891, duration: 20}, {id: 194153620, duration: 60}];
  138. var currentSong = 0;
  139. addToHistory(songs[currentSong], startedAt);
  140. function addToHistory(song, startedAt) {
  141. History.update({type: "edm"}, {$push: {history: {song: song, started: startedAt}}});
  142. }
  143. function skipSong() {
  144. if (currentSong < (songs.length - 1)) {
  145. currentSong++;
  146. } else currentSong = 0;
  147. songTimer();
  148. addToHistory(songs[currentSong], startedAt);
  149. }
  150. function songTimer() {
  151. startedAt = Date.now();
  152. Meteor.setTimeout(function() {
  153. skipSong();
  154. }, songs[currentSong].duration * 1000);
  155. }
  156. ServiceConfiguration.configurations.remove({
  157. service: "facebook"
  158. });
  159. ServiceConfiguration.configurations.insert({
  160. service: "facebook",
  161. appId: "1496014310695890",
  162. secret: "9a039f254a08a1488c08bb0737dbd2a6"
  163. });
  164. ServiceConfiguration.configurations.remove({
  165. service: "github"
  166. });
  167. ServiceConfiguration.configurations.insert({
  168. service: "github",
  169. clientId: "dcecd720f47c0e4001f7",
  170. secret: "375939d001ef1a0ca67c11dbf8fb9aeaa551e01b"
  171. });
  172. songTimer();
  173. Meteor.publish("history", function() {
  174. return History.find({type: "edm"})
  175. });
  176. }
  177. Router.route("/", {
  178. template: "Home"
  179. });
  180. Router.route("/:type", {
  181. template: "Room"
  182. });