stations.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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.playlist = data.playlist;
  26. this.currentSongIndex = data.currentSongIndex;
  27. this.paused = data.paused;
  28. this.displayName = data.displayName;
  29. this.description = data.description;
  30. this.timePaused = 0;
  31. this.timer = undefined;
  32. this.currentSong = this.playlist[this.currentSongIndex];
  33. this.nextSong();
  34. }
  35. nextSong() {
  36. if (this.playlist.length > 0) {
  37. if (this.timer !== undefined) this.timer.pause();
  38. if (this.currentSongIndex + 1 < this.playlist.length) {
  39. this.currentSongIndex++;
  40. }
  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. // isPaused() {
  70. // return this.paused;
  71. // }
  72. // getCurrentSong() {
  73. // return this.currentSong;
  74. // }
  75. updateDisplayName(newDisplayName) {
  76. // TODO: Update db
  77. this.displayName = newDisplayName;
  78. this.nsp.emit("updateDisplayName", newDisplayName);
  79. }
  80. updateDescription(newDescription) {
  81. // TODO: Update db
  82. this.description = newDescription;
  83. this.nsp.emit("updateDescription", newDescription);
  84. }
  85. getTimePaused() {
  86. return this.timePaused + this.timer.getTimePaused();
  87. }
  88. },
  89. addStation: (station) => {
  90. stations.push(station);
  91. },
  92. // getStation: id => {
  93. // let result;
  94. // stations.forEach(function(station) {
  95. // if (station.getId() === id) {
  96. // result = station;
  97. // }
  98. // });
  99. // return result;
  100. // },
  101. // Returns stations list when POSTing to '/stations'
  102. getStations: () => {
  103. return stations;
  104. }
  105. };