stations.js 3.4 KB

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