stations.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. // socket.on("pause", () => {
  12. // local.pause();
  13. // });
  14. // socket.on("unpause", () => {
  15. // local.unPause();
  16. // });
  17. socket.emit("connected", {
  18. currentSong: this.currentSong,
  19. paused: this.paused,
  20. timePaused: this.timePaused,
  21. currentTime: Date.now()
  22. });
  23. });
  24. this.id = id;
  25. this.users = 0;
  26. this.playlist = data.playlist;
  27. this.currentSongIndex = data.currentSongIndex;
  28. this.paused = data.paused;
  29. this.displayName = data.displayName;
  30. this.description = data.description;
  31. this.timePaused = 0;
  32. this.timer = undefined;
  33. this.currentSong = this.playlist[this.currentSongIndex];
  34. this.nextSong();
  35. }
  36. nextSong() {
  37. if (this.playlist.length > 0) {
  38. if (this.timer !== undefined) this.timer.pause();
  39. if (this.currentSongIndex + 1 <= this.playlist.length - 1) {
  40. this.currentSongIndex++;
  41. } else {
  42. this.currentSongIndex = 0;
  43. }
  44. this.currentSong = this.playlist[this.currentSongIndex];
  45. let self = this;
  46. this.timer = new global.Timer(() => {
  47. self.nextSong();
  48. }, this.currentSong.duration, this.paused);
  49. this.timePaused = 0;
  50. this.currentSong.startedAt = Date.now();
  51. this.nsp.emit("nextSong", this.currentSong);
  52. }
  53. }
  54. pause() {
  55. if (!this.paused) {
  56. this.paused = true;
  57. this.timer.pause();
  58. this.nsp.emit("pause");
  59. }
  60. }
  61. unPause() {
  62. if (this.paused) {
  63. this.paused = false;
  64. this.timer.resume();
  65. this.timePaused += this.timer.getTimePaused();
  66. this.nsp.emit("unpause", this.timePaused);
  67. }
  68. }
  69. updateDisplayName(newDisplayName) {
  70. // TODO: Update db
  71. this.displayName = newDisplayName;
  72. this.nsp.emit("updateDisplayName", newDisplayName);
  73. }
  74. updateDescription(newDescription) {
  75. // TODO: Update db
  76. this.description = newDescription;
  77. this.nsp.emit("updateDescription", newDescription);
  78. }
  79. getTimePaused() {
  80. return this.timePaused + this.timer.getTimePaused();
  81. }
  82. },
  83. addStation: (station) => {
  84. stations.push(station);
  85. },
  86. getStation: id => {
  87. let result;
  88. stations.forEach(function(station) {
  89. if (station.id === id) {
  90. result = station;
  91. }
  92. });
  93. return result;
  94. },
  95. // Returns stations list when POSTing to '/stations'
  96. getStations: () => {
  97. return stations;
  98. }
  99. };