stations.js 3.9 KB

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