stations.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. // console.log(this.currentSong.duration);
  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. updateDisplayName(newDisplayName) {
  71. // TODO: Update db
  72. this.displayName = newDisplayName;
  73. this.nsp.emit("updateDisplayName", newDisplayName);
  74. }
  75. updateDescription(newDescription) {
  76. // TODO: Update db
  77. this.description = newDescription;
  78. this.nsp.emit("updateDescription", newDescription);
  79. }
  80. getTimePaused() {
  81. return this.timePaused + this.timer.getTimePaused();
  82. }
  83. },
  84. addStation: (station) => {
  85. stations.push(station);
  86. },
  87. getStation: id => {
  88. let result;
  89. stations.forEach(function(station) {
  90. if (station.id === id) {
  91. result = station;
  92. }
  93. });
  94. return result;
  95. },
  96. // Returns stations list when POSTing to '/stations'
  97. getStations: () => {
  98. return stations;
  99. }
  100. };