diff --git a/examples/wordplay/client/wordplay.js b/examples/wordplay/client/wordplay.js index 0c0c2ce026..4f6167f53a 100644 --- a/examples/wordplay/client/wordplay.js +++ b/examples/wordplay/client/wordplay.js @@ -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 () { } } }); -}); \ No newline at end of file + + // 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); +}); diff --git a/examples/wordplay/model.js b/examples/wordplay/model.js index d61435b151..34eae8fb50 100644 --- a/examples/wordplay/model.js +++ b/examples/wordplay/model.js @@ -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 diff --git a/examples/wordplay/server/game.js b/examples/wordplay/server/game.js index 1e796c70b6..ec4719f2e3 100644 --- a/examples/wordplay/server/game.js +++ b/examples/wordplay/server/game.js @@ -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);