From 1b3199a4efefe20b976ced50b1e323c18937e711 Mon Sep 17 00:00:00 2001 From: calzoneman Date: Tue, 31 Dec 2013 15:12:47 -0500 Subject: [PATCH] Continue working on channel.js --- lib/channel-new.js | 282 ++++++++++++++++++++++++++++++++++++++++++++- lib/server.js | 2 +- lib/user.js | 2 +- 3 files changed, 282 insertions(+), 4 deletions(-) diff --git a/lib/channel-new.js b/lib/channel-new.js index e677b89c..8fd768fa 100644 --- a/lib/channel-new.js +++ b/lib/channel-new.js @@ -1,5 +1,13 @@ var util = require("./utilities"); +var db = require("./database"); var Playlist = require("./playlist"); +var Filter = require("./filter").Filter; +var Logger = require("./logger"); +var AsyncQueue = require("./asyncqueue"); + +var EventEmitter = require("events").EventEmitter; +var fs = require("fs"); +var path = require("path"); var DEFAULT_FILTERS = [ new Filter("monospace", "`(.+?)`", "g", "$1"), @@ -108,7 +116,7 @@ function Channel(name) { }); }; -Channel.prototype = EventEmitter; +Channel.prototype = EventEmitter.prototype; Channel.prototype.tryLoadState = function () { var self = this; @@ -539,7 +547,7 @@ Channel.prototype.sendModMessage = function (msg, minrank) { var notice = { username: "[server]", - msg: msg + msg: msg, meta: { addClass: "server-whisper" , addClassToNameAndTimestamp: true @@ -853,6 +861,274 @@ Channel.prototype.sendPlaylist = function (users) { }); }; +/** + * Updates the playlist count/time + */ +Channel.prototype.updatePlaylistMeta = function () { + var total = 0; + var iter = this.playlist.items.first; + while (iter !== null) { + if (iter.media !== null) { + total += iter.media.seconds; + } + iter = iter.next; + } + + var timestr = util.formatTime(total); + this.plmeta = { + count: this.playlist.items.length, + time: timestr + }; +}; + +/** + * Send the playlist count/time + */ +Channel.prototype.sendPlaylistMeta = function (users) { + var self = this; + users.forEach(function (u) { + u.socket.emit("setPlaylistMeta", self.plmeta); + }); +}; + +/** + * Sends a changeMedia packet + */ +Channel.prototype.sendMediaUpdate = function (users) { + var update = this.playlist.getFullUpdate(); + if (update) { + users.forEach(function (u) { + u.socket.emit("changeMedia", update); + }); + } +}; + +/** + * Send the userlist + */ +Channel.prototype.sendUserlist = function (toUsers) { + var users = []; + var detailedUsers = []; + + for (var i = 0; i < this.users.length; i++) { + var u = this.users[i]; + if (u.name === "") { + continue; + } + + users.push({ + name: u.name, + rank: u.rank, + profile: u.profile + }); + + detailedUsers.push({ + name: u.name, + rank: u.rank, + meta: u.meta, + profile: u.profile + }); + } + + toUsers.forEach(function (u) { + if (u.rank >= 2) { + u.socket.emit("userlist", detailedUsers); + } else { + u.socket.emit("userlist", users); + } + + if (this.leader !== null) { + u.socket.emit("setLeader", this.leader.name); + } + }); +}; + +/** + * Send the user count + */ +Channel.prototype.sendUsercount = function (users) { + var self = this; + users.forEach(function (u) { + u.socket.emit("usercount", self.users.length); + }); +}; + +/** + * Send the chat buffer + */ +Channel.prototype.sendRecentChat = function (users) { + var self = this; + users.forEach(function (u) { + for (var i = 0; i < self.chatbuffer.length; i++) { + u.socket.emit("chatMsg", self.chatbuffer[i]); + } + }); +}; + +/** + * Send a user join notification + */ +Channel.prototype.sendUserJoin = function (users, user) { + var self = this; + db.getAliases(user.ip, function (err, aliases) { + if (self.dead) { + return; + } + + if (err) { + aliases = [user.name]; + } + + user.meta.aliases = aliases; + + if (user.name.toLowerCase() in self.namebans) { + user.kick("You're banned!"); + return; + } + + if (self.mutedUsers.contains("[shadow]"+user.name.toLowerCase())) { + user.meta.muted = true; + user.meta.shadowmuted = true; + user.meta.icon = "icon-volume-off"; + } else if (self.mutedUsers.contains(user.name.toLowerCase())) { + user.meta.muted = true; + user.meta.shadowmuted = false; + user.meta.icon = "icon-volume-off"; + } + + var base = { + name: user.name, + rank: user.rank, + profile: user.profile, + meta: { + afk: user.meta.afk + } + }; + + if (user.meta.icon && !user.meta.shadowmuted) { + base.meta.icon = user.meta.icon; + } + + var mod = { + name: user.name, + rank: user.rank, + profile: user.profile, + meta: { + afk: user.meta.afk, + icon: user.meta.icon + } + }; + + users.forEach(function (u) { + if (u.rank >= 2) { + u.socket.emit("addUser", mod); + } else { + u.socket.emit("addUser", base); + } + }); + + self.sendModMessage(user.name + " joined (aliases: " + aliases.join(",") + ")", 2); + }); +}; + +/** + * Sends a poll notification + */ +Channel.prototype.sendPollUpdate = function (users) { + var self = this; + var unhidden = self.poll.packUpdate(true); + var hidden = self.poll.packUpdate(false); + + users.forEach(function (u) { + if (self.hasPermission(u, "viewhiddenpoll")) { + u.socket.emit("newPoll", unhidden); + } else { + u.socket.emit("newPoll", hidden); + } + }); +}; + +/** + * Sends a "poll closed" notification + */ +Channel.prototype.sendPollClose = function (users) { + users.forEach(function (u) { + u.socket.emit("closePoll"); + }); +}; + +/** + * Broadcasts the channel options + */ +Channel.prototype.sendOpts = function (users) { + var self = this; + users.forEach(function (u) { + u.socket.emit("channelOpts", self.opts); + }); +}; + +/** + * Calculates the number of eligible users to voteskip + */ +Channel.prototype.calcVoteskipMax = function () { + var self = this; + return this.users.map(function (u) { + if (!self.hasPermission(u, "voteskip")) { + return 0; + } + + return u.meta.afk ? 0 : 1; + }).reduce(function (a, b) { + return a + b; + }, 0); +}; + +/** + * Creates a voteskip update packet + */ +Channel.prototype.getVoteskipPacket = function () { + var have = this.voteskip ? this.voteskip.counts[0] : 0; + var max = this.calcVoteskipMax(); + var need = this.voteskip ? Math.ceil(max * this.opts.voteskip_ratio) : 0; + return { + count: have, + need: need + }; +}; + +/** + * Sends a voteskip update packet + */ +Channel.prototype.sendVoteskipUpdate = function (users) { + var self = this; + var update = self.getVoteskipPacket(); + users.forEach(function (u) { + if (u.rank >= 1.5) { + u.socket.emit("voteskip", update); + } + }); +}; + +/** + * Sends the MOTD + */ +Channel.prototype.sendMotd = function (users) { + var self = this; + users.forEach(function (u) { + u.socket.emit("setMotd", self.motd); + }); +}; + +/** + * Sends the drink count + */ +Channel.prototype.sendDrinks = function (users) { + var self = this; + users.forEach(function (u) { + u.socket.emit("drinkCount", self.drinks); + }); +}; + /** * Searches channel library */ @@ -963,3 +1239,5 @@ Channel.prototype.readLog = function (filterIp, callback) { }); }); }; + +module.exports = Channel; diff --git a/lib/server.js b/lib/server.js index 95a0be93..5b2767e1 100644 --- a/lib/server.js +++ b/lib/server.js @@ -40,7 +40,7 @@ var https = require("https"); var express = require("express"); var Config = require("./config"); var Logger = require("./logger"); -var Channel = require("./channel"); +var Channel = require("./channel-new"); var User = require("./user"); var $util = require("./utilities"); var ActionLog = require("./actionlog"); diff --git a/lib/user.js b/lib/user.js index 2a607fdf..d8b89422 100644 --- a/lib/user.js +++ b/lib/user.js @@ -143,7 +143,7 @@ User.prototype.initCallbacks = function () { self.rank = rank; }); } - self.pendingChannel.userJoin(self); + self.pendingChannel.join(self); }); self.socket.on("channelPassword", function (pw) {