From 5113f6d46a35e11c9b75d201b4fd6b85524d0a81 Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Thu, 24 Jan 2013 18:51:23 -0500 Subject: your own high score updates now --- public/javascripts/game.js | 59 ++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 52 insertions(+), 7 deletions(-) (limited to 'public/javascripts/game.js') 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.html("" + s.firstname + " " + s.lastname + " — " + 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 = $("
  • "); + + base.id = user.id; + base.count = user.count; + + base.name = $(""); + base.name.addClass("name"); + base.name.html(user.firstname + " " + user.lastname); + + base.time = $(""); + base.time.addClass("time"); + base.time.html( timeInWords(user.count) ); + + base.el.append(base.name); + base.el.append(" — "); + 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 = $("
    "); @@ -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(){ -- cgit v1.2.3-70-g09d2