Skip to content

Commit

Permalink
better account management
Browse files Browse the repository at this point in the history
  • Loading branch information
kristopolous committed Jun 7, 2012
1 parent 1b030ee commit 957e523
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 38 deletions.
2 changes: 1 addition & 1 deletion baked/channels.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
#lhs-expand{display:none}
#lhs-expand,#lhs-collapse{padding:0.5em 0.25em;font-size:0.6em;width:1em}
#channel{background:#222}
#channel .title img{width:65px}
#input-channel-search{font-size:1.25em}

#channel-wrap>div,#video-current-wrapper>div{display:inline-block}
Expand Down
48 changes: 30 additions & 18 deletions js/mt80s.js
Original file line number Diff line number Diff line change
Expand Up @@ -1129,12 +1129,14 @@ var Chat = (function(){
lastindex = 0;
$("#message").empty();

send("greet-response", {
color: MYCOLOR,
uid: Store("uid"),
lastid: _chat.lastid,
channel: _channel
});
if(!_ev.isset("greeted")) {
send("greet-response", {
color: MYCOLOR,
uid: Store("uid"),
lastid: _chat.lastid,
channel: _channel
});
}
_ev.set("greeted");
}

Expand Down Expand Up @@ -1262,10 +1264,15 @@ var Chat = (function(){
}
},
post = {
play: function(container, entry){
play: function(container, entry, isFirst){
if(isFirst) { return; }

if(_chat.lastentry.get(0).childNodes.length > 2) {
$(_chat.lastentry.get(0).firstChild).remove();
}
},
chat: function(container, entry, isFirst) {
$("a", entry).attr("target", "_blank");
}
},
combine = {
Expand All @@ -1276,13 +1283,15 @@ var Chat = (function(){
}
},
delist: function(previous, current) {
if(previous.type == 'play' && previous.vid == current.vid) {
_chat.lastentry.get(0).lastChild.innerHTML = "Skipped and delisted by " + current.who + ".";
return true;
} else if(previous.type == 'skip' && previous.vid == current.vid) {
_chat.lastentry.get(0).lastChild.innerHTML = "Skipped and delisted by " + current.who + ".";
return true;
}
if(previous.vid == current.vid) {
if(previous.type == 'play') {
_chat.lastentry.get(0).lastChild.innerHTML = "Skipped and delisted by " + current.who + ".";
return true;
} else if(previous.type == 'skip') {
_chat.lastentry.get(0).lastChild.innerHTML = "Skipped and delisted by " + current.who + ".";
return true;
}
}
},
skip: function(previous, current) {
if(previous.vid == current.vid) {
Expand Down Expand Up @@ -1319,8 +1328,9 @@ var Chat = (function(){

if(
!_chat.lastentry ||
(_chat.lastuid != row.uid ) ||
( _chat.type != row.type )
( _chat.lastuid != row.uid ) ||
( _chat.type != row.type ) ||
( _chat.type == "announce" )
) {

// If this is a new author or the first entry
Expand All @@ -1347,12 +1357,15 @@ var Chat = (function(){
// This is needed if the author says further things
// before someone else.
_chat.type = row.type;
if(post[row.type]) {
post[row.type](_chat.lastentry, entry, true);
}
} else {
$(entry).insertAfter(
_chat.lastentry.get(0).lastChild.previousSibling
);
if(post[row.type]) {
post[row.type](_chat.lastentry, entry);
post[row.type](_chat.lastentry, entry, false);
}
}
}
Expand All @@ -1361,7 +1374,6 @@ var Chat = (function(){
_chat.lastuid = row.uid;
}

$("a", entry).attr("target", "_blank");
_chat.lastrow = row;

lastindex++;
Expand Down
50 changes: 32 additions & 18 deletions node/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ var search = (function(){

IO.sockets.on('connection', function (socket) {
var
_user = {},
_user = {greeted: false},
_song = {},
_online = -1,
_ival = {};
Expand All @@ -104,6 +104,10 @@ IO.sockets.on('connection', function (socket) {
Channel.get(which, function(){
_channel.leave(function(){
Channel.join(which, _user.uid, function(){;
if(_user.loggedin && _user.channel && (_user.channel != which)) {
announce(_user.name + " left and went to <a href=#" + escape(which) + ">" + which + "</a>.");
announce(_user.name + " joined", which);
}
_user.channel = which;
socket.emit("channel-name", which);
});
Expand All @@ -120,6 +124,15 @@ IO.sockets.on('connection', function (socket) {
}
};

function logout() {
if(_user.name && _user.name != "anonymous") {
announce(_user.name + " logged out");
_user.loggedin = false;
_db.hdel("user", _user.uid);
_user.name = "anonymous";
}
}

function song() {
if(!_user.channel) {
return;
Expand Down Expand Up @@ -186,18 +199,17 @@ IO.sockets.on('connection', function (socket) {
});
}

function announce(message) {
function announce(message, channel) {
Chat.add(
_user.channel,
'announce',
message
(channel || _user.channel), {
type: 'announce',
text: message
}
);
}

socket.on("disconnect", function(){
if(_user.name && _user.name != "anonymous") {
announce(_user.name + " logged out");
}
logout();
for(var which in _ival) {
clearInterval(_ival[which]);
};
Expand All @@ -219,6 +231,11 @@ IO.sockets.on('connection', function (socket) {
});

socket.on("greet-response", function(p) {
if(_user.greeted) {
return;
}
_user.greeted = true;
_user.loggedin = false;
_user = p;
_user.channel = unescape(_user.channel);
_user.name = "anonymous";
Expand All @@ -229,9 +246,7 @@ IO.sockets.on('connection', function (socket) {
} else {
_db.hget("user", _user.uid, function(err, last) {
if(last) {
_user.name = last;
socket.emit("username", _user.name);
announce(_user.name + " logged in");
login(last);
} else {
socket.emit("username", false);
}
Expand Down Expand Up @@ -306,8 +321,9 @@ IO.sockets.on('connection', function (socket) {
});
});

function setuser(name) {
function login(name) {
announce(name + " logged in");
_user.loggedin = true;
_user.name = name;
_db.hset("user", _user.uid, name);
socket.emit("username", name);
Expand All @@ -326,7 +342,7 @@ IO.sockets.on('connection', function (socket) {
socket.emit("user-error", {text: "Username " + p.user + " exists"});
} else {
User.register(p.user, p.password, p.email);
setuser(p.user);
login(p.user);
}
});
// Reset
Expand All @@ -346,22 +362,20 @@ IO.sockets.on('connection', function (socket) {
if(exists) {
User.login(p.user, p.password, function(success) {
if(success) {
setuser(p.user);
login(p.user);
} else {
socket.emit("user-error", {text: "Login Failed, Please Try Again"});
}
});
} else {
// The user doesn't exist, and they are not
// trying to register it, so they get a free ride
setuser(p.user);
login(p.user);
}
});
// logout
} else {
announce(_user.name + " logged out");
_db.hdel("user", _user.uid);
_user.name = "anonymous";
logout();
}
});

Expand Down
3 changes: 2 additions & 1 deletion node/chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,12 @@ function append(key, data) {
* lands on a 5.
*/
if(Math.floor(Math.random() * 10) == 5) {
_db.ltrim(key, -150, -1);
_db.ltrim(key, -100, -1);
}
}

function add(channel, obj) {
console.log(channel, obj);
/*
* I'd rather crash here then be quiet.
if(!channel) {
Expand Down

0 comments on commit 957e523

Please sign in to comment.