Advertisement
sparkychild

Untitled

Sep 20th, 2020
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. class AmityPoll {
  2. constructor(schedule, options) {
  3. this.handler = schedule;
  4.  
  5. this.options = {};
  6. for (const opt of options) {
  7. this.options[opt] = 0;
  8. }
  9.  
  10. this.voted_ips = [];
  11. this.poll_timeout = setTimeout(() => this.end(), POLL_DURATION * 60000)
  12.  
  13. this.initiate();
  14. }
  15.  
  16. vote(choice, self) {
  17. const ip = self.connection.ip;
  18. if (this.voted_ips.includes(ip)) return this.alreadyvoted(self);
  19.  
  20. if (!(choice in this.options)) return;
  21.  
  22. this.voted_ips.push(ip);
  23. this.options[choice]++;
  24.  
  25. this.thanksforvoting(self);
  26. }
  27.  
  28. alreadyvoted(self) {
  29. self.user.sendTo(ROOM_ID, `|uhtmlchange|amity-poll|<div class=infobox>You have already voted!</div>`);
  30. }
  31.  
  32. thanksforvoting(self) {
  33. self.user.sendTo(ROOM_ID, `|uhtmlchange|amity-poll|<div class=infobox>Thanks for voting!</div>`);
  34. }
  35.  
  36. initiate() {
  37. this.handler.getRoom().add(`|notify|[${ROOM_NAME}] New Poll|A new poll is starting! Vote for the next tournament!`).update();
  38.  
  39. this.post();
  40. }
  41.  
  42. end() {
  43. let max = 0;
  44. for (const opt in this.options) {
  45. if (this.options[opt] > max) max = this.options[opt];
  46. }
  47.  
  48. const selected = Object.keys(this.options).filter(opt => this.options[opt] === max);
  49. this.handler.pollCallback(selected);
  50.  
  51. this.handler.getRoom().add().update();
  52.  
  53. this.destroy();
  54. }
  55.  
  56. destroy() {
  57. clearTimeout(this.poll_timeout);
  58. }
  59.  
  60. post() {
  61. let html = this.html();
  62. this.handler.getRoom().add(`|uhtml|amity-poll|<div class="infobox">${html}</div>`);
  63. }
  64.  
  65. html() {
  66. const title = `<h4>Automated Poll: Next Tournament?</h4>`;
  67. const options = Object.keys(this.options).map(opt => `<button name=send value="/amity vote ${opt}" style="border: 1px solid black; padding: 2px; border-radius: 5px;">${opt}</button>`).join(' ');
  68. return `<div class=infobox>${title}${options}</div>`;
  69. }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement