stations.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. 'use strict';
  2. const global = require('./global');
  3. const io = global.io;
  4. let stations = [];
  5. module.exports = {
  6. Station: class Station {
  7. constructor(id, data) {
  8. this.nsp = io.of(id);
  9. let local = this;
  10. this.nsp.on('connection', socket => {
  11. console.log('someone connected');
  12. socket.on("pause", function() {
  13. local.pause();
  14. });
  15. socket.on("unpause", function() {
  16. local.unPause();
  17. });
  18. socket.on("skipSong", function() {
  19. local.skipSong();
  20. });
  21. });
  22. this.id = id;
  23. this.playlist = data.playlist;
  24. this.currentSongIndex = data.currentSongIndex;
  25. this.currentSong = this.playlist[this.currentSongIndex];
  26. this.paused = data.paused;
  27. this.locked = data.locked;
  28. this.skipVotes = 0;
  29. this.users = [];
  30. this.displayName = data.displayName;
  31. this.description = data.description;
  32. this.timePaused = 0;
  33. this.timer = undefined;
  34. this.skipSong();
  35. }
  36. skipSong() {
  37. if (this.playlist.length > 0) {
  38. if (this.timer !== undefined) this.timer.pause();
  39. if (this.currentSongIndex + 1 < this.playlist.length) this.currentSongIndex++;
  40. else this.currentSongIndex = 0;
  41. this.skipVotes = 0;
  42. this.currentSong = this.playlist[this.currentSongIndex];
  43. var self = this;
  44. this.timer = new global.Timer(() => {
  45. self.skipSong();
  46. }, this.currentSong.duration, this.paused);
  47. this.timePaused = 0;
  48. this.currentSong.startedAt = Date.now();
  49. this.nsp.emit("skippedSong", this.currentSong);
  50. }
  51. }
  52. toggleVoteSkip(userId) {
  53. if (this.skipVotes.indexOf(userId) === -1) this.skipVotes.push(userId);
  54. else this.skipVotes = this.skipVotes.splice(this.skipVotes.indexOf(userId), 1);
  55. // TODO: Calculate if enough people voted to skip
  56. this.nsp.emit("voteSkip", this.skipVotes);
  57. }
  58. retrievePlaylist() {
  59. // TODO: get the Playlist for this station using db
  60. }
  61. pause() {
  62. console.log("PAUSE");
  63. if (!this.paused) {
  64. this.paused = true;
  65. this.timer.pause();
  66. this.nsp.emit("pause");
  67. }
  68. }
  69. unPause() {
  70. console.log("UNPAUSE");
  71. if (this.paused) {
  72. this.paused = false;
  73. this.timer.resume();
  74. this.timePaused += this.timer.getTimePaused();
  75. this.nsp.emit("unpause", this.timePaused);
  76. }
  77. }
  78. isPaused() {
  79. return this.paused;
  80. }
  81. getCurrentSong() {
  82. return this.currentSong;
  83. }
  84. lock() {
  85. if (!this.locked) {
  86. this.locked = true;
  87. this.nsp.emit("lock");
  88. }
  89. }
  90. unlock() {
  91. if (this.locked) {
  92. this.locked = false;
  93. this.nsp.emit("unlocked");
  94. }
  95. }
  96. isLocked() {
  97. return this.locked;
  98. }
  99. updateDisplayName(newDisplayName) {
  100. // TODO: Update db
  101. this.displayName = newDisplayName;
  102. this.nsp.emit("updateDisplayName", newDisplayName);
  103. }
  104. updateDescription(newDescription) {
  105. // TODO: Update db
  106. this.description = newDescription;
  107. this.nsp.emit("updateDescription", newDescription);
  108. }
  109. getId() {
  110. return this.id;
  111. }
  112. getDisplayName() {
  113. return this.displayName;
  114. }
  115. getDescription() {
  116. return this.description;
  117. }
  118. addUser(user) {
  119. this.users.add(user);
  120. this.nsp.emit("updateUsers", this.users);
  121. }
  122. removeUser(user) {
  123. this.users.splice(this.users.indexOf(user), 1);
  124. this.nsp.emit("updateUsers", this.users);
  125. }
  126. getUsers() {
  127. return this.users;
  128. }
  129. getTimePaused() {
  130. console.log(this.timePaused, " ", this.timer.getTimePaused());
  131. return this.timePaused + this.timer.getTimePaused();
  132. }
  133. },
  134. addStation: station => {
  135. stations.push(station);
  136. },
  137. getStation: id => {
  138. let result;
  139. stations.forEach(function(station) {
  140. if (station.getId() === id) {
  141. result = station;
  142. }
  143. });
  144. return result;
  145. },
  146. getStations: () => {
  147. return stations;
  148. }
  149. };