stations.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. 'use strict';
  2. const globals = require('./globals');
  3. const Promise = require('bluebird');
  4. const io = globals.io;
  5. let stations = [];
  6. module.exports = {
  7. Station: class Station {
  8. constructor(id, data) {
  9. this.nsp = io.of(id);
  10. let local = this;
  11. this.nsp.on('connection', (socket, cb) => {
  12. // socket.on("pause", () => {
  13. // local.pause();
  14. // });
  15. // socket.on("unpause", () => {
  16. // local.unPause();
  17. // });
  18. socket.emit("connected", {
  19. currentSong: this.currentSong,
  20. startedAt: this.startedAt,
  21. paused: this.paused,
  22. timePaused: this.timePaused,
  23. currentTime: Date.now()
  24. });
  25. });
  26. this.id = id;
  27. this.users = 0;
  28. function generatePlaylist(arr) {
  29. local.playlist = [];
  30. return arr.reduce((promise, id) => {
  31. return promise.then(() => {
  32. return globals.db.models.song.find({ id }, (err, song) => {
  33. if (err) throw err;
  34. local.playlist.push(song[0]);
  35. });
  36. });
  37. }, Promise.resolve());
  38. }
  39. generatePlaylist(data.playlist).then(() => {
  40. local.currentSongIndex = data.currentSongIndex;
  41. local.paused = data.paused;
  42. local.displayName = data.displayName;
  43. local.description = data.description;
  44. local.timePaused = 0;
  45. local.timer = undefined;
  46. if (local.playlist[this.currentSongIndex]) {
  47. local.currentSong = local.playlist[this.currentSongIndex];
  48. local.nextSong();
  49. }
  50. });
  51. }
  52. nextSong() {
  53. if (this.playlist.length > 0) {
  54. if (this.timer !== undefined) this.timer.pause();
  55. if (this.currentSongIndex + 1 <= this.playlist.length - 1) {
  56. this.currentSongIndex++;
  57. } else {
  58. this.currentSongIndex = 0;
  59. }
  60. this.currentSong = this.playlist[this.currentSongIndex];
  61. let self = this;
  62. this.timer = new globals.utils.Timer(() => {
  63. self.nextSong();
  64. }, this.currentSong.duration, this.paused);
  65. this.timePaused = 0;
  66. this.startedAt = Date.now();
  67. this.nsp.emit("nextSong", this.currentSong, this.startedAt);
  68. }
  69. }
  70. pause() {
  71. if (!this.paused) {
  72. this.paused = true;
  73. this.timer.pause();
  74. this.nsp.emit("pause");
  75. }
  76. }
  77. unPause() {
  78. if (this.paused) {
  79. this.paused = false;
  80. this.timer.resume();
  81. this.timePaused += this.timer.getTimePaused();
  82. this.nsp.emit("unpause", this.timePaused);
  83. }
  84. }
  85. getTimePaused() {
  86. return this.timePaused + this.timer.getTimePaused();
  87. }
  88. },
  89. addStation: station => stations.push(station),
  90. getStation: id => stations.find(station => station.id === id),
  91. getStations: _ => stations
  92. };