stations.js 3.4 KB

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