stations.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. 'use strict';
  2. const global = require('./global');
  3. let io = global.io;
  4. module.exports = class Station {
  5. constructor(id, data) {
  6. this.nsp = io.of('/' + id);
  7. this.nsp.on('connection', socket => {
  8. console.info('someone connected');
  9. });
  10. this.id = id;
  11. this.data = data;
  12. this.playlist = data.playlist;
  13. this.currentSong = this.playlist[0];
  14. this.currentSongIndex = data.currentSongIndex;
  15. this.paused = data.paused;
  16. this.locked = data.locked;
  17. this.skipVotes = data.skipVotes;
  18. this.users = data.users;
  19. this.displayName = data.displayName;
  20. this.description = data.description;
  21. this.timer = undefined;
  22. }
  23. skipSong() {
  24. if (this.playlist.length > 0) {
  25. if (this.timer !== undefined) this.timer.pause();
  26. if (this.currentSongIndex+1 < this.playlist.length) this.currentSongIndex++;
  27. else this.currentSongIndex = 0;
  28. this.skipVotes = 0;
  29. this.currentSong = this.playlist[this.currentSongIndex];
  30. this.timer = new global.Timer(() => {
  31. console.log("Skip!");
  32. self.skipSong();
  33. }, this.currentSong.duration, this.paused);
  34. nsp.emit("skippedSong", this.currentSong);
  35. }
  36. }
  37. toggleVoteSkip(userId) {
  38. if (this.skipVotes.indexOf(userId) === -1) this.skipVotes.push(userId);
  39. else this.skipVotes = this.skipVotes.splice(this.skipVotes.indexOf(userId), 1);
  40. // TODO: Calculate if enough people voted to skip
  41. nsp.emit("voteSkip", this.skipVotes);
  42. }
  43. retrievePlaylist() {
  44. // TODO: get the Playlist for this station using db
  45. }
  46. pause() {
  47. if (!this.paused) {
  48. this.paused = true;
  49. this.timer.pause();
  50. nsp.emit("pause");
  51. }
  52. }
  53. unPause() {
  54. if (this.paused) {
  55. this.paused = false;
  56. this.timer.resume();
  57. nsp.emit("unpause");
  58. }
  59. }
  60. isPaused() {
  61. return this.paused;
  62. }
  63. getCurrentSong() {
  64. return this.currentSong;
  65. }
  66. lock() {
  67. if (!this.locked) {
  68. this.locked = true;
  69. nsp.emit("lock");
  70. }
  71. }
  72. unlock() {
  73. if (this.locked) {
  74. this.locked = false;
  75. nsp.emit("unlocked");
  76. }
  77. }
  78. isLocked() {
  79. return this.locked;
  80. }
  81. updateDisplayName(newDisplayName) {
  82. // TODO: Update db
  83. this.displayName = newDisplayName;
  84. nsp.emit("updateDisplayName", newDisplayName);
  85. }
  86. updateDescription(newDescription) {
  87. // TODO: Update db
  88. this.description = newDescription;
  89. nsp.emit("updateDescription", newDescription);
  90. }
  91. getId() {
  92. return this.id;
  93. }
  94. getDisplayName() {
  95. return this.displayName;
  96. }
  97. getDescription() {
  98. return this.description;
  99. }
  100. addUser(user) {
  101. this.users.add(user);
  102. nsp.emit("updateUsers", this.users);
  103. }
  104. removeUser(user) {
  105. this.users.splice(this.users.indexOf(user), 1);
  106. nsp.emit("updateUsers", this.users);
  107. }
  108. getUsers() {
  109. return this.users;
  110. }
  111. }