Better model constructors

This commit is contained in:
Mattias Erming 2014-03-06 14:36:56 -08:00
parent 3acb7402be
commit ccba766dbe
3 changed files with 88 additions and 56 deletions

10
app.js
View File

@ -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 <n>", "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);

View File

@ -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: ""
}));
};

View File

@ -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,