Shuo/lib/server.js

140 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 15:11:25 +00:00
sockets.emit("event", _.assign(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(" ");
switch (args[0]) {
case "connect":
if (typeof args[1] !== "undefined") {
addNetwork(args[1], true);
}
break;
case "join":
console.log(args);
if (typeof args[1] === "undefined") {
return;
}
target.network.channels.push(
_.assign(new models.Channel, {
name: args[1]
})
);
refresh();
break;
default:
console.log("DEFAULT");
addMessage(
target,
"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) {
console.log("IRC");
network.irc = new irc.Client(addr, "default_user", {
channels: ["#default_channel"]
});
network.irc.addListener("raw", function() {
handleEvent.apply(this, [network].concat(arguments));
});
}
networks.push(network);
refresh();
}
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 15:11:25 +00:00
var message = _.assign(new models.Message, {text: text});
2014-03-06 18:02:43 +00:00
target.channel.messages.push(message);
sockets.emit("event", _.assign(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
}
}