app.js 5.8 KB

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