stations.js 3.1 KB

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