stations.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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.sockets = [];
  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. getSockets() {
  119. return this.sockets;
  120. }
  121. getTimePaused() {
  122. console.log(this.timePaused, " ", this.timer.getTimePaused());
  123. return this.timePaused + this.timer.getTimePaused();
  124. }
  125. emitToStation(...data) {
  126. this.sockets.forEach(function(socketId) {
  127. let socket = global.getSocketFromId(socketId);
  128. if (socket !== undefined) {
  129. socket.emit.apply(data);
  130. } else {
  131. // Remove socket and emit it
  132. }
  133. });
  134. }
  135. handleUserJoin(socketId) {
  136. const socket = global.getSocketFromId(socketId);
  137. if (socket !== undefined) {
  138. //TODO Check if banned from room & check if allowed to join room
  139. if (this.sockets.indexOf(socketId) === -1) {
  140. this.emitToStation("userJoin", "Person");
  141. this.sockets.push(socketId);
  142. return {
  143. displayName: this.getDisplayName(),
  144. users: this.getSockets().length,
  145. currentSong: this.getCurrentSong(),
  146. timePaused: this.getTimePaused(),
  147. paused: this.isPaused(),
  148. currentTime: Date.now()
  149. };
  150. } else {
  151. return {err: "Already in that station."};
  152. }
  153. } else {
  154. return {err: "Invalid socket id."};
  155. }
  156. }
  157. },
  158. addStation: station => {
  159. stations.push(station);
  160. },
  161. getStation: id => {
  162. let result;
  163. stations.forEach(function(station) {
  164. if (station.getId() === id) {
  165. result = station;
  166. }
  167. });
  168. return result;
  169. },
  170. getStations: () => {
  171. return stations;
  172. }
  173. };