stations.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // custom modules
  2. const global = require('./global');
  3. var io = global.io;
  4. function Station (id, data) {
  5. var self = this;
  6. //TODO Add startedAt and timePaused
  7. var playlist = data.playlist;
  8. var currentSong = playlist[0];
  9. var currentSongIndex = data.currentSongIndex;
  10. var paused = data.paused;
  11. var locked = data.locked;
  12. var skipVotes = data.skipVotes;
  13. var users = data.users;
  14. var displayName = data.displayName;
  15. var description = data.description;
  16. var timer;
  17. var nsp = io.of('/' + id);
  18. nsp.on('connection', function(socket){
  19. console.log('someone connected');
  20. });
  21. this.skipSong = function() {
  22. if (playlist.length > 0) {
  23. if (timer !== undefined) {
  24. timer.pause();
  25. }
  26. if (currentSongIndex+1 < playlist.length) {
  27. currentSongIndex++;
  28. } else {
  29. currentSongIndex = 0;
  30. }
  31. skipVotes = 0;
  32. currentSong = playlist[currentSongIndex];
  33. timer = new global.Timer(function() {
  34. console.log("Skip!");
  35. self.skipSong();
  36. }, currentSong.duration, paused);
  37. nsp.emit("skippedSong", currentSong);
  38. }
  39. };
  40. this.toggleVoteSkip = function(userId) {
  41. if (skipVotes.indexOf(userId) === -1) {
  42. skipVotes.push(userId);
  43. } else {
  44. skipVotes = skipVotes.splice(skipVotes.indexOf(userId), 1);
  45. }
  46. //TODO Calculate if enough people voted to skip
  47. nsp.emit("voteSkip", skipVotes);
  48. };
  49. this.retrievePlaylist = function() {
  50. //TODO Use Rethink to get the Playlist for this station
  51. };
  52. this.pause = function() {
  53. if (!paused) {
  54. paused = true;
  55. timer.pause();
  56. nsp.emit("pause");
  57. }
  58. };
  59. this.unpause = function() {
  60. if (paused) {
  61. paused = false;
  62. timer.resume();
  63. nsp.emit("unpause");
  64. }
  65. };
  66. this.isPaused = function() {
  67. return paused;
  68. };
  69. this.getCurrentSong = function() {
  70. return currentSong;
  71. };
  72. this.lock = function() {
  73. if (!locked) {
  74. locked = true;
  75. nsp.emit("lock");
  76. }
  77. };
  78. this.unlock = function() {
  79. if (locked) {
  80. locked = false;
  81. nsp.emit("unlocked");
  82. }
  83. };
  84. this.isLocked = function() {
  85. return locked;
  86. };
  87. this.updateDisplayName = function(newDisplayName) {
  88. //TODO Update RethinkDB
  89. displayName = newDisplayName;
  90. nsp.emit("updateDisplayName", newDisplayName);
  91. };
  92. this.updateDescription = function(newDescription) {
  93. //TODO Update RethinkDB
  94. description = newDescription;
  95. nsp.emit("updateDescription", newDescription);
  96. };
  97. this.getId = function() {
  98. return id;
  99. };
  100. this.getDisplayName = function() {
  101. return displayName;
  102. };
  103. this.getDescription = function() {
  104. return description;
  105. };
  106. this.addUser = function(user) {
  107. users.add(user);
  108. nsp.emit("updateUsers", users);
  109. };
  110. this.removeUser = function(user) {
  111. users.splice(users.indexOf(user), 1);
  112. nsp.emit("updateUsers", users);
  113. };
  114. this.getUsers = function() {
  115. return users;
  116. };
  117. this.skipSong();
  118. }
  119. module.exports = {
  120. stations: [],
  121. initStation: function (id, data) {
  122. if (!this.getStation(id)) {
  123. var station = new Station(id, data);
  124. this.stations.push(station);
  125. return station;
  126. }
  127. else {
  128. return false;
  129. }
  130. },
  131. getStation: function (id) {
  132. var s = null;
  133. this.stations.forEach(function (station) {
  134. if (station.id == id) s = station;
  135. });
  136. return s;
  137. },
  138. getStations: function () {
  139. return this.stations;
  140. },
  141. // creates a brand new station
  142. createStation: function (data) {
  143. //TODO: add createStation functionality
  144. this.initStation(null, data);
  145. },
  146. // loads a station from the database
  147. loadStation: function (id) {
  148. //TODO: Get the data from RethinkDB
  149. this.initStation(id, {
  150. playlist: [
  151. {
  152. mid: "3498fd83",
  153. duration: 20000,
  154. title: "Test1"
  155. },
  156. {
  157. mid: "3498fd83434",
  158. duration: 10000,
  159. title: "Test2"
  160. }
  161. ],
  162. currentSongIndex: 0,
  163. paused: false,
  164. locked: false,
  165. skipVotes: [],
  166. users: [],
  167. displayName: "",
  168. description: ""
  169. });
  170. }
  171. };