stations.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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) {
  40. this.currentSongIndex++;
  41. }
  42. else {
  43. this.currentSongIndex = 0;
  44. }
  45. this.currentSong = this.playlist[this.currentSongIndex];
  46. let self = this;
  47. this.timer = new global.Timer(() => {
  48. self.nextSong();
  49. }, this.currentSong.duration, this.paused);
  50. this.timePaused = 0;
  51. this.currentSong.startedAt = Date.now();
  52. this.nsp.emit("nextSong", this.currentSong);
  53. }
  54. }
  55. pause() {
  56. if (!this.paused) {
  57. this.paused = true;
  58. this.timer.pause();
  59. this.nsp.emit("pause");
  60. }
  61. }
  62. unPause() {
  63. if (this.paused) {
  64. this.paused = false;
  65. this.timer.resume();
  66. this.timePaused += this.timer.getTimePaused();
  67. this.nsp.emit("unpause", this.timePaused);
  68. }
  69. }
  70. // isPaused() {
  71. // return this.paused;
  72. // }
  73. // getCurrentSong() {
  74. // return this.currentSong;
  75. // }
  76. updateDisplayName(newDisplayName) {
  77. // TODO: Update db
  78. this.displayName = newDisplayName;
  79. this.nsp.emit("updateDisplayName", newDisplayName);
  80. }
  81. updateDescription(newDescription) {
  82. // TODO: Update db
  83. this.description = newDescription;
  84. this.nsp.emit("updateDescription", newDescription);
  85. }
  86. getTimePaused() {
  87. return this.timePaused + this.timer.getTimePaused();
  88. }
  89. },
  90. addStation: (station) => {
  91. stations.push(station);
  92. },
  93. getStation: id => {
  94. let result;
  95. stations.forEach(function(station) {
  96. if (station.id === id) {
  97. result = station;
  98. }
  99. });
  100. return result;
  101. },
  102. // Returns stations list when POSTing to '/stations'
  103. getStations: () => {
  104. return stations;
  105. }
  106. };