stations.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. this.nsp.on('connection', socket => {
  10. console.log('someone connected');
  11. });
  12. this.id = id;
  13. this.playlist = data.playlist;
  14. this.currentSongIndex = data.currentSongIndex;
  15. this.currentSong = this.playlist[this.currentSongIndex];
  16. this.paused = data.paused;
  17. this.locked = data.locked;
  18. this.skipVotes = 0;
  19. this.users = [];
  20. this.displayName = data.displayName;
  21. this.description = data.description;
  22. this.timer = undefined;
  23. this.skipSong();
  24. }
  25. skipSong() {
  26. if (this.playlist.length > 0) {
  27. if (this.timer !== undefined) this.timer.pause();
  28. if (this.currentSongIndex + 1 < this.playlist.length) this.currentSongIndex++;
  29. else this.currentSongIndex = 0;
  30. this.skipVotes = 0;
  31. this.currentSong = this.playlist[this.currentSongIndex];
  32. var self = this;
  33. this.timer = new global.Timer(() => {
  34. self.skipSong();
  35. }, this.currentSong.duration, this.paused);
  36. this.currentSong.startedAt = Date.now();
  37. this.nsp.emit("skippedSong", this.currentSong);
  38. }
  39. }
  40. toggleVoteSkip(userId) {
  41. if (this.skipVotes.indexOf(userId) === -1) this.skipVotes.push(userId);
  42. else this.skipVotes = this.skipVotes.splice(this.skipVotes.indexOf(userId), 1);
  43. // TODO: Calculate if enough people voted to skip
  44. this.nsp.emit("voteSkip", this.skipVotes);
  45. }
  46. retrievePlaylist() {
  47. // TODO: get the Playlist for this station using db
  48. }
  49. pause() {
  50. if (!this.paused) {
  51. this.paused = true;
  52. this.timer.pause();
  53. this.snp.emit("pause");
  54. }
  55. }
  56. unPause() {
  57. if (this.paused) {
  58. this.paused = false;
  59. this.timer.resume();
  60. this.snp.emit("unpause");
  61. }
  62. }
  63. isPaused() {
  64. return this.paused;
  65. }
  66. getCurrentSong() {
  67. return this.currentSong;
  68. }
  69. lock() {
  70. if (!this.locked) {
  71. this.locked = true;
  72. this.snp.emit("lock");
  73. }
  74. }
  75. unlock() {
  76. if (this.locked) {
  77. this.locked = false;
  78. this.snp.emit("unlocked");
  79. }
  80. }
  81. isLocked() {
  82. return this.locked;
  83. }
  84. updateDisplayName(newDisplayName) {
  85. // TODO: Update db
  86. this.displayName = newDisplayName;
  87. this.snp.emit("updateDisplayName", newDisplayName);
  88. }
  89. updateDescription(newDescription) {
  90. // TODO: Update db
  91. this.description = newDescription;
  92. this.snp.emit("updateDescription", newDescription);
  93. }
  94. getId() {
  95. return this.id;
  96. }
  97. getDisplayName() {
  98. return this.displayName;
  99. }
  100. getDescription() {
  101. return this.description;
  102. }
  103. addUser(user) {
  104. this.users.add(user);
  105. this.snp.emit("updateUsers", this.users);
  106. }
  107. removeUser(user) {
  108. this.users.splice(this.users.indexOf(user), 1);
  109. this.snp.emit("updateUsers", this.users);
  110. }
  111. getUsers() {
  112. return this.users;
  113. }
  114. },
  115. addStation: station => {
  116. stations.push(station);
  117. },
  118. getStation: id => {
  119. let result;
  120. stations.forEach(function(station) {
  121. if (station.getId() === id) {
  122. result = station;
  123. }
  124. });
  125. return result;
  126. },
  127. getStations: () => {
  128. return stations;
  129. }
  130. };