stations.js 2.9 KB

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