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

View File

@ -16,14 +16,21 @@ models.User = Backbone.Model.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({
defaults: {
time: moment().format("HH:mm"),
time: "",
user: "",
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.get("users").on(
"all",
function(type, model) {
"add remove",
function(model, collection) {
// Bubble event
this.trigger(
"users", {
target: this.get("id"),
data: model
data: collection
}
);
},
@ -60,12 +67,12 @@ models.Channel = Backbone.Model.extend({
this.set("messages", new models.MessageCollection());
this.get("messages").on(
"add",
function(type, model) {
function(model, collection) {
// Bubble event
this.trigger(
"messages", {
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.get("channels").on(
"all",
function(type, model) {
function(type, model, collection) {
if ([
"users",
"messages"
@ -103,7 +110,7 @@ models.Network = Backbone.Model.extend({
this.trigger(
"channels", {
target: this.get("id"),
data: model
data: collection
}
);
}

View File

@ -28,13 +28,15 @@ Server.prototype.listen = function(port) {
} else {
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) {
socket.emit(
"networks",
@ -69,30 +71,35 @@ function handleInput(input) {
break;
case "JOIN":
if (!argv[1]) {
return;
var irc = target.network.irc;
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;
case "CONNECT":
if (!argv[1]) {
return;
case "PART":
var irc = target.network.irc;
if (argv[1] && typeof irc !== "undefined") {
irc.part(argv[1]);
}
var network = this.networks.add(new models.Network({
host: argv[1]
}));
break;
case "SERVER":
case "CONNECT":
if (argv[1]) {
var network = this.networks.add(
new models.Network({host: argv[1]})
);
network.irc.addListener(
"raw",
function() {
handleEvent.apply(network, arguments);
}
);
}
break;
case "QUIT":
case "DISCONNECT":
this.networks.remove(target.network);
break;
@ -107,13 +114,73 @@ function handleInput(input) {
function handleEvent(argv) {
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({
user: argv.args[0],
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
console.log(argv);
}