Add keepalives and don't show idle players.

This commit is contained in:
Nick Martin
2012-04-09 20:49:10 -07:00
committed by David Greenspan
parent 13e494cc23
commit b40c23ec90
3 changed files with 35 additions and 7 deletions

View File

@@ -195,14 +195,17 @@ Template.words.total_score = function () {
return score;
};
// subscribe to all the players, the game i'm in, and all
// the words in that game.
// on client startup, create a fresh Player
Meteor.startup(function () {
var player_id = Players.insert({name: ''});
// Allocate a new player id.
//
// XXX this does not handle hot reload. In the reload case,
// Session.get('player_id') will return a real id.
var player_id = Players.insert({name: '', idle: false});
Session.set('player_id', player_id);
// subscribe to all the players, the game i'm in, and all
// the words in that game.
Meteor.autosubscribe(function () {
Meteor.subscribe('players');
@@ -214,4 +217,10 @@ Meteor.startup(function () {
}
}
});
});
// send keepalives so the server can tell when we go away.
Meteor.setInterval(function() {
if (Meteor.status().connected)
Meteor.call('keepalive', Session.get('player_id'));
}, 20*1000);
});

View File

@@ -118,7 +118,7 @@ Meteor.methods({
if (Meteor.is_server) {
Meteor.publish('players', function () {
return Players.find();
return Players.find({idle: false});
});
// publish single games

View File

@@ -5,7 +5,7 @@ Meteor.methods({
clock: 120});
// move everyone in the lobby to the game
Players.update({game_id: null},
Players.update({game_id: null, idle: false},
{$set: {game_id: game_id}},
{multi: true});
@@ -19,6 +19,25 @@ Meteor.methods({
}, 1000);
return game_id;
},
keepalive: function (player_id) {
Players.update({_id: player_id},
{$set: {last_keepalive: (new Date()).getTime(),
idle: false}});
}
});
Meteor.setInterval(function () {
var now = (new Date()).getTime();
var idle_threshold = now - 70*1000; // 70 sec
var remove_threshold = now - 60*60*1000; // 1hr
Players.update({$lt: {last_keepalive: idle_threshold}},
{$set: {idle: true}});
// XXX need to deal with people coming back!
// Players.remove({$lt: {last_keepalive: remove_threshold}});
}, 30*1000);