Better model constructors
This commit is contained in:
parent
3acb7402be
commit
ccba766dbe
10
app.js
10
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 <n>", "port to use", parseInt)
|
.option("-p, --port <n>", "port to use", parseInt)
|
||||||
.parse(process.argv);
|
.parse(process.argv);
|
||||||
|
|
||||||
PORT = 80; // Default port.
|
PORT = 80; // Default port
|
||||||
if (argv.port) {
|
if (argv.port) {
|
||||||
PORT = argv.port;
|
PORT = argv.port;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run the server.
|
listen(PORT);
|
||||||
(require("./lib/server.js")).listen(PORT);
|
|
||||||
|
@ -3,40 +3,55 @@ var _ = require("lodash");
|
|||||||
var models = exports;
|
var models = exports;
|
||||||
var id = 0;
|
var id = 0;
|
||||||
|
|
||||||
models.Network = function() {
|
models.Network = function(attr) {
|
||||||
this.id = id++;
|
attr = attr || {};
|
||||||
this.address = "";
|
_.extend(this, _.defaults(attr, {
|
||||||
this.nick = "";
|
id: id++,
|
||||||
this.channels = [];
|
address: "",
|
||||||
|
nick: "",
|
||||||
|
channels: []
|
||||||
|
}));
|
||||||
};
|
};
|
||||||
|
|
||||||
models.Network.prototype.toJSON = function() {
|
models.Network.prototype.toJSON = function() {
|
||||||
return _.omit(this, "irc");
|
return _.omit(this, "client");
|
||||||
};
|
};
|
||||||
|
|
||||||
models.Channel = function() {
|
models.Channel = function(attr) {
|
||||||
this.id = id++;
|
attr = attr || {};
|
||||||
this.name = "";
|
_.extend(this, _.defaults(attr, {
|
||||||
this.type = "channel";
|
id: id++,
|
||||||
this.topic = "";
|
name: "",
|
||||||
this.users = [];
|
type: "channel",
|
||||||
this.messages = [];
|
topic: "",
|
||||||
|
users: [],
|
||||||
|
messages: []
|
||||||
|
}));
|
||||||
};
|
};
|
||||||
|
|
||||||
models.User = function() {
|
models.User = function(attr) {
|
||||||
this.id = id++;
|
attr = attr || {};
|
||||||
this.name = "";
|
_.extend(this, _.defaults(attr, {
|
||||||
|
id: id++,
|
||||||
|
name: ""
|
||||||
|
}));
|
||||||
};
|
};
|
||||||
|
|
||||||
models.Message = function() {
|
models.Message = function(attr) {
|
||||||
this.text = "";
|
attr = attr || {};
|
||||||
this.time = "";
|
_.extend(this, _.defaults(attr, {
|
||||||
this.user = "";
|
text: "",
|
||||||
|
time: "",
|
||||||
|
user: ""
|
||||||
|
}));
|
||||||
};
|
};
|
||||||
|
|
||||||
models.Event = function() {
|
models.Event = function(attr) {
|
||||||
this.action = "";
|
attr = attr || {};
|
||||||
this.data = "";
|
_.extend(this, _.defaults(attr, {
|
||||||
this.target = "";
|
action: "",
|
||||||
this.type = "";
|
data: "",
|
||||||
|
target: "",
|
||||||
|
type: ""
|
||||||
|
}));
|
||||||
};
|
};
|
||||||
|
@ -35,7 +35,7 @@ function refresh() {
|
|||||||
if (typeof sockets === "undefined") {
|
if (typeof sockets === "undefined") {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
sockets.emit("event", _.assign(new models.Event, {
|
sockets.emit("event", new models.Event({
|
||||||
action: "refresh",
|
action: "refresh",
|
||||||
data: networks
|
data: networks
|
||||||
}));
|
}));
|
||||||
@ -50,23 +50,32 @@ function handleUserInput(input) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var args = text.substr(1).split(" ");
|
var args = text.substr(1).split(" ");
|
||||||
switch (args[0]) {
|
var cmd = args[0].toUpperCase();
|
||||||
case "connect":
|
|
||||||
if (typeof args[1] !== "undefined") {
|
switch (cmd) {
|
||||||
|
|
||||||
|
case "SERVER":
|
||||||
|
case "CONNECT":
|
||||||
|
if (args[1]) {
|
||||||
addNetwork(args[1], true);
|
addNetwork(args[1], true);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case "join":
|
case "JOIN":
|
||||||
if (typeof args[1] === "undefined") {
|
if (args[1]) {
|
||||||
return;
|
|
||||||
}
|
|
||||||
target.network.channels.push(
|
target.network.channels.push(
|
||||||
_.assign(new models.Channel, {
|
new models.Channel({
|
||||||
name: args[1]
|
name: args[1]
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
refresh();
|
refresh();
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case "PART":
|
||||||
|
target.network.channels =
|
||||||
|
_.without(target.network.channels, target.channel);
|
||||||
|
refresh();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
@ -75,28 +84,34 @@ function handleUserInput(input) {
|
|||||||
"Command '/" + args[0] + "' does not exist."
|
"Command '/" + args[0] + "' does not exist."
|
||||||
);
|
);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function addNetwork(addr, bool) {
|
function addNetwork(addr, bool) {
|
||||||
bool = bool || false;
|
bool = bool || false;
|
||||||
|
|
||||||
var chan = _.assign(new models.Channel, {name: addr, type: "network"});
|
var chan = new models.Channel({
|
||||||
var network = _.assign(
|
name: addr,
|
||||||
new models.Network, {channels: [chan]}
|
type: "network"
|
||||||
);
|
|
||||||
|
|
||||||
if (bool) {
|
|
||||||
network.irc = new irc.Client(addr, "default_user", {
|
|
||||||
channels: ["#default_channel"]
|
|
||||||
});
|
});
|
||||||
network.irc.addListener("raw", function() {
|
var network = new models.Network({
|
||||||
handleEvent.apply(this, [network].concat(arguments));
|
channels: [chan]
|
||||||
});
|
});
|
||||||
}
|
|
||||||
|
|
||||||
networks.push(network);
|
networks.push(network);
|
||||||
refresh();
|
refresh();
|
||||||
|
|
||||||
|
if (addr == "Lobby") {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
network.client = new irc.Client(addr, "default_user");
|
||||||
|
network.client.addListener("raw", function() {
|
||||||
|
handleEvent(
|
||||||
|
network, arguments
|
||||||
|
);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function handleEvent(network) {
|
function handleEvent(network) {
|
||||||
@ -111,9 +126,9 @@ function handleEvent(network) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function addMessage(target, text) {
|
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);
|
target.channel.messages.push(message);
|
||||||
sockets.emit("event", _.assign(new models.Event, {
|
sockets.emit("event", new models.Event({
|
||||||
action: "add",
|
action: "add",
|
||||||
type: "message",
|
type: "message",
|
||||||
target: target.channel.id,
|
target: target.channel.id,
|
||||||
|
Loading…
Reference in New Issue
Block a user