Implemented JOIN and PART events

This commit is contained in:
Mattias Erming 2014-03-12 16:09:37 +01:00
parent f57d994c5c
commit 62a40246a9
3 changed files with 108 additions and 33 deletions

View File

@ -4,7 +4,7 @@ $(function() {
$.each([ $.each([
"networks", "networks",
"channels", "channels",
"user", "users",
"messages" "messages"
], function(i, type) { ], function(i, type) {
socket.on(type, function(data) { socket.on(type, function(data) {
@ -56,6 +56,7 @@ $(function() {
case "users": case "users":
target = target.find(".users"); target = target.find(".users");
target.html(Mustache.render(users, {users: data.data})); target.html(Mustache.render(users, {users: data.data}));
console.log("RENDER USERS");
break; break;
case "messages": case "messages":

View File

@ -16,14 +16,21 @@ models.User = Backbone.Model.extend({
}); });
models.UserCollection = Backbone.Collection.extend({ models.UserCollection = Backbone.Collection.extend({
model: models.User model: models.User,
comparator: function(user) {
// Keep the collection sorted in alphabetical order
return user.get("name");
}
}); });
models.Message = Backbone.Model.extend({ models.Message = Backbone.Model.extend({
defaults: { defaults: {
time: moment().format("HH:mm"), time: "",
user: "", user: "",
text: "" text: ""
},
initialize: function() {
this.set("time", moment().format("HH:mm"));
} }
}); });
@ -44,13 +51,13 @@ models.Channel = Backbone.Model.extend({
this.set("users", new models.UserCollection()); this.set("users", new models.UserCollection());
this.get("users").on( this.get("users").on(
"all", "add remove",
function(type, model) { function(model, collection) {
// Bubble event // Bubble event
this.trigger( this.trigger(
"users", { "users", {
target: this.get("id"), target: this.get("id"),
data: model data: collection
} }
); );
}, },
@ -60,12 +67,12 @@ models.Channel = Backbone.Model.extend({
this.set("messages", new models.MessageCollection()); this.set("messages", new models.MessageCollection());
this.get("messages").on( this.get("messages").on(
"add", "add",
function(type, model) { function(model, collection) {
// Bubble event // Bubble event
this.trigger( this.trigger(
"messages", { "messages", {
target: this.get("id"), target: this.get("id"),
data: model.last() data: collection.last()
} }
); );
}, },
@ -92,7 +99,7 @@ models.Network = Backbone.Model.extend({
this.set("channels", new models.ChannelCollection()); this.set("channels", new models.ChannelCollection());
this.get("channels").on( this.get("channels").on(
"all", "all",
function(type, model) { function(type, model, collection) {
if ([ if ([
"users", "users",
"messages" "messages"
@ -103,7 +110,7 @@ models.Network = Backbone.Model.extend({
this.trigger( this.trigger(
"channels", { "channels", {
target: this.get("id"), target: this.get("id"),
data: model data: collection
} }
); );
} }

View File

@ -28,13 +28,15 @@ Server.prototype.listen = function(port) {
} else { } else {
self.sockets.emit("networks", self.networks); self.sockets.emit("networks", self.networks);
} }
// Debug
console.log(type + ": " + data);
} }
); );
this.sockets = io.listen(http, {log: false}).sockets; var options = {
log: false
};
this.sockets = io
.listen(http, options)
.sockets;
this.sockets.on("connection", function(socket) { this.sockets.on("connection", function(socket) {
socket.emit( socket.emit(
"networks", "networks",
@ -69,30 +71,35 @@ function handleInput(input) {
break; break;
case "JOIN": case "JOIN":
if (!argv[1]) { var irc = target.network.irc;
return; if (argv[1] && typeof irc !== "undefined") {
irc.join(argv[1]);
} }
var channels = this.networks.at(0).get("channels");
channels.add(new models.Channel({
name: argv[1]
}));
break; break;
case "CONNECT": case "PART":
if (!argv[1]) { var irc = target.network.irc;
return; if (argv[1] && typeof irc !== "undefined") {
irc.part(argv[1]);
} }
var network = this.networks.add(new models.Network({ break;
host: argv[1]
})); case "SERVER":
case "CONNECT":
if (argv[1]) {
var network = this.networks.add(
new models.Network({host: argv[1]})
);
network.irc.addListener( network.irc.addListener(
"raw", "raw",
function() { function() {
handleEvent.apply(network, arguments); handleEvent.apply(network, arguments);
} }
); );
}
break; break;
case "QUIT":
case "DISCONNECT": case "DISCONNECT":
this.networks.remove(target.network); this.networks.remove(target.network);
break; break;
@ -107,13 +114,73 @@ function handleInput(input) {
function handleEvent(argv) { function handleEvent(argv) {
var network = this; var network = this;
network.get("channels").at(0).get("messages").add( var channels = network.get("channels");
// Temp
var network_lobby = channels.first().get("messages");
network_lobby.add(
new models.Message({ new models.Message({
user: argv.args[0], user: argv.args[0],
text: argv.args[1] text: argv.args[1]
}) })
); );
var event = argv.command;
switch (event) {
case "JOIN":
if (argv.nick == network.get("nick")) {
var channel = new models.Channel({
name: argv.args[0]
});
channel.get("users").add(
new models.User({
name: network.get("nick")
})
)
channels.add(
channel
);
} else {
var channel = channels.findWhere({name: argv.args[0]});
var users = channel.get("users");
users.add(
new models.User({
name: argv.nick
})
);
var messages = channel.get("messages");
messages.add(
new models.Message({
user: argv.nick,
text: "has joined the channel."
})
);
}
break;
case "PART":
var channel = channels.findWhere({name: argv.args[0]});
if (argv.nick == network.get("nick")) {
channels.remove(channel);
} else {
var users = channel.get("users");
users.remove(
users.findWhere({
name: argv.nick
})
);
var messages = channel.get("messages");
messages.add(
new models.Message({
user: argv.nick,
text: "has left the channel."
})
);
}
break;
}
// Debug // Debug
console.log(argv); console.log(argv);
} }