Skip to content

Commit

Permalink
added status to user
Browse files Browse the repository at this point in the history
  • Loading branch information
Alvin S.J. Ng committed Jun 26, 2012
1 parent 1dfe360 commit bf08c1d
Show file tree
Hide file tree
Showing 4 changed files with 189 additions and 2 deletions.
153 changes: 153 additions & 0 deletions www/idle.js
@@ -0,0 +1,153 @@
// idle.js (c) Alexios Chouchoulas 2009 http://www.bedroomlan.org/coding/detecting-�idle�-and-�away�-timeouts-javascript
// Released under the terms of the GNU Public License version 2.0 (or later).

var _API_JQUERY = 1;
var _API_PROTOTYPE = 2;
var _api;

var _idleTimeout = 5000;
var _awayTimeout = 10000;

var _idleNow = false;
var _idleTimestamp = null;
var _idleTimer = null;
var _awayNow = false;
var _awayTimestamp = null;
var _awayTimer = null;

function setIdleTimeout(ms)
{
_idleTimeout = ms;
_idleTimestamp = new Date().getTime() + ms;
if (_idleTimer != null) {
clearTimeout (_idleTimer);
}
_idleTimer = setTimeout(_makeIdle, ms + 50);
//console.log('idle in ' + ms + ', tid = ' + _idleTimer);
}

function setAwayTimeout(ms)
{
_awayTimeout = ms;
_awayTimestamp = new Date().getTime() + ms;
if (_awayTimer != null) {
clearTimeout (_awayTimer);
}
_awayTimer = setTimeout(_makeAway, ms + 50);
//console.log('away in ' + ms);
}

function _makeIdle()
{
var t = new Date().getTime();
if (t < _idleTimestamp) {
//console.log('Not idle yet. Idle in ' + (_idleTimestamp - t + 50));
_idleTimer = setTimeout(_makeIdle, _idleTimestamp - t + 50);
return;
}
//console.log('** IDLE **');
_idleNow = true;

try {
if (document.onIdle) document.onIdle();
} catch (err) {
}
}

function _makeAway()
{
var t = new Date().getTime();
if (t < _awayTimestamp) {
//console.log('Not away yet. Away in ' + (_awayTimestamp - t + 50));
_awayTimer = setTimeout(_makeAway, _awayTimestamp - t + 50);
return;
}
//console.log('** AWAY **');
_awayNow = true;

try {
if (document.onAway) document.onAway();
} catch (err) {
}
}


function _initPrototype()
{
_api = _API_PROTOTYPE;
}

function _active(event)
{
var t = new Date().getTime();
_idleTimestamp = t + _idleTimeout;
_awayTimestamp = t + _awayTimeout;
//console.log('not idle.');

if (_idleNow) {
setIdleTimeout(_idleTimeout);
}

if (_awayNow) {
setAwayTimeout(_awayTimeout);
}

try {
//console.log('** BACK **');
if ((_idleNow || _awayNow) && document.onBack) document.onBack(_idleNow, _awayNow);
} catch (err) {
}

_idleNow = false;
_awayNow = false;
}

function _initJQuery()
{
_api = _API_JQUERY;
var doc = $(document);
doc.ready(function(){
doc.mousemove(_active);
try {
doc.mouseenter(_active);
} catch (err) { }
try {
doc.scroll(_active);
} catch (err) { }
try {
doc.keydown(_active);
} catch (err) { }
try {
doc.click(_active);
} catch (err) { }
try {
doc.dblclick(_active);
} catch (err) { }
});
}

function _initPrototype()
{
_api = _API_PROTOTYPE;
var doc = $(document);
Event.observe (window, 'load', function(event) {
Event.observe(window, 'click', _active);
Event.observe(window, 'mousemove', _active);
Event.observe(window, 'mouseenter', _active);
Event.observe(window, 'scroll', _active);
Event.observe(window, 'keydown', _active);
Event.observe(window, 'click', _active);
Event.observe(window, 'dblclick', _active);
});
}

// Detect the API
try {
if (Prototype) _initPrototype();
} catch (err) { }

try {
if (jQuery) _initJQuery();
} catch (err) { }

// End of file.
9 changes: 9 additions & 0 deletions www/index.css
Expand Up @@ -4,6 +4,15 @@
margin: 10px 0 0 10px;
}

.status-online {
color: green;
}

.status-away {
color: blue;

}

.role-label {
padding: 1px 4px 2px;
-webkit-border-radius: 3px;
Expand Down
18 changes: 17 additions & 1 deletion www/index.html
Expand Up @@ -20,7 +20,7 @@
<script type="text/javascript" charset="utf-8" src="knockout.js"></script>
<script type="text/javascript" charset="utf-8" src="underscore.js"></script>
<script type="text/javascript" charset="utf-8" src="cordova-1.8.1.js"></script>

<script type="text/javascript" charset="utf-8" src="idle.js"></script>

<script type="text/javascript" charset="utf-8" src="user.js"></script>
<script type="text/javascript" charset="utf-8" src="model.js"></script>
Expand All @@ -37,6 +37,7 @@
var e = document.createElement('script'); e.async = true;
e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
document.getElementById('fb-root').appendChild(e);

}());
</script>

Expand Down Expand Up @@ -165,6 +166,18 @@
}, 7000);
}

// Use away/back events created by idle.js to update our status information.;
document.onAway = function () {
if(FLApp.localUser)
FLModel.setUserStatus(FLApp.localUser().id,'away');
}
document.onBack = function (isIdle, isAway) {
if(FLApp.localUser)
FLModel.setUserStatus(FLApp.localUser().id,'online');
}

setAwayTimeout(10000);

</script>


Expand Down Expand Up @@ -273,6 +286,9 @@ <h3 data-bind="text: name">player</h3>
<li style="padding-left: 80px; min-height: 55px;" data-bind="attr: {'data-theme': ($data.id == FLApp.localUser().id) ? 'c' : 'b'}">
<img class="ui-li-thumb ui-corner-all leaderboard-profile-img" data-bind="attr: { src: fbProfileUrl }"/>
<h3>(<span data-bind="text: $index() + 1"></span>) <span data-bind="text: name"></span></h3>
<!-- ko if: status -->
<h3 data-bind="text: status, css: {'status-online': status == 'online', 'status-away': status == 'away'}"></h3>
<!-- /ko -->
<h4 style="position: absolute; right: 20px; top: 10px;"><span data-bind="text: score">pts</span></h4>
</li>
</ul>
Expand Down
11 changes: 10 additions & 1 deletion www/model.js
Expand Up @@ -31,6 +31,7 @@ function buildUser( userSnapShot ) {
data['role'] = d['role'];
data['score'] = d['score'];
data['kickedBy'] = d['kickedBy'];
data['status'] = d['status']? d['status']: 'online';
return new User(data);
}

Expand All @@ -56,7 +57,6 @@ var FLModel = {

var role = "nicehole";
var size = arrayFromObject(playerList).length;
console.log('number of users: ' + size);
if((size-1) % 2 == 0) {
role = "asshole";
}
Expand All @@ -75,6 +75,9 @@ var FLModel = {
callback(userId);
});
}
usersRef.child(userId).removeOnDisconnect();
FLModel.setUserStatus(userId,'online');

});

},
Expand Down Expand Up @@ -163,6 +166,12 @@ var FLModel = {
usersRef.child(localUser.id).child("kickedBy").set(-1);
return false;
}
},

setUserStatus: function(userId, status) {
usersRef.child(userId).child('status').transaction(function(snapshot){
return status;
});
}
}

Expand Down

0 comments on commit bf08c1d

Please sign in to comment.