From 82a0cd5c78ba29cd3db7bb9c6d5c03c9e18e7b57 Mon Sep 17 00:00:00 2001 From: btouellette Date: Thu, 10 May 2018 22:40:04 +0000 Subject: [PATCH] adding last modified time to gamestates --- app/gameserver.js | 2 +- app/models/gamestate.js | 5 ++++- config/passport.js | 2 +- views/game.ejs | 2 ++ 4 files changed, 8 insertions(+), 3 deletions(-) diff --git a/app/gameserver.js b/app/gameserver.js index e06d3f4..6d8d5b0 100644 --- a/app/gameserver.js +++ b/app/gameserver.js @@ -268,7 +268,7 @@ module.exports = function(server, sessionStore) { if(gamestate && gamestate.userIsInGame(currentUser)) { // add the message to the gamestate, trimming to 200 characters and limiting message array length to 500 message = message.substr(0,200); - Gamestate.findByIdAndUpdate(gameID, { $push: { messages: { $each: [{ username: currentUser.username, message: message}], $slice: -500 }}}, function(err, gamestate) { + Gamestate.findByIdAndUpdate(gameID, { $set: { lastModified: new Date() }, $push: { messages: { $each: [{ username: currentUser.username, message: message}], $slice: -500 }}}, function(err, gamestate) { gamestate.populate('players.user', function(err, gamestate) { // get distinct list of user IDs in the game var distinctUserIDs = gamestate.players.map(function(player) { return player.user._id; }).filter(function(value, index, self) { diff --git a/app/models/gamestate.js b/app/models/gamestate.js index 76f4e5a..35cc73b 100644 --- a/app/models/gamestate.js +++ b/app/models/gamestate.js @@ -85,7 +85,8 @@ var gamestateSchema = mongoose.Schema({ x: Number, y: Number, playerIndex: Number // which player placed this tile - }] + }], + lastModified: { type: Date, default: new Date() } }); function getMeepleFlagFromType(meepleType) { @@ -791,6 +792,8 @@ gamestateSchema.methods.drawTile = function(callback, autocomplete) { for (var key in groupedPlacements) { gamestate.activeTile.validPlacements.push(groupedPlacements[key]); } + gamestate.lastModified = new Date(); + gamestate.markModified('lastModified'); if(autocomplete) { callback(null, gamestate); return; diff --git a/config/passport.js b/config/passport.js index d68c301..9c08590 100644 --- a/config/passport.js +++ b/config/passport.js @@ -34,7 +34,7 @@ module.exports = function(passport) { } else { console.log('user found: ' + user.username); } - user.populate('activeGames friends', 'players.user players.active started finished username unusedTiles', function(err, user) { + user.populate('activeGames friends', 'players.user players.active started finished username unusedTiles lastModified', function(err, user) { if(err) { console.log('passport deserialize populate user err: ' + err); } else { diff --git a/views/game.ejs b/views/game.ejs index abf40cd..87f22cb 100644 --- a/views/game.ejs +++ b/views/game.ejs @@ -211,6 +211,8 @@ //TODO: add a message to the game chat when a tile is discarded var userGames = <%- JSON.stringify(user.activeGames) %>; +userGames.sort(function(a,b) {return (a.lastModified > b.lastModified) ? 1 : ((b.lastModified > a.lastModified) ? -1 : 0);} ); + var userFriends = <%- JSON.stringify(user.friends) %>; var userID = '<%- user._id %>'; var socket = io.connect('<%- process.env.SOCKET_URL %>');