stations.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. 'use strict';
  2. const global = require('./global');
  3. const Promise = require('bluebird');
  4. const io = global.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.currentSong.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 global.db.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. local.currentSong = local.playlist[this.currentSongIndex];
  47. local.nextSong();
  48. });
  49. }
  50. nextSong() {
  51. if (this.playlist.length > 0) {
  52. if (this.timer !== undefined) this.timer.pause();
  53. if (this.currentSongIndex + 1 <= this.playlist.length - 1) {
  54. this.currentSongIndex++;
  55. } else {
  56. this.currentSongIndex = 0;
  57. }
  58. this.currentSong = this.playlist[this.currentSongIndex];
  59. let self = this;
  60. this.timer = new global.Timer(() => {
  61. self.nextSong();
  62. }, this.currentSong.duration, this.paused);
  63. this.timePaused = 0;
  64. this.currentSong.startedAt = Date.now();
  65. this.nsp.emit("nextSong", this.currentSong, this.currentSong.startedAt);
  66. }
  67. }
  68. pause() {
  69. if (!this.paused) {
  70. this.paused = true;
  71. this.timer.pause();
  72. this.nsp.emit("pause");
  73. }
  74. }
  75. unPause() {
  76. if (this.paused) {
  77. this.paused = false;
  78. this.timer.resume();
  79. this.timePaused += this.timer.getTimePaused();
  80. this.nsp.emit("unpause", this.timePaused);
  81. }
  82. }
  83. getTimePaused() {
  84. return this.timePaused + this.timer.getTimePaused();
  85. }
  86. },
  87. addStation: (station) => {
  88. stations.push(station);
  89. },
  90. getStation: id => {
  91. let result;
  92. stations.forEach(function(station) {
  93. if (station.id === id) {
  94. result = station;
  95. }
  96. });
  97. return result;
  98. },
  99. // Returns stations list when POSTing to '/stations'
  100. getStations: () => {
  101. return stations;
  102. }
  103. };