From ccba766dbed77141e31e47953bca76d22a51fe68 Mon Sep 17 00:00:00 2001 From: Mattias Erming Date: Thu, 6 Mar 2014 14:36:56 -0800 Subject: [PATCH] Better model constructors --- app.js | 10 +++++--- lib/models.js | 65 +++++++++++++++++++++++++++++------------------- lib/server.js | 69 +++++++++++++++++++++++++++++++-------------------- 3 files changed, 88 insertions(+), 56 deletions(-) diff --git a/app.js b/app.js index d396b16..207b1d4 100644 --- a/app.js +++ b/app.js @@ -1,11 +1,13 @@ -var argv = require("commander") +var commander = require("commander") +var listen = require("./lib/server.js").listen; + +var argv = commander .option("-p, --port ", "port to use", parseInt) .parse(process.argv); -PORT = 80; // Default port. +PORT = 80; // Default port if (argv.port) { PORT = argv.port; } -// Run the server. -(require("./lib/server.js")).listen(PORT); +listen(PORT); diff --git a/lib/models.js b/lib/models.js index 8a08dca..06f8d88 100644 --- a/lib/models.js +++ b/lib/models.js @@ -3,40 +3,55 @@ var _ = require("lodash"); var models = exports; var id = 0; -models.Network = function() { - this.id = id++; - this.address = ""; - this.nick = ""; - this.channels = []; +models.Network = function(attr) { + attr = attr || {}; + _.extend(this, _.defaults(attr, { + id: id++, + address: "", + nick: "", + channels: [] + })); }; models.Network.prototype.toJSON = function() { - return _.omit(this, "irc"); + return _.omit(this, "client"); }; -models.Channel = function() { - this.id = id++; - this.name = ""; - this.type = "channel"; - this.topic = ""; - this.users = []; - this.messages = []; +models.Channel = function(attr) { + attr = attr || {}; + _.extend(this, _.defaults(attr, { + id: id++, + name: "", + type: "channel", + topic: "", + users: [], + messages: [] + })); }; -models.User = function() { - this.id = id++; - this.name = ""; +models.User = function(attr) { + attr = attr || {}; + _.extend(this, _.defaults(attr, { + id: id++, + name: "" + })); }; -models.Message = function() { - this.text = ""; - this.time = ""; - this.user = ""; +models.Message = function(attr) { + attr = attr || {}; + _.extend(this, _.defaults(attr, { + text: "", + time: "", + user: "" + })); }; -models.Event = function() { - this.action = ""; - this.data = ""; - this.target = ""; - this.type = ""; +models.Event = function(attr) { + attr = attr || {}; + _.extend(this, _.defaults(attr, { + action: "", + data: "", + target: "", + type: "" + })); }; diff --git a/lib/server.js b/lib/server.js index 81a169b..a660abd 100644 --- a/lib/server.js +++ b/lib/server.js @@ -35,7 +35,7 @@ function refresh() { if (typeof sockets === "undefined") { return; } - sockets.emit("event", _.assign(new models.Event, { + sockets.emit("event", new models.Event({ action: "refresh", data: networks })); @@ -50,22 +50,31 @@ function handleUserInput(input) { } var args = text.substr(1).split(" "); - switch (args[0]) { - case "connect": - if (typeof args[1] !== "undefined") { + var cmd = args[0].toUpperCase(); + + switch (cmd) { + + case "SERVER": + case "CONNECT": + if (args[1]) { addNetwork(args[1], true); } break; - case "join": - if (typeof args[1] === "undefined") { - return; + case "JOIN": + if (args[1]) { + target.network.channels.push( + new models.Channel({ + name: args[1] + }) + ); + refresh(); } - target.network.channels.push( - _.assign(new models.Channel, { - name: args[1] - }) - ); + break; + + case "PART": + target.network.channels = + _.without(target.network.channels, target.channel); refresh(); break; @@ -75,28 +84,34 @@ function handleUserInput(input) { "Command '/" + args[0] + "' does not exist." ); break; + } } function addNetwork(addr, bool) { bool = bool || false; - var chan = _.assign(new models.Channel, {name: addr, type: "network"}); - var network = _.assign( - new models.Network, {channels: [chan]} - ); - - if (bool) { - network.irc = new irc.Client(addr, "default_user", { - channels: ["#default_channel"] - }); - network.irc.addListener("raw", function() { - handleEvent.apply(this, [network].concat(arguments)); - }); - } + var chan = new models.Channel({ + name: addr, + type: "network" + }); + var network = new models.Network({ + channels: [chan] + }); networks.push(network); refresh(); + + if (addr == "Lobby") { + return; + } + + network.client = new irc.Client(addr, "default_user"); + network.client.addListener("raw", function() { + handleEvent( + network, arguments + ); + }); } function handleEvent(network) { @@ -111,9 +126,9 @@ function handleEvent(network) { } function addMessage(target, text) { - var message = _.assign(new models.Message, {text: text}); + var message = _.extend(new models.Message, {text: text}); target.channel.messages.push(message); - sockets.emit("event", _.assign(new models.Event, { + sockets.emit("event", new models.Event({ action: "add", type: "message", target: target.channel.id,