summaryrefslogtreecommitdiff
path: root/public/javascripts/game.js
diff options
context:
space:
mode:
Diffstat (limited to 'public/javascripts/game.js')
-rw-r--r--public/javascripts/game.js59
1 files changed, 52 insertions, 7 deletions
diff --git a/public/javascripts/game.js b/public/javascripts/game.js
index 90e3a67..3839a40 100644
--- a/public/javascripts/game.js
+++ b/public/javascripts/game.js
@@ -4,7 +4,7 @@ var own_cursor;
var socket = io.connect();
-var users = {}, cursors = [];
+var users = {}, scores = {}, cursors = [];
var game = {
cursor: null,
@@ -123,33 +123,75 @@ var UI = {
user.cursor.el.css({ 'margin-right': -user.x, 'margin-bottom': -user.y });
}
else if (user.id == _id && own_cursor) {
+ user.cursor = own_cursor;
own_cursor.name.html( user.firstname );
}
+
+ if (user.id in scores) {
+ user.cursor.score = scores[user.id];
+ }
+
}
}
},
updateScores: function(data){
- var scores = data.scores;
+ var current_scores = data.scores;
$("#scores").html("");
- for (var i = 0; i < scores.length; i++) {
- var s = scores[i];
- var $li = $("<li>");
- $li.html("<b>" + s.firstname + " " + s.lastname + "</b> &mdash; " + timeInWords(s.count));
- $("#scores").append($li);
+ for (var i = 0; i < current_scores.length; i++) {
+ var score = new Score( current_scores[i] );
+
+ if (score.id in users) {
+ users[score.id].cursor.score = score;
+ }
+
+ scores[score.id] = score;
}
}
}
+function Score ( user ) {
+ var base = this;
+ base.add = function(){
+ base.el = $("<li>");
+
+ base.id = user.id;
+ base.count = user.count;
+
+ base.name = $("<span>");
+ base.name.addClass("name");
+ base.name.html(user.firstname + " " + user.lastname);
+
+ base.time = $("<span>");
+ base.time.addClass("time");
+ base.time.html( timeInWords(user.count) );
+
+ base.el.append(base.name);
+ base.el.append(" &mdash; ");
+ base.el.append(base.time);
+
+ $("#scores").append(base.el);
+ }
+
+ base.update = function(howlong){
+ base.time.html( timeInWords( base.count + howlong ) );
+ }
+
+ base.add();
+
+}
+
function Cursor ( user, now ) {
var base = this;
+ base.id = user.id;
base.start = Date.now();
base.initial = user.current || 0;
base.delay = 0;
base.blurStart = 0;
base.afk = false;
base.own = false;
+ base.score = null;
base.add = function(){
base.el = $("<div>");
@@ -182,6 +224,9 @@ function Cursor ( user, now ) {
base.update = function(now){
var howlong = Math.floor( (now - base.start) ) + base.initial - base.delay;
base.time.html( timeInSecs( howlong ) );
+ if (base.score) {
+ base.score.update( howlong );
+ }
}
base.blur = function(){