Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class AmityPoll {
- constructor(schedule, options) {
- this.handler = schedule;
- this.options = {};
- for (const opt of options) {
- this.options[opt] = 0;
- }
- this.voted_ips = [];
- this.poll_timeout = setTimeout(() => this.end(), POLL_DURATION * 60000)
- this.initiate();
- }
- vote(choice, self) {
- const ip = self.connection.ip;
- if (this.voted_ips.includes(ip)) return this.alreadyvoted(self);
- if (!(choice in this.options)) return;
- this.voted_ips.push(ip);
- this.options[choice]++;
- this.thanksforvoting(self);
- }
- alreadyvoted(self) {
- self.user.sendTo(ROOM_ID, `|uhtmlchange|amity-poll|<div class=infobox>You have already voted!</div>`);
- }
- thanksforvoting(self) {
- self.user.sendTo(ROOM_ID, `|uhtmlchange|amity-poll|<div class=infobox>Thanks for voting!</div>`);
- }
- initiate() {
- this.handler.getRoom().add(`|notify|[${ROOM_NAME}] New Poll|A new poll is starting! Vote for the next tournament!`).update();
- this.post();
- }
- end() {
- let max = 0;
- for (const opt in this.options) {
- if (this.options[opt] > max) max = this.options[opt];
- }
- const selected = Object.keys(this.options).filter(opt => this.options[opt] === max);
- this.handler.pollCallback(selected);
- this.handler.getRoom().add().update();
- this.destroy();
- }
- destroy() {
- clearTimeout(this.poll_timeout);
- }
- post() {
- let html = this.html();
- this.handler.getRoom().add(`|uhtml|amity-poll|<div class="infobox">${html}</div>`);
- }
- html() {
- const title = `<h4>Automated Poll: Next Tournament?</h4>`;
- 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(' ');
- return `<div class=infobox>${title}${options}</div>`;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement