Shuo/lib/server.js

150 lines
2.5 KiB
JavaScript
Raw Normal View History

2014-03-06 15:11:25 +00:00
var _ = require("lodash");
2014-03-04 16:31:52 +00:00
var connect = require("connect");
2014-03-06 18:02:43 +00:00
var irc = require("irc");
2014-03-04 17:22:06 +00:00
var io = require("socket.io");
2014-03-06 15:11:25 +00:00
var models = require("./models.js");
exports.listen = listen;
var sockets;
var networks = [];
2014-03-06 18:02:43 +00:00
addNetwork("Lobby", false);
2014-03-06 15:11:25 +00:00
function listen(port) {
var http = connect()
.use(connect.static("client"))
.listen(port);
sockets = io.listen(http).sockets;
sockets.on("connection", function(socket) {
init(socket);
});
2014-03-06 18:02:43 +00:00
}
2014-03-04 16:31:52 +00:00
2014-03-06 15:11:25 +00:00
function init(socket) {
2014-03-06 18:02:43 +00:00
refresh();
2014-03-06 15:11:25 +00:00
socket.on(
"input",
function(input) {
handleUserInput(input)
2014-03-05 17:33:11 +00:00
}
2014-03-06 15:11:25 +00:00
);
2014-03-06 18:02:43 +00:00
}
function refresh() {
if (typeof sockets === "undefined") {
return;
}
2014-03-06 22:36:56 +00:00
sockets.emit("event", new models.Event({
2014-03-06 18:02:43 +00:00
action: "refresh",
2014-03-06 15:11:25 +00:00
data: networks
}));
2014-03-06 18:02:43 +00:00
}
2014-03-05 17:33:11 +00:00
2014-03-06 15:11:25 +00:00
function handleUserInput(input) {
var text = input.text;
2014-03-06 18:02:43 +00:00
var target = getChannel(input.id);
2014-03-06 15:11:25 +00:00
2014-03-06 18:02:43 +00:00
if (text.charAt(0) != "/") {
return addMessage(target, text);
}
var args = text.substr(1).split(" ");
2014-03-06 22:36:56 +00:00
var cmd = args[0].toUpperCase();
switch (cmd) {
case "SERVER":
case "CONNECT":
if (args[1]) {
2014-03-06 18:02:43 +00:00
addNetwork(args[1], true);
}
break;
2014-03-06 22:36:56 +00:00
case "JOIN":
if (args[1]) {
target.network.channels.push(
new models.Channel({
name: args[1]
})
);
refresh();
2014-03-06 18:02:43 +00:00
}
2014-03-06 22:36:56 +00:00
break;
case "PART":
target.network.channels =
_.without(target.network.channels, target.channel);
2014-03-06 18:02:43 +00:00
refresh();
break;
default:
addMessage(
target,
"Command '/" + args[0] + "' does not exist."
);
break;
2014-03-06 22:36:56 +00:00
2014-03-06 18:02:43 +00:00
}
}
function addNetwork(addr, bool) {
bool = bool || false;
2014-03-06 22:36:56 +00:00
var chan = new models.Channel({
name: addr,
type: "network"
});
var network = new models.Network({
channels: [chan]
});
2014-03-06 18:02:43 +00:00
networks.push(network);
refresh();
2014-03-06 22:36:56 +00:00
if (addr == "Lobby") {
return;
}
network.client = new irc.Client(addr, "default_user");
network.client.addListener("raw", function() {
handleEvent(
network, arguments
);
});
2014-03-06 18:02:43 +00:00
}
function handleEvent(network) {
var args = arguments;
var target = {
network: network,
channel: network.channels[0]
};
console.log(args[1]);
addMessage(target, args[1][0].args);
}
function addMessage(target, text) {
2014-03-06 22:36:56 +00:00
var message = _.extend(new models.Message, {text: text});
2014-03-06 18:02:43 +00:00
target.channel.messages.push(message);
2014-03-06 22:36:56 +00:00
sockets.emit("event", new models.Event({
2014-03-06 15:11:25 +00:00
action: "add",
type: "message",
2014-03-06 18:02:43 +00:00
target: target.channel.id,
data: message
}));
}
2014-03-06 15:11:25 +00:00
2014-03-06 18:02:43 +00:00
function getChannel(id) {
for (var i = 0; i < networks.length; i++) {
var find = {
network: networks[i],
channel: _.findWhere(networks[i].channels, {id: id})
};
if (typeof find.channel !== "undefined") {
return find;
2014-03-05 17:33:11 +00:00
}
2014-03-06 18:02:43 +00:00
}
}