app.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. var currentSong = undefined;
  90. var _sound = undefined;
  91. var size = 0;
  92. function getTimeElapsed() {
  93. if (currentSong !== undefined) {
  94. return Date.now() - currentSong.started;
  95. }
  96. return 0;
  97. }
  98. function startSong() {
  99. if (currentSong !== undefined) {
  100. if (_sound !== undefined)_sound.stop();
  101. SC.stream("/tracks/" + currentSong.song.id + "/", function(sound){
  102. _sound = sound;
  103. sound._player._volume = 0.3;
  104. sound.play();
  105. setTimeout(function() { // HACK, otherwise seek doesn't work.
  106. sound._player.seek(getTimeElapsed());
  107. }, 500);
  108. });
  109. }
  110. }
  111. Template.Room.onCreated(function () {
  112. /*var instance = this;
  113. HTTP.get('/api/room/edm', function (err, data) {
  114. instance.data = data;
  115. console.log(data);
  116. // PLAY SONG AND SUCH
  117. });*/
  118. /*console.log("Created!");
  119. Meteor.call("getDuration", function(err, res) {
  120. Session.set("duration", res);
  121. console.log(res);
  122. });
  123. Meteor.call("getStart", function(err, res) {
  124. Session.set("start", res);
  125. console.log(res);
  126. });
  127. Meteor.call("getSong", function(err, res) {
  128. Session.set("song", res);
  129. console.log(res);
  130. SC.stream("/tracks/" + res + "/", function(sound){
  131. console.log(sound);
  132. sound._player._volume = 0.3;
  133. sound.play();
  134. setTimeout(function() { // HACK, otherwise seek doesn't work.
  135. sound._player.seek(Date.now() - Session.get("start"));
  136. console.log((Date.now() - Session.get("start")) + "--");
  137. }, 500);
  138. });
  139. });*/
  140. });
  141. Meteor.subscribe("history");
  142. Meteor.setInterval(function() {
  143. var data = History.findOne();
  144. if (data.history.length > size) {
  145. currentSong = data.history[data.history.length-1];
  146. size = data.history.length;
  147. startSong();
  148. }
  149. }, 1000);
  150. //console.log(History, "History");
  151. }
  152. if (Meteor.isServer) {
  153. var startedAt = Date.now();
  154. var songs = [{id: 172055891, duration: 20}, {id: 101244339, duration: 60}];
  155. var currentSong = 0;
  156. addToHistory(songs[currentSong], startedAt);
  157. function addToHistory(song, startedAt) {
  158. History.update({type: "edm"}, {$push: {history: {song: song, started: startedAt}}});
  159. }
  160. function skipSong() {
  161. if (currentSong < (songs.length - 1)) {
  162. currentSong++;
  163. } else currentSong = 0;
  164. songTimer();
  165. addToHistory(songs[currentSong], startedAt);
  166. }
  167. function songTimer() {
  168. startedAt = Date.now();
  169. Meteor.setTimeout(function() {
  170. skipSong();
  171. }, songs[currentSong].duration * 1000);
  172. }
  173. ServiceConfiguration.configurations.remove({
  174. service: "facebook"
  175. });
  176. ServiceConfiguration.configurations.insert({
  177. service: "facebook",
  178. appId: "1496014310695890",
  179. secret: "9a039f254a08a1488c08bb0737dbd2a6"
  180. });
  181. ServiceConfiguration.configurations.remove({
  182. service: "github"
  183. });
  184. ServiceConfiguration.configurations.insert({
  185. service: "github",
  186. clientId: "dcecd720f47c0e4001f7",
  187. secret: "375939d001ef1a0ca67c11dbf8fb9aeaa551e01b"
  188. });
  189. songTimer();
  190. /*Meteor.methods({
  191. getDuration: function() {
  192. return "100 minutes";
  193. },
  194. getStart: function() {
  195. return startedAt;
  196. },
  197. getSong: function() {
  198. return songs[currentSong];
  199. }
  200. });*/
  201. Meteor.publish("history", function() {
  202. return History.find({type: "edm"})
  203. });
  204. }
  205. Router.route("/", {
  206. template: "Home"
  207. });
  208. Router.route("/:type", {
  209. template: "Room"
  210. });